]> git.sur5r.net Git - openldap/blob - servers/slapd/ch_malloc.c
Assert on malloc failure
[openldap] / servers / slapd / ch_malloc.c
1 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17
18 void *
19 ch_malloc(
20     ber_len_t   size
21 )
22 {
23         void    *new;
24
25         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
26                 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
27                         (long) size, 0, 0 );
28                 assert( 0 );
29                 exit( EXIT_FAILURE );
30         }
31
32         return( new );
33 }
34
35 void *
36 ch_realloc(
37     void                *block,
38     ber_len_t   size
39 )
40 {
41         void    *new;
42
43         if ( block == NULL ) {
44                 return( ch_malloc( size ) );
45         }
46
47         if( size == 0 ) {
48                 ch_free( block );
49         }
50
51         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
52                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
53                         (long) size, 0, 0 );
54                 assert( 0 );
55                 exit( EXIT_FAILURE );
56         }
57
58         return( new );
59 }
60
61 void *
62 ch_calloc(
63     ber_len_t   nelem,
64     ber_len_t   size
65 )
66 {
67         void    *new;
68
69         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
70                 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
71                   (long) nelem, (long) size, 0 );
72                 assert( 0 );
73                 exit( EXIT_FAILURE );
74         }
75
76         return( new );
77 }
78
79 char *
80 ch_strdup(
81     const char *string
82 )
83 {
84         char    *new;
85
86         if ( (new = ber_strdup( string )) == NULL ) {
87                 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
88                 assert( 0 );
89                 exit( EXIT_FAILURE );
90         }
91
92         return( new );
93 }
94
95 void
96 ch_free( void *ptr )
97 {
98         ber_memfree( ptr );
99 }