[xiph-commits] r13428 - in experimental/moritz: thread xalloc

moritz at svn.xiph.org moritz at svn.xiph.org
Thu Aug 2 10:59:16 PDT 2007


Author: moritz
Date: 2007-08-02 10:59:16 -0700 (Thu, 02 Aug 2007)
New Revision: 13428

Modified:
   experimental/moritz/thread/Makefile
   experimental/moritz/thread/main.c
   experimental/moritz/thread/thread.c
   experimental/moritz/thread/thread.h
   experimental/moritz/xalloc/Makefile
   experimental/moritz/xalloc/main.c
   experimental/moritz/xalloc/xalloc.c
   experimental/moritz/xalloc/xalloc.h
Log:
Do not let *_DEBUG influence the library ABIs. Unfortunately, I do not see
a way to do this with xasprintf() -- at least xalloc+xasprintf links nicely
with app+no_xasprintf.


Modified: experimental/moritz/thread/Makefile
===================================================================
--- experimental/moritz/thread/Makefile	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/thread/Makefile	2007-08-02 17:59:16 UTC (rev 13428)
@@ -10,7 +10,10 @@
 THREADS =	-pthread
 CC ?=		gcc
 CFLAGS ?=	-O2 -pipe
-CFLAGS +=	-DHAVE_CONFIG_H=1 -DTHREAD_DEBUG=1 -DXALLOC_DEBUG=1 -DXALLOC_WITH_XASPRINTF=1 -fstrict-aliasing -Wall -W -ansi -pedantic -Wwrite-strings -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes $(THREADS)
+CFLAGS +=	-DHAVE_CONFIG_H=1 -fstrict-aliasing -Wall -W -ansi -pedantic -Wwrite-strings -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes $(THREADS)
+#CFLAGS +=	-DTHREAD_DEBUG=1
+CFLAGS +=	-DTHREAD_CHECK_MUTEXES=1
+CFLAGS +=	-DXALLOC_WITH_XASPRINTF=1
 DEBUG ?=	-g -ggdb
 INCLUDEFLAGS =	$(COMPAT_INCLUDES) -I../xalloc -pthread
 LDFLAGS =	-L. -L../xalloc -lxalloc -lthread $(THREADS)

Modified: experimental/moritz/thread/main.c
===================================================================
--- experimental/moritz/thread/main.c	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/thread/main.c	2007-08-02 17:59:16 UTC (rev 13428)
@@ -23,14 +23,12 @@
 main(void)
 {
 	thread_t thread;
-	char *foo, *bar, *baz, *bla;
+	char *foo, *bar, *baz;
+#ifdef XALLOC_WITH_XASPRINTF
+	char *bla;
+#endif
 
-#ifdef XALLOC_DEBUG
 	xalloc_initialize_debug(1, stdout);
-#else
-	xalloc_initialize();
-#endif
-
 #ifdef THREAD_DEBUG
 	thread_initialize_debug(3, stdout);
 #else
@@ -46,7 +44,9 @@
 	baz = xmalloc(80);
 	baz = xrealloc(baz, 200, sizeof(char));
 	baz = xrealloc(baz, 100, sizeof(char));
+#ifdef XALLOC_WITH_XASPRINTF
 	xasprintf(&bla, "Blabla? Blah-%d!", 1000);
+#endif
 	xfree(foo);
 	xfree(baz);
 	xfree(baz);

Modified: experimental/moritz/thread/thread.c
===================================================================
--- experimental/moritz/thread/thread.c	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/thread/thread.c	2007-08-02 17:59:16 UTC (rev 13428)
@@ -59,13 +59,9 @@
 #define MUTEX_NEVERLOCKED	-1
 
 #define THREAD_DBGLVL_MAX	 4
-#ifndef THREAD_DEBUG
-void	thread_initialize_debug(unsigned int, FILE *);
-#else
-# ifdef THREAD_DIE_SILENT
-#  undef THREAD_DIE_SILENT
-# endif /* THREAD_DIE_SILENT */
-#endif /* !THREAD_DEBUG */
+#if defined(THREAD_DEBUG) && defined(THREAD_DIE_SILENT)
+# undef THREAD_DIE_SILENT
+#endif /* THREAD_DEBUG && THREAD_DIE_SILENT */
 
 void	_thread_fatal(int, const char *, ...);
 void	_thread_debug_printf(unsigned int, const char *, ...);
@@ -185,8 +181,7 @@
 					       fprintf(debug_output, ": %s\n",
 						       strerror(errnum));
 				       fflush(debug_output);
-			       }
-			      );
+			       });
 	else {
 		va_start(ap, fmt);
 		vfprintf(debug_output, fmt, ap);
@@ -202,28 +197,25 @@
 void
 _thread_debug_printf(unsigned int level, const char *fmt, ...)
 {
-#ifdef THREAD_DEBUG
-	va_list 		ap;
+	va_list ap;
 
 	if (level > debug_level)
 		return;
 
+	va_start(ap, fmt);
+#ifdef THREAD_DEBUG
 	if (thread_initialized)
 		THREAD_PROTECT(
 			       {
-				       va_start(ap, fmt);
 				       vfprintf(debug_output, fmt, ap);
-				       va_end(ap);
 				       fflush(debug_output);
-			       }
-			      );
+			       });
 	else {
-		va_start(ap, fmt);
 		vfprintf(debug_output, fmt, ap);
-		va_end(ap);
 		fflush(debug_output);
 	}
 #endif /* THREAD_DEBUG */
+	va_end(ap);
 }
 
 void
@@ -476,6 +468,11 @@
 void
 thread_initialize_debug(unsigned int level, FILE *output)
 {
+	struct thread	*thread;
+
+	if (thread_initialized)
+		_thread_fatal(0, "THREAD: Internal error: thread_initialize(): Thread library already initialized\n");
+
 	if ((debug_level = level) > THREAD_DBGLVL_MAX)
 		debug_level = THREAD_DBGLVL_MAX;
 	if (output == NULL)
@@ -483,20 +480,6 @@
 	else
 		debug_output = output;
 
-	thread_initialize();
-}
-
-void
-thread_initialize(void)
-{
-	struct thread	*thread;
-
-	if (thread_initialized)
-		_thread_fatal(0, "THREAD: Internal error: thread_init(): Thread library already initialized\n");
-
-	if (debug_output == NULL)
-		debug_output = THREAD_DEFAULT_OUTPUT;
-
 	_init_mutexes();
 
 	_thread_debug_printf(3, "======> INIT thread\n");

Modified: experimental/moritz/thread/thread.h
===================================================================
--- experimental/moritz/thread/thread.h	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/thread/thread.h	2007-08-02 17:59:16 UTC (rev 13428)
@@ -61,12 +61,10 @@
  * Library initialization and shutdown.
  */
 
-void		thread_initialize(void);
-#ifdef THREAD_DEBUG
-/* Output stream defaults to standard error, if NULL. */
+#define thread_initialize()					\
+	thread_initialize_debug(0, NULL)
 void		thread_initialize_debug(unsigned int /* level */,
 					FILE * /* output stream */);
-#endif /* THREAD_DEBUG */
 void		thread_shutdown(void);
 
 

Modified: experimental/moritz/xalloc/Makefile
===================================================================
--- experimental/moritz/xalloc/Makefile	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/xalloc/Makefile	2007-08-02 17:59:16 UTC (rev 13428)
@@ -10,7 +10,9 @@
 THREADS =	-pthread
 CC ?=		gcc
 CFLAGS ?=	-O2 -pipe
-CFLAGS +=	-DHAVE_CONFIG_H=1 -DXALLOC_DEBUG=1 -DXALLOC_WITH_XASPRINTF=1 -fstrict-aliasing -Wall -W -ansi -pedantic -Wwrite-strings -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes $(THREADS)
+CFLAGS +=	-DHAVE_CONFIG_H=1 -fstrict-aliasing -Wall -W -ansi -pedantic -Wwrite-strings -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes $(THREADS)
+#CFLAGS +=	-DXALLOC_DEBUG=1
+CFLAGS +=	-DXALLOC_WITH_XASPRINTF=1
 DEBUG ?=	-g -ggdb
 INCLUDEFLAGS =	$(COMPAT_INCLUDES)
 LDFLAGS =	-L. -lxalloc $(THREADS)

Modified: experimental/moritz/xalloc/main.c
===================================================================
--- experimental/moritz/xalloc/main.c	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/xalloc/main.c	2007-08-02 17:59:16 UTC (rev 13428)
@@ -13,7 +13,10 @@
 int
 main(void)
 {
-	char *foo, *bar, *baz, *bla;
+	char *foo, *bar, *baz;
+#ifdef XALLOC_WITH_XASPRINTF
+	char *bla;
+#endif
 
 #ifdef XALLOC_DEBUG
 	xalloc_initialize_debug(2, NULL);
@@ -25,7 +28,9 @@
 	bar = xcalloc(12, sizeof(char));
 	baz = xmalloc(80);
 	baz = xrealloc(baz, 100, sizeof(char));
+#ifdef XALLOC_WITH_XASPRINTF
 	xasprintf(&bla, "Blabla? Blah-%d!", 1000);
+#endif
 	xfree(foo);
 	xfree(baz);
 

Modified: experimental/moritz/xalloc/xalloc.c
===================================================================
--- experimental/moritz/xalloc/xalloc.c	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/xalloc/xalloc.c	2007-08-02 17:59:16 UTC (rev 13428)
@@ -83,11 +83,9 @@
 RB_PROTOTYPE(memory_tree, memory, entry, _memory_cmp)
 
 void	_memory_free(struct memory **);
-void	_xalloc_warn(const char *, ...);
-#else
-void	xalloc_initialize_debug(unsigned int, FILE *);
 #endif /* XALLOC_DEBUG */
 
+void	_xalloc_warn(const char *, ...);
 void	_xalloc_error(int, const char *, ...);
 void	_xalloc_fatal(const char *, ...);
 void	_xalloc_debug_printf(unsigned int, const char *, ...);
@@ -286,7 +284,7 @@
 #endif /* XALLOC_WITH_XASPRINTF */
 
 void
-xalloc_initialize(void)
+xalloc_initialize_debug(unsigned int level, FILE *output)
 {
 #ifdef THREAD_SAFE
 	int	err;
@@ -295,6 +293,13 @@
 	if (xalloc_initialized)
 		_xalloc_fatal("XALLOC: xalloc_initialize(): Xalloc library already initialized\n");
 
+	if ((debug_level = level) > XALLOC_DBGLVL_MAX)
+		debug_level = XALLOC_DBGLVL_MAX;
+	if (output == NULL)
+		debug_output = XALLOC_DEFAULT_OUTPUT;
+	else
+		debug_output = output;
+
 	real_malloc = malloc;
 	real_calloc = calloc;
 	real_realloc = realloc;
@@ -314,22 +319,6 @@
 }
 
 void
-xalloc_initialize_debug(unsigned int level, FILE *output)
-{
-	if (xalloc_initialized)
-		_xalloc_fatal("XALLOC: xalloc_initialize_debug(): Xalloc library already initialized\n");
-
-	if ((debug_level = level) > XALLOC_DBGLVL_MAX)
-		debug_level = XALLOC_DBGLVL_MAX;
-	if (output == NULL)
-		debug_output = XALLOC_DEFAULT_OUTPUT;
-	else
-		debug_output = output;
-
-	xalloc_initialize();
-}
-
-void
 xalloc_set_functions(void *(*malloc_func)(size_t),
 		     void *(*calloc_func)(size_t, size_t),
 		     void *(*realloc_func)(void *, size_t))
@@ -611,7 +600,6 @@
 	return (nstr);
 }
 
-#ifdef XALLOC_DEBUG
 void
 xfree_c(void **ptr_p, const char *file, unsigned int line)
 {
@@ -627,6 +615,7 @@
 		return;
 	}
 
+#ifdef XALLOC_DEBUG
 	if (debug_level > 0) {
 		struct memory	*mem = NULL, find_mem;
 
@@ -667,12 +656,16 @@
 		}
 		XALLOC_UNLOCK(xalloc_mutex);
 	}
+#endif /* XALLOC_DEBUG */
 
 	free(*ptr_p);
+#ifdef XALLOC_DEBUG
 	if (debug_level <= 1)
+#endif /* XALLOC_DEBUG */
+	{
 		*ptr_p = NULL;
+	}
 }
-#endif /* XALLOC_DEBUG */
 
 #ifdef XALLOC_WITH_XASPRINTF
 int
@@ -684,10 +677,10 @@
 	size_t	strsiz = 0;
 
 	if (!xalloc_initialized)
-		_xalloc_fatal("XALLOC: xfree(): Xalloc library not initialized\n");
+		_xalloc_fatal("XALLOC: xasprintf(): Xalloc library not initialized\n");
 
 	if (str_p == NULL || fmt == NULL)
-		_xalloc_fatal("XALLOC: xfree(): Bad argument(s)\n");
+		_xalloc_fatal("XALLOC: xasprintf(): Bad argument(s)\n");
 
 	va_start(ap, fmt);
 	ret = _xalloc_vasprintf(str_p, fmt, ap, &strsiz);
@@ -696,7 +689,7 @@
 		_xalloc_error(errno, "XALLOC: xasprintf(): %s:%u: Allocating %lu bytes",
 			      file ? file : unknown_file, line, strsiz);
 
-#ifdef XALLOC_DEBUG
+# ifdef XALLOC_DEBUG
 	if (debug_level > 0) {
 		struct memory	*mem;
 
@@ -717,7 +710,7 @@
 			xalloc_peak = xalloc_allocated;
 		XALLOC_UNLOCK(xalloc_mutex);
 	}
-#endif /* XALLOC_DEBUG */
+# endif /* XALLOC_DEBUG */
 
 	return (ret);
 }

Modified: experimental/moritz/xalloc/xalloc.h
===================================================================
--- experimental/moritz/xalloc/xalloc.h	2007-08-02 16:43:07 UTC (rev 13427)
+++ experimental/moritz/xalloc/xalloc.h	2007-08-02 17:59:16 UTC (rev 13428)
@@ -25,8 +25,9 @@
 #define __XALLOC_H__
 
 /*
- * Define XALLOC_DEBUG to compile the debugging features and expose the
- * xalloc_initialize_debug() interface. The debugging levels are
+ * Define XALLOC_DEBUG to compile the debugging features. Doing so will make
+ * this library more expensive in every case, but not change its (visible)
+ * behavior unless the debugging level is set > 0. The debugging levels are:
  *   0: disable debugging
  *   1: enable most debugging features
  *   2: additionally enable double-free checking
@@ -41,7 +42,7 @@
  * Define XALLOC_WITH_XASPRINTF to expose the xasprintf() interface. Doing
  * so will require libxalloc to be compiled with a compiler that supports C99
  * variadic macros, and work only on systems with vasprintf() or vsnprintf(),
- * and MS Windows.
+ * and MS Windows. Note that doing so constitutes an incompatible ABI change!
  *
  * Note that none of the x*_c() functions should be used directly, unless it
  * is ensured that /file/ is a const char * to a real string constant.
@@ -62,17 +63,16 @@
  * Library initialization and shutdown.
  */
 
-void	xalloc_initialize(void);
-
-#ifdef XALLOC_DEBUG
+#define xalloc_initialize()						\
+	xalloc_initialize_debug(0, NULL)
 void	xalloc_initialize_debug(unsigned int /* level */,
 				FILE * /* output stream */);
-#endif /* XALLOC_DEBUG */
 
 void	xalloc_set_functions(void *(*)(size_t) /* malloc function */,
 			     void *(*)(size_t, size_t) /* calloc function */,
 			     void *(*)(void *, size_t) /* realloc function */);
 
+/* Memory leak checks happen during shutdown! */
 void	xalloc_shutdown(void);
 
 
@@ -102,25 +102,11 @@
 char *	xstrdup_c(const char *,
 		  const char * /* file */, unsigned int /* line */);
 
-#ifdef XALLOC_DEBUG
-# define xfree(p)							\
+#define xfree(p)							\
 	xfree_c((void *)&(p), __FILE__, __LINE__)
 void	xfree_c(void **,
 		const char * /* file */, unsigned int /* line */);
-#else /* XALLOC_DEBUG */
-void	_xalloc_warn(const char *, ...);
 
-# define xfree(p)							\
-do {									\
-	if ((p) == NULL) {						\
-		_xalloc_warn("XALLOC: xfree(): Warning: Freeing NULL pointer\n"); \
-		break;							\
-	}								\
-	free(p);							\
-	(p) = NULL;							\
-} while (0)
-#endif /* XALLOC_DEBUG */
-
 #ifdef XALLOC_WITH_XASPRINTF
 # define xasprintf(s, f, ...)						\
 	xasprintf_c(__FILE__, __LINE__, s, f, __VA_ARGS__)



More information about the commits mailing list