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