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
28 #define bm_junk ber_align.bmu_len_t
29 #define bm_data ber_align.bmu_char[1]
31 #define BER_MEM_JUNK 0xddeeddeeU
32 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
33 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
34 #define BER_MEM_VALID(p) do { \
35 assert( (p) != BER_MEM_BADADDR ); \
36 assert( (p) != (void *) &ber_int_mem_hdr ); \
39 #define BER_MEM_VALID(p) /* no-op */
42 BerMemoryFunctions *ber_int_memory_fns = NULL;
44 #if 0 && defined( LDAP_MEMORY_DEBUG )
46 ber_int_memfree( void **p )
58 ber_memfree( void *p )
60 ber_int_options.lbo_valid = LBER_INITIALIZED;
68 if( ber_int_memory_fns == NULL ) {
69 #ifdef LDAP_MEMORY_DEBUG
70 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
71 ((char *)p - sizeof(struct ber_mem_hdr));
73 assert( mh->bm_junk == BER_MEM_JUNK );
74 mh->bm_junk = ~BER_MEM_JUNK;
82 assert( ber_int_memory_fns->bmf_free );
85 (*ber_int_memory_fns->bmf_free)( p );
90 ber_memvfree( void **vec )
94 ber_int_options.lbo_valid = LBER_INITIALIZED;
100 BER_MEM_VALID( vec );
102 for ( i = 0; vec[i] != NULL; i++ ) {
111 ber_memalloc( ber_len_t s )
113 ber_int_options.lbo_valid = LBER_INITIALIZED;
115 #ifdef LDAP_MEMORY_DEBUG
123 if( ber_int_memory_fns == NULL ) {
124 #ifdef LDAP_MEMORY_DEBUG
125 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
127 if( mh == NULL ) return NULL;
129 mh->bm_junk = BER_MEM_JUNK;
131 BER_MEM_VALID( &mh[1] );
138 assert( ber_int_memory_fns->bmf_malloc );
140 return (*ber_int_memory_fns->bmf_malloc)( s );
145 ber_memcalloc( ber_len_t n, ber_len_t s )
147 ber_int_options.lbo_valid = LBER_INITIALIZED;
149 #ifdef LDAP_MEMORY_DEBUG
150 assert( n != 0 && s != 0);
153 if( n == 0 || s == 0 ) {
157 if( ber_int_memory_fns == NULL ) {
158 #ifdef LDAP_MEMORY_DEBUG
159 struct ber_mem_hdr *mh = calloc(1,
160 (n * s) + sizeof(struct ber_mem_hdr) );
162 mh->bm_junk = BER_MEM_JUNK;
164 BER_MEM_VALID( &mh[1] );
167 return calloc( n, s );
171 assert( ber_int_memory_fns->bmf_calloc );
173 return (*ber_int_memory_fns->bmf_calloc)( n, s );
178 ber_memrealloc( void* p, ber_len_t s )
180 ber_int_options.lbo_valid = LBER_INITIALIZED;
182 /* realloc(NULL,s) -> malloc(s) */
184 return ber_memalloc( s );
187 /* realloc(p,0) -> free(p) */
195 if( ber_int_memory_fns == NULL ) {
196 #ifdef LDAP_MEMORY_DEBUG
197 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
198 ((char *)p - sizeof(struct ber_mem_hdr));
199 assert( mh->bm_junk == BER_MEM_JUNK );
201 p = realloc( mh, s + sizeof(struct ber_mem_hdr) );
203 if( p == NULL ) return NULL;
207 assert( mh->bm_junk == BER_MEM_JUNK );
209 BER_MEM_VALID( &mh[1] );
212 return realloc( p, s );
216 assert( ber_int_memory_fns->bmf_realloc );
218 return (*ber_int_memory_fns->bmf_realloc)( p, s );
223 ber_bvfree( struct berval *bv )
225 ber_int_options.lbo_valid = LBER_INITIALIZED;
233 if ( bv->bv_val != NULL )
234 LBER_FREE( bv->bv_val );
236 LBER_FREE( (char *) bv );
241 ber_bvecfree( struct berval **bv )
245 ber_int_options.lbo_valid = LBER_INITIALIZED;
253 for ( i = 0; bv[i] != NULL; i++ )
256 LBER_FREE( (char *) bv );
262 LDAP_CONST struct berval *bv )
266 ber_int_options.lbo_valid = LBER_INITIALIZED;
272 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
276 if ( bv->bv_val == NULL ) {
282 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
287 SAFEMEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
288 new->bv_val[bv->bv_len] = '\0';
289 new->bv_len = bv->bv_len;
295 ber_strdup( LDAP_CONST char *s )
300 ber_int_options.lbo_valid = LBER_INITIALIZED;
302 #ifdef LDAP_MEMORY_DEBUG
303 assert(s != NULL); /* bv damn better point to something */
310 len = strlen( s ) + 1;
312 if ( (p = LBER_MALLOC( len )) == NULL ) {
316 SAFEMEMCPY( p, s, len );