]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
5c3910f44a3496ed6d463661791d456eefaf3b29
[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 BerMemoryFunctions *ber_int_memory_fns = NULL;
13
14
15 void
16 ber_memfree( void *p )
17 {
18     ber_int_options.lbo_valid = LBER_INITIALIZED;
19
20         /* catch p == NULL when debugging */
21         assert( p != NULL );
22
23         /* ignore p == NULL when not debugging */
24         if( p == NULL ) {
25                 return;
26         }
27
28         if( ber_int_memory_fns == NULL ) {
29                 free( p );
30                 return;
31         }
32
33         assert( ber_int_memory_fns->bmf_free );
34
35         (*ber_int_memory_fns->bmf_free)( p );
36 }
37
38
39 void
40 ber_memvfree( void **vec )
41 {
42         int     i;
43
44         assert(vec != NULL);    /* vec damn better point to something */
45
46         for ( i = 0; vec[i] != NULL; i++ ) {
47                 LBER_FREE( vec[i] );
48         }
49
50         LBER_FREE( vec );
51 }
52
53
54 void *
55 ber_memalloc( size_t s )
56 {
57     ber_int_options.lbo_valid = LBER_INITIALIZED;
58
59         /* catch s == 0 when debugging */
60         assert( s );
61
62         /* ignore s == 0 when not debugging */
63         if( s == 0 ) {
64                 return NULL;
65         }
66
67         if( ber_int_memory_fns == NULL ) {
68                 return malloc( s );
69         }
70
71         assert( ber_int_memory_fns->bmf_malloc );
72
73         return (*ber_int_memory_fns->bmf_malloc)( s );
74 }
75
76
77 void *
78 ber_memcalloc( size_t n, size_t s )
79 {
80     ber_int_options.lbo_valid = LBER_INITIALIZED;
81
82         /* catch s,n == 0 when debugging */
83         assert( n && s );
84
85         /* ignore s,n == 0 when not debugging */
86         if( n == 0 || s == 0 ) {
87                 return NULL;
88         }
89
90         if( ber_int_memory_fns == NULL ) {
91                 return calloc( n, s );
92         }
93
94         assert( ber_int_memory_fns->bmf_calloc );
95
96         return (*ber_int_memory_fns->bmf_calloc)( n, s );
97 }
98
99
100 void *
101 ber_memrealloc( void* p, size_t s )
102 {
103     ber_int_options.lbo_valid = LBER_INITIALIZED;
104
105         /* realloc(NULL,s) -> malloc(s) */
106         if( p == NULL ) {
107                 return ber_memalloc( s );
108         }
109         
110         /* realloc(p,0) -> free(p) */
111         if( s == 0 ) {
112                 ber_memfree( p );
113                 return NULL;
114         }
115
116         if( ber_int_memory_fns == NULL ) {
117                 return realloc( p, s );
118         }
119
120         assert( ber_int_memory_fns->bmf_realloc );
121
122         return (*ber_int_memory_fns->bmf_realloc)( p, s );
123 }
124
125
126 void
127 ber_bvfree( struct berval *bv )
128 {
129         assert(bv != NULL);                     /* bv damn better point to something */
130
131         ber_int_options.lbo_valid = LBER_INITIALIZED;
132
133         if ( bv->bv_val != NULL )
134                 LBER_FREE( bv->bv_val );
135
136         LBER_FREE( (char *) bv );
137 }
138
139
140 void
141 ber_bvecfree( struct berval **bv )
142 {
143         int     i;
144
145         assert(bv != NULL);                     /* bv damn better point to something */
146
147         ber_int_options.lbo_valid = LBER_INITIALIZED;
148
149         for ( i = 0; bv[i] != NULL; i++ )
150                 ber_bvfree( bv[i] );
151
152         LBER_FREE( (char *) bv );
153 }
154
155
156 struct berval *
157 ber_bvdup(
158         LDAP_CONST struct berval *bv )
159 {
160         struct berval *new;
161
162         assert( bv != NULL );
163
164         ber_int_options.lbo_valid = LBER_INITIALIZED;
165
166         if( bv == NULL ) {
167                 return NULL;
168         }
169
170
171         if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
172                 return NULL;
173         }
174
175         if ( bv->bv_val == NULL ) {
176                 new->bv_val = NULL;
177                 new->bv_len = 0;
178                 return new;
179         }
180
181         if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
182                 LBER_FREE( new );
183                 return NULL;
184         }
185
186         SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
187         new->bv_val[bv->bv_len] = '\0';
188         new->bv_len = bv->bv_len;
189
190         return( new );
191 }