diff -cpr HEAD/src/interfaces/ecpg/ecpglib/connect.c ecpg-cleanup/src/interfaces/ecpg/ecpglib/connect.c
*** HEAD/src/interfaces/ecpg/ecpglib/connect.c	Wed Sep 26 19:57:00 2007
--- ecpg-cleanup/src/interfaces/ecpg/ecpglib/connect.c	Fri Sep 28 10:59:29 2007
***************
*** 17,30 ****
  #include "sqlca.h"
  
  #ifdef ENABLE_THREAD_SAFETY
! #ifndef WIN32
! static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
! static pthread_key_t actual_connection_key;
! static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
! #else
! static HANDLE connections_mutex = INVALID_HANDLE_VALUE;
! static DWORD actual_connection_key;
! #endif /* WIN32 */
  #endif
  static struct connection *actual_connection = NULL;
  static struct connection *all_connections = NULL;
--- 17,25 ----
  #include "sqlca.h"
  
  #ifdef ENABLE_THREAD_SAFETY
! static pthread_mutex_t	connections_mutex = PTHREAD_MUTEX_INITIALIZER;
! static pthread_key_t	actual_connection_key;
! static pthread_once_t	actual_connection_key_once = PTHREAD_ONCE_INIT;
  #endif
  static struct connection *actual_connection = NULL;
  static struct connection *all_connections = NULL;
*************** ecpg_actual_connection_init(void)
*** 39,51 ****
  void
  ecpg_pthreads_init(void)
  {
- #ifndef WIN32
  	pthread_once(&actual_connection_key_once, ecpg_actual_connection_init);
- #else
- 	static long has_run = 0;
- 	if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
- 		ecpg_actual_connection_init();
- #endif
  }
  #endif
  
--- 34,40 ----
*************** ecpg_finish(struct connection * act)
*** 134,139 ****
--- 123,129 ----
  		struct ECPGtype_information_cache *cache,
  				   *ptr;
  
+ 		ECPGdeallocate_all_conn(0, ECPG_COMPAT_PGSQL, act);
  		PQfinish(act->connection);
  
  		/*
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/extern.h ecpg-cleanup/src/interfaces/ecpg/ecpglib/extern.h
*** HEAD/src/interfaces/ecpg/ecpglib/extern.h	Wed Sep 26 19:57:00 2007
--- ecpg-cleanup/src/interfaces/ecpg/ecpglib/extern.h	Fri Sep 28 10:59:29 2007
*************** bool ECPGcheck_PQresult(PGresult *, int,
*** 146,151 ****
--- 146,152 ----
  void ECPGraise(int line, int code, const char *sqlstate, const char *str);
  void ECPGraise_backend(int line, PGresult *result, PGconn *conn, int compat);
  char *ECPGprepared(const char *, struct connection *, int);
+ bool ECPGdeallocate_all_conn(int lineno, enum COMPAT_MODE c, struct connection *conn);
  
  /* SQLSTATE values generated or processed by ecpglib (intentionally
   * not exported -- users should refer to the codes directly) */
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/memory.c ecpg-cleanup/src/interfaces/ecpg/ecpglib/memory.c
*** HEAD/src/interfaces/ecpg/ecpglib/memory.c	Wed Nov  8 19:46:47 2006
--- ecpg-cleanup/src/interfaces/ecpg/ecpglib/memory.c	Fri Sep 28 10:59:29 2007
***************
*** 3,8 ****
--- 3,15 ----
  #define POSTGRES_ECPG_INTERNAL
  #include "postgres_fe.h"
  
+ #ifdef ENABLE_THREAD_SAFETY
+ #ifndef WIN32
+ #include <pthread.h>
+ #else
+ #include "ecpg-pthread-win32.h"
+ #endif
+ #endif
  #include "ecpgtype.h"
  #include "ecpglib.h"
  #include "ecpgerrno.h"
*************** ECPGalloc(long size, int lineno)
*** 25,31 ****
  		return NULL;
  	}
  
- 	memset(new, '\0', size);
  	return (new);
  }
  
--- 32,37 ----
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/misc.c ecpg-cleanup/src/interfaces/ecpg/ecpglib/misc.c
*** HEAD/src/interfaces/ecpg/ecpglib/misc.c	Tue Aug 14 19:01:52 2007
--- ecpg-cleanup/src/interfaces/ecpg/ecpglib/misc.c	Fri Sep 28 10:59:29 2007
*************** static struct sqlca_t sqlca_init =
*** 62,74 ****
  };
  
  #ifdef ENABLE_THREAD_SAFETY
- #ifndef WIN32
  static pthread_key_t sqlca_key;
  static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
  #else
- static DWORD sqlca_key;
- #endif
- #else
  static struct sqlca_t sqlca =
  {
  	{
--- 62,70 ----
*************** static struct sqlca_t sqlca =
*** 98,110 ****
  #endif
  
  #ifdef ENABLE_THREAD_SAFETY
- #ifndef WIN32
  static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
  static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
- #else
- static HANDLE debug_mutex = INVALID_HANDLE_VALUE;
- static HANDLE debug_init_mutex = INVALID_HANDLE_VALUE;
- #endif /* WIN32 */
  #endif
  static int	simple_debug = 0;
  static FILE *debugstream = NULL;
--- 94,101 ----
*************** ECPGinit(const struct connection * con, 
*** 135,142 ****
  static void
  ecpg_sqlca_key_destructor(void *arg)
  {
! 	if (arg != NULL)
! 		free(arg);				/* sqlca structure allocated in ECPGget_sqlca */
  }
  
  static void
--- 126,132 ----
  static void
  ecpg_sqlca_key_destructor(void *arg)
  {
! 	free(arg);				/* sqlca structure allocated in ECPGget_sqlca */
  }
  
  static void
*************** ECPGget_sqlca(void)
*** 151,163 ****
  {
  #ifdef ENABLE_THREAD_SAFETY
  	struct sqlca_t *sqlca;
! #ifdef WIN32
! 	static long has_run = 0;
! 	if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
! 		ecpg_sqlca_key_init();
! #else
  	pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
- #endif
  
  	sqlca = pthread_getspecific(sqlca_key);
  	if (sqlca == NULL)
--- 141,148 ----
  {
  #ifdef ENABLE_THREAD_SAFETY
  	struct sqlca_t *sqlca;
! 
  	pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
  
  	sqlca = pthread_getspecific(sqlca_key);
  	if (sqlca == NULL)
*************** ECPGlog(const char *format,...)
*** 263,284 ****
  	va_list		ap;
  	struct sqlca_t *sqlca = ECPGget_sqlca();
  
- #ifdef ENABLE_THREAD_SAFETY
- 	pthread_mutex_lock(&debug_mutex);
- #endif
- 
  	if (simple_debug)
  	{
  		int			bufsize = strlen(format) + 100;
  		char	   *f = (char *) malloc(bufsize);
  
  		if (f == NULL)
- 		{
- #ifdef ENABLE_THREAD_SAFETY
- 			pthread_mutex_unlock(&debug_mutex);
- #endif
  			return;
- 		}
  
  		/*
  		 * regression tests set this environment variable to get the same
--- 248,260 ----
*************** ECPGlog(const char *format,...)
*** 289,294 ****
--- 265,274 ----
  		else
  			snprintf(f, bufsize, "[%d]: %s", (int) getpid(), format);
  
+ #ifdef ENABLE_THREAD_SAFETY
+ 		pthread_mutex_lock(&debug_mutex);
+ #endif
+ 
  		va_start(ap, format);
  		vfprintf(debugstream, f, ap);
  		va_end(ap);
*************** ECPGlog(const char *format,...)
*** 300,311 ****
  
  		fflush(debugstream);
  
- 		ECPGfree(f);
- 	}
- 
  #ifdef ENABLE_THREAD_SAFETY
! 	pthread_mutex_unlock(&debug_mutex);
  #endif
  }
  
  void
--- 280,291 ----
  
  		fflush(debugstream);
  
  #ifdef ENABLE_THREAD_SAFETY
! 		pthread_mutex_unlock(&debug_mutex);
  #endif
+ 
+ 		free(f);
+ 	}
  }
  
  void
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/prepare.c ecpg-cleanup/src/interfaces/ecpg/ecpglib/prepare.c
*** HEAD/src/interfaces/ecpg/ecpglib/prepare.c	Wed Sep 26 19:57:00 2007
--- ecpg-cleanup/src/interfaces/ecpg/ecpglib/prepare.c	Fri Sep 28 10:59:29 2007
*************** typedef struct 
*** 31,38 ****
  } stmtCacheEntry;
  
  static int             nextStmtID               = 1;
! static int             stmtCacheNBuckets        = 2039;     /* # buckets - a prime # */
! static int             stmtCacheEntPerBucket    = 8;        /* # entries/bucket     */
  static stmtCacheEntry  stmtCacheEntries[16384] = {{0,{0},0,0,0}};
  
  static struct prepared_statement *find_prepared_statement(const char *name,
--- 31,38 ----
  } stmtCacheEntry;
  
  static int             nextStmtID               = 1;
! const static int       stmtCacheNBuckets        = 2039;     /* # buckets - a prime # */
! const static int       stmtCacheEntPerBucket    = 8;        /* # entries/bucket     */
  static stmtCacheEntry  stmtCacheEntries[16384] = {{0,{0},0,0,0}};
  
  static struct prepared_statement *find_prepared_statement(const char *name,
*************** ECPGdeallocate(int lineno, int c, const 
*** 263,282 ****
  }
  
  bool
! ECPGdeallocate_all(int lineno, int compat, const char *connection_name)
  {
- 	struct connection		   *con;
- 
- 	con = ECPGget_connection(connection_name);
- 
  	/* deallocate all prepared statements */
  	while (con->prep_stmts)
  	{
! 		if (!deallocate_one(lineno, compat, con, NULL, con->prep_stmts))
  			return false;
  	}
  
  	return true;
  }
  
  char *
--- 263,284 ----
  }
  
  bool
! ECPGdeallocate_all_conn(int lineno, enum COMPAT_MODE c, struct connection *con)
  {
  	/* deallocate all prepared statements */
  	while (con->prep_stmts)
  	{
! 		if (!deallocate_one(lineno, c, con, NULL, con->prep_stmts))
  			return false;
  	}
  
  	return true;
+ }
+ 
+ bool
+ ECPGdeallocate_all(int lineno, int compat, const char *connection_name)
+ {
+ 	return ECPGdeallocate_all_conn(lineno, compat, ECPGget_connection(connection_name));
  }
  
  char *
diff -cpr HEAD/src/interfaces/ecpg/include/ecpg-pthread-win32.h ecpg-cleanup/src/interfaces/ecpg/include/ecpg-pthread-win32.h
*** HEAD/src/interfaces/ecpg/include/ecpg-pthread-win32.h	Thu Mar 29 21:02:24 2007
--- ecpg-cleanup/src/interfaces/ecpg/include/ecpg-pthread-win32.h	Fri Sep 28 10:59:29 2007
***************
*** 4,16 ****
   */
  #ifndef _ECPG_PTHREAD_WIN32_H
  #define _ECPG_PTHREAD_WIN32_H
! #define pthread_mutex_lock(x) do { \
! 	if (*x == INVALID_HANDLE_VALUE) \
! 	   *x = CreateMutex(NULL, FALSE, NULL); \
!     WaitForSingleObject(*x, INFINITE); \
! } while (0);
! #define pthread_mutex_unlock(x) ReleaseMutex(*x)
! #define pthread_getspecific(x) TlsGetValue(x)
! #define pthread_setspecific(x,y) TlsSetValue(x,y)
! #define pthread_key_create(x,y) *x = TlsAlloc();
! #endif
--- 4,46 ----
   */
  #ifndef _ECPG_PTHREAD_WIN32_H
  #define _ECPG_PTHREAD_WIN32_H
! 
! typedef HANDLE		pthread_mutex_t;
! typedef DWORD		pthread_key_t;
! typedef LONG		pthread_once_t;
! 
! #define PTHREAD_MUTEX_INITIALIZER	INVALID_HANDLE_VALUE
! #define	PTHREAD_ONCE_INIT			0
! 
! #define pthread_mutex_lock(mutex) do { \
! 	if (*(mutex) == INVALID_HANDLE_VALUE) { \
! 		HANDLE m = CreateMutex(NULL, FALSE, NULL); \
! 		if (InterlockedCompareExchangePointer((mutex), m, \
! 			INVALID_HANDLE_VALUE) != INVALID_HANDLE_VALUE) \
! 			CloseHandle(m); \
! 	} \
! 	WaitForSingleObject(*(mutex), INFINITE); \
! } while(0)
! 
! #define pthread_mutex_unlock(mutex) \
! 	ReleaseMutex(*(mutex))
! 
! #define pthread_getspecific(key) \
! 	TlsGetValue((key))
! 
! #define pthread_setspecific(key, value) \
! 	TlsSetValue((key), (value))
! 
! /*
!  * FIXME: destructor is never called in Win32.
!  */
! #define pthread_key_create(key, destructor) \
! 	do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
! 
! #define pthread_once(key, fn) \
! 	do { \
! 		if (InterlockedCompareExchange((key), 1, 0) == 0) \
! 			(fn)(); \
! 	} while(0)
! 
! #endif   /* _ECPG_PTHREAD_WIN32_H */
diff -cpr HEAD/src/interfaces/ecpg/pgtypeslib/dt_common.c ecpg-cleanup/src/interfaces/ecpg/pgtypeslib/dt_common.c
*** HEAD/src/interfaces/ecpg/pgtypeslib/dt_common.c	Wed Aug 22 17:20:58 2007
--- ecpg-cleanup/src/interfaces/ecpg/pgtypeslib/dt_common.c	Fri Sep 28 10:59:29 2007
*************** static datetkn deltatktbl[] = {
*** 496,503 ****
  	{"yrs", UNITS, DTK_YEAR},	/* "years" relative */
  };
  
! static unsigned int szdatetktbl = sizeof datetktbl / sizeof datetktbl[0];
! static unsigned int szdeltatktbl = sizeof deltatktbl / sizeof deltatktbl[0];
  
  static datetkn *datecache[MAXDATEFIELDS] = {NULL};
  
--- 496,503 ----
  	{"yrs", UNITS, DTK_YEAR},	/* "years" relative */
  };
  
! static const unsigned int szdatetktbl = lengthof(datetktbl);
! static const unsigned int szdeltatktbl = lengthof(deltatktbl);
  
  static datetkn *datecache[MAXDATEFIELDS] = {NULL};
  
diff -cpr HEAD/src/interfaces/ecpg/test/expected/preproc-autoprep.stderr ecpg-cleanup/src/interfaces/ecpg/test/expected/preproc-autoprep.stderr
*** HEAD/src/interfaces/ecpg/test/expected/preproc-autoprep.stderr	Tue Aug 14 19:01:53 2007
--- ecpg-cleanup/src/interfaces/ecpg/test/expected/preproc-autoprep.stderr	Fri Sep 28 11:08:20 2007
***************
*** 112,116 ****
--- 112,134 ----
  [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ECPGexecute line 42 Ok: DROP TABLE
  [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg8
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg7
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg6
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg5
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg4
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: i
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg3
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg2
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: ecpg1
+ [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ecpg_finish: Connection regress1 closed.
  [NO_PID]: sqlca: code: 0, state: 00000
diff -cpr HEAD/src/interfaces/ecpg/test/expected/sql-execute.stderr ecpg-cleanup/src/interfaces/ecpg/test/expected/sql-execute.stderr
*** HEAD/src/interfaces/ecpg/test/expected/sql-execute.stderr	Tue Aug 14 19:01:53 2007
--- ecpg-cleanup/src/interfaces/ecpg/test/expected/sql-execute.stderr	Fri Sep 28 11:08:49 2007
***************
*** 146,150 ****
--- 146,154 ----
  [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ECPGtrans line 90 action = commit connection = main
  [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: f
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: i
+ [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ecpg_finish: Connection main closed.
  [NO_PID]: sqlca: code: 0, state: 00000
diff -cpr HEAD/src/interfaces/ecpg/test/expected/sql-oldexec.stderr ecpg-cleanup/src/interfaces/ecpg/test/expected/sql-oldexec.stderr
*** HEAD/src/interfaces/ecpg/test/expected/sql-oldexec.stderr	Tue Aug 14 19:01:54 2007
--- ecpg-cleanup/src/interfaces/ecpg/test/expected/sql-oldexec.stderr	Fri Sep 28 11:09:12 2007
***************
*** 146,150 ****
--- 146,154 ----
  [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ECPGtrans line 89 action = commit connection = main
  [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: f
+ [NO_PID]: sqlca: code: 0, state: 00000
+ [NO_PID]: ECPGdeallocate line 0: NAME: i
+ [NO_PID]: sqlca: code: 0, state: 00000
  [NO_PID]: ecpg_finish: Connection main closed.
  [NO_PID]: sqlca: code: 0, state: 00000
