PATCH: Disable dynamic allocation in atexit

Schwarz, Konrad konrad.schwarz@siemens.com
Fri Apr 21 13:18:00 GMT 2006


> -----Original Message-----
> From: newlib-owner@sourceware.org 
> [mailto:newlib-owner@sourceware.org] On Behalf Of Mark Mitchell
> Sent: Saturday, March 18, 2006 2:36 AM
> To: newlib@sources.redhat.com
> Subject: PATCH: Disable dynamic allocation in atexit
 
> By far the biggest issue was that we're pulling in malloc, 
> and the primary reason for that (outside of some ARM-specific 
> issues that I will post as follow-on patches) is that crt0 
> registers fini via atexit, and atexit calls malloc, and 
> malloc is big.  This patch provides a configure option to 
> limit atexit to the 32 routines required by ISO C and 
> provided via static allocation in newlib.

I though about this some more, and the following solution may be useful:

The problem is that the malloc library is too big.

The GNU link editor, following the Unix tradition, links from left to
right, archive members are only loaded if they contain unresolved
references.

A relocatable object file, containing implementations of the routines
related to malloc, placed ahead of libc, will cause that implementation
of malloc functionality to be used.

This implementation can be much smaller than the original malloc.

In particular, a trivial implementation is possible, along the lines of

# include	<stddef.h>
# include	<stdlib.h>
# include	<assert.h>

void
*malloc (size_t const size)
{
	return 0;
}

void
*calloc (size_t const nmemb, size_t const size)
{
	return 0;
}

void
free (void *const p)
{
	assert (!p);
}

void
*realloc (void *const p, size_t const size)
{
	assert (!p);
	return 0;
}

This strategy requires newlib to be structured in a modular fashion with
respect to the memory management functions and routines such as atexit()
for which the C standard guarantees certain minimums need to implement
those limits via objects with static storage duration.

I suspect that the newlib library at some point fulfilled these
requirements, since memory footprint used to be much more important than
now.

Regards,

Konrad Schwarz



More information about the Newlib mailing list