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