diff options
author | Torbjörn SVENSSON <torbjorn.svensson@foss.st.com> | 2022-08-30 15:56:25 +0200 |
---|---|---|
committer | Jeff Johnston <jjohnstn@redhat.com> | 2022-09-01 14:40:27 -0400 |
commit | 0455ea28ce2bfa83ca36ec37b9c9fb00c54bbe54 (patch) | |
tree | 17ebfd8701c677db132ffe612a3b1b7b8201385d | |
parent | d92d3a3c4a7af1ebe56d58c32986ab410f6071ec (diff) | |
download | newlib-0455ea28ce2bfa83ca36ec37b9c9fb00c54bbe54.zip newlib-0455ea28ce2bfa83ca36ec37b9c9fb00c54bbe54.tar.gz newlib-0455ea28ce2bfa83ca36ec37b9c9fb00c54bbe54.tar.bz2 |
Used chunk needs to be removed from free_list
When using nano malloc and the remaning heap space is not big enough to
fullfill the allocation, malloc will attempt to merge the last chunk in
the free list with a new allocation in order to create a bigger chunk.
This is successful, but the chunk still remains in the free_list, so
any later call to malloc can give out the same region without it first
being freed.
Possible sequence to verify:
void *p1 = malloc(3000);
void *p2 = malloc(4000);
void *p3 = malloc(5000);
void *p4 = malloc(6000);
void *p5 = malloc(7000);
free(p2);
free(p4);
void *p6 = malloc(35000);
free(p6);
void *p7 = malloc(42000);
void *p8 = malloc(32000);
Without the change, p7 and p8 points to the same address.
Requirement, after malloc(35000), there is less than 42000 bytes
available on the heap.
Contributed by STMicroelectronics
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
-rw-r--r-- | newlib/libc/stdlib/nano-mallocr.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c index 99ad60d..43eb20e 100644 --- a/newlib/libc/stdlib/nano-mallocr.c +++ b/newlib/libc/stdlib/nano-mallocr.c @@ -336,6 +336,15 @@ void * nano_malloc(RARG malloc_size_t s) if (sbrk_aligned(RCALL alloc_size) != (void *)-1) { p->size += alloc_size; + + /* Remove chunk from free_list */ + r = free_list; + while (r && p != r->next) + { + r = r->next; + } + r->next = NULL; + r = p; } else |