Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / ch_malloc.c
blobc1b1b8a4d00bd582fd4708d0f4ff720936d53e4a
1 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/ch_malloc.c,v 1.28.2.3 2008/02/11 23:26:43 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 #define CH_FREE 1
29 #include "portable.h"
31 #include <stdio.h>
33 #include <ac/stdlib.h>
35 #include <ac/string.h>
36 #include <ac/socket.h>
38 #include "slap.h"
40 BerMemoryFunctions ch_mfuncs = {
41 (BER_MEMALLOC_FN *)ch_malloc,
42 (BER_MEMCALLOC_FN *)ch_calloc,
43 (BER_MEMREALLOC_FN *)ch_realloc,
44 (BER_MEMFREE_FN *)ch_free
47 void *
48 ch_malloc(
49 ber_len_t size
52 void *new;
54 if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) {
55 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
56 (long) size, 0, 0 );
57 assert( 0 );
58 exit( EXIT_FAILURE );
61 return( new );
64 void *
65 ch_realloc(
66 void *block,
67 ber_len_t size
70 void *new, *ctx;
72 if ( block == NULL ) {
73 return( ch_malloc( size ) );
76 if( size == 0 ) {
77 ch_free( block );
78 return NULL;
81 ctx = slap_sl_context( block );
82 if ( ctx ) {
83 return slap_sl_realloc( block, size, ctx );
86 if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) {
87 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
88 (long) size, 0, 0 );
89 assert( 0 );
90 exit( EXIT_FAILURE );
93 return( new );
96 void *
97 ch_calloc(
98 ber_len_t nelem,
99 ber_len_t size
102 void *new;
104 if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) {
105 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
106 (long) nelem, (long) size, 0 );
107 assert( 0 );
108 exit( EXIT_FAILURE );
111 return( new );
114 char *
115 ch_strdup(
116 const char *string
119 char *new;
121 if ( (new = ber_strdup_x( string, NULL )) == NULL ) {
122 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
123 assert( 0 );
124 exit( EXIT_FAILURE );
127 return( new );
130 void
131 ch_free( void *ptr )
133 void *ctx;
135 ctx = slap_sl_context( ptr );
136 if (ctx) {
137 slap_sl_free( ptr, ctx );
138 } else {
139 ber_memfree_x( ptr, NULL );