]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
more new logging (finally), behind NEW_LOGGING
[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-2000 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 #ifndef CSRIMALLOC
22
23 void *
24 ch_malloc(
25     ber_len_t   size
26 )
27 {
28         void    *new;
29
30         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
31 #ifdef NEW_LOGGING
32             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
33                        "ch_malloc: allocation of %lu bytes failed\n",
34                        (long)size ));
35 #else
36                 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
37                         (long) size, 0, 0 );
38 #endif
39                 assert( 0 );
40                 exit( EXIT_FAILURE );
41         }
42
43         return( new );
44 }
45
46 void *
47 ch_realloc(
48     void                *block,
49     ber_len_t   size
50 )
51 {
52         void    *new;
53
54         if ( block == NULL ) {
55                 return( ch_malloc( size ) );
56         }
57
58         if( size == 0 ) {
59                 ch_free( block );
60         }
61
62         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
63 #ifdef NEW_LOGGING
64             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
65                        "ch_realloc: reallocation of %lu bytes failed\n", (long)size ));
66 #else
67                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
68                         (long) size, 0, 0 );
69 #endif
70                 assert( 0 );
71                 exit( EXIT_FAILURE );
72         }
73
74         return( new );
75 }
76
77 void *
78 ch_calloc(
79     ber_len_t   nelem,
80     ber_len_t   size
81 )
82 {
83         void    *new;
84
85         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
86 #ifdef NEW_LOGGING
87             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
88                        "ch_calloc: allocation of %lu elements of %lu bytes faild\n",
89                        (long)nelem, (long)size ));
90 #else
91                 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
92                   (long) nelem, (long) size, 0 );
93 #endif
94                 assert( 0 );
95                 exit( EXIT_FAILURE );
96         }
97
98         return( new );
99 }
100
101 char *
102 ch_strdup(
103     const char *string
104 )
105 {
106         char    *new;
107
108         if ( (new = ber_strdup( string )) == NULL ) {
109 #ifdef NEW_LOGGING
110             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
111                        "chr_strdup: duplication of \"%s\" failed\n", string ));
112 #else
113                 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
114 #endif
115                 assert( 0 );
116                 exit( EXIT_FAILURE );
117         }
118
119         return( new );
120 }
121
122 void
123 ch_free( void *ptr )
124 {
125         ber_memfree( ptr );
126 }
127
128 #endif