1998-08-08 20:43:13 -04:00
|
|
|
/* ch_malloc.c - malloc routines that test returns from malloc and friends */
|
1999-09-08 15:06:24 -04:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-06 19:07:46 -04:00
|
|
|
/*
|
2003-01-03 15:20:47 -05:00
|
|
|
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-06 19:07:46 -04:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
|
*/
|
1998-08-08 20:43:13 -04:00
|
|
|
|
1999-08-19 13:06:28 -04:00
|
|
|
#define CH_FREE 1
|
|
|
|
|
|
1998-10-24 21:41:42 -04:00
|
|
|
#include "portable.h"
|
|
|
|
|
|
1998-08-08 20:43:13 -04:00
|
|
|
#include <stdio.h>
|
1999-06-02 20:37:44 -04:00
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-10-24 21:41:42 -04:00
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
1998-08-08 20:43:13 -04:00
|
|
|
#include "slap.h"
|
|
|
|
|
|
2003-04-09 19:37:00 -04:00
|
|
|
BerMemoryFunctions ch_mfuncs = {
|
|
|
|
|
(BER_MEMALLOC_FN *)ch_malloc,
|
|
|
|
|
(BER_MEMCALLOC_FN *)ch_calloc,
|
|
|
|
|
(BER_MEMREALLOC_FN *)ch_realloc,
|
|
|
|
|
(BER_MEMFREE_FN *)ch_free
|
|
|
|
|
};
|
1999-08-19 13:06:28 -04:00
|
|
|
|
1998-11-11 18:37:38 -05:00
|
|
|
void *
|
1998-08-08 20:43:13 -04:00
|
|
|
ch_malloc(
|
1999-06-18 19:53:05 -04:00
|
|
|
ber_len_t size
|
1998-08-08 20:43:13 -04:00
|
|
|
)
|
|
|
|
|
{
|
1998-11-11 18:37:38 -05:00
|
|
|
void *new;
|
1998-08-08 20:43:13 -04:00
|
|
|
|
2003-04-09 19:37:00 -04:00
|
|
|
if ( (new = (void *) ber_memalloc_x( size, NULL )) == NULL ) {
|
2001-01-11 12:11:23 -05:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-11 16:33:24 -04:00
|
|
|
LDAP_LOG( OPERATION, ERR,
|
|
|
|
|
"ch_malloc: allocation of %lu bytes failed\n", (long)size, 0,0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#else
|
1999-06-18 19:53:05 -04:00
|
|
|
Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
|
|
|
|
|
(long) size, 0, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#endif
|
1999-08-13 21:34:25 -04:00
|
|
|
assert( 0 );
|
|
|
|
|
exit( EXIT_FAILURE );
|
1998-08-08 20:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
|
}
|
|
|
|
|
|
1998-11-11 18:37:38 -05:00
|
|
|
void *
|
1998-08-08 20:43:13 -04:00
|
|
|
ch_realloc(
|
1998-11-11 18:37:38 -05:00
|
|
|
void *block,
|
1999-06-18 19:53:05 -04:00
|
|
|
ber_len_t size
|
1998-08-08 20:43:13 -04:00
|
|
|
)
|
|
|
|
|
{
|
2003-04-12 01:12:40 -04:00
|
|
|
void *new, *ctx;
|
1998-08-08 20:43:13 -04:00
|
|
|
|
|
|
|
|
if ( block == NULL ) {
|
|
|
|
|
return( ch_malloc( size ) );
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-18 19:53:05 -04:00
|
|
|
if( size == 0 ) {
|
|
|
|
|
ch_free( block );
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-12 01:12:40 -04:00
|
|
|
ctx = sl_context( block );
|
|
|
|
|
if ( ctx ) {
|
|
|
|
|
return sl_realloc( block, size, ctx );
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-09 19:37:00 -04:00
|
|
|
if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) {
|
2001-01-11 12:11:23 -05:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-11 16:33:24 -04:00
|
|
|
LDAP_LOG( OPERATION, ERR,
|
|
|
|
|
"ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#else
|
1999-06-18 19:53:05 -04:00
|
|
|
Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
|
|
|
|
|
(long) size, 0, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#endif
|
1999-08-13 21:34:25 -04:00
|
|
|
assert( 0 );
|
|
|
|
|
exit( EXIT_FAILURE );
|
1998-08-08 20:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
|
}
|
|
|
|
|
|
1998-11-11 18:37:38 -05:00
|
|
|
void *
|
1998-08-08 20:43:13 -04:00
|
|
|
ch_calloc(
|
1999-06-18 19:53:05 -04:00
|
|
|
ber_len_t nelem,
|
|
|
|
|
ber_len_t size
|
1998-08-08 20:43:13 -04:00
|
|
|
)
|
|
|
|
|
{
|
1998-11-11 18:37:38 -05:00
|
|
|
void *new;
|
1998-08-08 20:43:13 -04:00
|
|
|
|
2003-04-09 19:37:00 -04:00
|
|
|
if ( (new = (void *) ber_memcalloc_x( nelem, size, NULL )) == NULL ) {
|
2001-01-11 12:11:23 -05:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-11 16:33:24 -04:00
|
|
|
LDAP_LOG( OPERATION, ERR,
|
2001-01-17 11:35:53 -05:00
|
|
|
"ch_calloc: allocation of %lu elements of %lu bytes faild\n",
|
2002-07-11 16:33:24 -04:00
|
|
|
(long)nelem, (long)size, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#else
|
1999-06-18 19:53:05 -04:00
|
|
|
Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
|
|
|
|
|
(long) nelem, (long) size, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#endif
|
1999-08-13 21:34:25 -04:00
|
|
|
assert( 0 );
|
1999-08-03 14:14:24 -04:00
|
|
|
exit( EXIT_FAILURE );
|
1998-08-08 20:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
|
}
|
1998-11-27 15:21:54 -05:00
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
ch_strdup(
|
|
|
|
|
const char *string
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
char *new;
|
|
|
|
|
|
2003-04-09 19:37:00 -04:00
|
|
|
if ( (new = ber_strdup_x( string, NULL )) == NULL ) {
|
2001-01-11 12:11:23 -05:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-11 16:33:24 -04:00
|
|
|
LDAP_LOG( OPERATION, ERR,
|
|
|
|
|
"chr_strdup: duplication of \"%s\" failed\n", string, 0, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#else
|
1999-06-18 19:53:05 -04:00
|
|
|
Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
|
2001-01-11 12:11:23 -05:00
|
|
|
#endif
|
1999-08-13 21:34:25 -04:00
|
|
|
assert( 0 );
|
1999-08-03 14:14:24 -04:00
|
|
|
exit( EXIT_FAILURE );
|
1998-11-27 15:21:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-18 19:53:05 -04:00
|
|
|
void
|
|
|
|
|
ch_free( void *ptr )
|
|
|
|
|
{
|
2003-04-12 01:12:40 -04:00
|
|
|
void *ctx;
|
|
|
|
|
|
|
|
|
|
ctx = sl_context( ptr );
|
|
|
|
|
if (ctx) {
|
|
|
|
|
sl_free( ptr, ctx );
|
|
|
|
|
} else {
|
|
|
|
|
ber_memfree_x( ptr, NULL );
|
|
|
|
|
}
|
1999-07-13 00:11:49 -04:00
|
|
|
}
|
1999-08-19 13:06:28 -04:00
|
|
|
|