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;
64 if( ber_int_memory_fns == NULL ) {
65 #ifdef LDAP_MEMORY_DEBUG
66 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
67 ((char *)p - sizeof(struct ber_mem_hdr));
69 assert( mh->bm_junk == BER_MEM_JUNK );
70 mh->bm_junk = ~BER_MEM_JUNK;
78 assert( ber_int_memory_fns->bmf_free );
81 (*ber_int_memory_fns->bmf_free)( p );
86 ber_memvfree( void **vec )
90 ber_int_options.lbo_valid = LBER_INITIALIZED;
98 for ( i = 0; vec[i] != NULL; i++ ) {
107 ber_memalloc( size_t s )
109 ber_int_options.lbo_valid = LBER_INITIALIZED;
111 #ifdef LDAP_MEMORY_DEBUG
119 if( ber_int_memory_fns == NULL ) {
120 #ifdef LDAP_MEMORY_DEBUG
121 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
123 if( mh == NULL ) return NULL;
125 mh->bm_junk = BER_MEM_JUNK;
127 BER_MEM_VALID( &mh[1] );
134 assert( ber_int_memory_fns->bmf_malloc );
136 return (*ber_int_memory_fns->bmf_malloc)( s );
141 ber_memcalloc( size_t n, size_t s )
143 ber_int_options.lbo_valid = LBER_INITIALIZED;
145 #ifdef LDAP_MEMORY_DEBUG
146 assert( n != 0 && s != 0);
149 if( n == 0 || s == 0 ) {
153 if( ber_int_memory_fns == NULL ) {
154 #ifdef LDAP_MEMORY_DEBUG
155 struct ber_mem_hdr *mh = calloc(1,
156 (n * s) + sizeof(struct ber_mem_hdr) );
158 mh->bm_junk = BER_MEM_JUNK;
160 BER_MEM_VALID( &mh[1] );
163 return calloc( n, s );
167 assert( ber_int_memory_fns->bmf_calloc );
169 return (*ber_int_memory_fns->bmf_calloc)( n, s );
174 ber_memrealloc( void* p, size_t s )
176 ber_int_options.lbo_valid = LBER_INITIALIZED;
178 /* realloc(NULL,s) -> malloc(s) */
180 return ber_memalloc( s );
183 /* realloc(p,0) -> free(p) */
191 if( ber_int_memory_fns == NULL ) {
192 #ifdef LDAP_MEMORY_DEBUG
193 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
194 ((char *)p - sizeof(struct ber_mem_hdr));
195 assert( mh->bm_junk == BER_MEM_JUNK );
197 p = realloc( mh, s );
199 if( p == NULL ) return NULL;
203 assert( mh->bm_junk == BER_MEM_JUNK );
205 BER_MEM_VALID( &mh[1] );
208 return realloc( p, s );
212 assert( ber_int_memory_fns->bmf_realloc );
214 return (*ber_int_memory_fns->bmf_realloc)( p, s );
219 ber_bvfree( struct berval *bv )
221 ber_int_options.lbo_valid = LBER_INITIALIZED;
229 if ( bv->bv_val != NULL )
230 LBER_FREE( bv->bv_val );
232 LBER_FREE( (char *) bv );
237 ber_bvecfree( struct berval **bv )
241 ber_int_options.lbo_valid = LBER_INITIALIZED;
249 for ( i = 0; bv[i] != NULL; i++ )
252 LBER_FREE( (char *) bv );
258 LDAP_CONST struct berval *bv )
262 ber_int_options.lbo_valid = LBER_INITIALIZED;
268 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
272 if ( bv->bv_val == NULL ) {
278 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
283 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
284 new->bv_val[bv->bv_len] = '\0';
285 new->bv_len = bv->bv_len;
291 ber_strdup( LDAP_CONST char *s )
296 ber_int_options.lbo_valid = LBER_INITIALIZED;
298 #ifdef LDAP_MEMORY_DEBUG
299 assert(s != NULL); /* bv damn better point to something */
306 len = strlen( s ) + 1;
308 if ( (p = LBER_MALLOC( len )) == NULL ) {
312 SAFEMEMCPY( p, s, len );