]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
ITS#2764, #2781 revert backend.c patch, just catch the NULL referral
[openldap] / servers / slapd / ch_malloc.c
1 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #define CH_FREE 1
28
29 #include "portable.h"
30
31 #include <stdio.h>
32
33 #include <ac/stdlib.h>
34
35 #include <ac/string.h>
36 #include <ac/socket.h>
37
38 #include "slap.h"
39
40 BerMemoryFunctions ch_mfuncs = {
41         (BER_MEMALLOC_FN *)ch_malloc,
42         (BER_MEMCALLOC_FN *)ch_calloc,
43         (BER_MEMREALLOC_FN *)ch_realloc,
44         (BER_MEMFREE_FN *)ch_free 
45 };
46
47 void *
48 ch_malloc(
49     ber_len_t   size
50 )
51 {
52         void    *new;
53
54         if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) {
55 #ifdef NEW_LOGGING
56                 LDAP_LOG( OPERATION, ERR, 
57                            "ch_malloc: allocation of %lu bytes failed\n", (long)size, 0,0 );
58 #else
59                 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
60                         (long) size, 0, 0 );
61 #endif
62                 assert( 0 );
63                 exit( EXIT_FAILURE );
64         }
65
66         return( new );
67 }
68
69 void *
70 ch_realloc(
71     void                *block,
72     ber_len_t   size
73 )
74 {
75         void    *new, *ctx;
76
77         if ( block == NULL ) {
78                 return( ch_malloc( size ) );
79         }
80
81         if( size == 0 ) {
82                 ch_free( block );
83         }
84
85         ctx = sl_context( block );
86         if ( ctx ) {
87                 return sl_realloc( block, size, ctx );
88         }
89
90         if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) {
91 #ifdef NEW_LOGGING
92                 LDAP_LOG( OPERATION, ERR, 
93                            "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
94 #else
95                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
96                         (long) size, 0, 0 );
97 #endif
98                 assert( 0 );
99                 exit( EXIT_FAILURE );
100         }
101
102         return( new );
103 }
104
105 void *
106 ch_calloc(
107     ber_len_t   nelem,
108     ber_len_t   size
109 )
110 {
111         void    *new;
112
113         if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) {
114 #ifdef NEW_LOGGING
115                 LDAP_LOG( OPERATION, ERR, 
116                            "ch_calloc: allocation of %lu elements of %lu bytes faild\n",
117                            (long)nelem, (long)size, 0 );
118 #else
119                 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
120                   (long) nelem, (long) size, 0 );
121 #endif
122                 assert( 0 );
123                 exit( EXIT_FAILURE );
124         }
125
126         return( new );
127 }
128
129 char *
130 ch_strdup(
131     const char *string
132 )
133 {
134         char    *new;
135
136         if ( (new = ber_strdup_x( string, NULL )) == NULL ) {
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( OPERATION, ERR, 
139                         "chr_strdup: duplication of \"%s\" failed\n", string, 0, 0 );
140 #else
141                 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
142 #endif
143                 assert( 0 );
144                 exit( EXIT_FAILURE );
145         }
146
147         return( new );
148 }
149
150 void
151 ch_free( void *ptr )
152 {
153         void *ctx;
154
155         ctx = sl_context( ptr );
156         if (ctx) {
157                 sl_free( ptr, ctx );
158         } else {
159                 ber_memfree_x( ptr, NULL );
160         }
161 }
162