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
15 #ifdef LDAP_MEMORY_DEBUG
23 #define bm_junk ber_align.bmu_size_t
25 #define BER_MEM_JUNK 0xddeeddeeU
28 BerMemoryFunctions *ber_int_memory_fns = NULL;
31 ber_memfree( void *p )
33 ber_int_options.lbo_valid = LBER_INITIALIZED;
35 /* catch p == NULL when debugging */
38 /* ignore p == NULL when not debugging */
43 if( ber_int_memory_fns == NULL ) {
44 #ifdef LDAP_MEMORY_DEBUG
45 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
46 ((char *)p - sizeof(struct ber_mem_hdr));
47 assert( mh->bm_junk == BER_MEM_JUNK );
55 assert( ber_int_memory_fns->bmf_free );
58 (*ber_int_memory_fns->bmf_free)( p );
63 ber_memvfree( void **vec )
67 assert(vec != NULL); /* vec damn better point to something */
69 for ( i = 0; vec[i] != NULL; i++ ) {
78 ber_memalloc( size_t s )
80 ber_int_options.lbo_valid = LBER_INITIALIZED;
82 /* catch s == 0 when debugging */
85 /* ignore s == 0 when not debugging */
90 if( ber_int_memory_fns == NULL ) {
91 #ifdef LDAP_MEMORY_DEBUG
92 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
94 if( mh == NULL ) return NULL;
96 mh->bm_junk = BER_MEM_JUNK;
104 assert( ber_int_memory_fns->bmf_malloc );
106 return (*ber_int_memory_fns->bmf_malloc)( s );
111 ber_memcalloc( size_t n, size_t s )
113 ber_int_options.lbo_valid = LBER_INITIALIZED;
115 /* catch s,n == 0 when debugging */
118 /* ignore s,n == 0 when not debugging */
119 if( n == 0 || s == 0 ) {
123 if( ber_int_memory_fns == NULL ) {
124 #ifdef LDAP_MEMORY_DEBUG
125 struct ber_mem_hdr *mh = calloc(1,
126 (n * s) + sizeof(struct ber_mem_hdr) );
128 mh->bm_junk = BER_MEM_JUNK;
131 return calloc( n, s );
135 assert( ber_int_memory_fns->bmf_calloc );
137 return (*ber_int_memory_fns->bmf_calloc)( n, s );
142 ber_memrealloc( void* p, size_t s )
144 ber_int_options.lbo_valid = LBER_INITIALIZED;
146 /* realloc(NULL,s) -> malloc(s) */
148 return ber_memalloc( s );
151 /* realloc(p,0) -> free(p) */
157 if( ber_int_memory_fns == NULL ) {
158 #ifdef LDAP_MEMORY_DEBUG
159 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
160 ((char *)p - sizeof(struct ber_mem_hdr));
161 assert( mh->bm_junk == BER_MEM_JUNK );
163 p = realloc( mh, s );
165 if( p == NULL ) return NULL;
171 return realloc( p, s );
175 assert( ber_int_memory_fns->bmf_realloc );
177 return (*ber_int_memory_fns->bmf_realloc)( p, s );
182 ber_bvfree( struct berval *bv )
184 assert(bv != NULL); /* bv damn better point to something */
186 ber_int_options.lbo_valid = LBER_INITIALIZED;
188 if ( bv->bv_val != NULL )
189 LBER_FREE( bv->bv_val );
191 LBER_FREE( (char *) bv );
196 ber_bvecfree( struct berval **bv )
200 assert(bv != NULL); /* bv damn better point to something */
202 ber_int_options.lbo_valid = LBER_INITIALIZED;
204 for ( i = 0; bv[i] != NULL; i++ )
207 LBER_FREE( (char *) bv );
213 LDAP_CONST struct berval *bv )
217 assert( bv != NULL );
219 ber_int_options.lbo_valid = LBER_INITIALIZED;
226 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
230 if ( bv->bv_val == NULL ) {
236 if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
241 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
242 new->bv_val[bv->bv_len] = '\0';
243 new->bv_len = bv->bv_len;
249 (ber_strdup)( LDAP_CONST char *s )
252 size_t len = strlen( s ) + 1;
254 if ( (p = (char *) LBER_MALLOC( len )) == NULL ) {
258 SAFEMEMCPY( p, s, len );