]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
Move old files to the Attic
[openldap] / libraries / liblber / memory.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <ac/stdlib.h>
9 #include <ac/string.h>
10
11 #include "lber-int.h"
12
13 #if LDAP_MEMORY_DEBUG
14 /*
15  * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
16  * debugging memory management within OpenLDAP libraries and slapd.
17  * It should only be enabled by an experienced developer as it
18  * causes the inclusion of numerous assert()'s, many of which may
19  * be triggered by a prefectly valid program.
20  *
21  * The code behind this macro is subject to change as needed to
22  * support this testing.
23  */
24
25 struct ber_mem_hdr {
26         union bmu_align_u {
27                 ber_len_t       bmu_len_t;
28                 ber_tag_t       bmu_tag_t;
29                 ber_int_t       bmu_int_t;
30
31                 size_t  bmu_size_t;
32                 void *  bmu_voidp;
33                 double  bmu_double;
34                 long    bmu_long;
35                 long    (*bmu_funcp)( double );
36                 char    bmu_char[4];
37         } ber_align;
38 #define bm_junk ber_align.bmu_len_t
39 #define bm_data ber_align.bmu_char[1]
40 };
41 #define BER_MEM_JUNK 0xdeaddadaU
42 static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK };
43 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
44 #define BER_MEM_VALID(p)        do { \
45                 assert( (p) != BER_MEM_BADADDR );       \
46                 assert( (p) != (void *) &ber_int_mem_hdr );     \
47         } while(0)
48
49 #else
50 #define BER_MEM_VALID(p)        /* no-op */
51 #endif
52
53 BerMemoryFunctions *ber_int_memory_fns = NULL;
54
55 #if 0 && defined( LDAP_MEMORY_DEBUG )
56 void
57 ber_int_memfree( void **p )
58 {
59         assert( p != NULL );
60         BER_MEM_VALID( *p );
61
62         ber_memfree( p );
63
64         *p = BER_MEM_BADADDR;
65 }
66 #endif
67
68 void
69 ber_memfree( void *p )
70 {
71     ber_int_options.lbo_valid = LBER_INITIALIZED;
72
73         if( p == NULL ) {
74                 return;
75         }
76
77         BER_MEM_VALID( p );
78
79         if( ber_int_memory_fns == NULL ) {
80 #ifdef LDAP_MEMORY_DEBUG
81                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
82                         ((char *)p - sizeof(struct ber_mem_hdr));
83
84                 assert( mh->bm_junk == BER_MEM_JUNK );                          
85                 mh->bm_junk = ~BER_MEM_JUNK;
86                 free( mh );
87 #else
88                 free( p );
89 #endif
90                 return;
91         }
92
93         assert( ber_int_memory_fns->bmf_free );
94
95         (*ber_int_memory_fns->bmf_free)( p );
96 }
97
98
99 void
100 ber_memvfree( void **vec )
101 {
102         int     i;
103
104     ber_int_options.lbo_valid = LBER_INITIALIZED;
105
106         if( vec == NULL ) {
107                 return;
108         }
109
110         BER_MEM_VALID( vec );
111
112         for ( i = 0; vec[i] != NULL; i++ ) {
113                 LBER_FREE( vec[i] );
114         }
115
116         LBER_FREE( vec );
117 }
118
119
120 void *
121 ber_memalloc( ber_len_t s )
122 {
123         void *new;
124     ber_int_options.lbo_valid = LBER_INITIALIZED;
125
126 #ifdef LDAP_MEMORY_DEBUG
127         assert( s != 0 );
128 #endif
129
130         if( s == 0 ) {
131                 return NULL;
132         }
133
134         if( ber_int_memory_fns == NULL ) {
135 #ifdef LDAP_MEMORY_DEBUG
136                 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr));
137
138                 if( mh == NULL ) return NULL;
139
140                 mh->bm_junk = BER_MEM_JUNK;
141
142                 BER_MEM_VALID( &mh[1] );
143                 new = &mh[1];
144 #else
145                 new = malloc( s );
146 #endif
147         } else {
148                 new = (*ber_int_memory_fns->bmf_malloc)( s );
149         }
150
151         if( new == NULL ) {
152                 ber_errno = LBER_ERROR_MEMORY;
153         }
154
155         return new;
156 }
157
158
159 void *
160 ber_memcalloc( ber_len_t n, ber_len_t s )
161 {
162         void *new;
163     ber_int_options.lbo_valid = LBER_INITIALIZED;
164
165 #ifdef LDAP_MEMORY_DEBUG
166         assert( n != 0 && s != 0);
167 #endif
168
169         if( n == 0 || s == 0 ) {
170                 return NULL;
171         }
172
173         if( ber_int_memory_fns == NULL ) {
174 #ifdef LDAP_MEMORY_DEBUG
175                 struct ber_mem_hdr *mh = calloc(1,
176                         (n * s) + sizeof(struct ber_mem_hdr) );
177
178                 mh->bm_junk = BER_MEM_JUNK;
179
180                 BER_MEM_VALID( &mh[1] );
181                 new = &mh[1];
182 #else
183                 new = calloc( n, s );
184 #endif
185
186         } else {
187                 new = (*ber_int_memory_fns->bmf_calloc)( n, s );
188         }
189
190         if( new == NULL ) {
191                 ber_errno = LBER_ERROR_MEMORY;
192         }
193
194         return new;
195 }
196
197
198 void *
199 ber_memrealloc( void* p, ber_len_t s )
200 {
201         void *new = NULL;
202     ber_int_options.lbo_valid = LBER_INITIALIZED;
203
204         /* realloc(NULL,s) -> malloc(s) */
205         if( p == NULL ) {
206                 return ber_memalloc( s );
207         }
208         
209         /* realloc(p,0) -> free(p) */
210         if( s == 0 ) {
211                 ber_memfree( p );
212                 return NULL;
213         }
214
215         BER_MEM_VALID( p );
216
217         if( ber_int_memory_fns == NULL ) {
218 #ifdef LDAP_MEMORY_DEBUG
219                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
220                         ((char *)p - sizeof(struct ber_mem_hdr));
221                 assert( mh->bm_junk == BER_MEM_JUNK );
222
223                 p = realloc( mh, s + sizeof(struct ber_mem_hdr) );
224
225                 if( p != NULL ) {
226                         mh = p;
227
228                         assert( mh->bm_junk == BER_MEM_JUNK );
229
230                         BER_MEM_VALID( &mh[1] );
231                         new = &mh[1];
232                 }
233 #else
234                 new = realloc( p, s );
235 #endif
236         } else {
237                 new = (*ber_int_memory_fns->bmf_realloc)( p, s );
238         }
239
240         if( new == NULL ) {
241                 ber_errno = LBER_ERROR_MEMORY;
242         }
243
244         return new;
245 }
246
247
248 void
249 ber_bvfree( struct berval *bv )
250 {
251         ber_int_options.lbo_valid = LBER_INITIALIZED;
252
253         if( bv == NULL ) {
254                 return;
255         }
256
257         BER_MEM_VALID( bv );
258
259         if ( bv->bv_val != NULL )
260                 LBER_FREE( bv->bv_val );
261
262         LBER_FREE( (char *) bv );
263 }
264
265
266 void
267 ber_bvecfree( struct berval **bv )
268 {
269         int     i;
270
271         ber_int_options.lbo_valid = LBER_INITIALIZED;
272
273         if( bv == NULL ) {
274                 return;
275         }
276
277         BER_MEM_VALID( bv );
278
279         for ( i = 0; bv[i] != NULL; i++ )
280                 ber_bvfree( bv[i] );
281
282         LBER_FREE( (char *) bv );
283 }
284
285 int
286 ber_bvecadd( struct berval ***bvec, struct berval *bv )
287 {
288         ber_len_t i;
289         struct berval **new;
290
291         ber_int_options.lbo_valid = LBER_INITIALIZED;
292
293         if( bvec == NULL ) {
294                 if( bv == NULL ) {
295                         /* nothing to add */
296                         return 0;
297                 }
298
299                 *bvec = ber_memalloc( 2 * sizeof(struct berval *) );
300
301                 if( *bvec == NULL ) {
302                         return -1;
303                 }
304
305                 (*bvec)[0] = bv;
306                 (*bvec)[1] = NULL;
307
308                 return 1;
309         }
310
311         BER_MEM_VALID( bvec );
312
313         /* count entries */
314         for ( i = 0; bvec[i] != NULL; i++ ) {
315                 /* EMPTY */;
316         }
317
318         if( bv == NULL ) {
319                 return i;
320         }
321
322         new = ber_memrealloc( *bvec, (i+2) * sizeof(struct berval *));
323
324         if( new == NULL ) {
325                 return -1;
326         }
327
328         *bvec = new;
329
330         (*bvec)[i++] = bv;
331         (*bvec)[i] = NULL;
332
333         return i;
334 }
335
336
337 struct berval *
338 ber_bvdup(
339         LDAP_CONST struct berval *bv )
340 {
341         struct berval *new;
342
343         ber_int_options.lbo_valid = LBER_INITIALIZED;
344
345         if( bv == NULL ) {
346                 ber_errno = LBER_ERROR_PARAM;
347                 return NULL;
348         }
349
350         if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
351                 ber_errno = LBER_ERROR_MEMORY;
352                 return NULL;
353         }
354
355         if ( bv->bv_val == NULL ) {
356                 new->bv_val = NULL;
357                 new->bv_len = 0;
358                 return new;
359         }
360
361         if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
362                 ber_errno = LBER_ERROR_MEMORY;
363                 LBER_FREE( new );
364                 return NULL;
365         }
366
367         SAFEMEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
368         new->bv_val[bv->bv_len] = '\0';
369         new->bv_len = bv->bv_len;
370
371         return( new );
372 }
373
374 struct berval *
375 ber_bvstr(
376         LDAP_CONST char *s )
377 {
378         struct berval *new;
379
380         ber_int_options.lbo_valid = LBER_INITIALIZED;
381
382         if( s == NULL ) {
383                 ber_errno = LBER_ERROR_PARAM;
384                 return NULL;
385         }
386
387         if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
388                 ber_errno = LBER_ERROR_MEMORY;
389                 return NULL;
390         }
391
392         if ( *s == '\0' ) {
393                 new->bv_val = NULL;
394                 new->bv_len = 0;
395                 return new;
396         }
397
398         new->bv_val = (char *) s;
399         new->bv_len = strlen( s );
400
401         return( new );
402 }
403
404 struct berval *
405 ber_bvstrdup(
406         LDAP_CONST char *s )
407 {
408         struct berval *new;
409         char *p;
410
411         ber_int_options.lbo_valid = LBER_INITIALIZED;
412
413         if( s == NULL ) {
414                 ber_errno = LBER_ERROR_PARAM;
415                 return NULL;
416         }
417
418         p = LBER_STRDUP( s );
419
420         if( p == NULL ) {
421                 ber_errno = LBER_ERROR_MEMORY;
422                 return NULL;
423         }
424
425         new = ber_bvstr( p );
426
427         if( new == NULL || *p == '\0' ) {
428                 LBER_FREE( p );
429         }
430
431         return( new );
432 }
433
434 char *
435 ber_strdup( LDAP_CONST char *s )
436 {
437         char    *p;
438         size_t  len;
439         
440         ber_int_options.lbo_valid = LBER_INITIALIZED;
441
442 #ifdef LDAP_MEMORY_DEBUG
443         assert(s != NULL);                      /* bv damn better point to something */
444 #endif
445
446         if( s == NULL ) {
447                 ber_errno = LBER_ERROR_PARAM;
448                 return( NULL );
449         }
450
451         len = strlen( s ) + 1;
452
453         if ( (p = LBER_MALLOC( len )) == NULL ) {
454                 ber_errno = LBER_ERROR_MEMORY;
455                 return( NULL );
456         }
457
458         SAFEMEMCPY( p, s, len );
459         return( p );
460 }