2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
18 #include <ac/stdlib.h>
19 #include <ac/string.h>
23 #ifdef LDAP_MEMORY_TRACE
24 # ifndef LDAP_MEMORY_DEBUG
25 # define LDAP_MEMORY_DEBUG 1
32 * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
33 * debugging memory management within OpenLDAP libraries and slapd.
34 * It should only be enabled by an experienced developer as it
35 * causes the inclusion of numerous assert()'s, many of which may
36 * be triggered by a prefectly valid program.
38 * The code behind this macro is subject to change as needed to
39 * support this testing.
43 ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
44 ber_int_t bm_length; /* Length of user allocated area */
45 #ifdef LDAP_MEMORY_TRACE
46 ber_int_t bm_sequence; /* Allocation sequence number */
48 union bmu_align_u { /* Force alignment, pattern to detect back clobber */
57 long (*bmu_funcp)( double );
58 unsigned char bmu_char[4];
60 #define bm_junk ber_align.bmu_len_t
61 #define bm_data ber_align.bmu_char[1]
62 #define bm_char ber_align.bmu_char
65 /* Pattern at top of allocated space */
66 #define LBER_MEM_JUNK 0xdeaddadaU
68 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
70 /* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
71 * thread safe. If you want to use these values for multithreaded applications,
72 * you must put mutexes around them, otherwise they will have incorrect values.
73 * When debugging, if you sort the debug output, the sequence number will
74 * put allocations/frees together. It is then a simple matter to write a script
75 * to find any allocations that don't have a buffer free function.
77 #ifdef LDAP_MEMORY_TRACE
78 static ber_int_t sequence = 0;
81 /* Pattern placed just before user data */
82 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
83 /* Pattern placed just after user data */
84 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
86 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
88 /* Test if pattern placed just before user data is good */
89 #define testdatatop(val) ( \
90 *(val->bm_char+mbu_len-4)==toppattern[0] && \
91 *(val->bm_char+mbu_len-3)==toppattern[1] && \
92 *(val->bm_char+mbu_len-2)==toppattern[2] && \
93 *(val->bm_char+mbu_len-1)==toppattern[3] )
95 /* Place pattern just before user data */
96 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
97 *(val->bm_char+mbu_len-3)=toppattern[1]; \
98 *(val->bm_char+mbu_len-2)=toppattern[2]; \
99 *(val->bm_char+mbu_len-1)=toppattern[3];
101 /* Test if pattern placed just after user data is good */
102 #define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
103 *((unsigned char *)val+1)==endpattern[1] && \
104 *((unsigned char *)val+2)==endpattern[2] && \
105 *((unsigned char *)val+3)==endpattern[3] )
107 /* Place pattern just after user data */
108 #define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
109 *((unsigned char *)val+1)=endpattern[1]; \
110 *((unsigned char *)val+2)=endpattern[2]; \
111 *((unsigned char *)val+3)=endpattern[3];
113 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
114 #define BER_MEM_VALID(p) do { \
115 assert( (p) != BER_MEM_BADADDR ); \
116 assert( (p) != (void *) &ber_int_mem_hdr ); \
120 #define BER_MEM_VALID(p) /* no-op */
123 BerMemoryFunctions *ber_int_memory_fns = NULL;
126 ber_memfree_x( void *p, void *ctx )
128 ber_int_options.lbo_valid = LBER_INITIALIZED;
136 if( ber_int_memory_fns == NULL || ctx == NULL ) {
137 #ifdef LDAP_MEMORY_DEBUG
138 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
139 ((char *)p - sizeof(struct ber_mem_hdr));
140 assert( mh->bm_top == LBER_MEM_JUNK);
141 assert( testdatatop( mh));
142 assert( testend( (char *)&mh[1] + mh->bm_length) );
143 ber_int_options.lbo_meminuse -= mh->bm_length;
145 #ifdef LDAP_MEMORY_TRACE
146 fprintf(stderr, "0x%08lx 0x%08lx -f- %ld ber_memfree %ld\n",
147 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
148 ber_int_options.lbo_meminuse);
150 /* Fill the free space with poison */
151 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
159 assert( ber_int_memory_fns->bmf_free != 0 );
161 (*ber_int_memory_fns->bmf_free)( p, ctx );
165 ber_memfree( void *p )
167 ber_memfree_x(p, NULL);
171 ber_memvfree_x( void **vec, void *ctx )
175 ber_int_options.lbo_valid = LBER_INITIALIZED;
181 BER_MEM_VALID( vec );
183 for ( i = 0; vec[i] != NULL; i++ ) {
184 ber_memfree_x( vec[i], ctx );
187 ber_memfree_x( vec, ctx );
191 ber_memvfree( void **vec )
193 ber_memvfree_x( vec, NULL );
197 ber_memalloc_x( ber_len_t s, void *ctx )
200 ber_int_options.lbo_valid = LBER_INITIALIZED;
202 #ifdef LDAP_MEMORY_DEBUG
210 if( ber_int_memory_fns == NULL || ctx == NULL ) {
211 #ifdef LDAP_MEMORY_DEBUG
212 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
213 if( mh == NULL ) return NULL;
215 mh->bm_top = LBER_MEM_JUNK;
218 setend( (char *)&mh[1] + mh->bm_length );
220 ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
222 #ifdef LDAP_MEMORY_TRACE
223 mh->bm_sequence = sequence++;
224 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memalloc %ld\n",
225 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
226 ber_int_options.lbo_meminuse);
228 /* poison new memory */
229 memset( (char *)&mh[1], 0xff, s);
231 BER_MEM_VALID( &mh[1] );
237 new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
241 ber_errno = LBER_ERROR_MEMORY;
248 ber_memalloc( ber_len_t s )
250 return ber_memalloc_x( s, NULL );
254 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
257 ber_int_options.lbo_valid = LBER_INITIALIZED;
259 #ifdef LDAP_MEMORY_DEBUG
260 assert( n != 0 && s != 0);
263 if( n == 0 || s == 0 ) {
267 if( ber_int_memory_fns == NULL || ctx == NULL ) {
268 #ifdef LDAP_MEMORY_DEBUG
269 struct ber_mem_hdr *mh = calloc(1,
270 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
271 if( mh == NULL ) return NULL;
273 mh->bm_top = LBER_MEM_JUNK;
276 setend( (char *)&mh[1] + mh->bm_length );
278 ber_int_options.lbo_meminuse += mh->bm_length;
280 #ifdef LDAP_MEMORY_TRACE
281 mh->bm_sequence = sequence++;
282 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memcalloc %ld\n",
283 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
284 ber_int_options.lbo_meminuse);
286 BER_MEM_VALID( &mh[1] );
289 new = calloc( n, s );
293 new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
297 ber_errno = LBER_ERROR_MEMORY;
304 ber_memcalloc( ber_len_t n, ber_len_t s )
306 return ber_memcalloc_x( n, s, NULL );
310 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
313 ber_int_options.lbo_valid = LBER_INITIALIZED;
315 /* realloc(NULL,s) -> malloc(s) */
317 return ber_memalloc_x( s, ctx );
320 /* realloc(p,0) -> free(p) */
322 ber_memfree_x( p, ctx );
328 if( ber_int_memory_fns == NULL || ctx == NULL ) {
329 #ifdef LDAP_MEMORY_DEBUG
331 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
332 ((char *)p - sizeof(struct ber_mem_hdr));
333 assert( mh->bm_top == LBER_MEM_JUNK);
334 assert( testdatatop( mh));
335 assert( testend( (char *)&mh[1] + mh->bm_length) );
336 oldlen = mh->bm_length;
338 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
340 ber_errno = LBER_ERROR_MEMORY;
346 setend( (char *)&mh[1] + mh->bm_length );
348 /* poison any new memory */
349 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
352 assert( mh->bm_top == LBER_MEM_JUNK);
353 assert( testdatatop( mh));
355 ber_int_options.lbo_meminuse += s - oldlen;
356 #ifdef LDAP_MEMORY_TRACE
357 fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memrealloc %ld\n",
358 (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
359 ber_int_options.lbo_meminuse);
361 BER_MEM_VALID( &mh[1] );
364 new = realloc( p, s );
367 new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
371 ber_errno = LBER_ERROR_MEMORY;
378 ber_memrealloc( void* p, ber_len_t s )
380 return ber_memrealloc_x( p, s, NULL );
384 ber_bvfree_x( struct berval *bv, void *ctx )
386 ber_int_options.lbo_valid = LBER_INITIALIZED;
394 if ( bv->bv_val != NULL ) {
395 ber_memfree_x( bv->bv_val, ctx );
398 ber_memfree_x( (char *) bv, ctx );
402 ber_bvfree( struct berval *bv )
404 ber_bvfree_x( bv, NULL );
408 ber_bvecfree_x( struct berval **bv, void *ctx )
412 ber_int_options.lbo_valid = LBER_INITIALIZED;
421 for ( i = 0; bv[i] != NULL; i++ ) ;
423 /* free in reverse order */
424 for ( i--; i >= 0; i-- ) {
425 ber_bvfree_x( bv[i], ctx );
428 ber_memfree_x( (char *) bv, ctx );
432 ber_bvecfree( struct berval **bv )
434 ber_bvecfree_x( bv, NULL );
438 ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
443 ber_int_options.lbo_valid = LBER_INITIALIZED;
445 if( *bvec == NULL ) {
451 *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
453 if( *bvec == NULL ) {
463 BER_MEM_VALID( bvec );
466 for ( i = 0; (*bvec)[i] != NULL; i++ ) {
474 new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
489 ber_bvecadd( struct berval ***bvec, struct berval *bv )
491 return ber_bvecadd_x( bvec, bv, NULL );
496 struct berval *dst, struct berval *src, void *ctx )
500 ber_int_options.lbo_valid = LBER_INITIALIZED;
503 ber_errno = LBER_ERROR_PARAM;
510 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
511 ber_errno = LBER_ERROR_MEMORY;
516 if ( src->bv_val == NULL ) {
522 if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
523 ber_errno = LBER_ERROR_MEMORY;
525 ber_memfree_x( new, ctx );
529 AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
530 new->bv_val[src->bv_len] = '\0';
531 new->bv_len = src->bv_len;
538 struct berval *dst, struct berval *src )
540 return ber_dupbv_x( dst, src, NULL );
547 return ber_dupbv_x( NULL, src, NULL );
552 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
557 ber_int_options.lbo_valid = LBER_INITIALIZED;
560 ber_errno = LBER_ERROR_PARAM;
567 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
568 ber_errno = LBER_ERROR_MEMORY;
573 new->bv_len = len ? len : strlen( s );
575 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
576 ber_errno = LBER_ERROR_MEMORY;
578 ber_memfree_x( new, ctx );
582 AC_MEMCPY( new->bv_val, s, new->bv_len );
583 new->bv_val[new->bv_len] = '\0';
585 new->bv_val = (char *) s;
593 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
595 return ber_str2bv_x( s, len, dup, bv, NULL );
600 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
605 ber_int_options.lbo_valid = LBER_INITIALIZED;
608 ber_errno = LBER_ERROR_PARAM;
615 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
616 ber_errno = LBER_ERROR_MEMORY;
623 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
624 ber_errno = LBER_ERROR_MEMORY;
626 ber_memfree_x( new, ctx );
631 AC_MEMCPY( new->bv_val, s, new->bv_len );
632 new->bv_val[new->bv_len] = '\0';
634 new->bv_val = (char *) s;
642 LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
644 return ber_mem2bv_x( s, len, dup, bv, NULL );
648 ber_strdup_x( LDAP_CONST char *s, void *ctx )
653 ber_int_options.lbo_valid = LBER_INITIALIZED;
655 #ifdef LDAP_MEMORY_DEBUG
656 assert(s != NULL); /* bv damn better point to something */
660 ber_errno = LBER_ERROR_PARAM;
664 len = strlen( s ) + 1;
666 if ( (p = ber_memalloc_x( len, ctx )) == NULL ) {
667 ber_errno = LBER_ERROR_MEMORY;
671 AC_MEMCPY( p, s, len );
676 ber_strdup( LDAP_CONST char *s )
678 return ber_strdup_x( s, NULL );
682 ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
687 ber_int_options.lbo_valid = LBER_INITIALIZED;
689 #ifdef LDAP_MEMORY_DEBUG
690 assert(s != NULL); /* bv damn better point to something */
694 ber_errno = LBER_ERROR_PARAM;
704 if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) {
705 ber_errno = LBER_ERROR_MEMORY;
709 AC_MEMCPY( p, s, len );
715 ber_strndup( LDAP_CONST char *s, ber_len_t l )
717 return ber_strndup_x( s, l, NULL );
721 * dst is resized as required by src and the value of src is copied into dst
722 * dst->bv_val must be NULL (and dst->bv_len must be 0), or it must be
723 * alloc'ed with the context ctx
726 ber_bvreplace_x( struct berval *dst, LDAP_CONST struct berval *src, void *ctx )
728 assert( dst != NULL );
730 if ( dst->bv_len < src->bv_len ) {
731 dst->bv_val = ber_memrealloc_x( dst->bv_val, src->bv_len + 1, ctx );
734 AC_MEMCPY( dst->bv_val, src->bv_val, src->bv_len + 1 );
735 dst->bv_len = src->bv_len;
741 ber_bvreplace( struct berval *dst, LDAP_CONST struct berval *src )
743 return ber_bvreplace_x( dst, src, NULL );
747 ber_bvarray_free_x( BerVarray a, void *ctx )
751 ber_int_options.lbo_valid = LBER_INITIALIZED;
757 for (i=0; a[i].bv_val; i++) ;
759 /* free in reverse order */
760 for (i--; i>=0; i--) {
761 ber_memfree_x(a[i].bv_val, ctx);
764 ber_memfree_x(a, ctx);
769 ber_bvarray_free( BerVarray a )
771 ber_bvarray_free_x(a, NULL);
775 ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
779 ber_int_options.lbo_valid = LBER_INITIALIZED;
787 *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
796 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
797 ; /* just count them */
804 atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
805 (n + 2) * sizeof(BerValue), ctx );
815 (*a)[n].bv_val = NULL;
821 ber_bvarray_add( BerVarray *a, BerValue *bv )
823 return ber_bvarray_add_x( a, bv, NULL );