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