3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
15 * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
16 * debugging memory management within OpenLDAP libraries and slapd.
17 * It should only be enabled by an experienced developer as it
18 * causes the inclusion of numerous assert()'s, many of which may
19 * be triggered by a prefectly valid program.
21 * The code behind this macro is subject to change as needed to
22 * support this testing.
35 long (*bmu_funcp)( double );
38 #define bm_junk ber_align.bmu_len_t
39 #define bm_data ber_align.bmu_char[1]
41 #define BER_MEM_JUNK 0xdeaddadaU
42 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
43 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
44 #define BER_MEM_VALID(p) do { \
45 assert( (p) != BER_MEM_BADADDR ); \
46 assert( (p) != (void *) &ber_int_mem_hdr ); \
50 #define BER_MEM_VALID(p) /* no-op */
53 BerMemoryFunctions *ber_int_memory_fns = NULL;
55 #if 0 && defined( LDAP_MEMORY_DEBUG )
57 ber_int_memfree( void **p )
69 ber_memfree( void *p )
71 ber_int_options.lbo_valid = LBER_INITIALIZED;
79 if( ber_int_memory_fns == NULL ) {
80 #ifdef LDAP_MEMORY_DEBUG
81 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
82 ((char *)p - sizeof(struct ber_mem_hdr));
84 assert( mh->bm_junk == BER_MEM_JUNK );
85 mh->bm_junk = ~BER_MEM_JUNK;
93 assert( ber_int_memory_fns->bmf_free );
95 (*ber_int_memory_fns->bmf_free)( p );
100 ber_memvfree( void **vec )
104 ber_int_options.lbo_valid = LBER_INITIALIZED;
110 BER_MEM_VALID( vec );
112 for ( i = 0; vec[i] != NULL; i++ ) {
121 ber_memalloc( ber_len_t s )
124 ber_int_options.lbo_valid = LBER_INITIALIZED;
126 #ifdef LDAP_MEMORY_DEBUG
134 if( ber_int_memory_fns == NULL ) {
135 #ifdef LDAP_MEMORY_DEBUG
136 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
138 if( mh == NULL ) return NULL;
140 mh->bm_junk = BER_MEM_JUNK;
142 BER_MEM_VALID( &mh[1] );
148 new = (*ber_int_memory_fns->bmf_malloc)( s );
152 ber_errno = LBER_ERROR_MEMORY;
160 ber_memcalloc( ber_len_t n, ber_len_t s )
163 ber_int_options.lbo_valid = LBER_INITIALIZED;
165 #ifdef LDAP_MEMORY_DEBUG
166 assert( n != 0 && s != 0);
169 if( n == 0 || s == 0 ) {
173 if( ber_int_memory_fns == NULL ) {
174 #ifdef LDAP_MEMORY_DEBUG
175 struct ber_mem_hdr *mh = calloc(1,
176 (n * s) + sizeof(struct ber_mem_hdr) );
178 mh->bm_junk = BER_MEM_JUNK;
180 BER_MEM_VALID( &mh[1] );
183 new = calloc( n, s );
187 new = (*ber_int_memory_fns->bmf_calloc)( n, s );
191 ber_errno = LBER_ERROR_MEMORY;
199 ber_memrealloc( void* p, ber_len_t s )
202 ber_int_options.lbo_valid = LBER_INITIALIZED;
204 /* realloc(NULL,s) -> malloc(s) */
206 return ber_memalloc( s );
209 /* realloc(p,0) -> free(p) */
217 if( ber_int_memory_fns == NULL ) {
218 #ifdef LDAP_MEMORY_DEBUG
219 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
220 ((char *)p - sizeof(struct ber_mem_hdr));
221 assert( mh->bm_junk == BER_MEM_JUNK );
223 p = realloc( mh, s + sizeof(struct ber_mem_hdr) );
228 assert( mh->bm_junk == BER_MEM_JUNK );
230 BER_MEM_VALID( &mh[1] );
234 new = realloc( p, s );
237 new = (*ber_int_memory_fns->bmf_realloc)( p, s );
241 ber_errno = LBER_ERROR_MEMORY;
249 ber_bvfree( struct berval *bv )
251 ber_int_options.lbo_valid = LBER_INITIALIZED;
259 if ( bv->bv_val != NULL )
260 LBER_FREE( bv->bv_val );
262 LBER_FREE( (char *) bv );
267 ber_bvecfree( struct berval **bv )
271 ber_int_options.lbo_valid = LBER_INITIALIZED;
279 for ( i = 0; bv[i] != NULL; i++ )
282 LBER_FREE( (char *) bv );
286 ber_bvecadd( struct berval ***bvec, struct berval *bv )
291 ber_int_options.lbo_valid = LBER_INITIALIZED;
299 *bvec = ber_memalloc( 2 * sizeof(struct berval *) );
301 if( *bvec == NULL ) {
311 BER_MEM_VALID( bvec );
314 for ( i = 0; bvec[i] != NULL; i++ ) {
322 new = ber_memrealloc( *bvec, (i+2) * sizeof(struct berval *));
339 LDAP_CONST struct berval *bv )
343 ber_int_options.lbo_valid = LBER_INITIALIZED;
346 ber_errno = LBER_ERROR_PARAM;
350 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
351 ber_errno = LBER_ERROR_MEMORY;
355 if ( bv->bv_val == NULL ) {
361 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
362 ber_errno = LBER_ERROR_MEMORY;
367 SAFEMEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
368 new->bv_val[bv->bv_len] = '\0';
369 new->bv_len = bv->bv_len;
380 ber_int_options.lbo_valid = LBER_INITIALIZED;
383 ber_errno = LBER_ERROR_PARAM;
387 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
388 ber_errno = LBER_ERROR_MEMORY;
398 new->bv_val = (char *) s;
399 new->bv_len = strlen( s );
411 ber_int_options.lbo_valid = LBER_INITIALIZED;
414 ber_errno = LBER_ERROR_PARAM;
418 p = LBER_STRDUP( s );
421 ber_errno = LBER_ERROR_MEMORY;
425 new = ber_bvstr( p );
427 if( new == NULL || *p == '\0' ) {
435 ber_strdup( LDAP_CONST char *s )
440 ber_int_options.lbo_valid = LBER_INITIALIZED;
442 #ifdef LDAP_MEMORY_DEBUG
443 assert(s != NULL); /* bv damn better point to something */
447 ber_errno = LBER_ERROR_PARAM;
451 len = strlen( s ) + 1;
453 if ( (p = LBER_MALLOC( len )) == NULL ) {
454 ber_errno = LBER_ERROR_MEMORY;
458 SAFEMEMCPY( p, s, len );