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#ifndef _TBB_malloc_Customize_H_
18#define _TBB_malloc_Customize_H_
19
20// customizing MALLOC_ASSERT macro
21#include "tbb/tbb_stddef.h"
22#define MALLOC_ASSERT(assertion, message) __TBB_ASSERT(assertion, message)
23#define MALLOC_ASSERT_EX(assertion, message) __TBB_ASSERT_EX(assertion, message)
24
25#ifndef MALLOC_DEBUG
26#define MALLOC_DEBUG TBB_USE_DEBUG
27#endif
28
29#include "Synchronize.h"
30
31#if DO_ITT_NOTIFY
32#include "tbb/itt_notify.h"
33#define MALLOC_ITT_SYNC_PREPARE(pointer) ITT_NOTIFY(sync_prepare, (pointer))
34#define MALLOC_ITT_SYNC_ACQUIRED(pointer) ITT_NOTIFY(sync_acquired, (pointer))
35#define MALLOC_ITT_SYNC_RELEASING(pointer) ITT_NOTIFY(sync_releasing, (pointer))
36#define MALLOC_ITT_SYNC_CANCEL(pointer) ITT_NOTIFY(sync_cancel, (pointer))
37#define MALLOC_ITT_FINI_ITTLIB() ITT_FINI_ITTLIB()
38#else
39#define MALLOC_ITT_SYNC_PREPARE(pointer) ((void)0)
40#define MALLOC_ITT_SYNC_ACQUIRED(pointer) ((void)0)
41#define MALLOC_ITT_SYNC_RELEASING(pointer) ((void)0)
42#define MALLOC_ITT_SYNC_CANCEL(pointer) ((void)0)
43#define MALLOC_ITT_FINI_ITTLIB() ((void)0)
44#endif
45
46inline intptr_t BitScanRev(uintptr_t x) {
47 return !x? -1 : __TBB_Log2(x);
48}
49
50template<typename T>
51static inline bool isAligned(T* arg, uintptr_t alignment) {
52 return tbb::internal::is_aligned(arg,alignment);
53}
54
55static inline bool isPowerOfTwo(uintptr_t arg) {
56 return tbb::internal::is_power_of_two(arg);
57}
58static inline bool isPowerOfTwoAtLeast(uintptr_t arg, uintptr_t power2) {
59 return arg && tbb::internal::is_power_of_two_at_least(arg,power2);
60}
61
62#define MALLOC_STATIC_ASSERT(condition,msg) __TBB_STATIC_ASSERT(condition,msg)
63
64#define USE_DEFAULT_MEMORY_MAPPING 1
65
66// To support malloc replacement
67#include "proxy.h"
68
69#if MALLOC_UNIXLIKE_OVERLOAD_ENABLED
70#define malloc_proxy __TBB_malloc_proxy
71extern "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
74const bool malloc_proxy = true;
75#else
76const bool malloc_proxy = false;
77#endif
78
79namespace rml {
80namespace 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.
87namespace tbb {
88 namespace internal {
89
90 enum notify_type {prepare=0, cancel, acquired, releasing};
91
92#if TBB_USE_THREADING_TOOLS
93 inline void call_itt_notify(notify_type t, void *ptr) {
94 switch ( t ) {
95 case prepare:
96 MALLOC_ITT_SYNC_PREPARE( ptr );
97 break;
98 case cancel:
99 MALLOC_ITT_SYNC_CANCEL( ptr );
100 break;
101 case acquired:
102 MALLOC_ITT_SYNC_ACQUIRED( ptr );
103 break;
104 case releasing:
105 MALLOC_ITT_SYNC_RELEASING( ptr );
106 break;
107 }
108 }
109#else
110 inline void call_itt_notify(notify_type /*t*/, void * /*ptr*/) {}
111#endif // TBB_USE_THREADING_TOOLS
112
113 template <typename T>
114 inline void itt_store_word_with_release(T& dst, T src) {
115#if TBB_USE_THREADING_TOOLS
116 call_itt_notify(releasing, &dst);
117#endif // TBB_USE_THREADING_TOOLS
118 FencedStore(*(intptr_t*)&dst, src);
119 }
120
121 template <typename T>
122 inline T itt_load_word_with_acquire(T& src) {
123 T result = FencedLoad(*(intptr_t*)&src);
124#if TBB_USE_THREADING_TOOLS
125 call_itt_notify(acquired, &src);
126#endif // TBB_USE_THREADING_TOOLS
127 return result;
128
129 }
130 } // namespace internal
131} // namespace tbb
132
133#include "tbb/internal/_aggregator_impl.h"
134
135template <typename OperationType>
136struct MallocAggregator {
137 typedef tbb::internal::aggregator_generic<OperationType> type;
138};
139
140//! aggregated_operation base class
141template <typename Derived>
142struct MallocAggregatedOperation {
143 typedef tbb::internal::aggregated_operation<Derived> type;
144};
145
146#endif /* _TBB_malloc_Customize_H_ */
147