]> git.sur5r.net Git - openldap/blob - servers/slurpd/ch_malloc.c
ber_int_t, ber_tag_t, ber_socket_t, ber_len_t
[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 *) malloc( 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 ( (new = (void *) realloc( block, size )) == NULL ) {
68                 fprintf( stderr, "realloc of %lu bytes failed\n",
69                         (long) size );
70                 exit( 1 );
71         }
72
73         return( new );
74 }
75
76
77
78
79 /*
80  * Just like calloc, except we check the returned value and exit
81  * if anything goes wrong.
82  */
83 void *
84 ch_calloc(
85     ber_len_t   nelem,
86     ber_len_t   size
87 )
88 {
89         void    *new;
90
91         if ( (new = (void *) calloc( nelem, size )) == NULL ) {
92                 fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
93                     (long) nelem, (long) size );
94                 exit( 1 );
95         }
96
97         return( new );
98 }
99
100
101 /*
102  * Just like free, except we check to see if p is null.
103  */
104 void
105 ch_free(
106     void *p
107 )
108 {
109     if ( p != NULL ) {
110         free( p );
111     }
112     return;
113 }
114