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