Index: /trunk/archive/pslib/include/psArray.h
===================================================================
--- /trunk/archive/pslib/include/psArray.h	(revision 181)
+++ /trunk/archive/pslib/include/psArray.h	(revision 181)
@@ -0,0 +1,137 @@
+#if !defined(PS_ARRAY_H)
+#define PS_ARRAY_H
+
+#include <stdlib.h>
+/*
+ * Declare TYPEArray
+ */
+#define PS_DECLARE_ARRAY_TYPE(TYPE) \
+    typedef struct { \
+	int size;			/* number of allocated elements */ \
+	int n;				/* number of elements in use */ \
+	\
+	TYPE *arr;			/* the actual data */ \
+    } PS_CONCAT(TYPE, Array); \
+    \
+PS_CONCAT(TYPE, Array) *PS_CONCAT3(TYPE,Array,Alloc)(int n, int s); \
+PS_CONCAT(TYPE, Array) *PS_CONCAT3(TYPE,Array,Realloc)(PS_CONCAT(TYPE, Array) *arr, int n); \
+void PS_CONCAT3(TYPE,Array,Free)(PS_CONCAT(TYPE, Array) *arr)
+
+/*****************************************************************************/
+/*
+ * Generate the code TYPEArray's constructors/destructors
+ */
+#define PS_CREATE_ARRAY_TYPE(TYPE) \
+    P_PS_CREATE_ARRAY_TYPE(,,TYPE)
+
+#define P_PS_CREATE_ARRAY_TYPE(QUAL,PREFIX,TYPE) \
+\
+QUAL PS_CONCAT(TYPE, Array) *PS_CONCAT4(PREFIX,TYPE,Array,Alloc)(int n, /* initial dimension */\
+                                                   int s) /* initial number of elements */ \
+{ \
+    PS_CONCAT(TYPE, Array) *arr = psAlloc(sizeof(PS_CONCAT(TYPE, Array))); \
+    \
+    if (n > s) { \
+	psLogMsg(PS_STRING(TYPE) "ArrayAlloc", PS_LOG_ERROR, "Too few elements requested in array (%d < %d)\n", s, n); \
+	s = n; \
+    } \
+    \
+    arr->size = s; \
+    arr->n = n; \
+    \
+    if (s == 0) { \
+	arr->arr = NULL; \
+    } else { \
+	arr->arr = psAlloc(s*sizeof(TYPE)); \
+    } \
+    \
+    return arr; \
+} \
+\
+QUAL PS_CONCAT(TYPE, Array) *PS_CONCAT4(PREFIX,TYPE,Array,Realloc)(PS_CONCAT(TYPE, Array) *arr, int n) \
+{ \
+    if (arr == NULL) { \
+	return PS_CONCAT4(PREFIX,TYPE,Array,Alloc)(n, n); \
+    } \
+    \
+    if (n <= arr->size) { \
+	if (arr->n < n) { \
+	    arr->n = n; \
+	} \
+    } else { \
+        arr->arr = psRealloc(arr->arr, n*sizeof(TYPE)); \
+	arr->size = n; \
+    } \
+    \
+    return arr; \
+} \
+\
+QUAL void PS_CONCAT4(PREFIX,TYPE,Array,Free)(PS_CONCAT(TYPE,Array) *arr) \
+{ \
+    if (arr == NULL) { \
+	return; \
+    } \
+    \
+    psFree(arr->arr); \
+    psFree(arr); \
+}
+
+/*****************************************************************************/
+/*
+ * Support for pointer types
+ */
+#define PS_DECLARE_ARRAY_PTR_TYPE(TYPE) \
+    typedef TYPE *P_PS_CONCAT(TYPE, Ptr); \
+    PS_DECLARE_ARRAY_TYPE(PS_CONCAT(TYPE, Ptr))
+
+#define PS_CREATE_ARRAY_PTR_TYPE(TYPE); \
+    P_PS_CREATE_ARRAY_TYPE(static, my_, P_PS_CONCAT(TYPE, Ptr)) \
+    \
+    PS_CONCAT(TYPE, PtrArray) *PS_CONCAT3(TYPE,PtrArray,Alloc)(int n, int s) \
+    { \
+	PS_CONCAT(TYPE, PtrArray) *arr = PS_CONCAT4(my_,TYPE,PtrArray,Alloc)(n, s); \
+	for (int i = 0; i < arr->n; i++) { \
+	    arr->arr[i] = psMemIncrRefCounter,(PS_CONCAT(TYPE, Alloc)());\
+	} \
+        \
+	return arr; \
+    } \
+    \
+    PS_CONCAT(TYPE, PtrArray) *PS_CONCAT3(TYPE,PtrArray,Realloc)(PS_CONCAT(TYPE, PtrArray) *arr, int n) \
+    { \
+	for (int i = n; i < arr->n; i++) { \
+	    PS_CONCAT(TYPE, Free)(psMemDecrRefCounter(arr->arr[i]));\
+	} \
+        \
+	return PS_CONCAT4(my_,TYPE,PtrArray,Realloc)(arr, n); \
+    } \
+    \
+    void PS_CONCAT3(TYPE,PtrArray,Free)(PS_CONCAT2(TYPE,PtrArray) *arr) \
+    { \
+	if (arr == NULL) { return; } \
+	for (int i = 0; i < arr->size; i++) { \
+	    PS_CONCAT(TYPE, Free)(psMemDecrRefCounter(arr->arr[i])); \
+	} \
+	PS_CONCAT4(my_, TYPE, PtrArray, Free)(arr); \
+    }
+
+/*****************************************************************************/
+/*
+ * Declare some common types of arrays
+ *
+ * psVoidPtrArray is special, as it needs to have a destructor that
+ * accepts a destructor for the elements, so we cannot simply use
+ *       PS_DECLARE_ARRAY_TYPE(psVoidPtr);
+ */
+typedef struct {
+   int n, size;
+   void **arr;
+} psVoidPtrArray;
+
+psVoidPtrArray *psVoidPtrArrayAlloc(int n, int s);
+psVoidPtrArray *psVoidPtrArrayRealloc(psVoidPtrArray *arr, int n);
+void psVoidPtrArrayFree(psVoidPtrArray *arr,
+			void (*elemFree)(void *)); // destructor for array data
+
+
+#endif
Index: /trunk/archive/pslib/include/psDlist.h
===================================================================
--- /trunk/archive/pslib/include/psDlist.h	(revision 181)
+++ /trunk/archive/pslib/include/psDlist.h	(revision 181)
@@ -0,0 +1,62 @@
+#if !defined(PS_DLIST_H)
+#define PS_DLIST_H
+/*
+ * Support for doubly linked lists
+ */
+typedef struct psDlistElem {
+   struct psDlistElem *prev;		// previous link in list
+   struct psDlistElem *next;		// next link in list
+   void *data;				// real data item
+} psDlistElem;
+
+typedef struct {
+   int n;				// number of elements on list
+   psDlistElem *head;			// first element on list (may be NULL)
+   psDlistElem *tail;			// last element on list (may be NULL)
+   psDlistElem *iter;			// iteration cursor
+} psDlist;
+
+enum {					// Special values of index into list
+   PS_DLIST_HEAD = 0,			// at head
+   PS_DLIST_TAIL = -1,			// at tail
+   PS_DLIST_UNKNOWN = -2,		// unknown position
+   PS_DLIST_PREV = -3,			// previous element
+   PS_DLIST_NEXT = -4			// next element
+};
+
+/*****************************************************************************/
+/*
+ * Constructors and Destructors
+ */
+psDlist *psDlistAlloc(void *data);	// initial data item; may be NULL
+
+void psDlistFree(psDlist *list,		// list to destroy
+		void (*elemFree)(void *)); // destructor for data on list
+
+/*
+ * List maintainence functions
+ */
+psDlist *psDlistAdd(psDlist *list,	// list to add to (may be NULL)
+		    void *data,		// data item to add
+		    int where);		// index, PS_DLIST_HEAD, or PS_DLIST_TAIL
+psDlist *psDlistAppend(psDlist *list,	// list to append to (may be NULL)
+		       void *data);	// data item to add
+void *psDlistRemove(psDlist *list,	// list to remove element from
+		    void *data,		// data item to remove
+		    int which);		// index of item, or PS_DLIST_UNKNOWN,
+					// or PS_DLIST_NEXT, or PS_DLIST_PREV
+void *psDlistGet(const psDlist *list,	// list to retrieve element from
+		 int which);		// index of item, or PS_DLIST_NEXT,
+					// or PS_DLIST_PREV
+/*
+ * convenience functions to use psDlistGet as an iterator
+ */
+void psDlistSetIterator(psDlist *list, int where, int which);
+void *psDlistGetNext(psDlist *list, int which);
+void *psDlistGetPrev(psDlist *list, int which);
+/*
+ * Conversions to/from arrays
+ */
+psVoidPtrArray *psDlistToArray(psDlist *dlist);
+psDlist *psArrayToDlist(psVoidPtrArray *arr);
+#endif
Index: /trunk/archive/pslib/include/psHash.h
===================================================================
--- /trunk/archive/pslib/include/psHash.h	(revision 181)
+++ /trunk/archive/pslib/include/psHash.h	(revision 181)
@@ -0,0 +1,21 @@
+#if !defined(PS_HASH_H)
+#define PS_HASH_H
+
+typedef struct HashTable psHash;
+
+psHash *psHashAlloc(int nbucket);	// initial number of buckets
+void psHashFree(psHash *table,		// hash table to be freed
+		void (*itemFree)(void *item)); // how to free hashed data;
+					// or NULL
+
+void *psHashInsert(psHash *table,	// table to insert in
+		   const char *key,	// key to use
+		   void *data,		// data to insert
+		   void (*itemFree)(void *item)); // how to free hashed data;
+					// or NULL
+void *psHashLookup(psHash *table,	// table to lookup key in
+		   const char *key);	// key to lookup
+
+void *psHashRemove(psHash *table,	// table to lookup key in
+		   const char *key);	// key to lookup
+#endif
Index: /trunk/archive/pslib/include/psLogMsg.h
===================================================================
--- /trunk/archive/pslib/include/psLogMsg.h	(revision 181)
+++ /trunk/archive/pslib/include/psLogMsg.h	(revision 181)
@@ -0,0 +1,16 @@
+#if !defined(PS_LOG_MSG_H)
+#define PS_LOG_MSG_H
+#include <stdarg.h>
+
+enum { PS_LOG_ABORT = 0, PS_LOG_ERROR, PS_LOG_WARN, PS_LOG_INFO };
+
+enum { PS_LOG_TO_STDERR, PS_LOG_TO_STDOUT };
+
+int psSetLogDestination(int dest);
+int psSetLogLevel(int level);
+void psSetLogFormat(const char *fmt);
+
+void psLogMsg(const char *name, int level, const char *fmt, ...);
+void p_psVLogMsg(const char *name, int level, const char *fmt, va_list ap);
+
+#endif
Index: /trunk/archive/pslib/include/psMemory.h
===================================================================
--- /trunk/archive/pslib/include/psMemory.h	(revision 181)
+++ /trunk/archive/pslib/include/psMemory.h	(revision 181)
@@ -0,0 +1,71 @@
+#if !defined(PS_MEMORY_H)
+#define PS_MEMORY_H
+#include <stdio.h>
+/*
+ * Book-keeping data for storage allocator
+ *
+ * N.b. sizeof(psMemBlock) must be chosen such that if ptr is a pointer
+ * returned by malloc, then ((char *)ptr + sizeof(psMemBlock)) is properly
+ * aligned for all storage types.
+ */
+typedef struct {
+    const void *magic0;			// initialised to p_psMEMMAGIC
+    const unsigned long id;		// a unique ID for this allocation
+    const char *file;			// set from __FILE__ in e.g. p_psAlloc
+    const int lineno;			// set from __LINE__ in e.g. p_psAlloc
+    int refCounter;			// how many times pointer is referenced
+    const void *magic;			// initialised to p_psMEMMAGIC
+} psMemBlock;
+
+typedef int (*psMemCallback)(const psMemBlock *ptr);
+typedef void (*psMemProblemCallback)(const psMemBlock *ptr,
+				     const char *file, int lineno);
+typedef void *(*psMemExhaustedCallback)(size_t size);
+
+/*****************************************************************************/
+
+void *psAlloc(size_t size);
+void *psRealloc(void *ptr, size_t size);
+void psFree(void *ptr);
+
+void *p_psAlloc(size_t size, const char *file, int lineno);
+void *p_psRealloc(void *ptr, size_t size, const char *file, int lineno);
+void p_psFree(void *ptr, const char *file, int lineno);
+
+#define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__)
+#define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__)
+#define psFree(size) p_psFree(size, __FILE__, __LINE__)
+
+/*****************************************************************************/
+/*
+ * Checks of memory system 
+ */
+int psMemCheckLeaks(
+    int id0,				// don't list blocks with id < id0
+    psMemBlock ***arr,			// pointer to array of pointers to
+					// leaked blocks, or NULL
+    FILE *fd);				// print list of leaks to fd (or NULL)
+int psMemCheckCorruption(int abort_on_error);
+
+/*****************************************************************************/
+/*
+ * Manipulate reference counter
+ */
+int psMemGetRefCounter(void *vptr);	// return refCounter
+void *psMemIncrRefCounter(void *vptr);	// increment refCounter and return vptr
+void *psMemDecrRefCounter(void *vptr);	// decrement refCounter and return vptr
+
+/*****************************************************************************/
+/*
+ * Functions to set and control callbacks
+ */
+psMemProblemCallback psMemProblemSetCB(psMemProblemCallback func);
+psMemExhaustedCallback psMemExhaustedSetCB(psMemExhaustedCallback func);
+
+psMemCallback psMemAllocateSetCB(psMemCallback func);
+psMemCallback psMemFreeSetCB(psMemCallback func);
+
+int psMemGetId(void);			// get next memory ID
+long psMemSetAllocateID(long id);	// set p_psMemAllocateID to id
+long psMemSetFreeID(long id);		// set p_psMemFreeID to id
+#endif
Index: /trunk/archive/pslib/include/psMisc.h
===================================================================
--- /trunk/archive/pslib/include/psMisc.h	(revision 181)
+++ /trunk/archive/pslib/include/psMisc.h	(revision 181)
@@ -0,0 +1,19 @@
+#if !defined(PS_MISC_H)
+#define PS_MISC_H
+/*
+ * Concatenate two macro arguments
+ */
+#define P_PS_CONCAT(A, B) A ## B	// Expands to AB
+#define PS_CONCAT(A, B) A ## B		// Also Expands to AB
+#define PS_CONCAT2(A, B) PS_CONCAT(A, B) // Also expands to AB
+#define PS_CONCAT3(A, B, C) A ## B ## C // Expands to ABC
+#define PS_CONCAT4(A, B, C, D) A ## B ## C ## D // Expands to ABCD
+
+#define PS_STRING(S) #S			// converts argument to string
+
+void psAbort(const char *name, const char *fmt, ...);
+void psError(const char *name, const char *fmt, ...);
+
+char *psStringCopy(const char *str);
+
+#endif
Index: /trunk/archive/pslib/include/psTrace.h
===================================================================
--- /trunk/archive/pslib/include/psTrace.h	(revision 181)
+++ /trunk/archive/pslib/include/psTrace.h	(revision 181)
@@ -0,0 +1,22 @@
+#if !defined(PS_TRACE_H)
+#define PS_TRACE_H 1
+
+//#define PS_NTRACE 1			/* to turn off all tracing */
+#if defined(PS_NTRACE)
+#  define psTrace(facil, level, ...) /* do nothing */
+#else
+#  define psTrace(facil, level, ...) \
+          p_psTrace(facil, level, __VA_ARGS__)
+#endif
+
+void p_psTrace(const char *facil, int level, ...);
+
+int psSetTraceLevel(const char *facil,	// facilty of interest
+		    int level);		// desired trace level
+int psGetTraceLevel(const char *name);	// facilty of interest
+
+void psTraceReset(void);		// turn off all tracing, and free trace's allocated memory
+
+void psPrintTraceLevels(void);		// print trace levels
+
+#endif
Index: /trunk/archive/pslib/include/psUtils.h
===================================================================
--- /trunk/archive/pslib/include/psUtils.h	(revision 181)
+++ /trunk/archive/pslib/include/psUtils.h	(revision 181)
@@ -0,0 +1,13 @@
+#if !defined(PS_UTILS)
+#define PS_UTILS
+
+#include "psArray.h"
+#include "psDlist.h"
+#include "psHash.h"
+#include "psLogMsg.h"
+#include "psMemory.h"
+#include "psTrace.h"
+
+#include "psMisc.h"			// must go last
+
+#endif
