3 * Copyright 1998-2003 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 LBER_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_x( void *p, void *ctx )
118 ber_int_options.lbo_valid = LBER_INITIALIZED;
126 if( ber_int_memory_fns == NULL || ctx == 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, ctx );
154 ber_memfree( void *p )
156 ber_memfree_x(p, NULL);
160 ber_memvfree_x( void **vec, void *ctx )
164 ber_int_options.lbo_valid = LBER_INITIALIZED;
170 BER_MEM_VALID( vec );
172 for ( i = 0; vec[i] != NULL; i++ ) {
173 ber_memfree_x( vec[i], ctx );
176 ber_memfree_x( vec, ctx );
180 ber_memvfree( void **vec )
182 ber_memvfree_x( vec, NULL );
186 ber_memalloc_x( ber_len_t s, void *ctx )
189 ber_int_options.lbo_valid = LBER_INITIALIZED;
191 #ifdef LDAP_MEMORY_DEBUG
199 if( ber_int_memory_fns == NULL || ctx == NULL ) {
200 #ifdef LDAP_MEMORY_DEBUG
201 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
202 if( mh == NULL ) return NULL;
204 mh->bm_top = LBER_MEM_JUNK;
207 setend( (char *)&mh[1] + mh->bm_length );
209 ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
211 #ifdef LDAP_MEMORY_TRACE
212 mh->bm_sequence = sequence++;
213 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
214 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
216 /* poison new memory */
217 memset( (char *)&mh[1], 0xff, s);
219 BER_MEM_VALID( &mh[1] );
225 new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
229 ber_errno = LBER_ERROR_MEMORY;
236 ber_memalloc( ber_len_t s )
238 return ber_memalloc_x( s, NULL );
242 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
245 ber_int_options.lbo_valid = LBER_INITIALIZED;
247 #ifdef LDAP_MEMORY_DEBUG
248 assert( n != 0 && s != 0);
251 if( n == 0 || s == 0 ) {
255 if( ber_int_memory_fns == NULL || ctx == NULL ) {
256 #ifdef LDAP_MEMORY_DEBUG
257 struct ber_mem_hdr *mh = calloc(1,
258 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
259 if( mh == NULL ) return NULL;
261 mh->bm_top = LBER_MEM_JUNK;
264 setend( (char *)&mh[1] + mh->bm_length );
266 ber_int_options.lbo_meminuse += mh->bm_length;
268 #ifdef LDAP_MEMORY_TRACE
269 mh->bm_sequence = sequence++;
270 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
271 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
273 BER_MEM_VALID( &mh[1] );
276 new = calloc( n, s );
280 new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
284 ber_errno = LBER_ERROR_MEMORY;
291 ber_memcalloc( ber_len_t n, ber_len_t s )
293 return ber_memcalloc_x( n, s, NULL );
297 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
300 ber_int_options.lbo_valid = LBER_INITIALIZED;
302 /* realloc(NULL,s) -> malloc(s) */
304 return ber_memalloc_x( s, ctx );
307 /* realloc(p,0) -> free(p) */
309 ber_memfree_x( p, ctx );
315 if( ber_int_memory_fns == NULL || ctx == NULL ) {
316 #ifdef LDAP_MEMORY_DEBUG
318 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
319 ((char *)p - sizeof(struct ber_mem_hdr));
320 assert( mh->bm_top == LBER_MEM_JUNK);
321 assert( testdatatop( mh));
322 assert( testend( (char *)&mh[1] + mh->bm_length) );
323 oldlen = mh->bm_length;
325 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
327 ber_errno = LBER_ERROR_MEMORY;
333 setend( (char *)&mh[1] + mh->bm_length );
334 if( (s - oldlen) > 0 ) {
335 /* poison any new memory */
336 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
339 assert( mh->bm_top == LBER_MEM_JUNK);
340 assert( testdatatop( mh));
342 ber_int_options.lbo_meminuse += s - oldlen;
343 #ifdef LDAP_MEMORY_TRACE
344 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
345 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
347 BER_MEM_VALID( &mh[1] );
350 new = realloc( p, s );
353 new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
357 ber_errno = LBER_ERROR_MEMORY;
364 ber_memrealloc( void* p, ber_len_t s )
366 return ber_memrealloc_x( p, s, NULL );
370 ber_bvfree_x( struct berval *bv, void *ctx )
372 ber_int_options.lbo_valid = LBER_INITIALIZED;
380 if ( bv->bv_val != NULL ) {
381 ber_memfree_x( bv->bv_val, ctx );
384 ber_memfree_x( (char *) bv, ctx );
388 ber_bvfree( struct berval *bv )
390 ber_bvfree_x( bv, NULL );
394 ber_bvecfree_x( struct berval **bv, void *ctx )
398 ber_int_options.lbo_valid = LBER_INITIALIZED;
407 for ( i = 0; bv[i] != NULL; i++ ) ;
409 /* free in reverse order */
410 for ( i--; i >= 0; i-- ) {
411 ber_bvfree_x( bv[i], ctx );
414 ber_memfree_x( (char *) bv, ctx );
418 ber_bvecfree( struct berval **bv )
420 ber_bvecfree_x( bv, NULL );
424 ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
429 ber_int_options.lbo_valid = LBER_INITIALIZED;
431 if( *bvec == NULL ) {
437 *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
439 if( *bvec == NULL ) {
449 BER_MEM_VALID( bvec );
452 for ( i = 0; (*bvec)[i] != NULL; i++ ) {
460 new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
475 ber_bvecadd( struct berval ***bvec, struct berval *bv )
477 return ber_bvecadd_x( bvec, bv, NULL );
482 struct berval *dst, struct berval *src, void *ctx )
486 ber_int_options.lbo_valid = LBER_INITIALIZED;
489 ber_errno = LBER_ERROR_PARAM;
496 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
497 ber_errno = LBER_ERROR_MEMORY;
502 if ( src->bv_val == NULL ) {
508 if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
509 ber_errno = LBER_ERROR_MEMORY;
511 ber_memfree_x( new, ctx );
515 AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
516 new->bv_val[src->bv_len] = '\0';
517 new->bv_len = src->bv_len;
524 struct berval *dst, struct berval *src )
526 return ber_dupbv_x( dst, src, NULL );
533 return ber_dupbv_x( NULL, src, NULL );
538 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
543 ber_int_options.lbo_valid = LBER_INITIALIZED;
546 ber_errno = LBER_ERROR_PARAM;
553 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
554 ber_errno = LBER_ERROR_MEMORY;
559 new->bv_len = len ? len : strlen( s );
561 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
562 ber_errno = LBER_ERROR_MEMORY;
564 ber_memfree_x( new, ctx );
568 AC_MEMCPY( new->bv_val, s, new->bv_len );
569 new->bv_val[new->bv_len] = '\0';
571 new->bv_val = (char *) s;
579 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
581 return ber_str2bv_x( s, len, dup, bv, NULL );
586 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
591 ber_int_options.lbo_valid = LBER_INITIALIZED;
594 ber_errno = LBER_ERROR_PARAM;
601 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
602 ber_errno = LBER_ERROR_MEMORY;
609 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
610 ber_errno = LBER_ERROR_MEMORY;
612 ber_memfree_x( new, ctx );
617 AC_MEMCPY( new->bv_val, s, new->bv_len );
618 new->bv_val[new->bv_len] = '\0';
620 new->bv_val = (char *) s;
628 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
630 return ber_mem2bv_x( s, len, dup, bv, NULL );
634 ber_strdup_x( LDAP_CONST char *s, void *ctx )
639 ber_int_options.lbo_valid = LBER_INITIALIZED;
641 #ifdef LDAP_MEMORY_DEBUG
642 assert(s != NULL); /* bv damn better point to something */
646 ber_errno = LBER_ERROR_PARAM;
650 len = strlen( s ) + 1;
652 if ( (p = ber_memalloc_x( len, ctx )) == NULL ) {
653 ber_errno = LBER_ERROR_MEMORY;
657 AC_MEMCPY( p, s, len );
662 ber_strdup( LDAP_CONST char *s )
664 return ber_strdup_x( s, NULL );
668 ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
673 ber_int_options.lbo_valid = LBER_INITIALIZED;
675 #ifdef LDAP_MEMORY_DEBUG
676 assert(s != NULL); /* bv damn better point to something */
680 ber_errno = LBER_ERROR_PARAM;
690 if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) {
691 ber_errno = LBER_ERROR_MEMORY;
695 AC_MEMCPY( p, s, len );
701 ber_strndup( LDAP_CONST char *s, ber_len_t l )
703 return ber_strndup_x( s, l, NULL );
707 ber_bvarray_free_x( BerVarray a, void *ctx )
711 ber_int_options.lbo_valid = LBER_INITIALIZED;
717 for (i=0; a[i].bv_val; i++) ;
719 /* free in reverse order */
720 for (i--; i>=0; i--) {
721 ber_memfree_x(a[i].bv_val, ctx);
724 ber_memfree_x(a, ctx);
729 ber_bvarray_free( BerVarray a )
731 ber_bvarray_free_x(a, NULL);
735 ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
739 ber_int_options.lbo_valid = LBER_INITIALIZED;
747 *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
756 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
757 ; /* just count them */
764 atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
765 (n + 2) * sizeof(BerValue), ctx );
775 (*a)[n].bv_val = NULL;
781 ber_bvarray_add( BerVarray *a, BerValue *bv )
783 return ber_bvarray_add_x( a, bv, NULL );