realloc.c (4515B)
1/*------------------------------------------------------------------------- 2 realloc.c - allocate memory. 3 4 Always behaves according to C90 (i.e. does not take advantage of 5 undefined behaviour introduced in C2X or implementation-defined 6 behaviour introduced in C17. 7 8 Copyright (C) 2015-2020, Philipp Klaus Krause, pkk@spth.de 9 10 This library is free software; you can redistribute it and/or modify it 11 under the terms of the GNU General Public License as published by the 12 Free Software Foundation; either version 2, or (at your option) any 13 later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this library; see the file COPYING. If not, write to the 22 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 23 MA 02110-1301, USA. 24 25 As a special exception, if you link this library with other files, 26 some of which are compiled with SDCC, to produce an executable, 27 this library does not by itself cause the resulting executable to 28 be covered by the GNU General Public License. This exception does 29 not however invalidate any other reasons why the executable file 30 might be covered by the GNU General Public License. 31-------------------------------------------------------------------------*/ 32 33#include <stdlib.h> 34#include <stddef.h> 35#include <string.h> 36 37#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) 38#define HEAPSPACE __xdata 39#elif defined(__SDCC_pdk13) || defined(__SDCC_pdk14) || defined(__SDCC_pdk15) 40#define HEAPSPACE __near 41#else 42#define HEAPSPACE 43#endif 44 45typedef struct header HEAPSPACE header_t; 46 47struct header 48{ 49 header_t *next; 50 header_t *next_free; 51}; 52 53extern header_t *HEAPSPACE __sdcc_heap_free; 54 55void __sdcc_heap_init(void); 56 57#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) 58void HEAPSPACE *realloc(void *ptr, size_t size) 59#else 60void *realloc(void *ptr, size_t size) 61#endif 62{ 63 void HEAPSPACE *ret; 64 header_t *h, *next_free, *prev_free; 65 header_t *HEAPSPACE *f, *HEAPSPACE *pf; 66 size_t blocksize, oldblocksize, maxblocksize; 67 68#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) || defined(__SDCC_hc08) || defined(__SDCC_s08) 69 if(!__sdcc_heap_free) 70 __sdcc_heap_init(); 71#endif 72 73 if(!ptr) 74 return(malloc(size)); 75 76 if(!size) 77 { 78 free(ptr); 79 return(0); 80 } 81 82 prev_free = 0, pf = 0; 83 for(h = __sdcc_heap_free, f = &__sdcc_heap_free; h && h < ptr; prev_free = h, pf = f, f = &(h->next_free), h = h->next_free); // Find adjacent blocks in free list 84 next_free = h; 85 86 if(size + offsetof(struct header, next_free) < size) // Handle overflow 87 return(0); 88 blocksize = size + offsetof(struct header, next_free); 89 if(blocksize < sizeof(struct header)) // Requiring a minimum size makes it easier to implement free(), and avoid memory leaks. 90 blocksize = sizeof(struct header); 91 92 h = (void HEAPSPACE *)((char HEAPSPACE *)(ptr) - offsetof(struct header, next_free)); 93 oldblocksize = (char HEAPSPACE *)(h->next) - (char HEAPSPACE *)h; 94 95 maxblocksize = oldblocksize; 96 if(prev_free && prev_free->next == h) // Can merge with previous block 97 maxblocksize += (char HEAPSPACE *)h - (char HEAPSPACE *)prev_free; 98 if(next_free == h->next) // Can merge with next block 99 maxblocksize += (char HEAPSPACE *)(next_free->next) - (char HEAPSPACE *)next_free; 100 101 if(blocksize <= maxblocksize) // Can resize in place. 102 { 103 if(prev_free && prev_free->next == h) // Always move into previous block to defragment 104 { 105 memmove(prev_free, h, blocksize <= oldblocksize ? blocksize : oldblocksize); 106 h = prev_free; 107 *pf = next_free; 108 f = pf; 109 } 110 111 if(next_free && next_free == h->next) // Merge with following block 112 { 113 h->next = next_free->next; 114 *f = next_free->next_free; 115 } 116 117 if(maxblocksize >= blocksize + sizeof(struct header)) // Create new block from free space 118 { 119 header_t *const newheader = (header_t *const)((char HEAPSPACE *)h + blocksize); 120 newheader->next = h->next; 121 newheader->next_free = *f; 122 *f = newheader; 123 h->next = newheader; 124 } 125 126 return(&(h->next_free)); 127 } 128 129 if(ret = malloc(size)) 130 { 131 size_t oldsize = oldblocksize - offsetof(struct header, next_free); 132 memcpy(ret, ptr, size <= oldsize ? size : oldsize); 133 free(ptr); 134 return(ret); 135 } 136 137 return(0); 138} 139