[PATCH] malloc: destroy thread cache on thread exit
Joern Engel
joern@purestorage.com
Tue Jan 26 00:27:00 GMT 2016
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
More information about the Libc-alpha
mailing list