[Date Prev][Date Next]
Re: malloc & free functions
- To: Hallvard B Furuseth <firstname.lastname@example.org>
- Subject: Re: malloc & free functions
- From: Howard Chu <email@example.com>
- Date: Sun, 31 Jul 2005 20:57:56 -0700
- Cc: openldap-devel@OpenLDAP.org
- In-reply-to: <firstname.lastname@example.org>
- References: <email@example.com>
- User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b2) Gecko/20050621
Hallvard B Furuseth wrote:
Yes, I've wished for a cleaner implementation. If liblber is using the
memctx to allocate, it ought to also use it to free. Typically slapd is
the only thing that ever sets the memctx.
I'm getting lost in all the malloc and free functions...
When is one supposed to use which function? When does it matter?
LBER_FREE(), LBER_MALLOC() -defined as the two above,
ber_memalloc_x(), ber_memfree_x() - as as above but with ctx parameter,
LDAP_FREE(), LDAP_MALLOC() plus _X variants,
ch_malloc, ch_free() - also as above,
slap_sl_malloc(), slap_sl_free() - separate slapd allocator.
E.g. liblber/decode.c often uses ber_memalloc_x(,ber->ber_memctx) to
allocate but then frees that memory with LBER_FREE() which does not
use a ctx. Should the free use ber->ber_memctx again? Or not, since
ber_set_option(,LBER_OPT_BER_MEMCTX,) can change a ber_memctx and
slapd uses it sometimes?
In slapd, since it was impossible to keep the two types of memory
straight, ch_free() and slap_sl_free() will try to detect if they
actually own the memory, and call the other free routine if not. So the
only consequence of calling the wrong free() function is an extra
function call overhead.
In liblber, there's no such protection, so calling the wrong free will
probably SEGV. Looks like decode.c needs to be fixed.
It would have probably been better if the sl_malloc routines were in
liblber, but it also has some dependencies on the thread pool functions,
so that wasn't possible.
-- Howard Chu
Chief Architect, Symas Corp. http://www.symas.com
Director, Highland Sun http://highlandsun.com/hyc
OpenLDAP Core Team http://www.openldap.org/project/