xref: /oneTBB/src/tbbmalloc/Customize.h (revision 8dcbd5b1)
1 /*
2     Copyright (c) 2005-2020 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 #define USE_DEFAULT_MEMORY_MAPPING 1
65 
66 // To support malloc replacement
67 #include "../tbbmalloc_proxy/proxy.h"
68 
69 #if MALLOC_UNIXLIKE_OVERLOAD_ENABLED
70 #define malloc_proxy __TBB_malloc_proxy
71 extern "C" void * __TBB_malloc_proxy(size_t)  __attribute__ ((weak));
72 #elif MALLOC_ZONE_OVERLOAD_ENABLED
73 // as there is no significant overhead, always suppose that proxy can be present
74 const bool malloc_proxy = true;
75 #else
76 const bool malloc_proxy = false;
77 #endif
78 
79 namespace rml {
80 namespace internal {
81     void init_tbbmalloc();
82 } } // namespaces
83 
84 #define MALLOC_EXTRA_INITIALIZATION rml::internal::init_tbbmalloc()
85 
86 // Need these to work regardless of tools support.
87 namespace tbb {
88 namespace detail {
89 namespace d1 {
90 
91     enum notify_type {prepare=0, cancel, acquired, releasing};
92 
93 #if TBB_USE_PROFILING_TOOLS
94     inline void call_itt_notify(notify_type t, void *ptr) {
95         // unreferenced formal parameter warning
96         detail::suppress_unused_warning(ptr);
97         switch ( t ) {
98         case prepare:
99             MALLOC_ITT_SYNC_PREPARE( ptr );
100             break;
101         case cancel:
102             MALLOC_ITT_SYNC_CANCEL( ptr );
103             break;
104         case acquired:
105             MALLOC_ITT_SYNC_ACQUIRED( ptr );
106             break;
107         case releasing:
108             MALLOC_ITT_SYNC_RELEASING( ptr );
109             break;
110         }
111     }
112 #else
113     inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
114 #endif // TBB_USE_PROFILING_TOOLS
115 
116     template <typename T>
117     inline void store_with_release_itt(std::atomic<T>& dst, T src) {
118 #if TBB_USE_PROFILING_TOOLS
119         call_itt_notify(releasing, &dst);
120 #endif // TBB_USE_PROFILING_TOOLS
121         dst.store(src, std::memory_order_release);
122     }
123 
124     template <typename T>
125     inline T load_with_acquire_itt(const std::atomic<T>& src) {
126 #if TBB_USE_PROFILING_TOOLS
127         call_itt_notify(acquired, &src);
128 #endif // TBB_USE_PROFILING_TOOLS
129         return src.load(std::memory_order_acquire);
130     }
131 
132 } // namespace d1
133 } // namespace detail
134 } // namespace tbb
135 
136 #include "oneapi/tbb/detail/_aggregator.h"
137 
138 template <typename OperationType>
139 struct MallocAggregator {
140     typedef tbb::detail::d1::aggregator_generic<OperationType> type;
141 };
142 
143 //! aggregated_operation base class
144 template <typename Derived>
145 struct MallocAggregatedOperation {
146     typedef tbb::detail::d1::aggregated_operation<Derived> type;
147 };
148 
149 #endif /* _TBB_malloc_Customize_H_ */
150