[PATCH] Add a test for the TSD system

Mike Hommey mh+jemalloc at glandium.org
Tue Apr 17 10:46:26 PDT 2012


From: Mike Hommey <mh at glandium.org>

---

Note that this test doesn't compile without --enable-debug, because of
arena_malloc. Any ideas how to best fix this?

 Makefile.in  |    3 ++-
 test/tsd.c   |   76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/tsd.exp |    9 +++++++
 3 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100644 test/tsd.c
 create mode 100644 test/tsd.exp

diff --git a/Makefile.in b/Makefile.in
index 8a34928..24f07ab 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -89,7 +89,7 @@ DOCS := $(DOCS_HTML) $(DOCS_MAN3)
 CTESTS := $(srcroot)test/aligned_alloc.c $(srcroot)test/allocated.c \
 	$(srcroot)test/bitmap.c $(srcroot)test/mremap.c \
 	$(srcroot)test/posix_memalign.c $(srcroot)test/thread_arena.c \
-	$(srcroot)test/thread_tcache_enabled.c
+	$(srcroot)test/thread_tcache_enabled.c $(srcroot)test/tsd.c
 ifeq ($(enable_experimental), 1)
 CTESTS += $(srcroot)test/allocm.c $(srcroot)test/rallocm.c
 endif
@@ -156,6 +156,7 @@ $(STATIC_LIBS):
 	ar crus $@ $+
 
 $(objroot)test/bitmap$(EXE): $(objroot)src/bitmap.$(O)
+$(objroot)test/tsd$(EXE): $(objroot)src/tsd.$(O)
 
 $(objroot)test/%$(EXE): $(objroot)test/%.$(O) $(objroot)src/util.$(O) $(DSOS)
 	@mkdir -p $(@D)
diff --git a/test/tsd.c b/test/tsd.c
new file mode 100644
index 0000000..c812f5c
--- /dev/null
+++ b/test/tsd.c
@@ -0,0 +1,76 @@
+#define	JEMALLOC_MANGLE
+#include "jemalloc_test.h"
+
+/* Stubs to avoid pulling the entire jemalloc internals */
+arena_t *_arenas[1] = { NULL };
+arena_t **arenas = _arenas;
+bool    opt_abort = true;
+
+void *
+arena_malloc(arena_t *arena, size_t size, bool zero, bool try_tcache)
+{
+	static unsigned char buf[4096];
+	static unsigned char *next_ptr = buf;
+	unsigned char *ret = next_ptr;
+
+	next_ptr += size;
+	return next_ptr;
+}
+
+void
+idalloc(void *ptr)
+{
+}
+
+#define THREAD_DATA 0x72b65c10
+
+typedef unsigned int data_t;
+
+void
+data_cleanup(void *arg)
+{
+	data_t *data = (data_t *)arg;
+
+	malloc_printf("Cleanup for data %x.\n", *data);
+}
+
+malloc_tsd_protos(, data, data_t)
+malloc_tsd_externs(data, data_t)
+#define DATA_INIT 0x12345678
+malloc_tsd_data(, data, data_t, DATA_INIT)
+malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
+
+void *
+je_thread_start(void *arg)
+{
+	data_t d = (data_t)(uintptr_t) arg;
+	malloc_printf("Initial tsd_get returns %x. Expected %x.\n",
+		*data_tsd_get(), DATA_INIT);
+
+	data_tsd_set(&d);
+	malloc_printf("After tsd_set: %x. Expected %x.\n",
+		*data_tsd_get(), d);
+
+	d = 0;
+	malloc_printf("After resetting local data: %x. Expected %x.\n",
+		*data_tsd_get(), (data_t)(uintptr_t) arg);
+
+	return NULL;
+}
+
+int
+main(void)
+{
+	je_thread_t thread;
+
+	malloc_printf("Test begin\n");
+
+	data_tsd_boot();
+	je_thread_start((void *) 0xa5f3e329);
+
+	je_thread_create(&thread, je_thread_start, (void *) THREAD_DATA);
+	je_thread_join(thread, NULL);
+
+	malloc_printf("Test end\n");
+	return (0);
+}
diff --git a/test/tsd.exp b/test/tsd.exp
new file mode 100644
index 0000000..b4abedc
--- /dev/null
+++ b/test/tsd.exp
@@ -0,0 +1,9 @@
+Test begin
+Initial tsd_get returns 12345678. Expected 12345678.
+After tsd_set: a5f3e329. Expected a5f3e329.
+After resetting local data: a5f3e329. Expected a5f3e329.
+Initial tsd_get returns 12345678. Expected 12345678.
+After tsd_set: 72b65c10. Expected 72b65c10.
+After resetting local data: 72b65c10. Expected 72b65c10.
+Cleanup for data 72b65c10.
+Test end
-- 
1.7.10




More information about the jemalloc-discuss mailing list