]> git.sur5r.net Git - openldap/blob - servers/slurpd/ch_malloc.c
Rework test suite to use run script.
[openldap] / servers / slurpd / ch_malloc.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #define CH_FREE 1
19
20 /*
21  * ch_malloc.c - malloc() and friends, with check for NULL return.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30
31 #include "../slapd/slap.h"
32
33
34 #ifndef CSRIMALLOC
35
36 /*
37  * Just like malloc, except we check the returned value and exit
38  * if anything goes wrong.
39  */
40 void *
41 ch_malloc(
42     ber_len_t   size
43 )
44 {
45         void    *new;
46
47         if ( (new = (void *) ber_memalloc( size )) == NULL ) {
48                 fprintf( stderr, "malloc of %lu bytes failed\n",
49                         (long) size );
50                 exit( EXIT_FAILURE );
51         }
52
53         return( new );
54 }
55
56
57
58
59 /*
60  * Just like realloc, except we check the returned value and exit
61  * if anything goes wrong.
62  */
63 void *
64 ch_realloc(
65     void                *block,
66     ber_len_t   size
67 )
68 {
69         void    *new;
70
71         if ( block == NULL ) {
72                 return( ch_malloc( size ) );
73         }
74
75         if ( size == 0 ) {
76                 ch_free( block );
77         }
78
79         if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
80                 fprintf( stderr, "realloc of %lu bytes failed\n",
81                         (long) size );
82                 exit( EXIT_FAILURE );
83         }
84
85         return( new );
86 }
87
88
89
90
91 /*
92  * Just like calloc, except we check the returned value and exit
93  * if anything goes wrong.
94  */
95 void *
96 ch_calloc(
97     ber_len_t   nelem,
98     ber_len_t   size
99 )
100 {
101         void    *new;
102
103         if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
104                 fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
105                     (long) nelem, (long) size );
106                 exit( EXIT_FAILURE );
107         }
108
109         return( new );
110 }
111
112 /*
113  * Just like strdup, except we check the returned value and exit
114  * if anything goes wrong.
115  */
116 char *
117 ch_strdup(
118     const char *string
119 )
120 {
121         char    *new;
122
123         if ( (new = ber_strdup( string )) == NULL ) {
124                 fprintf( stderr, "ch_strdup: duplication of \"%s\" failed\n",
125                                 string );
126                 exit( EXIT_FAILURE );
127         }
128
129         return( new );
130 }
131
132 /*
133  * Just like free, except we check to see if p is null.
134  */
135 void
136 ch_free(
137     void *p
138 )
139 {
140     if ( p != NULL ) {
141                 ber_memfree( p );
142     }
143     return;
144 }
145
146 #endif