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