From: Kurt Zeilenga Date: Sun, 30 May 1999 23:00:52 +0000 (+0000) Subject: Initial round 2 memory allocation changes. THIS IS A WORK IN PROGRESS. X-Git-Tag: OPENLDAP_REL_ENG_2_BP~469 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=2e5a52414a0d29382b971bce0450dfd6a8ff6b54;p=openldap Initial round 2 memory allocation changes. THIS IS A WORK IN PROGRESS. includes single to multiple hooks changes. ber_mem* reimplementation. namespace glue (finally naming has not be decided upon nor implemented). Added ldap_int_strdup to handle "internal" strdup'ing, this version uses hooks. ldap_pvt_strdup still available for when strdup() is missing, this version directly uses system allocators. Updated -lldif to use ber allocators. Items returned by ldif routines should be ber_memfree()d as needed. --- diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index 7e97e98228..49c02495ba 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -539,7 +539,7 @@ write_ldif_value( char *type, char *value, unsigned long vallen ) } fputs( ldif, stdout ); - free( ldif ); + ber_memfree( ldif ); return( 0 ); } diff --git a/include/lber.h b/include/lber.h index fc3c5ab51b..ae8fa586e5 100644 --- a/include/lber.h +++ b/include/lber.h @@ -98,10 +98,21 @@ typedef int (*BERTranslateProc) LDAP_P(( #define LBER_OPT_DEBUG_LEVEL LBER_OPT_BER_DEBUG #define LBER_OPT_LOG_PRINT_FN 0x8001 -#define LBER_OPT_MEMORY_FN 0x8002 +#define LBER_OPT_MEMORY_FNS 0x8002 typedef void (*BER_LOG_PRINT_FN) LDAP_P(( char *buf )); -typedef void* (*BER_MEMORY_FN) LDAP_P(( void *p, size_t size )); + +typedef void* (*BER_MEMALLOC_FN) LDAP_P(( size_t size )); +typedef void* (*BER_MEMCALLOC_FN) LDAP_P(( size_t n, size_t size )); +typedef void* (*BER_MEMREALLOC_FN) LDAP_P(( void *p, size_t size )); +typedef void (*BER_MEMFREE_FN) LDAP_P(( void *p )); + +typedef struct lber_memory_fns { + BER_MEMALLOC_FN bmf_malloc; + BER_MEMCALLOC_FN bmf_calloc; + BER_MEMREALLOC_FN bmf_realloc; + BER_MEMFREE_FN bmf_free; +} BerMemoryFunctions; /* LBER Sockbuf options */ #define LBER_TO_FILE 0x01 /* to a file referenced by sb_fd */ diff --git a/include/portable.h.nt b/include/portable.h.nt index 91dc847022..6c97807072 100644 --- a/include/portable.h.nt +++ b/include/portable.h.nt @@ -723,6 +723,6 @@ typedef char * caddr_t; #include "ldap_cdefs.h" #include "ldap_features.h" -#include "ac/assert.h" +#include #endif /* _LDAP_PORTABLE_H */ diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h index 5bd643f04f..a64f2d3a17 100644 --- a/libraries/liblber/lber-int.h +++ b/libraries/liblber/lber-int.h @@ -23,8 +23,6 @@ LDAP_BEGIN_DECL -extern BER_MEMORY_FN ber_int_realloc; - struct lber_options { short lbo_valid; #define LBER_UNINITIALIZED 0x0 @@ -195,31 +193,17 @@ ber_log_sos_dump LDAP_P(( /* memory.c */ /* simple macros to realloc for now */ -#define LBER_MALLOC(s) \ - (ber_int_realloc \ - ? (*ber_int_realloc)(NULL,(s)) \ - : realloc(NULL,(s))) - -#define LBER_CALLOC(n,s) \ - (ber_int_realloc \ - ? ber_int_calloc((n),(s)) \ - : calloc((n),(s))) - -#define LBER_REALLOC(p,s) \ - (ber_int_realloc \ - ? (*ber_int_realloc)((p),(s)) \ - : realloc((p),(s))) - -#define LBER_FREE(p) \ - (ber_int_realloc \ - ? (*ber_int_realloc)((p),(size_t)0) \ - : realloc((p),(size_t)0)) - -LDAP_F( void * ) -ber_int_calloc LDAP_P(( - size_t n, - size_t size )); +extern BerMemoryFunctions* ber_int_memory_fns; +#define LBER_INT_MALLOC(s) ber_memalloc((s)) +#define LBER_INT_CALLOC(n,s) ber_memcalloc((n),(s)) +#define LBER_INT_REALLOC(p,s) ber_memrealloc((p),(s)) +#define LBER_INT_FREE(p) ber_memfree((p)) + +#define LBER_MALLOC(s) ber_memalloc((s)) +#define LBER_CALLOC(n,s) ber_memcalloc((n),(s)) +#define LBER_REALLOC(p,s) ber_memrealloc((p),(s)) +#define LBER_FREE(p) ber_memfree((p)) /* sockbuf.c */ diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c index f893cfab7c..d4bbdde8bc 100644 --- a/libraries/liblber/memory.c +++ b/libraries/liblber/memory.c @@ -9,45 +9,77 @@ #include "lber-int.h" +BerMemoryFunctions *ber_int_memory_fns = NULL; + void ber_memfree( void *p ) { ber_int_options.lbo_valid = LBER_INITIALIZED; - LBER_FREE( p ); + + assert( p != NULL ); + + if( ber_int_memory_fns == NULL ) { + free( p ); + return; + } + + assert( ber_int_memory_fns->bmf_free ); + + (*ber_int_memory_fns->bmf_free)( p ); } void * ber_memalloc( size_t s ) { ber_int_options.lbo_valid = LBER_INITIALIZED; - return LBER_MALLOC( s ); + + assert( s ); + + if( ber_int_memory_fns == NULL ) { + return malloc( s ); + } + + assert( ber_int_memory_fns->bmf_malloc ); + + return (*ber_int_memory_fns->bmf_malloc)( s ); } void * ber_memcalloc( size_t n, size_t s ) { ber_int_options.lbo_valid = LBER_INITIALIZED; - return LBER_CALLOC( n, s ); + + assert( n && s ); + + if( ber_int_memory_fns == NULL ) { + return calloc( n, s ); + } + + assert( ber_int_memory_fns->bmf_calloc ); + + return (*ber_int_memory_fns->bmf_calloc)( n, s ); } void * ber_memrealloc( void* p, size_t s ) { ber_int_options.lbo_valid = LBER_INITIALIZED; - return LBER_REALLOC( p, s ); -} - -BER_MEMORY_FN ber_int_realloc = NULL; -void * -ber_int_calloc( size_t n, size_t s ) -{ - ber_int_options.lbo_valid = LBER_INITIALIZED; + if( p == NULL ) { + return ber_memalloc( s ); + } + + if( s == 0 ) { + ber_memfree( p ); + return NULL; + } - { - size_t size = n * s; - void *p = (*ber_int_realloc)( NULL, size ); - return p ? memset( p, 0, size ) : p; + if( ber_int_memory_fns == NULL ) { + return realloc( p, s ); } + + assert( ber_int_memory_fns->bmf_realloc ); + + return (*ber_int_memory_fns->bmf_realloc)( p, s ); } diff --git a/libraries/liblber/options.c b/libraries/liblber/options.c index eafc812eda..863f9396d5 100644 --- a/libraries/liblber/options.c +++ b/libraries/liblber/options.c @@ -5,6 +5,7 @@ #include "portable.h" #include +#include #include "lber-int.h" @@ -68,10 +69,28 @@ ber_set_option( Sockbuf *sb; if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED) - && ( option == LBER_OPT_MEMORY_FN ) + && ( ber_int_memory_fns == NULL ) + && ( option == LBER_OPT_MEMORY_FNS ) && ( invalue != NULL )) { - ber_int_realloc = (BER_MEMORY_FN) invalue; + BerMemoryFunctions *f = (BerMemoryFunctions *) invalue; + + /* make sure all functions are provided */ + if(!( f->bmf_malloc && f->bmf_calloc + && f->bmf_realloc && f->bmf_free )) + { + return LBER_OPT_ERROR; + } + + ber_int_memory_fns = (BerMemoryFunctions *) + (*(f->bmf_malloc))(sizeof(BerMemoryFunctions)); + + if ( ber_int_memory_fns == NULL ) { + return LBER_OPT_ERROR; + } + + memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions)); + ber_int_options.lbo_valid = LBER_INITIALIZED; return LBER_OPT_SUCCESS; } diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 9d6a3e845e..bd07b0b4ab 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -265,10 +265,17 @@ void ldap_int_initialize LDAP_P((void)); /* memory.c */ /* simple macros to realloc for now */ +#define LDAP_INT_MALLOC(s) (LBER_MALLOC((s))) +#define LDAP_INT_CALLOC(n,s) (LBER_CALLOC((n),(s))) +#define LDAP_INT_REALLOC(p,s) (LBER_REALLOC((p),(s))) +#define LDAP_INT_FREE(p) (LBER_FREE((p))) + +#ifndef LDAP_MALLOC #define LDAP_MALLOC(s) (LBER_MALLOC((s))) #define LDAP_CALLOC(n,s) (LBER_CALLOC((n),(s))) #define LDAP_REALLOC(p,s) (LBER_REALLOC((p),(s))) #define LDAP_FREE(p) (LBER_FREE((p))) +#endif /* * in unit-int.c @@ -396,11 +403,11 @@ BerElement *ldap_build_search_req LDAP_P(( int sizelimit )); /* - * in strdup.c + * in string.c */ -char *ldap_pvt_strdup LDAP_P(( const char * )); +char *ldap_int_strdup LDAP_P(( const char * )); #undef strdup -#define strdup ldap_pvt_strdup +#define strdup ldap_int_strdup /* * in unbind.c diff --git a/libraries/libldap/string.c b/libraries/libldap/string.c index c3fdb7b9c3..e2d142d958 100644 --- a/libraries/libldap/string.c +++ b/libraries/libldap/string.c @@ -96,10 +96,24 @@ char * char *p; size_t len = strlen( s ) + 1; - if ( (p = (char *) LDAP_MALLOC( len )) == NULL ) { + if ( (p = (char *) malloc( len )) == NULL ) { return( NULL ); } memcpy( p, s, len ); return( p ); } + +char * +(ldap_int_strdup)( const char *s ) +{ + char *p; + size_t len = strlen( s ) + 1; + + if ( (p = (char *) LDAP_MALLOC( len )) == NULL ) { + return( NULL ); + } + + memcpy( p, s, len ); + return( p ); +} \ No newline at end of file diff --git a/libraries/libldif/line64.c b/libraries/libldif/line64.c index 65ec9a750e..b4e4abbd07 100644 --- a/libraries/libldif/line64.c +++ b/libraries/libldif/line64.c @@ -300,14 +300,14 @@ ldif_put_type_and_value( char * ldif_type_and_value( LDAP_CONST char *type, LDAP_CONST char *val, int vlen ) /* - * return malloc'd, zero-terminated LDIF line + * return BER malloc'd, zero-terminated LDIF line */ { char *buf, *p; int tlen; tlen = strlen( type ); - if (( buf = (char *) malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) + if (( buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) == NULL ) { ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,