2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #define LDAP_F_PRE LDAP_F_EXPORT
24 #define bm_junk ber_align.bmu_size_t
25 #define bm_data ber_align.bmu_char[1]
27 #define BER_MEM_JUNK 0xddeeddeeU
28 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
29 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
30 #define BER_MEM_VALID(p) do { \
31 assert( (p) != BER_MEM_BADADDR ); \
32 assert( (p) != (void *) &ber_int_mem_hdr ); \
35 #define BER_MEM_VALID(p) /* no-op */
38 BerMemoryFunctions *ber_int_memory_fns = NULL;
40 #if 0 && defined( LDAP_MEMORY_DEBUG )
42 ber_int_memfree( void **p )
54 ber_memfree( void *p )
56 ber_int_options.lbo_valid = LBER_INITIALIZED;
58 /* ignore p == NULL when not debugging */
65 if( ber_int_memory_fns == NULL ) {
66 #ifdef LDAP_MEMORY_DEBUG
67 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
68 ((char *)p - sizeof(struct ber_mem_hdr));
70 assert( mh->bm_junk == BER_MEM_JUNK );
71 mh->bm_junk = ~BER_MEM_JUNK;
79 assert( ber_int_memory_fns->bmf_free );
82 (*ber_int_memory_fns->bmf_free)( p );
87 ber_memvfree( void **vec )
91 ber_int_options.lbo_valid = LBER_INITIALIZED;
99 for ( i = 0; vec[i] != NULL; i++ ) {
108 ber_memalloc( size_t s )
110 ber_int_options.lbo_valid = LBER_INITIALIZED;
112 #ifdef LDAP_MEMORY_DEBUG
113 /* catch s == 0 when debugging */
117 /* ignore s == 0 when not debugging */
122 if( ber_int_memory_fns == NULL ) {
123 #ifdef LDAP_MEMORY_DEBUG
124 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
126 if( mh == NULL ) return NULL;
128 mh->bm_junk = BER_MEM_JUNK;
130 BER_MEM_VALID( &mh[1] )
137 assert( ber_int_memory_fns->bmf_malloc );
139 return (*ber_int_memory_fns->bmf_malloc)( s );
144 ber_memcalloc( size_t n, size_t s )
146 ber_int_options.lbo_valid = LBER_INITIALIZED;
148 #ifdef LDAP_MEMORY_DEBUG
149 /* catch s,n == 0 when debugging */
153 /* ignore s,n == 0 when not debugging */
154 if( n == 0 || s == 0 ) {
158 if( ber_int_memory_fns == NULL ) {
159 #ifdef LDAP_MEMORY_DEBUG
160 struct ber_mem_hdr *mh = calloc(1,
161 (n * s) + sizeof(struct ber_mem_hdr) );
163 mh->bm_junk = BER_MEM_JUNK;
165 BER_MEM_VALID( &mh[1] )
168 return calloc( n, s );
172 assert( ber_int_memory_fns->bmf_calloc );
174 return (*ber_int_memory_fns->bmf_calloc)( n, s );
179 ber_memrealloc( void* p, size_t s )
181 ber_int_options.lbo_valid = LBER_INITIALIZED;
183 /* realloc(NULL,s) -> malloc(s) */
185 return ber_memalloc( s );
188 /* realloc(p,0) -> free(p) */
196 if( ber_int_memory_fns == NULL ) {
197 #ifdef LDAP_MEMORY_DEBUG
198 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
199 ((char *)p - sizeof(struct ber_mem_hdr));
200 assert( mh->bm_junk == BER_MEM_JUNK );
202 p = realloc( mh, s );
204 if( p == NULL ) return NULL;
208 assert( mh->bm_junk == BER_MEM_JUNK );
210 BER_MEM_VALID( &mh[1] )
213 return realloc( p, s );
217 assert( ber_int_memory_fns->bmf_realloc );
219 return (*ber_int_memory_fns->bmf_realloc)( p, s );
224 ber_bvfree( struct berval *bv )
226 ber_int_options.lbo_valid = LBER_INITIALIZED;
234 if ( bv->bv_val != NULL )
235 LBER_FREE( bv->bv_val );
237 LBER_FREE( (char *) bv );
242 ber_bvecfree( struct berval **bv )
246 ber_int_options.lbo_valid = LBER_INITIALIZED;
254 for ( i = 0; bv[i] != NULL; i++ )
257 LBER_FREE( (char *) bv );
263 LDAP_CONST struct berval *bv )
267 ber_int_options.lbo_valid = LBER_INITIALIZED;
273 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
277 if ( bv->bv_val == NULL ) {
283 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
288 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
289 new->bv_val[bv->bv_len] = '\0';
290 new->bv_len = bv->bv_len;
296 ber_strdup( LDAP_CONST char *s )
301 ber_int_options.lbo_valid = LBER_INITIALIZED;
303 #ifdef LDAP_MEMORY_DEBUG
304 assert(s != NULL); /* bv damn better point to something */
311 len = strlen( s ) + 1;
313 if ( (p = (char *) LBER_MALLOC( len )) == NULL ) {
317 SAFEMEMCPY( p, s, len );