]> git.sur5r.net Git - openldap/blob - servers/slurpd/ch_malloc.c
Happy new year!
[openldap] / servers / slurpd / ch_malloc.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 #define CH_FREE 1
31
32 /*
33  * ch_malloc.c - malloc() and friends, with check for NULL return.
34  */
35
36 #include "portable.h"
37
38 #include <stdio.h>
39
40 #include <ac/stdlib.h>
41 #include <ac/socket.h>
42
43 #include "../slapd/slap.h"
44
45
46 #ifndef CSRIMALLOC
47
48 /*
49  * Just like malloc, except we check the returned value and exit
50  * if anything goes wrong.
51  */
52 void *
53 ch_malloc(
54     ber_len_t   size
55 )
56 {
57         void    *new;
58
59         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
60                 fprintf( stderr, "malloc of %lu bytes failed\n",
61                         (long) size );
62                 exit( EXIT_FAILURE );
63         }
64
65         return( new );
66 }
67
68
69
70
71 /*
72  * Just like realloc, except we check the returned value and exit
73  * if anything goes wrong.
74  */
75 void *
76 ch_realloc(
77     void                *block,
78     ber_len_t   size
79 )
80 {
81         void    *new;
82
83         if ( block == NULL ) {
84                 return( ch_malloc( size ) );
85         }
86
87         if ( size == 0 ) {
88                 ch_free( block );
89         }
90
91         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
92                 fprintf( stderr, "realloc of %lu bytes failed\n",
93                         (long) size );
94                 exit( EXIT_FAILURE );
95         }
96
97         return( new );
98 }
99
100
101
102
103 /*
104  * Just like calloc, except we check the returned value and exit
105  * if anything goes wrong.
106  */
107 void *
108 ch_calloc(
109     ber_len_t   nelem,
110     ber_len_t   size
111 )
112 {
113         void    *new;
114
115         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
116                 fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
117                     (long) nelem, (long) size );
118                 exit( EXIT_FAILURE );
119         }
120
121         return( new );
122 }
123
124 /*
125  * Just like strdup, except we check the returned value and exit
126  * if anything goes wrong.
127  */
128 char *
129 ch_strdup(
130     const char *string
131 )
132 {
133         char    *new;
134
135         if ( (new = ber_strdup( string )) == NULL ) {
136                 fprintf( stderr, "ch_strdup: duplication of \"%s\" failed\n",
137                                 string );
138                 exit( EXIT_FAILURE );
139         }
140
141         return( new );
142 }
143
144 /*
145  * Just like free, except we check to see if p is null.
146  */
147 void
148 ch_free(
149     void *p
150 )
151 {
152     if ( p != NULL ) {
153                 ber_memfree( p );
154     }
155     return;
156 }
157
158 #endif