Re: __malloc_init_space (example)

Nikola Vladov <[email protected]>
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
On Wed, May 20, 2009 at 10:33:07AM +0200, Laurent Bercot wrote:
Thanks Laurent!

I am interested to hear what the community means.
I'll explain more about this feature here.  May be it will
not default.  One must uncomment one line in dietfeatures.h
I don't know also if this will accepted in dietlibc!

There is no code for it in libc (no one byte !!!).  If we make
the pointer __malloc_init_space global the feature can be used.
If last pointer is static it is impossible to use it.

>  However, no matter how smart it is, this coding style is very specific
> and a libc should be optimized for the generic case. Unfortunately, in
> the generic case, the static buffer trick is actually detrimental, because
> it's impossible to reclaim the 4 kB once they're used. It's one page per
> executable, and it can add up.
>  The diet libc implements malloc()/free() with mmap()/munmap(). This is
> simple and fast, and works. And the kernel can actually reclaim a page
> that has been unmapped ! This is much more useful in the general case.

Here is one example:

 extern __alloc_t *__malloc_init_space;
 static __alloc_t x_space[(4096)/sizeof(__alloc_t)];
 
 int main(int argc, char **argv) {
 ...
 x_space->size = sizeof(x_space);
 x_space->next = __malloc_init_space;
 __malloc_init_space = x_space;


then we can do just or latter
x = mallc(30); y = malloc(100);

Aftter free(x); free(y); "x" and "y" are zeroed in x_space and
available again for malloc/calloc/realloc.  This is the same as malloc
first gets one page with mmap and then it shares it for small pages.
Only system call mmap is missing.  By DJB is is impossible to free the
buffer really.  Here it is different.  And again all depends from this
if __malloc_init_space is static or global in libc.

You will help more if you try to compile and look at the code.
Please, do it!

I don't like to include in libc any static buffer by default.
If someone want to use above -- welcome!  I tried also with static
buffers 256K.  They go in BSS section.  If the program uses only
24K then all works with 24K.  The rest (256-24) is in the kernel.

I'll be happy to hear from you more ;-)
If you ask I'll write more examples for you.

Nikola
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.