3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #define LDAP_F_PRE LDAP_F_EXPORT
27 long (*bmu_funcp)( double );
30 #define bm_junk ber_align.bmu_len_t
31 #define bm_data ber_align.bmu_char[1]
33 #define BER_MEM_JUNK 0xddeeddeeU
34 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
35 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
36 #define BER_MEM_VALID(p) do { \
37 assert( (p) != BER_MEM_BADADDR ); \
38 assert( (p) != (void *) &ber_int_mem_hdr ); \
41 #define BER_MEM_VALID(p) /* no-op */
44 BerMemoryFunctions *ber_int_memory_fns = NULL;
46 #if 0 && defined( LDAP_MEMORY_DEBUG )
48 ber_int_memfree( void **p )
60 ber_memfree( void *p )
62 ber_int_options.lbo_valid = LBER_INITIALIZED;
70 if( ber_int_memory_fns == NULL ) {
71 #ifdef LDAP_MEMORY_DEBUG
72 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
73 ((char *)p - sizeof(struct ber_mem_hdr));
75 assert( mh->bm_junk == BER_MEM_JUNK );
76 mh->bm_junk = ~BER_MEM_JUNK;
84 assert( ber_int_memory_fns->bmf_free );
87 (*ber_int_memory_fns->bmf_free)( p );
92 ber_memvfree( void **vec )
96 ber_int_options.lbo_valid = LBER_INITIALIZED;
102 BER_MEM_VALID( vec );
104 for ( i = 0; vec[i] != NULL; i++ ) {
113 ber_memalloc( ber_len_t s )
115 ber_int_options.lbo_valid = LBER_INITIALIZED;
117 #ifdef LDAP_MEMORY_DEBUG
125 if( ber_int_memory_fns == NULL ) {
126 #ifdef LDAP_MEMORY_DEBUG
127 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
129 if( mh == NULL ) return NULL;
131 mh->bm_junk = BER_MEM_JUNK;
133 BER_MEM_VALID( &mh[1] );
140 assert( ber_int_memory_fns->bmf_malloc );
142 return (*ber_int_memory_fns->bmf_malloc)( s );
147 ber_memcalloc( ber_len_t n, ber_len_t s )
149 ber_int_options.lbo_valid = LBER_INITIALIZED;
151 #ifdef LDAP_MEMORY_DEBUG
152 assert( n != 0 && s != 0);
155 if( n == 0 || s == 0 ) {
159 if( ber_int_memory_fns == NULL ) {
160 #ifdef LDAP_MEMORY_DEBUG
161 struct ber_mem_hdr *mh = calloc(1,
162 (n * s) + sizeof(struct ber_mem_hdr) );
164 mh->bm_junk = BER_MEM_JUNK;
166 BER_MEM_VALID( &mh[1] );
169 return calloc( n, s );
173 assert( ber_int_memory_fns->bmf_calloc );
175 return (*ber_int_memory_fns->bmf_calloc)( n, s );
180 ber_memrealloc( void* p, ber_len_t s )
182 ber_int_options.lbo_valid = LBER_INITIALIZED;
184 /* realloc(NULL,s) -> malloc(s) */
186 return ber_memalloc( s );
189 /* realloc(p,0) -> free(p) */
197 if( ber_int_memory_fns == NULL ) {
198 #ifdef LDAP_MEMORY_DEBUG
199 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
200 ((char *)p - sizeof(struct ber_mem_hdr));
201 assert( mh->bm_junk == BER_MEM_JUNK );
203 p = realloc( mh, s + sizeof(struct ber_mem_hdr) );
205 if( p == NULL ) return NULL;
209 assert( mh->bm_junk == BER_MEM_JUNK );
211 BER_MEM_VALID( &mh[1] );
214 return realloc( p, s );
218 assert( ber_int_memory_fns->bmf_realloc );
220 return (*ber_int_memory_fns->bmf_realloc)( p, s );
225 ber_bvfree( struct berval *bv )
227 ber_int_options.lbo_valid = LBER_INITIALIZED;
235 if ( bv->bv_val != NULL )
236 LBER_FREE( bv->bv_val );
238 LBER_FREE( (char *) bv );
243 ber_bvecfree( struct berval **bv )
247 ber_int_options.lbo_valid = LBER_INITIALIZED;
255 for ( i = 0; bv[i] != NULL; i++ )
258 LBER_FREE( (char *) bv );
264 LDAP_CONST struct berval *bv )
268 ber_int_options.lbo_valid = LBER_INITIALIZED;
274 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
278 if ( bv->bv_val == NULL ) {
284 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
289 SAFEMEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
290 new->bv_val[bv->bv_len] = '\0';
291 new->bv_len = bv->bv_len;
297 ber_strdup( LDAP_CONST char *s )
302 ber_int_options.lbo_valid = LBER_INITIALIZED;
304 #ifdef LDAP_MEMORY_DEBUG
305 assert(s != NULL); /* bv damn better point to something */
312 len = strlen( s ) + 1;
314 if ( (p = LBER_MALLOC( len )) == NULL ) {
318 SAFEMEMCPY( p, s, len );