]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
Regarding previous commit:
[openldap] / libraries / liblber / memory.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <ac/stdlib.h>
8 #include <ac/string.h>
9
10 #undef LDAP_F_PRE
11 #define LDAP_F_PRE LDAP_F_EXPORT
12
13 #include "lber-int.h"
14
15 #if LDAP_MEMORY_DEBUG
16 struct ber_mem_hdr {
17         union bmu_align_u {
18                 ber_len_t       bmu_len_t;
19                 ber_tag_t       bmu_tag_t;
20                 ber_int_t       bmu_int_t;
21
22                 size_t  bmu_size_t;
23                 void *  bmu_voidp;
24                 double  bmu_double;
25                 long    bmu_long;
26                 char    bmu_char[4];
27         } ber_align;
28 #define bm_junk ber_align.bmu_len_t
29 #define bm_data ber_align.bmu_char[1]
30 };
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 );     \
37         } while(0)
38 #else
39 #define BER_MEM_VALID(p)        /* no-op */
40 #endif
41
42 BerMemoryFunctions *ber_int_memory_fns = NULL;
43
44 #if 0 && defined( LDAP_MEMORY_DEBUG )
45 void
46 ber_int_memfree( void **p )
47 {
48         assert( p != NULL );
49         BER_MEM_VALID( *p );
50
51         ber_memfree( p );
52
53         *p = BER_MEM_BADADDR;
54 }
55 #endif
56
57 void
58 ber_memfree( void *p )
59 {
60     ber_int_options.lbo_valid = LBER_INITIALIZED;
61
62         if( p == NULL ) {
63                 return;
64         }
65
66         BER_MEM_VALID( p );
67
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));
72
73                 assert( mh->bm_junk == BER_MEM_JUNK );                          
74                 mh->bm_junk = ~BER_MEM_JUNK;
75                 free( mh );
76 #else
77                 free( p );
78 #endif
79                 return;
80         }
81
82         assert( ber_int_memory_fns->bmf_free );
83
84                 
85         (*ber_int_memory_fns->bmf_free)( p );
86 }
87
88
89 void
90 ber_memvfree( void **vec )
91 {
92         int     i;
93
94     ber_int_options.lbo_valid = LBER_INITIALIZED;
95
96         if( vec == NULL ) {
97                 return;
98         }
99
100         BER_MEM_VALID( vec );
101
102         for ( i = 0; vec[i] != NULL; i++ ) {
103                 LBER_FREE( vec[i] );
104         }
105
106         LBER_FREE( vec );
107 }
108
109
110 void *
111 ber_memalloc( ber_len_t s )
112 {
113     ber_int_options.lbo_valid = LBER_INITIALIZED;
114
115 #ifdef LDAP_MEMORY_DEBUG
116         assert( s != 0 );
117 #endif
118
119         if( s == 0 ) {
120                 return NULL;
121         }
122
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));
126
127                 if( mh == NULL ) return NULL;
128
129                 mh->bm_junk = BER_MEM_JUNK;
130
131                 BER_MEM_VALID( &mh[1] );
132                 return &mh[1];
133 #else
134                 return malloc( s );
135 #endif
136         }
137
138         assert( ber_int_memory_fns->bmf_malloc );
139
140         return (*ber_int_memory_fns->bmf_malloc)( s );
141 }
142
143
144 void *
145 ber_memcalloc( ber_len_t n, ber_len_t s )
146 {
147     ber_int_options.lbo_valid = LBER_INITIALIZED;
148
149 #ifdef LDAP_MEMORY_DEBUG
150         assert( n != 0 && s != 0);
151 #endif
152
153         if( n == 0 || s == 0 ) {
154                 return NULL;
155         }
156
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) );
161
162                 mh->bm_junk = BER_MEM_JUNK;
163
164                 BER_MEM_VALID( &mh[1] );
165                 return &mh[1];
166 #else
167                 return calloc( n, s );
168 #endif
169         }
170
171         assert( ber_int_memory_fns->bmf_calloc );
172
173         return (*ber_int_memory_fns->bmf_calloc)( n, s );
174 }
175
176
177 void *
178 ber_memrealloc( void* p, ber_len_t s )
179 {
180     ber_int_options.lbo_valid = LBER_INITIALIZED;
181
182         /* realloc(NULL,s) -> malloc(s) */
183         if( p == NULL ) {
184                 return ber_memalloc( s );
185         }
186         
187         /* realloc(p,0) -> free(p) */
188         if( s == 0 ) {
189                 ber_memfree( p );
190                 return NULL;
191         }
192
193         BER_MEM_VALID( p );
194
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 );
200
201                 p = realloc( mh, s + sizeof(struct ber_mem_hdr) );
202
203                 if( p == NULL ) return NULL;
204
205                 mh = p;
206
207                 assert( mh->bm_junk == BER_MEM_JUNK );
208
209                 BER_MEM_VALID( &mh[1] );
210                 return &mh[1];
211 #else
212                 return realloc( p, s );
213 #endif
214         }
215
216         assert( ber_int_memory_fns->bmf_realloc );
217
218         return (*ber_int_memory_fns->bmf_realloc)( p, s );
219 }
220
221
222 void
223 ber_bvfree( struct berval *bv )
224 {
225         ber_int_options.lbo_valid = LBER_INITIALIZED;
226
227         if( bv == NULL ) {
228                 return;
229         }
230
231         BER_MEM_VALID( bv );
232
233         if ( bv->bv_val != NULL )
234                 LBER_FREE( bv->bv_val );
235
236         LBER_FREE( (char *) bv );
237 }
238
239
240 void
241 ber_bvecfree( struct berval **bv )
242 {
243         int     i;
244
245         ber_int_options.lbo_valid = LBER_INITIALIZED;
246
247         if( bv == NULL ) {
248                 return;
249         }
250
251         BER_MEM_VALID( bv );
252
253         for ( i = 0; bv[i] != NULL; i++ )
254                 ber_bvfree( bv[i] );
255
256         LBER_FREE( (char *) bv );
257 }
258
259
260 struct berval *
261 ber_bvdup(
262         LDAP_CONST struct berval *bv )
263 {
264         struct berval *new;
265
266         ber_int_options.lbo_valid = LBER_INITIALIZED;
267
268         if( bv == NULL ) {
269                 return NULL;
270         }
271
272         if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
273                 return NULL;
274         }
275
276         if ( bv->bv_val == NULL ) {
277                 new->bv_val = NULL;
278                 new->bv_len = 0;
279                 return new;
280         }
281
282         if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
283                 LBER_FREE( new );
284                 return NULL;
285         }
286
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;
290
291         return( new );
292 }
293
294 char *
295 ber_strdup( LDAP_CONST char *s )
296 {
297         char    *p;
298         size_t  len;
299         
300         ber_int_options.lbo_valid = LBER_INITIALIZED;
301
302 #ifdef LDAP_MEMORY_DEBUG
303         assert(s != NULL);                      /* bv damn better point to something */
304 #endif
305
306         if( s == NULL ) {
307                 return( NULL );
308         }
309
310         len = strlen( s ) + 1;
311
312         if ( (p = LBER_MALLOC( len )) == NULL ) {
313                 return( NULL );
314         }
315
316         SAFEMEMCPY( p, s, len );
317         return( p );
318 }