[OpenACC] Enable 'attach' clause for combined constructs
[llvm-project.git] / compiler-rt / lib / sanitizer_common / sanitizer_flat_map.h
blobd246781fe1df5e22feacb0d3bf213771b8d2e68b
1 //===-- sanitizer_flat_map.h ------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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>
27 class FlatMap {
28 public:
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 {
35 CHECK_LT(idx, kSize);
36 return true;
39 T &operator[](uptr idx) {
40 DCHECK_LT(idx, kSize);
41 return map_[idx];
44 const T &operator[](uptr idx) const {
45 DCHECK_LT(idx, kSize);
46 return map_[idx];
49 private:
50 T map_[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>
60 class TwoLevelMap {
61 static_assert(IsPowerOfTwo(kSize2), "Use a power of two for performance.");
63 public:
64 using AddressSpaceView = AddressSpaceViewTy;
65 void Init() {
66 mu_.Init();
67 internal_memset(map1_, 0, sizeof(map1_));
70 void TestOnlyUnmap() {
71 for (uptr i = 0; i < kSize1; i++) {
72 T *p = Get(i);
73 if (!p)
74 continue;
75 UnmapOrDie(p, kSize2);
77 Init();
80 uptr MemoryUsage() const {
81 uptr res = 0;
82 for (uptr i = 0; i < kSize1; i++) {
83 T *p = Get(i);
84 if (!p)
85 continue;
86 res += MmapSize();
88 return res;
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(); }
116 private:
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
136 // barrier.
137 T *res = reinterpret_cast<T *>(atomic_load_relaxed(&map1_[idx]));
138 if (LIKELY(res))
139 return res;
140 return Create(idx);
143 NOINLINE T *Create(uptr idx) const {
144 SpinMutexLock l(&mu_);
145 T *res = Get(idx);
146 if (!res) {
147 res = reinterpret_cast<T *>(MmapOrDie(MmapSize(), "TwoLevelMap"));
148 atomic_store(&map1_[idx], reinterpret_cast<uptr>(res),
149 memory_order_release);
151 return res;
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
166 #endif