3 * Copyright 1998-2000 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 BER_MEM_JUNK 0xdeaddadaU
58 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_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;
115 #if 0 && defined( LDAP_MEMORY_DEBUG )
117 ber_int_memfree( void **p )
124 *p = BER_MEM_BADADDR;
129 ber_memfree( void *p )
131 ber_int_options.lbo_valid = LBER_INITIALIZED;
139 if( ber_int_memory_fns == NULL ) {
140 #ifdef LDAP_MEMORY_DEBUG
141 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
142 ((char *)p - sizeof(struct ber_mem_hdr));
143 assert( mh->bm_top == BER_MEM_JUNK);
144 assert( testdatatop( mh));
145 assert( testend( (char *)&mh[1] + mh->bm_length) );
146 ber_int_options.lbo_meminuse -= mh->bm_length;
148 #ifdef LDAP_MEMORY_TRACE
149 fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
150 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
152 /* Fill the free space with poison */
153 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
161 assert( ber_int_memory_fns->bmf_free );
163 (*ber_int_memory_fns->bmf_free)( p );
168 ber_memvfree( void **vec )
172 ber_int_options.lbo_valid = LBER_INITIALIZED;
178 BER_MEM_VALID( vec );
180 for ( i = 0; vec[i] != NULL; i++ ) {
189 ber_memalloc( ber_len_t s )
192 ber_int_options.lbo_valid = LBER_INITIALIZED;
194 #ifdef LDAP_MEMORY_DEBUG
202 if( ber_int_memory_fns == NULL ) {
203 #ifdef LDAP_MEMORY_DEBUG
204 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
205 if( mh == NULL ) return NULL;
207 mh->bm_top = BER_MEM_JUNK;
210 setend( (char *)&mh[1] + mh->bm_length );
212 ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
214 #ifdef LDAP_MEMORY_TRACE
215 mh->bm_sequence = sequence++;
216 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
217 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
219 /* poison new memory */
220 memset( (char *)&mh[1], 0xff, s);
222 BER_MEM_VALID( &mh[1] );
228 new = (*ber_int_memory_fns->bmf_malloc)( s );
232 ber_errno = LBER_ERROR_MEMORY;
240 ber_memcalloc( ber_len_t n, ber_len_t s )
243 ber_int_options.lbo_valid = LBER_INITIALIZED;
245 #ifdef LDAP_MEMORY_DEBUG
246 assert( n != 0 && s != 0);
249 if( n == 0 || s == 0 ) {
253 if( ber_int_memory_fns == NULL ) {
254 #ifdef LDAP_MEMORY_DEBUG
255 struct ber_mem_hdr *mh = calloc(1,
256 (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
257 if( mh == NULL ) return NULL;
259 mh->bm_top = BER_MEM_JUNK;
262 setend( (char *)&mh[1] + mh->bm_length );
264 ber_int_options.lbo_meminuse += mh->bm_length;
266 #ifdef LDAP_MEMORY_TRACE
267 mh->bm_sequence = sequence++;
268 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
269 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
271 BER_MEM_VALID( &mh[1] );
274 new = calloc( n, s );
278 new = (*ber_int_memory_fns->bmf_calloc)( n, s );
282 ber_errno = LBER_ERROR_MEMORY;
290 ber_memrealloc( void* p, ber_len_t s )
293 ber_int_options.lbo_valid = LBER_INITIALIZED;
295 /* realloc(NULL,s) -> malloc(s) */
297 return ber_memalloc( s );
300 /* realloc(p,0) -> free(p) */
308 if( ber_int_memory_fns == NULL ) {
309 #ifdef LDAP_MEMORY_DEBUG
311 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
312 ((char *)p - sizeof(struct ber_mem_hdr));
313 assert( mh->bm_top == BER_MEM_JUNK);
314 assert( testdatatop( mh));
315 assert( testend( (char *)&mh[1] + mh->bm_length) );
316 oldlen = mh->bm_length;
318 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
320 ber_errno = LBER_ERROR_MEMORY;
326 setend( (char *)&mh[1] + mh->bm_length );
327 if( (s - oldlen) > 0 ) {
328 /* poison any new memory */
329 memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
332 assert( mh->bm_top == BER_MEM_JUNK);
333 assert( testdatatop( mh));
335 ber_int_options.lbo_meminuse += s - oldlen;
336 #ifdef LDAP_MEMORY_TRACE
337 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
338 mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
340 BER_MEM_VALID( &mh[1] );
343 new = realloc( p, s );
346 new = (*ber_int_memory_fns->bmf_realloc)( p, s );
350 ber_errno = LBER_ERROR_MEMORY;
358 ber_bvfree( struct berval *bv )
360 ber_int_options.lbo_valid = LBER_INITIALIZED;
368 if ( bv->bv_val != NULL ) {
369 LBER_FREE( bv->bv_val );
372 LBER_FREE( (char *) bv );
377 ber_bvecfree( struct berval **bv )
381 ber_int_options.lbo_valid = LBER_INITIALIZED;
389 for ( i = 0; bv[i] != NULL; i++ ) {
393 LBER_FREE( (char *) bv );
397 ber_bvecadd( struct berval ***bvec, struct berval *bv )
402 ber_int_options.lbo_valid = LBER_INITIALIZED;
410 *bvec = ber_memalloc( 2 * sizeof(struct berval *) );
412 if( *bvec == NULL ) {
422 BER_MEM_VALID( bvec );
425 for ( i = 0; bvec[i] != NULL; i++ ) {
433 new = ber_memrealloc( *bvec, (i+2) * sizeof(struct berval *));
450 LDAP_CONST struct berval *bv )
454 ber_int_options.lbo_valid = LBER_INITIALIZED;
457 ber_errno = LBER_ERROR_PARAM;
461 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
462 ber_errno = LBER_ERROR_MEMORY;
466 if ( bv->bv_val == NULL ) {
472 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
473 ber_errno = LBER_ERROR_MEMORY;
478 AC_MEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
479 new->bv_val[bv->bv_len] = '\0';
480 new->bv_len = bv->bv_len;
491 ber_int_options.lbo_valid = LBER_INITIALIZED;
494 ber_errno = LBER_ERROR_PARAM;
498 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
499 ber_errno = LBER_ERROR_MEMORY;
503 new->bv_val = (char *) s;
504 new->bv_len = strlen( s );
516 ber_int_options.lbo_valid = LBER_INITIALIZED;
519 ber_errno = LBER_ERROR_PARAM;
523 p = LBER_STRDUP( s );
526 ber_errno = LBER_ERROR_MEMORY;
530 new = ber_bvstr( p );
532 if( new == NULL || *p == '\0' ) {
540 ber_strdup( LDAP_CONST char *s )
545 ber_int_options.lbo_valid = LBER_INITIALIZED;
547 #ifdef LDAP_MEMORY_DEBUG
548 assert(s != NULL); /* bv damn better point to something */
552 ber_errno = LBER_ERROR_PARAM;
556 len = strlen( s ) + 1;
558 if ( (p = LBER_MALLOC( len )) == NULL ) {
559 ber_errno = LBER_ERROR_MEMORY;
563 AC_MEMCPY( p, s, len );