__alloc_init_space
Nikola Vladov <[email protected]>
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <[email protected]> |
Hi!, I found a nice way to import DJB alloc trick in dietlibc.
In qmail DJB uses a static buffer of 4K in alloc.c. After using
it is impossible to free the buffer!
I want to add such static buffer in programs with arbitrary length.
It's nice if there is a minimal code in libc malloc. After using
(some parts of the buffer) it must be possible to free the parts.
This static buffer will be used for small pages only (less than 4k).
I solve above problems with 37 bytes code in malloc (i386).
Acctually I added in my alloc.c the following code:
#ifdef WANT_ALLOC_INIT_SPACE
void *__alloc_init_space=0;
#endif
static void* REGPARM(1) __small_malloc(size_t size) {
...
#ifdef WANT_ALLOC_INIT_SPACE
if (__alloc_init_space) {
ptr = __alloc_init_space;
ptr->next = space;
space = ptr;
__alloc_init_space = 0;
}
#endif
...
A) This can be used in the following way in the main program.
typedef struct {
void* next;
size_t size;
} __alloc_t;
extern void *__alloc_init_space;
static char alloc_space[16*4096];
int main(int argc, char *argv[]) {
...
__alloc_init_space=alloc_space;
((__alloc_t *)alloc_space)->size = sizeof(alloc_space);
...
In the above example 16 pages are placed in BCC section.
They are zeroed. The program will use them only if they are needed.
If the program need 8K for small pages the remain 14 pages are
unused and even not mmaped. I suppose this is faster than mmap.
B) The only restriction for such buffers is their length to be
multiple of 64. One can add them at any time. After malloc
accept them it set __alloc_init_space pointer to zero. After
that we can check ((__alloc_t *)alloc_space)->size and see how many
bytes are used for small pages.
C) One can alloc such buffer also with alloca in main program.
Alloca don't zeroed the returned space. It alloca is call
immediately after main the space is zeroed may be. We cannot
use memset(alloc_space, 0, X). In this case the program allocate
the whole buffer immediately.
D) I looked how to make this compatible with glibc.
They have function void *__malloc_initialize_hook(void)
Let see first if anybody will be interested from this feature.
Nikola