]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
b44579697044bcd91e5c22ed2f1e66649bb34862
[openldap] / servers / slapd / ch_malloc.c
1 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/stdlib.h>
8
9 #include <ac/string.h>
10 #include <ac/socket.h>
11
12 #include "slap.h"
13
14 void *
15 ch_malloc(
16     unsigned long       size
17 )
18 {
19         void    *new;
20
21         if ( (new = (void *) malloc( size )) == NULL ) {
22                 Debug( LDAP_DEBUG_ANY, "malloc of %lu bytes failed\n", size, 0, 0 );
23                 exit( 1 );
24         }
25
26         return( new );
27 }
28
29 void *
30 ch_realloc(
31     void                *block,
32     unsigned long       size
33 )
34 {
35         void    *new;
36
37         if ( block == NULL ) {
38                 return( ch_malloc( size ) );
39         }
40
41         if ( (new = (void *) realloc( block, size )) == NULL ) {
42                 Debug( LDAP_DEBUG_ANY, "realloc of %lu bytes failed\n", size, 0, 0 );
43                 exit( 1 );
44         }
45
46         return( new );
47 }
48
49 void *
50 ch_calloc(
51     unsigned long       nelem,
52     unsigned long       size
53 )
54 {
55         void    *new;
56
57         if ( (new = (void *) calloc( nelem, size )) == NULL ) {
58                 Debug( LDAP_DEBUG_ANY, "calloc of %lu elems of %lu bytes failed\n",
59                   nelem, size, 0 );
60                 exit( 1 );
61         }
62
63         return( new );
64 }
65
66 char *
67 ch_strdup(
68     const char *string
69 )
70 {
71         char    *new;
72
73         if ( (new = strdup( string )) == NULL ) {
74                 Debug( LDAP_DEBUG_ANY, "strdup(%s) failed\n", string, 0, 0 );
75                 exit( 1 );
76         }
77
78         return( new );
79 }
80