Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / lib / realloc.c
blob41fa4944eef66be1ee6474ff2bef77d170616ff7
1 /*-------------------------------------------------------------------------
2 realloc.c - allocate memory.
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.
8 Copyright (C) 2015-2020, Philipp Klaus Krause, pkk@spth.de
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.
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.
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.
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 -------------------------------------------------------------------------*/
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <string.h>
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
45 typedef struct header HEAPSPACE header_t;
47 struct header
49 header_t *next;
50 header_t *next_free;
53 extern header_t *HEAPSPACE __sdcc_heap_free;
55 void __sdcc_heap_init(void);
57 #if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
58 void HEAPSPACE *realloc(void *ptr, size_t size)
59 #else
60 void *realloc(void *ptr, size_t size)
61 #endif
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;
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
73 if(!ptr)
74 return(malloc(size));
76 if(!size)
78 free(ptr);
79 return(0);
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;
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);
92 h = (void HEAPSPACE *)((char HEAPSPACE *)(ptr) - offsetof(struct header, next_free));
93 oldblocksize = (char HEAPSPACE *)(h->next) - (char HEAPSPACE *)h;
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;
101 if(blocksize <= maxblocksize) // Can resize in place.
103 if(prev_free && prev_free->next == h) // Always move into previous block to defragment
105 memmove(prev_free, h, blocksize <= oldblocksize ? blocksize : oldblocksize);
106 h = prev_free;
107 *pf = next_free;
108 f = pf;
111 if(next_free && next_free == h->next) // Merge with following block
113 h->next = next_free->next;
114 *f = next_free->next_free;
117 if(maxblocksize >= blocksize + sizeof(struct header)) // Create new block from free space
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;
126 return(&(h->next_free));
129 if(ret = malloc(size))
131 size_t oldsize = oldblocksize - offsetof(struct header, next_free);
132 memcpy(ret, ptr, size <= oldsize ? size : oldsize);
133 free(ptr);
134 return(ret);
137 return(0);