From 10c94f4e58f193e89b9c0806d7436ad331671396 Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Sat, 12 Nov 2005 18:26:17 +0000
Subject: [PATCH] Cleanup lbo_valid / meminuse / memfuncs

---
 libraries/liblber/io.c       | 10 -------
 libraries/liblber/lber-int.h | 11 +++++++-
 libraries/liblber/memory.c   | 48 ++++++-------------------------
 libraries/liblber/options.c  | 55 ++++++++++++++++--------------------
 libraries/liblber/sockbuf.c  |  2 --
 5 files changed, 44 insertions(+), 82 deletions(-)

diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c
index fc37f8dfb2..19757b7826 100644
--- a/libraries/liblber/io.c
+++ b/libraries/liblber/io.c
@@ -248,8 +248,6 @@ ber_alloc_t( int options )
 {
 	BerElement	*ber;
 
-    ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
 
 	if ( ber == NULL ) {
@@ -301,8 +299,6 @@ ber_init2( BerElement *ber, struct berval *bv, int options )
 {
 	assert( ber != NULL );
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	(void) memset( (char *)ber, '\0', sizeof( BerElement ));
 	ber->ber_valid = LBER_VALID_BERELEMENT;
 	ber->ber_tag = LBER_DEFAULT;
@@ -336,8 +332,6 @@ ber_init( struct berval *bv )
 
 	assert( bv != NULL );
 
-    ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if ( bv == NULL ) {
 		return NULL;
 	}
@@ -377,8 +371,6 @@ int ber_flatten2(
 {
 	assert( bv != NULL );
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if ( bv == NULL ) {
 		return -1;
 	}
@@ -416,8 +408,6 @@ int ber_flatten(
  
 	assert( bvPtr != NULL );
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if(bvPtr == NULL) {
 		return -1;
 	}
diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h
index 3133540405..16cabd995c 100644
--- a/libraries/liblber/lber-int.h
+++ b/libraries/liblber/lber-int.h
@@ -38,11 +38,20 @@ typedef void (*BER_LOG_FN)(FILE *file,
 
 LBER_V (BER_ERRNO_FN) ber_int_errno_fn;
 
+#ifdef LDAP_MEMORY_TRACE
+# ifndef LDAP_MEMORY_DEBUG
+#  define LDAP_MEMORY_DEBUG 1
+# endif
+#endif
+
+#ifdef LDAP_MEMORY_DEBUG
+LBER_V (long)	ber_int_meminuse;
+#endif
+
 struct lber_options {
 	short lbo_valid;
 	unsigned short		lbo_options;
 	int			lbo_debug;
-	long		lbo_meminuse;
 };
 
 LBER_F( int ) ber_pvt_log_output(
diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c
index a9dc92b915..899babb87c 100644
--- a/libraries/liblber/memory.c
+++ b/libraries/liblber/memory.c
@@ -21,9 +21,6 @@
 #include "lber-int.h"
 
 #ifdef LDAP_MEMORY_TRACE
-# ifndef LDAP_MEMORY_DEBUG
-#  define LDAP_MEMORY_DEBUG 1
-# endif
 #include <stdio.h>
 #endif
 
@@ -67,7 +64,7 @@ struct ber_mem_hdr {
 
 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
 
-/* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
+/* Note sequence and ber_int_meminuse are counters, but are not
  * thread safe.  If you want to use these values for multithreaded applications,
  * you must put mutexes around them, otherwise they will have incorrect values.
  * When debugging, if you sort the debug output, the sequence number will 
@@ -125,8 +122,6 @@ BerMemoryFunctions *ber_int_memory_fns = NULL;
 void
 ber_memfree_x( void *p, void *ctx )
 {
-    ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( p == NULL ) {
 		return;
 	}
@@ -140,12 +135,12 @@ ber_memfree_x( void *p, void *ctx )
 		assert( mh->bm_top == LBER_MEM_JUNK);
 		assert( testdatatop( mh));
 		assert( testend( (char *)&mh[1] + mh->bm_length) );
-		ber_int_options.lbo_meminuse -= mh->bm_length;
+		ber_int_meminuse -= mh->bm_length;
 
 #ifdef LDAP_MEMORY_TRACE
 		fprintf(stderr, "0x%08lx 0x%08lx -f- %ld ber_memfree %ld\n",
 			(long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
-			ber_int_options.lbo_meminuse);
+			ber_int_meminuse);
 #endif
 		/* Fill the free space with poison */
 		memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
@@ -172,8 +167,6 @@ ber_memvfree_x( void **vec, void *ctx )
 {
 	int	i;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( vec == NULL ) {
 		return;
 	}
@@ -197,7 +190,6 @@ void *
 ber_memalloc_x( ber_len_t s, void *ctx )
 {
 	void *new;
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
 
 #ifdef LDAP_MEMORY_DEBUG
 	assert( s != 0 );
@@ -217,13 +209,13 @@ ber_memalloc_x( ber_len_t s, void *ctx )
 		setdatatop( mh);
 		setend( (char *)&mh[1] + mh->bm_length );
 
-		ber_int_options.lbo_meminuse += mh->bm_length;	/* Count mem inuse */
+		ber_int_meminuse += mh->bm_length;	/* Count mem inuse */
 
 #ifdef LDAP_MEMORY_TRACE
 		mh->bm_sequence = sequence++;
 		fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memalloc %ld\n",
 			(long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
-			ber_int_options.lbo_meminuse);
+			ber_int_meminuse);
 #endif
 		/* poison new memory */
 		memset( (char *)&mh[1], 0xff, s);
@@ -254,7 +246,6 @@ void *
 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
 {
 	void *new;
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
 
 #ifdef LDAP_MEMORY_DEBUG
 	assert( n != 0 && s != 0);
@@ -275,13 +266,13 @@ ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
 		setdatatop( mh);
 		setend( (char *)&mh[1] + mh->bm_length );
 
-		ber_int_options.lbo_meminuse += mh->bm_length;
+		ber_int_meminuse += mh->bm_length;
 
 #ifdef LDAP_MEMORY_TRACE
 		mh->bm_sequence = sequence++;
 		fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memcalloc %ld\n",
 			(long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
-			ber_int_options.lbo_meminuse);
+			ber_int_meminuse);
 #endif
 		BER_MEM_VALID( &mh[1] );
 		new = &mh[1];
@@ -310,7 +301,6 @@ void *
 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
 {
 	void *new = NULL;
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
 
 	/* realloc(NULL,s) -> malloc(s) */
 	if( p == NULL ) {
@@ -352,11 +342,11 @@ ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
 		assert( mh->bm_top == LBER_MEM_JUNK);
 		assert( testdatatop( mh));
 
-		ber_int_options.lbo_meminuse += s - oldlen;
+		ber_int_meminuse += s - oldlen;
 #ifdef LDAP_MEMORY_TRACE
 		fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memrealloc %ld\n",
 			(long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
-			ber_int_options.lbo_meminuse);
+			ber_int_meminuse);
 #endif
 			BER_MEM_VALID( &mh[1] );
 		return &mh[1];
@@ -383,8 +373,6 @@ ber_memrealloc( void* p, ber_len_t s )
 void
 ber_bvfree_x( struct berval *bv, void *ctx )
 {
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( bv == NULL ) {
 		return;
 	}
@@ -409,8 +397,6 @@ ber_bvecfree_x( struct berval **bv, void *ctx )
 {
 	int	i;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( bv == NULL ) {
 		return;
 	}
@@ -440,8 +426,6 @@ ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
 	ber_len_t i;
 	struct berval **new;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( *bvec == NULL ) {
 		if( bv == NULL ) {
 			/* nothing to add */
@@ -497,8 +481,6 @@ ber_dupbv_x(
 {
 	struct berval *new;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( src == NULL ) {
 		ber_errno = LBER_ERROR_PARAM;
 		return NULL;
@@ -554,8 +536,6 @@ ber_str2bv_x(
 {
 	struct berval *new;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( s == NULL ) {
 		ber_errno = LBER_ERROR_PARAM;
 		return NULL;
@@ -602,8 +582,6 @@ ber_mem2bv_x(
 {
 	struct berval *new;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if( s == NULL ) {
 		ber_errno = LBER_ERROR_PARAM;
 		return NULL;
@@ -650,8 +628,6 @@ ber_strdup_x( LDAP_CONST char *s, void *ctx )
 	char    *p;
 	size_t	len;
 	
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 #ifdef LDAP_MEMORY_DEBUG
 	assert(s != NULL);			/* bv damn better point to something */
 #endif
@@ -684,8 +660,6 @@ ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
 	char    *p;
 	size_t	len;
 	
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 #ifdef LDAP_MEMORY_DEBUG
 	assert(s != NULL);			/* bv damn better point to something */
 #endif
@@ -748,8 +722,6 @@ ber_bvarray_free_x( BerVarray a, void *ctx )
 {
 	int i;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if (a) {
 		BER_MEM_VALID( a );
 
@@ -776,8 +748,6 @@ ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
 {
 	int	n;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if ( *a == NULL ) {
 		if (bv == NULL) {
 			return 0;
diff --git a/libraries/liblber/options.c b/libraries/liblber/options.c
index 39f58a6bef..6982ab79f4 100644
--- a/libraries/liblber/options.c
+++ b/libraries/liblber/options.c
@@ -23,7 +23,7 @@
 char ber_pvt_opt_on;	/* used to get a non-NULL address for *_OPT_ON */
 
 struct lber_options ber_int_options = {
-	LBER_UNINITIALIZED, 0, 0, 0 };
+	LBER_UNINITIALIZED, 0, 0 };
 
 static BerMemoryFunctions	ber_int_memory_fns_datum;
 
@@ -36,8 +36,6 @@ ber_get_option(
 	const BerElement *ber;
 	const Sockbuf *sb;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if(outvalue == NULL) {
 		/* no place to get to */
 		ber_errno = LBER_ERROR_PARAM;
@@ -59,7 +57,7 @@ ber_get_option(
 			 * The counter is not accurate for multithreaded ldap applications.
 			 */
 #ifdef LDAP_MEMORY_DEBUG
-			* (int *) outvalue = ber_int_options.lbo_meminuse;
+			* (int *) outvalue = ber_int_meminuse;
 			return LBER_OPT_SUCCESS;
 #else
 			return LBER_OPT_ERROR;
@@ -126,31 +124,6 @@ ber_set_option(
 	BerElement *ber;
 	Sockbuf *sb;
 
-	if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
-		&& ( ber_int_memory_fns == NULL )
-		&& ( option == LBER_OPT_MEMORY_FNS )
-		&& ( invalue != NULL ) )
-	{
-		const BerMemoryFunctions *f =
-			(const BerMemoryFunctions *) invalue;
-		/* make sure all functions are provided */
-		if(!( f->bmf_malloc && f->bmf_calloc
-			&& f->bmf_realloc && f->bmf_free ))
-		{
-			ber_errno = LBER_ERROR_PARAM;
-			return LBER_OPT_ERROR;
-		}
-
-		ber_int_memory_fns = &ber_int_memory_fns_datum;
-
-		AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
-
-		ber_int_options.lbo_valid = LBER_INITIALIZED;
-		return LBER_OPT_SUCCESS;
-	}
-
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	if(invalue == NULL) {
 		/* no place to set from */
 		ber_errno = LBER_ERROR_PARAM;
@@ -180,11 +153,33 @@ ber_set_option(
 			 * The counter is not accurate for multithreaded applications.
 			 */
 #ifdef LDAP_MEMORY_DEBUG
-			ber_int_options.lbo_meminuse = * (int *) invalue;
+			ber_int_meminuse = * (int *) invalue;
 			return LBER_OPT_SUCCESS;
 #else
 			return LBER_OPT_ERROR;
 #endif
+		case LBER_OPT_MEMORY_FNS:
+			if ( ber_int_memory_fns == NULL )
+			{
+				const BerMemoryFunctions *f =
+					(const BerMemoryFunctions *) invalue;
+				/* make sure all functions are provided */
+				if(!( f->bmf_malloc && f->bmf_calloc
+					&& f->bmf_realloc && f->bmf_free ))
+				{
+					ber_errno = LBER_ERROR_PARAM;
+					return LBER_OPT_ERROR;
+				}
+
+				ber_int_memory_fns = &ber_int_memory_fns_datum;
+
+				AC_MEMCPY(ber_int_memory_fns, f,
+					 sizeof(BerMemoryFunctions));
+
+				return LBER_OPT_SUCCESS;
+			}
+			break;
+
 		case LBER_OPT_LOG_PROC:
 			ber_int_log_proc = (BER_LOG_FN)invalue;
 			return LBER_OPT_SUCCESS;
diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c
index c30469cdec..21cf9eacb4 100644
--- a/libraries/liblber/sockbuf.c
+++ b/libraries/liblber/sockbuf.c
@@ -57,8 +57,6 @@ ber_sockbuf_alloc( void )
 {
 	Sockbuf			*sb;
 
-	ber_int_options.lbo_valid = LBER_INITIALIZED;
-
 	sb = LBER_CALLOC( 1, sizeof( Sockbuf ) );
 
 	if( sb == NULL ) return NULL;
-- 
GitLab