This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] malloc: destroy thread cache on thread exit
- From: Joern Engel <joern at purestorage dot com>
- To: "GNU C. Library" <libc-alpha at sourceware dot org>
- Cc: Siddhesh Poyarekar <siddhesh dot poyarekar at gmail dot com>, Joern Engel <joern at purestorage dot com>
- Date: Mon, 25 Jan 2016 16:25:17 -0800
- Subject: [PATCH] malloc: destroy thread cache on thread exit
- Authentication-results: sourceware.org; auth=none
- References: <1453767942-19369-1-git-send-email-joern at purestorage dot com>
I don't think this matters much - current glibc malloc actually leaks
memory every time a thread exits. But for correctness sake.
JIRA: PURE-27597
---
tpc/malloc2.13/arena.h | 2 +-
tpc/malloc2.13/malloc.c | 1 +
tpc/malloc2.13/tcache.h | 15 +++++++++++++++
3 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/tpc/malloc2.13/arena.h b/tpc/malloc2.13/arena.h
index 599774ea1300..9236a3231f07 100644
--- a/tpc/malloc2.13/arena.h
+++ b/tpc/malloc2.13/arena.h
@@ -363,7 +363,7 @@ static void ptmalloc_init(void)
}
mutex_init(&list_lock);
- tsd_key_create(&cache_key, NULL);
+ tsd_key_create(&cache_key, tcache_destroy);
tsd_key_create(&arena_key, NULL);
tsd_setspecific(arena_key, (Void_t *) & main_arena);
thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2);
diff --git a/tpc/malloc2.13/malloc.c b/tpc/malloc2.13/malloc.c
index 022b9a4ce712..94b7e223ec6f 100644
--- a/tpc/malloc2.13/malloc.c
+++ b/tpc/malloc2.13/malloc.c
@@ -2209,6 +2209,7 @@ static Void_t* sYSMALLOc(INTERNAL_SIZE_T, struct malloc_state *);
static int sYSTRIm(size_t, struct malloc_state *);
static void malloc_consolidate(struct malloc_state *);
static Void_t** iALLOc(struct malloc_state *, size_t, size_t*, int, Void_t**);
+static void tcache_destroy(void *_cache);
/* -------------- Early definitions for debugging hooks ---------------- */
diff --git a/tpc/malloc2.13/tcache.h b/tpc/malloc2.13/tcache.h
index 1d324526194f..628dbc00256a 100644
--- a/tpc/malloc2.13/tcache.h
+++ b/tpc/malloc2.13/tcache.h
@@ -202,6 +202,21 @@ static void tcache_gc(struct thread_cache *cache)
}
}
+static void tcache_destroy(void *_cache)
+{
+ struct thread_cache *cache = _cache;
+
+ /*
+ * tcache_gc almost does what we want. It tries hard to only
+ * free part of the cache, but call it often enough and it
+ * will free everything. Probably slightly slower than having
+ * a specialized copy of the same code, but also much simpler.
+ */
+ while (cache->tc_count)
+ tcache_gc(cache);
+ public_fREe(cache);
+}
+
static void add_to_bin(struct malloc_chunk **bin, struct malloc_chunk *p)
{
struct malloc_chunk *old;
--
2.7.0.rc3