[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: malloc & free functions



Hallvard B Furuseth wrote:
I'm getting lost in all the malloc and free functions...
When is one supposed to use which function?  When does it matter?

  ber_memalloc(), ber_memfree(),
  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?

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.

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/