New libc malloc patch

Jason Evans jasone at
Sun Dec 11 22:32:12 PST 2005

On Dec 11, 2005, at 9:58 PM, Daniel Eischen wrote:
> On Sun, 11 Dec 2005, Jason Evans wrote:
>> I have been contemplating creating a separate spinlock API that
>> doesn't require the threads library to track the spinlocks across
>> fork.  This would (if I understand correctly) remove the current
>> static spinlock limitations.
> What about using pthread_atfork()?

Aren't there potential ordering issues for that?  It seems to me that  
the malloc pre-fork code would need to be run after any other pre- 
fork functions, in order to avoid potential deadlock, and that the  
malloc post-fork code would need to be run before any other post-fork  
functions, again to avoid potential deadlock.

After looking at the spinlock code some more, it's no longer clear to  
me why the thread/thr_spinlock.c code uses a static array for  
spinlocks.  It seems to me that it would work fine to allow the  
program to provide space for a spinlock and manually initialize it.   
This would remove the limitation on the number of spinlocks.

>> As for supporting recursive spinlocks, I doubt that the overhead
>> would be acceptable in general.  If I could get rid of the need for
>> the one recursive lock in malloc.c, I certainly would. =)
> Why do we need a recursive mutex?  Can you not restructure the
> code so that it is not needed?

There is an internal arena that the malloc code uses for allocating  
internal data structures.  In some cases, the internal arena has to  
recursively allocate.  If there were no object caching, it might be  
possible to pre-allocate, such that recursion never happens, but  
given the object caching, it's difficult to reason about precisely  
what will occur internally for a single malloc/free operation.  There  
are some other possibilities, but nothing I've thought of so far is  
simple or elegant.

Fixing this would make all locking in the malloc code a bit cheaper,  
which is why it continues to bother me.


More information about the freebsd-current mailing list