]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
Allow SLAPI plug-ins to override OpenLDAP extended operations
[openldap] / servers / slapd / ch_malloc.c
1 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #define CH_FREE 1
9
10 #include "portable.h"
11
12 #include <stdio.h>
13
14 #include <ac/stdlib.h>
15
16 #include <ac/string.h>
17 #include <ac/socket.h>
18
19 #include "slap.h"
20
21 BerMemoryFunctions ch_mfuncs = {
22         (BER_MEMALLOC_FN *)ch_malloc,
23         (BER_MEMCALLOC_FN *)ch_calloc,
24         (BER_MEMREALLOC_FN *)ch_realloc,
25         (BER_MEMFREE_FN *)ch_free 
26 };
27
28 void *
29 ch_malloc(
30     ber_len_t   size
31 )
32 {
33         void    *new;
34
35         if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) {
36 #ifdef NEW_LOGGING
37                 LDAP_LOG( OPERATION, ERR, 
38                            "ch_malloc: allocation of %lu bytes failed\n", (long)size, 0,0 );
39 #else
40                 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
41                         (long) size, 0, 0 );
42 #endif
43                 assert( 0 );
44                 exit( EXIT_FAILURE );
45         }
46
47         return( new );
48 }
49
50 void *
51 ch_realloc(
52     void                *block,
53     ber_len_t   size
54 )
55 {
56         void    *new, *ctx;
57
58         if ( block == NULL ) {
59                 return( ch_malloc( size ) );
60         }
61
62         if( size == 0 ) {
63                 ch_free( block );
64         }
65
66         ctx = sl_context( block );
67         if ( ctx ) {
68                 return sl_realloc( block, size, ctx );
69         }
70
71         if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) {
72 #ifdef NEW_LOGGING
73                 LDAP_LOG( OPERATION, ERR, 
74                            "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
75 #else
76                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
77                         (long) size, 0, 0 );
78 #endif
79                 assert( 0 );
80                 exit( EXIT_FAILURE );
81         }
82
83         return( new );
84 }
85
86 void *
87 ch_calloc(
88     ber_len_t   nelem,
89     ber_len_t   size
90 )
91 {
92         void    *new;
93
94         if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) {
95 #ifdef NEW_LOGGING
96                 LDAP_LOG( OPERATION, ERR, 
97                            "ch_calloc: allocation of %lu elements of %lu bytes faild\n",
98                            (long)nelem, (long)size, 0 );
99 #else
100                 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
101                   (long) nelem, (long) size, 0 );
102 #endif
103                 assert( 0 );
104                 exit( EXIT_FAILURE );
105         }
106
107         return( new );
108 }
109
110 char *
111 ch_strdup(
112     const char *string
113 )
114 {
115         char    *new;
116
117         if ( (new = ber_strdup_x( string, NULL )) == NULL ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG( OPERATION, ERR, 
120                         "chr_strdup: duplication of \"%s\" failed\n", string, 0, 0 );
121 #else
122                 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
123 #endif
124                 assert( 0 );
125                 exit( EXIT_FAILURE );
126         }
127
128         return( new );
129 }
130
131 void
132 ch_free( void *ptr )
133 {
134         void *ctx;
135
136         ctx = sl_context( ptr );
137         if (ctx) {
138                 sl_free( ptr, ctx );
139         } else {
140                 ber_memfree_x( ptr, NULL );
141         }
142 }
143