1/* $NetBSD: ch_malloc.c,v 1.3 2021/08/14 16:14:58 christos Exp $ */ 2 3/* ch_malloc.c - malloc routines that test returns from malloc and friends */ 4/* $OpenLDAP$ */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2021 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 <sys/cdefs.h> 32__RCSID("$NetBSD: ch_malloc.c,v 1.3 2021/08/14 16:14:58 christos Exp $"); 33 34#include "portable.h" 35 36#include <stdio.h> 37 38#include <ac/stdlib.h> 39 40#include <ac/string.h> 41#include <ac/socket.h> 42 43#include "slap.h" 44 45BerMemoryFunctions ch_mfuncs = { 46 (BER_MEMALLOC_FN *)ch_malloc, 47 (BER_MEMCALLOC_FN *)ch_calloc, 48 (BER_MEMREALLOC_FN *)ch_realloc, 49 (BER_MEMFREE_FN *)ch_free 50}; 51 52void * 53ch_malloc( 54 ber_len_t size 55) 56{ 57 void *new; 58 59 if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) { 60 Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n", 61 (long) size ); 62 assert( 0 ); 63 exit( EXIT_FAILURE ); 64 } 65 66 return( new ); 67} 68 69void * 70ch_realloc( 71 void *block, 72 ber_len_t size 73) 74{ 75 void *new, *ctx; 76 77 if ( block == NULL ) { 78 return( ch_malloc( size ) ); 79 } 80 81 if( size == 0 ) { 82 ch_free( block ); 83 return NULL; 84 } 85 86 ctx = slap_sl_context( block ); 87 if ( ctx ) { 88 return slap_sl_realloc( block, size, ctx ); 89 } 90 91 if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) { 92 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n", 93 (long) size ); 94 assert( 0 ); 95 exit( EXIT_FAILURE ); 96 } 97 98 return( new ); 99} 100 101void * 102ch_calloc( 103 ber_len_t nelem, 104 ber_len_t size 105) 106{ 107 void *new; 108 109 if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) { 110 Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n", 111 (long) nelem, (long) size ); 112 assert( 0 ); 113 exit( EXIT_FAILURE ); 114 } 115 116 return( new ); 117} 118 119char * 120ch_strdup( 121 const char *string 122) 123{ 124 char *new; 125 126 if ( (new = ber_strdup_x( string, NULL )) == NULL ) { 127 Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string ); 128 assert( 0 ); 129 exit( EXIT_FAILURE ); 130 } 131 132 return( new ); 133} 134 135void 136ch_free( void *ptr ) 137{ 138 void *ctx; 139 140 ctx = slap_sl_context( ptr ); 141 if (ctx) { 142 slap_sl_free( ptr, ctx ); 143 } else { 144 ber_memfree_x( ptr, NULL ); 145 } 146} 147 148