1 //===-- sanitizer_flat_map.h ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Part of the Sanitizer Allocator.
11 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_FLAT_MAP_H
14 #define SANITIZER_FLAT_MAP_H
16 #include "sanitizer_atomic.h"
17 #include "sanitizer_common.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_local_address_space_view.h"
20 #include "sanitizer_mutex.h"
22 namespace __sanitizer
{
24 // Maps integers in rage [0, kSize) to values.
25 template <typename T
, u64 kSize
,
26 typename AddressSpaceViewTy
= LocalAddressSpaceView
>
29 using AddressSpaceView
= AddressSpaceViewTy
;
30 void Init() { internal_memset(map_
, 0, sizeof(map_
)); }
32 constexpr uptr
size() const { return kSize
; }
34 bool contains(uptr idx
) const {
39 T
&operator[](uptr idx
) {
40 DCHECK_LT(idx
, kSize
);
44 const T
&operator[](uptr idx
) const {
45 DCHECK_LT(idx
, kSize
);
53 // TwoLevelMap maps integers in range [0, kSize1*kSize2) to values.
54 // It is implemented as a two-dimensional array: array of kSize1 pointers
55 // to kSize2-byte arrays. The secondary arrays are mmaped on demand.
56 // Each value is initially zero and can be set to something else only once.
57 // Setting and getting values from multiple threads is safe w/o extra locking.
58 template <typename T
, u64 kSize1
, u64 kSize2
,
59 typename AddressSpaceViewTy
= LocalAddressSpaceView
>
61 static_assert(IsPowerOfTwo(kSize2
), "Use a power of two for performance.");
64 using AddressSpaceView
= AddressSpaceViewTy
;
67 internal_memset(map1_
, 0, sizeof(map1_
));
70 void TestOnlyUnmap() {
71 for (uptr i
= 0; i
< kSize1
; i
++) {
75 UnmapOrDie(p
, kSize2
);
80 uptr
MemoryUsage() const {
82 for (uptr i
= 0; i
< kSize1
; i
++) {
91 constexpr uptr
size() const { return kSize1
* kSize2
; }
92 constexpr uptr
size1() const { return kSize1
; }
93 constexpr uptr
size2() const { return kSize2
; }
95 bool contains(uptr idx
) const {
96 CHECK_LT(idx
, kSize1
* kSize2
);
97 return Get(idx
/ kSize2
);
100 const T
&operator[](uptr idx
) const {
101 DCHECK_LT(idx
, kSize1
* kSize2
);
102 T
*map2
= GetOrCreate(idx
/ kSize2
);
103 return *AddressSpaceView::Load(&map2
[idx
% kSize2
]);
106 T
&operator[](uptr idx
) {
107 DCHECK_LT(idx
, kSize1
* kSize2
);
108 T
*map2
= GetOrCreate(idx
/ kSize2
);
109 return *AddressSpaceView::LoadWritable(&map2
[idx
% kSize2
]);
112 void Lock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS
{ mu_
.Lock(); }
114 void Unlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS
{ mu_
.Unlock(); }
117 constexpr uptr
MmapSize() const {
118 return RoundUpTo(kSize2
* sizeof(T
), GetPageSizeCached());
121 T
*Get(uptr idx
) const {
122 DCHECK_LT(idx
, kSize1
);
123 return reinterpret_cast<T
*>(
124 atomic_load(&map1_
[idx
], memory_order_acquire
));
127 T
*GetOrCreate(uptr idx
) const {
128 DCHECK_LT(idx
, kSize1
);
129 // This code needs to use memory_order_acquire/consume, but we use
130 // memory_order_relaxed for performance reasons (matters for arm64). We
131 // expect memory_order_relaxed to be effectively equivalent to
132 // memory_order_consume in this case for all relevant architectures: all
133 // dependent data is reachable only by dereferencing the resulting pointer.
134 // If relaxed load fails to see stored ptr, the code will fall back to
135 // Create() and reload the value again with locked mutex as a memory
137 T
*res
= reinterpret_cast<T
*>(atomic_load_relaxed(&map1_
[idx
]));
143 NOINLINE T
*Create(uptr idx
) const {
144 SpinMutexLock
l(&mu_
);
147 res
= reinterpret_cast<T
*>(MmapOrDie(MmapSize(), "TwoLevelMap"));
148 atomic_store(&map1_
[idx
], reinterpret_cast<uptr
>(res
),
149 memory_order_release
);
154 mutable StaticSpinMutex mu_
;
155 mutable atomic_uintptr_t map1_
[kSize1
];
158 template <u64 kSize
, typename AddressSpaceViewTy
= LocalAddressSpaceView
>
159 using FlatByteMap
= FlatMap
<u8
, kSize
, AddressSpaceViewTy
>;
161 template <u64 kSize1
, u64 kSize2
,
162 typename AddressSpaceViewTy
= LocalAddressSpaceView
>
163 using TwoLevelByteMap
= TwoLevelMap
<u8
, kSize1
, kSize2
, AddressSpaceViewTy
>;
164 } // namespace __sanitizer