]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
s/SUBSTRINGS/SUBSTR/
[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                 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
32                         (long) size, 0, 0 );
33                 assert( 0 );
34                 exit( EXIT_FAILURE );
35         }
36
37         return( new );
38 }
39
40 void *
41 ch_realloc(
42     void                *block,
43     ber_len_t   size
44 )
45 {
46         void    *new;
47
48         if ( block == NULL ) {
49                 return( ch_malloc( size ) );
50         }
51
52         if( size == 0 ) {
53                 ch_free( block );
54         }
55
56         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
57                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
58                         (long) size, 0, 0 );
59                 assert( 0 );
60                 exit( EXIT_FAILURE );
61         }
62
63         return( new );
64 }
65
66 void *
67 ch_calloc(
68     ber_len_t   nelem,
69     ber_len_t   size
70 )
71 {
72         void    *new;
73
74         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
75                 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
76                   (long) nelem, (long) size, 0 );
77                 assert( 0 );
78                 exit( EXIT_FAILURE );
79         }
80
81         return( new );
82 }
83
84 char *
85 ch_strdup(
86     const char *string
87 )
88 {
89         char    *new;
90
91         if ( (new = ber_strdup( string )) == NULL ) {
92                 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
93                 assert( 0 );
94                 exit( EXIT_FAILURE );
95         }
96
97         return( new );
98 }
99
100 void
101 ch_free( void *ptr )
102 {
103         ber_memfree( ptr );
104 }
105
106 #endif