Back to index

openldap  2.4.31
ch_malloc.c
Go to the documentation of this file.
00001 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 
00027 #define CH_FREE 1
00028 
00029 #include "portable.h"
00030 
00031 #include <stdio.h>
00032 
00033 #include <ac/stdlib.h>
00034 
00035 #include <ac/string.h>
00036 #include <ac/socket.h>
00037 
00038 #include "slap.h"
00039 
00040 BerMemoryFunctions ch_mfuncs = {
00041        (BER_MEMALLOC_FN *)ch_malloc,
00042        (BER_MEMCALLOC_FN *)ch_calloc,
00043        (BER_MEMREALLOC_FN *)ch_realloc,
00044        (BER_MEMFREE_FN *)ch_free 
00045 };
00046 
00047 void *
00048 ch_malloc(
00049     ber_len_t size
00050 )
00051 {
00052        void   *new;
00053 
00054        if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) {
00055               Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
00056                      (long) size, 0, 0 );
00057               assert( 0 );
00058               exit( EXIT_FAILURE );
00059        }
00060 
00061        return( new );
00062 }
00063 
00064 void *
00065 ch_realloc(
00066     void             *block,
00067     ber_len_t size
00068 )
00069 {
00070        void   *new, *ctx;
00071 
00072        if ( block == NULL ) {
00073               return( ch_malloc( size ) );
00074        }
00075 
00076        if( size == 0 ) {
00077               ch_free( block );
00078               return NULL;
00079        }
00080 
00081        ctx = slap_sl_context( block );
00082        if ( ctx ) {
00083               return slap_sl_realloc( block, size, ctx );
00084        }
00085 
00086        if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) {
00087               Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
00088                      (long) size, 0, 0 );
00089               assert( 0 );
00090               exit( EXIT_FAILURE );
00091        }
00092 
00093        return( new );
00094 }
00095 
00096 void *
00097 ch_calloc(
00098     ber_len_t nelem,
00099     ber_len_t size
00100 )
00101 {
00102        void   *new;
00103 
00104        if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) {
00105               Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
00106                 (long) nelem, (long) size, 0 );
00107               assert( 0 );
00108               exit( EXIT_FAILURE );
00109        }
00110 
00111        return( new );
00112 }
00113 
00114 char *
00115 ch_strdup(
00116     const char *string
00117 )
00118 {
00119        char   *new;
00120 
00121        if ( (new = ber_strdup_x( string, NULL )) == NULL ) {
00122               Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
00123               assert( 0 );
00124               exit( EXIT_FAILURE );
00125        }
00126 
00127        return( new );
00128 }
00129 
00130 void
00131 ch_free( void *ptr )
00132 {
00133        void *ctx;
00134 
00135        ctx = slap_sl_context( ptr );
00136        if (ctx) {
00137               slap_sl_free( ptr, ctx );
00138        } else {
00139               ber_memfree_x( ptr, NULL );
00140        }
00141 }
00142