1 /* $NetBSD: ch_malloc.c,v 1.1.1.3 2010/12/12 15:22:24 adam Exp $ */ 2 3 /* ch_malloc.c - malloc routines that test returns from malloc and friends */ 4 /* OpenLDAP: pkg/ldap/servers/slapd/ch_malloc.c,v 1.28.2.5 2010/04/13 20:23:12 kurt Exp */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2010 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* Portions Copyright (c) 1995 Regents of the University of Michigan. 19 * All rights reserved. 20 * 21 * Redistribution and use in source and binary forms are permitted 22 * provided that this notice is preserved and that due credit is given 23 * to the University of Michigan at Ann Arbor. The name of the University 24 * may not be used to endorse or promote products derived from this 25 * software without specific prior written permission. This software 26 * is provided ``as is'' without express or implied warranty. 27 */ 28 29 #define CH_FREE 1 30 31 #include "portable.h" 32 33 #include <stdio.h> 34 35 #include <ac/stdlib.h> 36 37 #include <ac/string.h> 38 #include <ac/socket.h> 39 40 #include "slap.h" 41 42 BerMemoryFunctions ch_mfuncs = { 43 (BER_MEMALLOC_FN *)ch_malloc, 44 (BER_MEMCALLOC_FN *)ch_calloc, 45 (BER_MEMREALLOC_FN *)ch_realloc, 46 (BER_MEMFREE_FN *)ch_free 47 }; 48 49 void * 50 ch_malloc( 51 ber_len_t size 52 ) 53 { 54 void *new; 55 56 if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) { 57 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n", 58 (long) size, 0, 0 ); 59 assert( 0 ); 60 exit( EXIT_FAILURE ); 61 } 62 63 return( new ); 64 } 65 66 void * 67 ch_realloc( 68 void *block, 69 ber_len_t size 70 ) 71 { 72 void *new, *ctx; 73 74 if ( block == NULL ) { 75 return( ch_malloc( size ) ); 76 } 77 78 if( size == 0 ) { 79 ch_free( block ); 80 return NULL; 81 } 82 83 ctx = slap_sl_context( block ); 84 if ( ctx ) { 85 return slap_sl_realloc( block, size, ctx ); 86 } 87 88 if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) { 89 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n", 90 (long) size, 0, 0 ); 91 assert( 0 ); 92 exit( EXIT_FAILURE ); 93 } 94 95 return( new ); 96 } 97 98 void * 99 ch_calloc( 100 ber_len_t nelem, 101 ber_len_t size 102 ) 103 { 104 void *new; 105 106 if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) { 107 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n", 108 (long) nelem, (long) size, 0 ); 109 assert( 0 ); 110 exit( EXIT_FAILURE ); 111 } 112 113 return( new ); 114 } 115 116 char * 117 ch_strdup( 118 const char *string 119 ) 120 { 121 char *new; 122 123 if ( (new = ber_strdup_x( string, NULL )) == NULL ) { 124 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 ); 125 assert( 0 ); 126 exit( EXIT_FAILURE ); 127 } 128 129 return( new ); 130 } 131 132 void 133 ch_free( void *ptr ) 134 { 135 void *ctx; 136 137 ctx = slap_sl_context( ptr ); 138 if (ctx) { 139 slap_sl_free( ptr, ctx ); 140 } else { 141 ber_memfree_x( ptr, NULL ); 142 } 143 } 144 145