xref: /oneTBB/src/tbbmalloc/Customize.h (revision 155a9af9)
1 /*
2     Copyright (c) 2005-2021 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 #ifndef _TBB_malloc_Customize_H_
18 #define _TBB_malloc_Customize_H_
19 
20 // customizing MALLOC_ASSERT macro
21 #define MALLOC_ASSERT(assertion, message) __TBB_ASSERT(assertion, message)
22 #define MALLOC_ASSERT_EX(assertion, message) __TBB_ASSERT_EX(assertion, message)
23 
24 #ifndef MALLOC_DEBUG
25 #define MALLOC_DEBUG TBB_USE_DEBUG
26 #endif
27 
28 #include "oneapi/tbb/detail/_utils.h"
29 #include "oneapi/tbb/detail/_assert.h"
30 
31 #include "Synchronize.h"
32 
33 #if __TBB_USE_ITT_NOTIFY
34 #include "../tbb/itt_notify.h"
35 #define MALLOC_ITT_SYNC_PREPARE(pointer) ITT_NOTIFY(sync_prepare, (pointer))
36 #define MALLOC_ITT_SYNC_ACQUIRED(pointer) ITT_NOTIFY(sync_acquired, (pointer))
37 #define MALLOC_ITT_SYNC_RELEASING(pointer) ITT_NOTIFY(sync_releasing, (pointer))
38 #define MALLOC_ITT_SYNC_CANCEL(pointer) ITT_NOTIFY(sync_cancel, (pointer))
39 #define MALLOC_ITT_FINI_ITTLIB()        ITT_FINI_ITTLIB()
40 #else
41 #define MALLOC_ITT_SYNC_PREPARE(pointer) ((void)0)
42 #define MALLOC_ITT_SYNC_ACQUIRED(pointer) ((void)0)
43 #define MALLOC_ITT_SYNC_RELEASING(pointer) ((void)0)
44 #define MALLOC_ITT_SYNC_CANCEL(pointer) ((void)0)
45 #define MALLOC_ITT_FINI_ITTLIB()        ((void)0)
46 #endif
47 
48 inline intptr_t BitScanRev(uintptr_t x) {
49     return !x? -1 : tbb::detail::log2(x);
50 }
51 
52 template<typename T>
53 static inline bool isAligned(T* arg, uintptr_t alignment) {
54     return tbb::detail::is_aligned(arg,alignment);
55 }
56 
57 static inline bool isPowerOfTwo(uintptr_t arg) {
58     return tbb::detail::is_power_of_two(arg);
59 }
60 static inline bool isPowerOfTwoAtLeast(uintptr_t arg, uintptr_t power2) {
61     return arg && tbb::detail::is_power_of_two_at_least(arg,power2);
62 }
63 
64 inline void do_yield() {
65     tbb::detail::yield();
66 }
67 
68 #define USE_DEFAULT_MEMORY_MAPPING 1
69 
70 // To support malloc replacement
71 #include "../tbbmalloc_proxy/proxy.h"
72 
73 #if MALLOC_UNIXLIKE_OVERLOAD_ENABLED
74 #define malloc_proxy __TBB_malloc_proxy
75 extern "C" void * __TBB_malloc_proxy(size_t)  __attribute__ ((weak));
76 #elif MALLOC_ZONE_OVERLOAD_ENABLED
77 // as there is no significant overhead, always suppose that proxy can be present
78 const bool malloc_proxy = true;
79 #else
80 const bool malloc_proxy = false;
81 #endif
82 
83 namespace rml {
84 namespace internal {
85     void init_tbbmalloc();
86 } } // namespaces
87 
88 #define MALLOC_EXTRA_INITIALIZATION rml::internal::init_tbbmalloc()
89 
90 // Need these to work regardless of tools support.
91 namespace tbb {
92 namespace detail {
93 namespace d1 {
94 
95     enum notify_type {prepare=0, cancel, acquired, releasing};
96 
97 #if TBB_USE_PROFILING_TOOLS
98     inline void call_itt_notify(notify_type t, void *ptr) {
99         // unreferenced formal parameter warning
100         detail::suppress_unused_warning(ptr);
101         switch ( t ) {
102         case prepare:
103             MALLOC_ITT_SYNC_PREPARE( ptr );
104             break;
105         case cancel:
106             MALLOC_ITT_SYNC_CANCEL( ptr );
107             break;
108         case acquired:
109             MALLOC_ITT_SYNC_ACQUIRED( ptr );
110             break;
111         case releasing:
112             MALLOC_ITT_SYNC_RELEASING( ptr );
113             break;
114         }
115     }
116 #else
117     inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
118 #endif // TBB_USE_PROFILING_TOOLS
119 
120 } // namespace d1
121 } // namespace detail
122 } // namespace tbb
123 
124 #include "oneapi/tbb/detail/_aggregator.h"
125 
126 template <typename OperationType>
127 struct MallocAggregator {
128     typedef tbb::detail::d1::aggregator_generic<OperationType> type;
129 };
130 
131 //! aggregated_operation base class
132 template <typename Derived>
133 struct MallocAggregatedOperation {
134     typedef tbb::detail::d1::aggregated_operation<Derived> type;
135 };
136 
137 #endif /* _TBB_malloc_Customize_H_ */
138