| 1 | /* |
| 2 | Copyright (c) 2005-2019 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "TypeDefinitions.h" // Customize.h and proxy.h get included |
| 18 | #include "tbbmalloc_internal_api.h" |
| 19 | |
| 20 | #include "../tbb/tbb_assert_impl.h" // Out-of-line TBB assertion handling routines are instantiated here. |
| 21 | |
| 22 | #undef UNICODE |
| 23 | |
| 24 | #if USE_PTHREAD |
| 25 | #include <dlfcn.h> // dlopen |
| 26 | #elif USE_WINTHREAD |
| 27 | #include "tbb/machine/windows_api.h" |
| 28 | #endif |
| 29 | |
| 30 | namespace rml { |
| 31 | namespace internal { |
| 32 | |
| 33 | #if TBB_USE_DEBUG |
| 34 | #define DEBUG_SUFFIX "_debug" |
| 35 | #else |
| 36 | #define DEBUG_SUFFIX |
| 37 | #endif /* TBB_USE_DEBUG */ |
| 38 | |
| 39 | // MALLOCLIB_NAME is the name of the TBB memory allocator library. |
| 40 | #if _WIN32||_WIN64 |
| 41 | #define MALLOCLIB_NAME "tbbmalloc" DEBUG_SUFFIX ".dll" |
| 42 | #elif __APPLE__ |
| 43 | #define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".dylib" |
| 44 | #elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __ANDROID__ |
| 45 | #define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".so" |
| 46 | #elif __linux__ |
| 47 | #define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX __TBB_STRING(.so.TBB_COMPATIBLE_INTERFACE_VERSION) |
| 48 | #else |
| 49 | #error Unknown OS |
| 50 | #endif |
| 51 | |
| 52 | void init_tbbmalloc() { |
| 53 | #if DO_ITT_NOTIFY |
| 54 | MallocInitializeITT(); |
| 55 | #endif |
| 56 | |
| 57 | /* Preventing TBB allocator library from unloading to prevent |
| 58 | resource leak, as memory is not released on the library unload. |
| 59 | */ |
| 60 | #if USE_WINTHREAD && !__TBB_SOURCE_DIRECTLY_INCLUDED && !__TBB_WIN8UI_SUPPORT |
| 61 | // Prevent Windows from displaying message boxes if it fails to load library |
| 62 | UINT prev_mode = SetErrorMode (SEM_FAILCRITICALERRORS); |
| 63 | HMODULE lib; |
| 64 | BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
| 65 | |GET_MODULE_HANDLE_EX_FLAG_PIN, |
| 66 | (LPCTSTR)&scalable_malloc, &lib); |
| 67 | MALLOC_ASSERT(lib && ret, "Allocator can't find itself." ); |
| 68 | SetErrorMode (prev_mode); |
| 69 | #endif /* USE_PTHREAD && !__TBB_SOURCE_DIRECTLY_INCLUDED */ |
| 70 | } |
| 71 | |
| 72 | #if !__TBB_SOURCE_DIRECTLY_INCLUDED |
| 73 | #if USE_WINTHREAD |
| 74 | extern "C" BOOL WINAPI DllMain( HINSTANCE /*hInst*/, DWORD callReason, LPVOID lpvReserved) |
| 75 | { |
| 76 | if (callReason==DLL_THREAD_DETACH) |
| 77 | { |
| 78 | __TBB_mallocThreadShutdownNotification(); |
| 79 | } |
| 80 | else if (callReason==DLL_PROCESS_DETACH) |
| 81 | { |
| 82 | __TBB_mallocProcessShutdownNotification(lpvReserved != NULL); |
| 83 | } |
| 84 | return TRUE; |
| 85 | } |
| 86 | #else /* !USE_WINTHREAD */ |
| 87 | struct RegisterProcessShutdownNotification { |
| 88 | // Work around non-reentrancy in dlopen() on Android |
| 89 | #if !__TBB_USE_DLOPEN_REENTRANCY_WORKAROUND |
| 90 | RegisterProcessShutdownNotification() { |
| 91 | // prevents unloading, POSIX case |
| 92 | dlopen(MALLOCLIB_NAME, RTLD_NOW); |
| 93 | } |
| 94 | #endif /* !__TBB_USE_DLOPEN_REENTRANCY_WORKAROUND */ |
| 95 | ~RegisterProcessShutdownNotification() { |
| 96 | __TBB_mallocProcessShutdownNotification(false); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | static RegisterProcessShutdownNotification reg; |
| 101 | #endif /* !USE_WINTHREAD */ |
| 102 | #endif /* !__TBB_SOURCE_DIRECTLY_INCLUDED */ |
| 103 | |
| 104 | } } // namespaces |
| 105 | |
| 106 | #if __TBB_ipf |
| 107 | /* It was found that on IA-64 architecture inlining of __TBB_machine_lockbyte leads |
| 108 | to serious performance regression with ICC. So keep it out-of-line. |
| 109 | |
| 110 | This code is copy-pasted from tbb_misc.cpp. |
| 111 | */ |
| 112 | extern "C" intptr_t __TBB_machine_lockbyte( volatile unsigned char& flag ) { |
| 113 | tbb::internal::atomic_backoff backoff; |
| 114 | while( !__TBB_TryLockByte(flag) ) backoff.pause(); |
| 115 | return 0; |
| 116 | } |
| 117 | #endif |
| 118 | |