includes single to multiple hooks changes.
ber_mem* reimplementation.
namespace glue (finally naming has not be decided upon nor implemented).
Added ldap_int_strdup to handle "internal" strdup'ing, this version uses hooks.
ldap_pvt_strdup still available for when strdup() is missing, this version
directly uses system allocators.
Updated -lldif to use ber allocators. Items returned by ldif routines
should be ber_memfree()d as needed.
}
fputs( ldif, stdout );
- free( ldif );
+ ber_memfree( ldif );
return( 0 );
}
#define LBER_OPT_DEBUG_LEVEL LBER_OPT_BER_DEBUG
#define LBER_OPT_LOG_PRINT_FN 0x8001
-#define LBER_OPT_MEMORY_FN 0x8002
+#define LBER_OPT_MEMORY_FNS 0x8002
typedef void (*BER_LOG_PRINT_FN) LDAP_P(( char *buf ));
-typedef void* (*BER_MEMORY_FN) LDAP_P(( void *p, size_t size ));
+
+typedef void* (*BER_MEMALLOC_FN) LDAP_P(( size_t size ));
+typedef void* (*BER_MEMCALLOC_FN) LDAP_P(( size_t n, size_t size ));
+typedef void* (*BER_MEMREALLOC_FN) LDAP_P(( void *p, size_t size ));
+typedef void (*BER_MEMFREE_FN) LDAP_P(( void *p ));
+
+typedef struct lber_memory_fns {
+ BER_MEMALLOC_FN bmf_malloc;
+ BER_MEMCALLOC_FN bmf_calloc;
+ BER_MEMREALLOC_FN bmf_realloc;
+ BER_MEMFREE_FN bmf_free;
+} BerMemoryFunctions;
/* LBER Sockbuf options */
#define LBER_TO_FILE 0x01 /* to a file referenced by sb_fd */
#include "ldap_cdefs.h"
#include "ldap_features.h"
-#include "ac/assert.h"
+#include <ac/assert.h>
#endif /* _LDAP_PORTABLE_H */
LDAP_BEGIN_DECL
-extern BER_MEMORY_FN ber_int_realloc;
-
struct lber_options {
short lbo_valid;
#define LBER_UNINITIALIZED 0x0
/* memory.c */
/* simple macros to realloc for now */
-#define LBER_MALLOC(s) \
- (ber_int_realloc \
- ? (*ber_int_realloc)(NULL,(s)) \
- : realloc(NULL,(s)))
-
-#define LBER_CALLOC(n,s) \
- (ber_int_realloc \
- ? ber_int_calloc((n),(s)) \
- : calloc((n),(s)))
-
-#define LBER_REALLOC(p,s) \
- (ber_int_realloc \
- ? (*ber_int_realloc)((p),(s)) \
- : realloc((p),(s)))
-
-#define LBER_FREE(p) \
- (ber_int_realloc \
- ? (*ber_int_realloc)((p),(size_t)0) \
- : realloc((p),(size_t)0))
-
-LDAP_F( void * )
-ber_int_calloc LDAP_P((
- size_t n,
- size_t size ));
+extern BerMemoryFunctions* ber_int_memory_fns;
+#define LBER_INT_MALLOC(s) ber_memalloc((s))
+#define LBER_INT_CALLOC(n,s) ber_memcalloc((n),(s))
+#define LBER_INT_REALLOC(p,s) ber_memrealloc((p),(s))
+#define LBER_INT_FREE(p) ber_memfree((p))
+
+#define LBER_MALLOC(s) ber_memalloc((s))
+#define LBER_CALLOC(n,s) ber_memcalloc((n),(s))
+#define LBER_REALLOC(p,s) ber_memrealloc((p),(s))
+#define LBER_FREE(p) ber_memfree((p))
/* sockbuf.c */
#include "lber-int.h"
+BerMemoryFunctions *ber_int_memory_fns = NULL;
+
void
ber_memfree( void *p )
{
ber_int_options.lbo_valid = LBER_INITIALIZED;
- LBER_FREE( p );
+
+ assert( p != NULL );
+
+ if( ber_int_memory_fns == NULL ) {
+ free( p );
+ return;
+ }
+
+ assert( ber_int_memory_fns->bmf_free );
+
+ (*ber_int_memory_fns->bmf_free)( p );
}
void *
ber_memalloc( size_t s )
{
ber_int_options.lbo_valid = LBER_INITIALIZED;
- return LBER_MALLOC( s );
+
+ assert( s );
+
+ if( ber_int_memory_fns == NULL ) {
+ return malloc( s );
+ }
+
+ assert( ber_int_memory_fns->bmf_malloc );
+
+ return (*ber_int_memory_fns->bmf_malloc)( s );
}
void *
ber_memcalloc( size_t n, size_t s )
{
ber_int_options.lbo_valid = LBER_INITIALIZED;
- return LBER_CALLOC( n, s );
+
+ assert( n && s );
+
+ if( ber_int_memory_fns == NULL ) {
+ return calloc( n, s );
+ }
+
+ assert( ber_int_memory_fns->bmf_calloc );
+
+ return (*ber_int_memory_fns->bmf_calloc)( n, s );
}
void *
ber_memrealloc( void* p, size_t s )
{
ber_int_options.lbo_valid = LBER_INITIALIZED;
- return LBER_REALLOC( p, s );
-}
-
-BER_MEMORY_FN ber_int_realloc = NULL;
-void *
-ber_int_calloc( size_t n, size_t s )
-{
- ber_int_options.lbo_valid = LBER_INITIALIZED;
+ if( p == NULL ) {
+ return ber_memalloc( s );
+ }
+
+ if( s == 0 ) {
+ ber_memfree( p );
+ return NULL;
+ }
- {
- size_t size = n * s;
- void *p = (*ber_int_realloc)( NULL, size );
- return p ? memset( p, 0, size ) : p;
+ if( ber_int_memory_fns == NULL ) {
+ return realloc( p, s );
}
+
+ assert( ber_int_memory_fns->bmf_realloc );
+
+ return (*ber_int_memory_fns->bmf_realloc)( p, s );
}
#include "portable.h"
#include <stdlib.h>
+#include <ac/string.h>
#include "lber-int.h"
Sockbuf *sb;
if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
- && ( option == LBER_OPT_MEMORY_FN )
+ && ( ber_int_memory_fns == NULL )
+ && ( option == LBER_OPT_MEMORY_FNS )
&& ( invalue != NULL ))
{
- ber_int_realloc = (BER_MEMORY_FN) invalue;
+ BerMemoryFunctions *f = (BerMemoryFunctions *) invalue;
+
+ /* make sure all functions are provided */
+ if(!( f->bmf_malloc && f->bmf_calloc
+ && f->bmf_realloc && f->bmf_free ))
+ {
+ return LBER_OPT_ERROR;
+ }
+
+ ber_int_memory_fns = (BerMemoryFunctions *)
+ (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
+
+ if ( ber_int_memory_fns == NULL ) {
+ return LBER_OPT_ERROR;
+ }
+
+ memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
+
ber_int_options.lbo_valid = LBER_INITIALIZED;
return LBER_OPT_SUCCESS;
}
/* memory.c */
/* simple macros to realloc for now */
+#define LDAP_INT_MALLOC(s) (LBER_MALLOC((s)))
+#define LDAP_INT_CALLOC(n,s) (LBER_CALLOC((n),(s)))
+#define LDAP_INT_REALLOC(p,s) (LBER_REALLOC((p),(s)))
+#define LDAP_INT_FREE(p) (LBER_FREE((p)))
+
+#ifndef LDAP_MALLOC
#define LDAP_MALLOC(s) (LBER_MALLOC((s)))
#define LDAP_CALLOC(n,s) (LBER_CALLOC((n),(s)))
#define LDAP_REALLOC(p,s) (LBER_REALLOC((p),(s)))
#define LDAP_FREE(p) (LBER_FREE((p)))
+#endif
/*
* in unit-int.c
int sizelimit ));
/*
- * in strdup.c
+ * in string.c
*/
-char *ldap_pvt_strdup LDAP_P(( const char * ));
+char *ldap_int_strdup LDAP_P(( const char * ));
#undef strdup
-#define strdup ldap_pvt_strdup
+#define strdup ldap_int_strdup
/*
* in unbind.c
char *p;
size_t len = strlen( s ) + 1;
- if ( (p = (char *) LDAP_MALLOC( len )) == NULL ) {
+ if ( (p = (char *) malloc( len )) == NULL ) {
return( NULL );
}
memcpy( p, s, len );
return( p );
}
+
+char *
+(ldap_int_strdup)( const char *s )
+{
+ char *p;
+ size_t len = strlen( s ) + 1;
+
+ if ( (p = (char *) LDAP_MALLOC( len )) == NULL ) {
+ return( NULL );
+ }
+
+ memcpy( p, s, len );
+ return( p );
+}
\ No newline at end of file
char *
ldif_type_and_value( LDAP_CONST char *type, LDAP_CONST char *val, int vlen )
/*
- * return malloc'd, zero-terminated LDIF line
+ * return BER malloc'd, zero-terminated LDIF line
*/
{
char *buf, *p;
int tlen;
tlen = strlen( type );
- if (( buf = (char *) malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 ))
+ if (( buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 ))
== NULL )
{
ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,