Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1 | #define JEMALLOC_TSD_C_ |
| 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| 3 | |
| 4 | /******************************************************************************/ |
| 5 | /* Data. */ |
| 6 | |
| 7 | static unsigned ncleanups; |
| 8 | static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX]; |
| 9 | |
| 10 | /******************************************************************************/ |
| 11 | |
| 12 | void * |
| 13 | malloc_tsd_malloc(size_t size) |
| 14 | { |
| 15 | |
| 16 | /* Avoid choose_arena() in order to dodge bootstrapping issues. */ |
Jason Evans | fad100b | 2012-04-06 12:24:46 -0700 | [diff] [blame] | 17 | return arena_malloc(arenas[0], size, false, false); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | void |
| 21 | malloc_tsd_dalloc(void *wrapper) |
| 22 | { |
| 23 | |
| 24 | idalloc(wrapper); |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | malloc_tsd_no_cleanup(void *arg) |
| 29 | { |
| 30 | |
| 31 | not_reached(); |
| 32 | } |
| 33 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 34 | #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32) |
Jason Evans | 86e5858 | 2012-04-18 19:01:00 -0700 | [diff] [blame] | 35 | JEMALLOC_ATTR(visibility("default")) |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 36 | void |
| 37 | _malloc_thread_cleanup(void) |
| 38 | { |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame^] | 39 | bool pending[MALLOC_TSD_CLEANUPS_MAX], again; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 40 | 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 Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 49 | pending[i] = cleanups[i](); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 50 | if (pending[i]) |
| 51 | again = true; |
| 52 | } |
| 53 | } |
| 54 | } while (again); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | void |
Mike Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 59 | malloc_tsd_cleanup_register(bool (*f)(void)) |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 60 | { |
| 61 | |
| 62 | assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX); |
Mike Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 63 | cleanups[ncleanups] = f; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 64 | ncleanups++; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | malloc_tsd_boot(void) |
| 69 | { |
| 70 | |
| 71 | ncleanups = 0; |
| 72 | } |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 73 | |
| 74 | #ifdef _WIN32 |
| 75 | static 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 | |
| 94 | JEMALLOC_ATTR(section(".CRT$XLY")) JEMALLOC_ATTR(used) |
| 95 | static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL, |
| 96 | DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; |
| 97 | #endif |