3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #ifdef LDAP_MEMORY_TRACE
14 # ifndef LDAP_MEMORY_DEBUG
15 # define LDAP_MEMORY_DEBUG 1
22 * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
23 * debugging memory management within OpenLDAP libraries and slapd.
24 * It should only be enabled by an experienced developer as it
25 * causes the inclusion of numerous assert()'s, many of which may
26 * be triggered by a prefectly valid program.
28 * The code behind this macro is subject to change as needed to
29 * support this testing.
33 ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
34 ber_int_t bm_length; /* Length of user allocated area */
35 #ifdef LDAP_MEMORY_TRACE
36 ber_int_t bm_sequence; /* Allocation sequence number */
38 union bmu_align_u { /* Force alignment, pattern to detect back clobber */
47 long (*bmu_funcp)( double );
48 unsigned char bmu_char[4];
50 #define bm_junk ber_align.bmu_len_t
51 #define bm_data ber_align.bmu_char[1]
52 #define bm_char ber_align.bmu_char
55 /* Pattern at top of allocated space */
56 #define LLBER_MEM_JUNK 0xdeaddadaU
58 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
60 /* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
61 * thread safe. If you want to use these values for multithreaded applications,
62 * you must put mutexes around them, otherwise they will have incorrect values.
63 * When debugging, if you sort the debug output, the sequence number will
64 * put allocations/frees together. It is then a simple matter to write a script
65 * to find any allocations that don't have a buffer free function.
67 #ifdef LDAP_MEMORY_TRACE
68 static ber_int_t sequence = 0;
71 /* Pattern placed just before user data */
72 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
73 /* Pattern placed just after user data */
74 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
76 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
78 /* Test if pattern placed just before user data is good */
79 #define testdatatop(val) ( \
80 *(val->bm_char+mbu_len-4)==toppattern[0] && \
81 *(val->bm_char+mbu_len-3)==toppattern[1] && \
82 *(val->bm_char+mbu_len-2)==toppattern[2] && \
83 *(val->bm_char+mbu_len-1)==toppattern[3] )
85 /* Place pattern just before user data */
86 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
87 *(val->bm_char+mbu_len-3)=toppattern[1]; \
88 *(val->bm_char+mbu_len-2)=toppattern[2]; \
89 *(val->bm_char+mbu_len-1)=toppattern[3];
91 /* Test if pattern placed just after user data is good */
92 #define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
93 *((unsigned char *)val+1)==endpattern[1] && \
94 *((unsigned char *)val+2)==endpattern[2] && \
95 *((unsigned char *)val+3)==endpattern[3] )
97 /* Place pattern just after user data */
98 #define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
99 *((unsigned char *)val+1)=endpattern[1]; \
100 *((unsigned char *)val+2)=endpattern[2]; \
101 *((unsigned char *)val+3)=endpattern[3];
103 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
104 #define BER_MEM_VALID(p) do { \
105 assert( (p) != BER_MEM_BADADDR ); \
106 assert( (p) != (void *) &ber_int_mem_hdr ); \
110 #define BER_MEM_VALID(p) /* no-op */
113 BerMemoryFunctions *ber_int_memory_fns = NULL;
116 ber_memfree( void *p )
118 ber_int_options.lbo_valid = LBER_INITIALIZED;
126 if( ber_int_memory_fns == NULL ) {
127 #ifdef LDAP_MEMORY_DEBUG
128 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
129 ((char *)p - sizeof(struct ber_mem_hdr));
130 assert( mh->bm_top == LBER_MEM_JUNK);
131 assert( testdatatop( mh));
132 assert( testend( (char *)&mh[1] + mh->bm_length) );
133 ber_int_options.lbo_meminuse -= mh->bm_length;
135 #ifdef LDAP_MEMORY_TRACE
136 fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
137 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
139 /* Fill the free space with poison */
140 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
148 assert( ber_int_memory_fns->bmf_free );
150 (*ber_int_memory_fns->bmf_free)( p );
155 ber_memvfree( void **vec )
159 ber_int_options.lbo_valid = LBER_INITIALIZED;
165 BER_MEM_VALID( vec );
167 for ( i = 0; vec[i] != NULL; i++ ) {
176 ber_memalloc( ber_len_t s )
179 ber_int_options.lbo_valid = LBER_INITIALIZED;
181 #ifdef LDAP_MEMORY_DEBUG
189 if( ber_int_memory_fns == NULL ) {
190 #ifdef LDAP_MEMORY_DEBUG
191 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
192 if( mh == NULL ) return NULL;
194 mh->bm_top = LBER_MEM_JUNK;
197 setend( (char *)&mh[1] + mh->bm_length );
199 ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
201 #ifdef LDAP_MEMORY_TRACE
202 mh->bm_sequence = sequence++;
203 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
204 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
206 /* poison new memory */
207 memset( (char *)&mh[1], 0xff, s);
209 BER_MEM_VALID( &mh[1] );
215 new = (*ber_int_memory_fns->bmf_malloc)( s );
219 ber_errno = LBER_ERROR_MEMORY;
227 ber_memcalloc( ber_len_t n, ber_len_t s )
230 ber_int_options.lbo_valid = LBER_INITIALIZED;
232 #ifdef LDAP_MEMORY_DEBUG
233 assert( n != 0 && s != 0);
236 if( n == 0 || s == 0 ) {
240 if( ber_int_memory_fns == NULL ) {
241 #ifdef LDAP_MEMORY_DEBUG
242 struct ber_mem_hdr *mh = calloc(1,
243 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
244 if( mh == NULL ) return NULL;
246 mh->bm_top = LBER_MEM_JUNK;
249 setend( (char *)&mh[1] + mh->bm_length );
251 ber_int_options.lbo_meminuse += mh->bm_length;
253 #ifdef LDAP_MEMORY_TRACE
254 mh->bm_sequence = sequence++;
255 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
256 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
258 BER_MEM_VALID( &mh[1] );
261 new = calloc( n, s );
265 new = (*ber_int_memory_fns->bmf_calloc)( n, s );
269 ber_errno = LBER_ERROR_MEMORY;
277 ber_memrealloc( void* p, ber_len_t s )
280 ber_int_options.lbo_valid = LBER_INITIALIZED;
282 /* realloc(NULL,s) -> malloc(s) */
284 return LBER_MALLOC( s );
287 /* realloc(p,0) -> free(p) */
295 if( ber_int_memory_fns == NULL ) {
296 #ifdef LDAP_MEMORY_DEBUG
298 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
299 ((char *)p - sizeof(struct ber_mem_hdr));
300 assert( mh->bm_top == LBER_MEM_JUNK);
301 assert( testdatatop( mh));
302 assert( testend( (char *)&mh[1] + mh->bm_length) );
303 oldlen = mh->bm_length;
305 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
307 ber_errno = LBER_ERROR_MEMORY;
313 setend( (char *)&mh[1] + mh->bm_length );
314 if( (s - oldlen) > 0 ) {
315 /* poison any new memory */
316 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
319 assert( mh->bm_top == LBER_MEM_JUNK);
320 assert( testdatatop( mh));
322 ber_int_options.lbo_meminuse += s - oldlen;
323 #ifdef LDAP_MEMORY_TRACE
324 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
325 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
327 BER_MEM_VALID( &mh[1] );
330 new = realloc( p, s );
333 new = (*ber_int_memory_fns->bmf_realloc)( p, s );
337 ber_errno = LBER_ERROR_MEMORY;
345 ber_bvfree( struct berval *bv )
347 ber_int_options.lbo_valid = LBER_INITIALIZED;
355 if ( bv->bv_val != NULL ) {
356 LBER_FREE( bv->bv_val );
359 LBER_FREE( (char *) bv );
364 ber_bvecfree( struct berval **bv )
368 ber_int_options.lbo_valid = LBER_INITIALIZED;
376 for ( i = 0; bv[i] != NULL; i++ ) {
380 LBER_FREE( (char *) bv );
384 ber_bvecadd( struct berval ***bvec, struct berval *bv )
389 ber_int_options.lbo_valid = LBER_INITIALIZED;
391 if( *bvec == NULL ) {
397 *bvec = LBER_MALLOC( 2 * sizeof(struct berval *) );
399 if( *bvec == NULL ) {
409 BER_MEM_VALID( bvec );
412 for ( i = 0; (*bvec)[i] != NULL; i++ ) {
420 new = LBER_REALLOC( *bvec, (i+2) * sizeof(struct berval *));
437 struct berval *dst, struct berval *src )
441 ber_int_options.lbo_valid = LBER_INITIALIZED;
444 ber_errno = LBER_ERROR_PARAM;
451 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
452 ber_errno = LBER_ERROR_MEMORY;
457 if ( src->bv_val == NULL ) {
463 if(( new->bv_val = LBER_MALLOC( src->bv_len + 1 )) == NULL ) {
464 ber_errno = LBER_ERROR_MEMORY;
470 AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
471 new->bv_val[src->bv_len] = '\0';
472 new->bv_len = src->bv_len;
482 return ber_dupbv( NULL, src );
488 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
492 ber_int_options.lbo_valid = LBER_INITIALIZED;
495 ber_errno = LBER_ERROR_PARAM;
502 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
503 ber_errno = LBER_ERROR_MEMORY;
508 new->bv_len = len ? len : strlen( s );
510 if ( (new->bv_val = LBER_MALLOC( new->bv_len+1 )) == NULL ) {
511 ber_errno = LBER_ERROR_MEMORY;
517 AC_MEMCPY( new->bv_val, s, new->bv_len );
518 new->bv_val[new->bv_len] = '\0';
520 new->bv_val = (char *) s;
527 ber_strdup( LDAP_CONST char *s )
532 ber_int_options.lbo_valid = LBER_INITIALIZED;
534 #ifdef LDAP_MEMORY_DEBUG
535 assert(s != NULL); /* bv damn better point to something */
539 ber_errno = LBER_ERROR_PARAM;
543 len = strlen( s ) + 1;
545 if ( (p = LBER_MALLOC( len )) == NULL ) {
546 ber_errno = LBER_ERROR_MEMORY;
550 AC_MEMCPY( p, s, len );
555 ber_strndup( LDAP_CONST char *s, ber_len_t l )
560 ber_int_options.lbo_valid = LBER_INITIALIZED;
562 #ifdef LDAP_MEMORY_DEBUG
563 assert(s != NULL); /* bv damn better point to something */
567 ber_errno = LBER_ERROR_PARAM;
577 if ( (p = LBER_MALLOC( len + 1 )) == NULL ) {
578 ber_errno = LBER_ERROR_MEMORY;
582 AC_MEMCPY( p, s, len );
588 ber_strndup__( LDAP_CONST char *s, size_t l )
598 if (( p = LBER_MALLOC( len + 1 ) ) == NULL ) {
602 AC_MEMCPY( p, s, len );