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