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
26 long (*bmu_funcp)( double );
29 #define bm_junk ber_align.bmu_len_t
30 #define bm_data ber_align.bmu_char[1]
32 #define BER_MEM_JUNK 0xddeeddeeU
33 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
34 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
35 #define BER_MEM_VALID(p) do { \
36 assert( (p) != BER_MEM_BADADDR ); \
37 assert( (p) != (void *) &ber_int_mem_hdr ); \
40 #define BER_MEM_VALID(p) /* no-op */
43 BerMemoryFunctions *ber_int_memory_fns = NULL;
45 #if 0 && defined( LDAP_MEMORY_DEBUG )
47 ber_int_memfree( void **p )
59 ber_memfree( void *p )
61 ber_int_options.lbo_valid = LBER_INITIALIZED;
69 if( ber_int_memory_fns == NULL ) {
70 #ifdef LDAP_MEMORY_DEBUG
71 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
72 ((char *)p - sizeof(struct ber_mem_hdr));
74 assert( mh->bm_junk == BER_MEM_JUNK );
75 mh->bm_junk = ~BER_MEM_JUNK;
83 assert( ber_int_memory_fns->bmf_free );
86 (*ber_int_memory_fns->bmf_free)( p );
91 ber_memvfree( void **vec )
95 ber_int_options.lbo_valid = LBER_INITIALIZED;
101 BER_MEM_VALID( vec );
103 for ( i = 0; vec[i] != NULL; i++ ) {
112 ber_memalloc( ber_len_t s )
114 ber_int_options.lbo_valid = LBER_INITIALIZED;
116 #ifdef LDAP_MEMORY_DEBUG
124 if( ber_int_memory_fns == NULL ) {
125 #ifdef LDAP_MEMORY_DEBUG
126 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
128 if( mh == NULL ) return NULL;
130 mh->bm_junk = BER_MEM_JUNK;
132 BER_MEM_VALID( &mh[1] );
139 assert( ber_int_memory_fns->bmf_malloc );
141 return (*ber_int_memory_fns->bmf_malloc)( s );
146 ber_memcalloc( ber_len_t n, ber_len_t s )
148 ber_int_options.lbo_valid = LBER_INITIALIZED;
150 #ifdef LDAP_MEMORY_DEBUG
151 assert( n != 0 && s != 0);
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, ber_len_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 + sizeof(struct ber_mem_hdr) );
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, 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 = LBER_MALLOC( len )) == NULL ) {
317 SAFEMEMCPY( p, s, len );