thoughs on splitting up malloc.c

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Sep 2 14:15:12 GMT 2025



On 29/08/25 18:11, DJ Delorie wrote:
> 
> I've already heard one request to move the stdio-dependent parts out
> of malloc.c to reduce static linking size.  That and my recent
> additions got me thinking about splitting it up.  It's a mess.
> 
> arena.c is already a separate file, but it's #included at a key point
> in malloc.c instead of being truly independent.
> 
> malloc-internal.h is mostly empty and looks like it's only rarly used,
> but is used by other modules in libc.
> 
> malloc.h is what we install.
> 
> A large chunk of malloc.c at the beginning is all structs and defines,
> mixed together.  The first real function is on line 1656 but there's a
> couple hundred more lines of structs and defines after that.
> 
> arena.c is a mix of structs and defines, arena support routines, and -
> for some reason - tunables.  It's included on line 2081 of malloc.c.
> 
> So I propose we move all the definitions and declarations into
> malloc-internal.h and split up malloc.c/arena.c into compilable
> modules as such:
> 
> * minimum needed for malloc() and free() that isn't listed below
> 
> * realloc()
> 
> * calloc()
> 
> * the various memalign() clones
> 
> * tunables-related stuff, mallopt
> 
> * tcache-related routines (mostly in tcache.h for inline functions, but init/fini need .c)
> 
> * mallinfo and malloc_info get their own files
> 
> etc.
> 
> Thoughts?
> 

Sounds reasonable. I think there is no need to keep arena.c in a different file,
and maybe move __malloc_arena_thread_freeres to a different TU (since it is only
required if the program uses pthread).

Also, maybe move __malloc_stats to its own file as well.


More information about the Libc-alpha mailing list