]> git.sur5r.net Git - openldap/blob - servers/slurpd/ch_malloc.c
Update install docs
[openldap] / servers / slurpd / ch_malloc.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 #define CH_FREE 1
15
16 /*
17  * ch_malloc.c - malloc() and friends, with check for NULL return.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25 #include <ac/socket.h>
26
27 #include "../slapd/slap.h"
28
29
30 #ifndef CSRIMALLOC
31
32 /*
33  * Just like malloc, except we check the returned value and exit
34  * if anything goes wrong.
35  */
36 void *
37 ch_malloc(
38     ber_len_t   size
39 )
40 {
41         void    *new;
42
43         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
44                 fprintf( stderr, "malloc of %lu bytes failed\n",
45                         (long) size );
46                 exit( EXIT_FAILURE );
47         }
48
49         return( new );
50 }
51
52
53
54
55 /*
56  * Just like realloc, except we check the returned value and exit
57  * if anything goes wrong.
58  */
59 void *
60 ch_realloc(
61     void                *block,
62     ber_len_t   size
63 )
64 {
65         void    *new;
66
67         if ( block == NULL ) {
68                 return( ch_malloc( size ) );
69         }
70
71         if ( size == 0 ) {
72                 ch_free( block );
73         }
74
75         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
76                 fprintf( stderr, "realloc of %lu bytes failed\n",
77                         (long) size );
78                 exit( EXIT_FAILURE );
79         }
80
81         return( new );
82 }
83
84
85
86
87 /*
88  * Just like calloc, except we check the returned value and exit
89  * if anything goes wrong.
90  */
91 void *
92 ch_calloc(
93     ber_len_t   nelem,
94     ber_len_t   size
95 )
96 {
97         void    *new;
98
99         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
100                 fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
101                     (long) nelem, (long) size );
102                 exit( EXIT_FAILURE );
103         }
104
105         return( new );
106 }
107
108
109 /*
110  * Just like free, except we check to see if p is null.
111  */
112 void
113 ch_free(
114     void *p
115 )
116 {
117     if ( p != NULL ) {
118                 ber_memfree( p );
119     }
120     return;
121 }
122
123 #endif