]> git.sur5r.net Git - openldap/blob - servers/slurpd/ch_malloc.c
9f119825fa8cd0774c119c79d320de6e826b65c8
[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 /*
14  * ch_malloc.c - malloc() and friends, with check for NULL return.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/stdlib.h>
22 #include <ac/socket.h>
23
24 #include "../slapd/slap.h"
25
26
27
28 /*
29  * Just like malloc, except we check the returned value and exit
30  * if anything goes wrong.
31  */
32 void *
33 ch_malloc(
34     ber_len_t   size
35 )
36 {
37         void    *new;
38
39         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
40                 fprintf( stderr, "malloc of %lu bytes failed\n",
41                         (long) size );
42                 exit( 1 );
43         }
44
45         return( new );
46 }
47
48
49
50
51 /*
52  * Just like realloc, except we check the returned value and exit
53  * if anything goes wrong.
54  */
55 void *
56 ch_realloc(
57     void                *block,
58     ber_len_t   size
59 )
60 {
61         void    *new;
62
63         if ( block == NULL ) {
64                 return( ch_malloc( size ) );
65         }
66
67         if ( size == 0 ) {
68                 ch_free( block );
69         }
70
71         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
72                 fprintf( stderr, "realloc of %lu bytes failed\n",
73                         (long) size );
74                 exit( 1 );
75         }
76
77         return( new );
78 }
79
80
81
82
83 /*
84  * Just like calloc, except we check the returned value and exit
85  * if anything goes wrong.
86  */
87 void *
88 ch_calloc(
89     ber_len_t   nelem,
90     ber_len_t   size
91 )
92 {
93         void    *new;
94
95         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
96                 fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
97                     (long) nelem, (long) size );
98                 exit( 1 );
99         }
100
101         return( new );
102 }
103
104
105 /*
106  * Just like free, except we check to see if p is null.
107  */
108 void
109 ch_free(
110     void *p
111 )
112 {
113     if ( p != NULL ) {
114                 ber_memfree( p );
115     }
116     return;
117 }
118