blob: d7714b023b924443641a6e8386842cc97ee4fc3b [file] [log] [blame]
Jason Evanscd9a1342012-03-21 18:33:03 -07001#define JEMALLOC_TSD_C_
2#include "jemalloc/internal/jemalloc_internal.h"
3
4/******************************************************************************/
5/* Data. */
6
7static unsigned ncleanups;
8static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];
9
10/******************************************************************************/
11
12void *
13malloc_tsd_malloc(size_t size)
14{
15
16 /* Avoid choose_arena() in order to dodge bootstrapping issues. */
Jason Evansfad100b2012-04-06 12:24:46 -070017 return arena_malloc(arenas[0], size, false, false);
Jason Evanscd9a1342012-03-21 18:33:03 -070018}
19
20void
21malloc_tsd_dalloc(void *wrapper)
22{
23
24 idalloc(wrapper);
25}
26
27void
28malloc_tsd_no_cleanup(void *arg)
29{
30
31 not_reached();
32}
33
Mike Hommeya19e87f2012-04-21 21:27:46 -070034#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
Jason Evans86e58582012-04-18 19:01:00 -070035JEMALLOC_ATTR(visibility("default"))
Jason Evanscd9a1342012-03-21 18:33:03 -070036void
37_malloc_thread_cleanup(void)
38{
Mike Hommey8b499712012-04-24 23:22:02 +020039 bool pending[MALLOC_TSD_CLEANUPS_MAX], again;
Jason Evanscd9a1342012-03-21 18:33:03 -070040 unsigned i;
41
42 for (i = 0; i < ncleanups; i++)
43 pending[i] = true;
44
45 do {
46 again = false;
47 for (i = 0; i < ncleanups; i++) {
48 if (pending[i]) {
Mike Hommey13067ec2012-04-18 18:29:49 +020049 pending[i] = cleanups[i]();
Jason Evanscd9a1342012-03-21 18:33:03 -070050 if (pending[i])
51 again = true;
52 }
53 }
54 } while (again);
55}
56#endif
57
58void
Mike Hommey13067ec2012-04-18 18:29:49 +020059malloc_tsd_cleanup_register(bool (*f)(void))
Jason Evanscd9a1342012-03-21 18:33:03 -070060{
61
62 assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);
Mike Hommey13067ec2012-04-18 18:29:49 +020063 cleanups[ncleanups] = f;
Jason Evanscd9a1342012-03-21 18:33:03 -070064 ncleanups++;
65}
66
67void
68malloc_tsd_boot(void)
69{
70
71 ncleanups = 0;
72}
Mike Hommeya19e87f2012-04-21 21:27:46 -070073
74#ifdef _WIN32
75static BOOL WINAPI
76_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
77{
78
79 switch (fdwReason) {
80#ifdef JEMALLOC_LAZY_LOCK
81 case DLL_THREAD_ATTACH:
82 isthreaded = true;
83 break;
84#endif
85 case DLL_THREAD_DETACH:
86 _malloc_thread_cleanup();
87 break;
88 default:
89 break;
90 }
91 return (true);
92}
93
94JEMALLOC_ATTR(section(".CRT$XLY")) JEMALLOC_ATTR(used)
95static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL,
96 DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
97#endif