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