]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
Clean up LDAP_BOOL_GET and fetching via ldap_get_option().
[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 void
15 ber_memfree( void *p )
16 {
17     ber_int_options.lbo_valid = LBER_INITIALIZED;
18
19         /* catch p == NULL when debugging */
20         assert( p != NULL );
21
22         /* ignore p == NULL when not debugging */
23         if( p == NULL ) {
24                 return;
25         }
26
27         if( ber_int_memory_fns == NULL ) {
28                 free( p );
29                 return;
30         }
31
32         assert( ber_int_memory_fns->bmf_free );
33
34         (*ber_int_memory_fns->bmf_free)( p );
35 }
36
37 void *
38 ber_memalloc( size_t s )
39 {
40     ber_int_options.lbo_valid = LBER_INITIALIZED;
41
42         /* catch s == 0 when debugging */
43         assert( s );
44
45         /* ignore s == 0 when not debugging */
46         if( s == 0 ) {
47                 return NULL;
48         }
49
50         if( ber_int_memory_fns == NULL ) {
51                 return malloc( s );
52         }
53
54         assert( ber_int_memory_fns->bmf_malloc );
55
56         return (*ber_int_memory_fns->bmf_malloc)( s );
57 }
58
59 void *
60 ber_memcalloc( size_t n, size_t s )
61 {
62     ber_int_options.lbo_valid = LBER_INITIALIZED;
63
64         /* catch s,n == 0 when debugging */
65         assert( n && s );
66
67         /* ignore s,n == 0 when not debugging */
68         if( n == 0 || s == 0 ) {
69                 return NULL;
70         }
71
72         if( ber_int_memory_fns == NULL ) {
73                 return calloc( n, s );
74         }
75
76         assert( ber_int_memory_fns->bmf_calloc );
77
78         return (*ber_int_memory_fns->bmf_calloc)( n, s );
79 }
80
81 void *
82 ber_memrealloc( void* p, size_t s )
83 {
84     ber_int_options.lbo_valid = LBER_INITIALIZED;
85
86         /* realloc(NULL,s) -> malloc(s) */
87         if( p == NULL ) {
88                 return ber_memalloc( s );
89         }
90         
91         /* realloc(p,0) -> free(p) */
92         if( s == 0 ) {
93                 ber_memfree( p );
94                 return NULL;
95         }
96
97         if( ber_int_memory_fns == NULL ) {
98                 return realloc( p, s );
99         }
100
101         assert( ber_int_memory_fns->bmf_realloc );
102
103         return (*ber_int_memory_fns->bmf_realloc)( p, s );
104 }
105