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