NewLib malloc on top of heap breaks more than needed
I have noticed a peculiarity with malloc(). I am not sure if it is a bug or feature but it is for sure unexpected.
p1 = malloc(30000); // heap will peak at 30000.
free(p1); // Will be put back in top bin
p1 = malloc(40000); // heap will now peak at 70000
I would have expected that this scenario would not require more than 40000 bytes of RAM (and e.g. fit into 64 KB).
I think I found why this happens. Line 2561 and 2575 of mallocr.c handles the case where allocation is made from top bin and top bin is not big enough:
2561: if (chunksize(top) < nb || remainder_size < (long)MINSIZE)
...
2575: malloc_
So, since top only has 30000 another 40000 is sbrk() making top 70000, it would have sufficed to extend with only 10000 and not hitting the maximum.
I have also studied but not tested the nane-mallocr.c and it too seems to have the same semantics that if the while-loop in malloc() cannot find a large enough chunk it will end and sbrk() the requested size without considering what already is available.
Question information
- Language:
- English Edit question
- Status:
- Expired
- Assignee:
- No assignee Edit question
- Last query:
- Last reply: