]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
5dcc1ec38864a68125075bd41b9a87aeba0a7870
[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 #ifdef 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         } ber_align;
23 #define bm_junk ber_align.bmu_size_t
24 };
25 #define BER_MEM_JUNK 0xddeeddeeU
26 #endif
27
28 BerMemoryFunctions *ber_int_memory_fns = NULL;
29
30 void
31 ber_memfree( void *p )
32 {
33     ber_int_options.lbo_valid = LBER_INITIALIZED;
34
35         /* catch p == NULL when debugging */
36         assert( p != NULL );
37
38         /* ignore p == NULL when not debugging */
39         if( p == NULL ) {
40                 return;
41         }
42
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 );
48                 free( mh );
49 #else
50                 free( p );
51 #endif
52                 return;
53         }
54
55         assert( ber_int_memory_fns->bmf_free );
56
57                 
58         (*ber_int_memory_fns->bmf_free)( p );
59 }
60
61
62 void
63 ber_memvfree( void **vec )
64 {
65         int     i;
66
67         assert(vec != NULL);    /* vec damn better point to something */
68
69         for ( i = 0; vec[i] != NULL; i++ ) {
70                 LBER_FREE( vec[i] );
71         }
72
73         LBER_FREE( vec );
74 }
75
76
77 void *
78 ber_memalloc( size_t s )
79 {
80     ber_int_options.lbo_valid = LBER_INITIALIZED;
81
82         /* catch s == 0 when debugging */
83         assert( s );
84
85         /* ignore s == 0 when not debugging */
86         if( s == 0 ) {
87                 return NULL;
88         }
89
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));
93
94                 if( mh == NULL ) return NULL;
95
96                 mh->bm_junk = BER_MEM_JUNK;
97
98                 return &mh[1];
99 #else
100                 return malloc( s );
101 #endif
102         }
103
104         assert( ber_int_memory_fns->bmf_malloc );
105
106         return (*ber_int_memory_fns->bmf_malloc)( s );
107 }
108
109
110 void *
111 ber_memcalloc( size_t n, size_t s )
112 {
113     ber_int_options.lbo_valid = LBER_INITIALIZED;
114
115         /* catch s,n == 0 when debugging */
116         assert( n && s );
117
118         /* ignore s,n == 0 when not debugging */
119         if( n == 0 || 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 = calloc(1,
126                         (n * s) + sizeof(struct ber_mem_hdr) );
127
128                 mh->bm_junk = BER_MEM_JUNK;
129                 return &mh[1];
130 #else
131                 return calloc( n, s );
132 #endif
133         }
134
135         assert( ber_int_memory_fns->bmf_calloc );
136
137         return (*ber_int_memory_fns->bmf_calloc)( n, s );
138 }
139
140
141 void *
142 ber_memrealloc( void* p, size_t s )
143 {
144     ber_int_options.lbo_valid = LBER_INITIALIZED;
145
146         /* realloc(NULL,s) -> malloc(s) */
147         if( p == NULL ) {
148                 return ber_memalloc( s );
149         }
150         
151         /* realloc(p,0) -> free(p) */
152         if( s == 0 ) {
153                 ber_memfree( p );
154                 return NULL;
155         }
156
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 );
162
163                 p = realloc( mh, s );
164
165                 if( p == NULL ) return NULL;
166
167                 mh = p;
168
169                 return &mh[1];
170 #else
171                 return realloc( p, s );
172 #endif
173         }
174
175         assert( ber_int_memory_fns->bmf_realloc );
176
177         return (*ber_int_memory_fns->bmf_realloc)( p, s );
178 }
179
180
181 void
182 ber_bvfree( struct berval *bv )
183 {
184         assert(bv != NULL);                     /* bv damn better point to something */
185
186         ber_int_options.lbo_valid = LBER_INITIALIZED;
187
188         if ( bv->bv_val != NULL )
189                 LBER_FREE( bv->bv_val );
190
191         LBER_FREE( (char *) bv );
192 }
193
194
195 void
196 ber_bvecfree( struct berval **bv )
197 {
198         int     i;
199
200         assert(bv != NULL);                     /* bv damn better point to something */
201
202         ber_int_options.lbo_valid = LBER_INITIALIZED;
203
204         for ( i = 0; bv[i] != NULL; i++ )
205                 ber_bvfree( bv[i] );
206
207         LBER_FREE( (char *) bv );
208 }
209
210
211 struct berval *
212 ber_bvdup(
213         LDAP_CONST struct berval *bv )
214 {
215         struct berval *new;
216
217         assert( bv != NULL );
218
219         ber_int_options.lbo_valid = LBER_INITIALIZED;
220
221         if( bv == NULL ) {
222                 return NULL;
223         }
224
225
226         if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
227                 return NULL;
228         }
229
230         if ( bv->bv_val == NULL ) {
231                 new->bv_val = NULL;
232                 new->bv_len = 0;
233                 return new;
234         }
235
236         if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
237                 LBER_FREE( new );
238                 return NULL;
239         }
240
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;
244
245         return( new );
246 }
247
248 char *
249 (ber_strdup)( LDAP_CONST char *s )
250 {
251         char    *p;
252         size_t  len = strlen( s ) + 1;
253
254         if ( (p = (char *) LBER_MALLOC( len )) == NULL ) {
255                 return( NULL );
256         }
257
258         SAFEMEMCPY( p, s, len );
259         return( p );
260 }