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