Malloc not trimming sbrk space once it fills up
Jakub Jelinek
jakub@redhat.com
Wed Jan 7 17:13:00 GMT 2004
Hi!
The attached testcase shows that once morecore fails for the first time
and malloc needs to fall back into mmap arena, no more sbrk trimming
happens even if all chunks in mmap areas are freed.
Jakub
-------------- next part --------------
/* Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
ptrdiff_t sum;
__malloc_ptr_t
counting_morecore (ptrdiff_t size)
{
sum += size;
return __default_morecore (size);
}
int
main (void)
{
size_t pagesize = sysconf (_SC_PAGESIZE);
char *p = sbrk (0);
p += (128 * pagesize) & ~(pagesize - 1);
if (mmap (p, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0) != p)
{
puts ("Couldn't test");
return 0;
}
if (mallopt (M_TRIM_THRESHOLD, 32 * pagesize) == 0)
{
puts ("Couldn't test");
return 0;
}
__morecore = counting_morecore;
char *array[512];
int i;
for (i = 0; i < 512; ++i)
array[i] = malloc (pagesize / 4);
ptrdiff_t midsum = sum;
for (i = 511; i >= 0; --i)
free (array[i]);
__morecore = __default_morecore;
if (midsum == sum && midsum > 32 * pagesize)
{
printf ("free did not trim sbrk area at all: midsum %td sum %td\n",
midsum, sum);
return 1;
}
return 0;
}
More information about the Libc-hacker
mailing list