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