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