free() does not call sbrk()
Jeff Johnston
jjohnstn@redhat.com
Tue Sep 15 22:27:00 GMT 2009
On 15/09/09 03:29 PM, Martin Walter wrote:
>> ...
>> For a discussion on the design see: http://g.oswego.edu/dl/html/malloc.html
>> which is referenced in libc/stdlib/mallocr.c
>
> Thanks for clarification!
>
>> My simple test of mallocing a block of 4000 and freeing it back again in an
>> infinite loop results in the same pointer being given back each time and no
>> increment in memory usage. I do not know why you are not seeing similar
>> behavior for your test. Is there something else in your loop other than
>> malloc/free? (mine has a printf as well)
>
> Actually no. Please take a look at my code:
>
I believe the culprit has to be UART_write which is not a part of
newlib. I took your code and ran it with an mn10300-elf newlib build
(using a simulator) and for x86-linux newlib locally on my x86-linux
machine. I replaced the UART_write call with a direct "write" syscall
and it works just as expected; the same pointer each time.
-- Jeff J.
> #include "UART.h"
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
>
> static void alloc (int size)
> {
> char buf[128];
> int len;
>
> char *ptr = malloc (size);
> if (ptr != NULL)
> {
> len = sprintf (buf, "ptr: %d\r\n", (int)ptr);
> UART_write(0, buf, len);
> free (ptr);
> }
> }
>
> int main (int argc, char *argv[])
> {
> register int i;
>
> for (i = 0; i< 10; ++i)
> alloc (4000);
>
> return 0;
> }
>
> And the output:
>
> ptr: 2016
> ptr: 6024
> ptr: 10032
> ptr: 14040
> ptr: 18048
> ptr: 22056
> ptr: 26064
> ptr: 30072
> ptr: 34080
> ptr: 38088
>
> Kind regards,
> Martin
More information about the Newlib
mailing list