2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #ifdef LDAP_MEMORY_DEBUG
20 #define bm_junk ber_align.bmu_size_t
22 #define BER_MEM_JUNK 0xddeeddeeU
25 BerMemoryFunctions *ber_int_memory_fns = NULL;
28 ber_memfree( void *p )
30 ber_int_options.lbo_valid = LBER_INITIALIZED;
32 /* catch p == NULL when debugging */
35 /* ignore p == NULL when not debugging */
40 if( ber_int_memory_fns == NULL ) {
41 #ifdef LDAP_MEMORY_DEBUG
42 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
43 ((char *)p - sizeof(struct ber_mem_hdr));
44 assert( mh->bm_junk == BER_MEM_JUNK );
52 assert( ber_int_memory_fns->bmf_free );
55 (*ber_int_memory_fns->bmf_free)( p );
60 ber_memvfree( void **vec )
64 assert(vec != NULL); /* vec damn better point to something */
66 for ( i = 0; vec[i] != NULL; i++ ) {
75 ber_memalloc( size_t s )
77 ber_int_options.lbo_valid = LBER_INITIALIZED;
79 /* catch s == 0 when debugging */
82 /* ignore s == 0 when not debugging */
87 if( ber_int_memory_fns == NULL ) {
88 #ifdef LDAP_MEMORY_DEBUG
89 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
91 if( mh == NULL ) return NULL;
93 mh->bm_junk = BER_MEM_JUNK;
101 assert( ber_int_memory_fns->bmf_malloc );
103 return (*ber_int_memory_fns->bmf_malloc)( s );
108 ber_memcalloc( size_t n, size_t s )
110 ber_int_options.lbo_valid = LBER_INITIALIZED;
112 /* catch s,n == 0 when debugging */
115 /* ignore s,n == 0 when not debugging */
116 if( n == 0 || s == 0 ) {
120 if( ber_int_memory_fns == NULL ) {
121 #ifdef LDAP_MEMORY_DEBUG
122 struct ber_mem_hdr *mh = calloc(1,
123 (n * s) + sizeof(struct ber_mem_hdr) );
125 mh->bm_junk = BER_MEM_JUNK;
128 return calloc( n, s );
132 assert( ber_int_memory_fns->bmf_calloc );
134 return (*ber_int_memory_fns->bmf_calloc)( n, s );
139 ber_memrealloc( void* p, size_t s )
141 ber_int_options.lbo_valid = LBER_INITIALIZED;
143 /* realloc(NULL,s) -> malloc(s) */
145 return ber_memalloc( s );
148 /* realloc(p,0) -> free(p) */
154 if( ber_int_memory_fns == NULL ) {
155 #ifdef LDAP_MEMORY_DEBUG
156 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
157 ((char *)p - sizeof(struct ber_mem_hdr));
158 assert( mh->bm_junk == BER_MEM_JUNK );
160 p = realloc( mh, s );
162 if( p == NULL ) return NULL;
168 return realloc( p, s );
172 assert( ber_int_memory_fns->bmf_realloc );
174 return (*ber_int_memory_fns->bmf_realloc)( p, s );
179 ber_bvfree( struct berval *bv )
181 assert(bv != NULL); /* bv damn better point to something */
183 ber_int_options.lbo_valid = LBER_INITIALIZED;
185 if ( bv->bv_val != NULL )
186 LBER_FREE( bv->bv_val );
188 LBER_FREE( (char *) bv );
193 ber_bvecfree( struct berval **bv )
197 assert(bv != NULL); /* bv damn better point to something */
199 ber_int_options.lbo_valid = LBER_INITIALIZED;
201 for ( i = 0; bv[i] != NULL; i++ )
204 LBER_FREE( (char *) bv );
210 LDAP_CONST struct berval *bv )
214 assert( bv != NULL );
216 ber_int_options.lbo_valid = LBER_INITIALIZED;
223 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
227 if ( bv->bv_val == NULL ) {
233 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
238 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
239 new->bv_val[bv->bv_len] = '\0';
240 new->bv_len = bv->bv_len;
246 (ber_strdup)( LDAP_CONST char *s )
249 size_t len = strlen( s ) + 1;
251 if ( (p = (char *) LBER_MALLOC( len )) == NULL ) {
255 SAFEMEMCPY( p, s, len );