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