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