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
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,
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 -------------------------------------------------------------------------*/
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
45 typedef struct header HEAPSPACE header_t
;
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
)
60 void *realloc(void *ptr
, size_t size
)
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)
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
86 if(size
+ offsetof(struct header
, next_free
) < size
) // Handle overflow
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
);
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
;
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
);