1 //===-- xray_segmented_array.h ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of XRay, a dynamic runtime instrumentation system. 11 // 12 // Defines the implementation of a segmented array, with fixed-size chunks 13 // backing the segments. 14 // 15 //===----------------------------------------------------------------------===// 16 #ifndef XRAY_SEGMENTED_ARRAY_H 17 #define XRAY_SEGMENTED_ARRAY_H 18 19 #include "sanitizer_common/sanitizer_allocator.h" 20 #include "xray_allocator.h" 21 #include <type_traits> 22 #include <utility> 23 24 namespace __xray { 25 26 namespace { 27 28 constexpr size_t gcd(size_t a, size_t b) { 29 return (b == 0) ? a : gcd(b, a % b); 30 } 31 32 constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); } 33 34 } // namespace 35 36 /// The Array type provides an interface similar to std::vector<...> but does 37 /// not shrink in size. Once constructed, elements can be appended but cannot be 38 /// removed. The implementation is heavily dependent on the contract provided by 39 /// the Allocator type, in that all memory will be released when the Allocator 40 /// is destroyed. When an Array is destroyed, it will destroy elements in the 41 /// backing store but will not free the memory. The parameter N defines how many 42 /// elements of T there should be in a single block. 43 /// 44 /// We compute the least common multiple of the size of T and the cache line 45 /// size, to allow us to maximise the number of T objects we can place in 46 /// cache-line multiple sized blocks. To get back the number of T's, we divide 47 /// this least common multiple by the size of T. 48 template <class T, size_t N = lcm(sizeof(T), kCacheLineSize) / sizeof(T)> 49 struct Array { 50 static constexpr size_t ChunkSize = N; 51 static constexpr size_t AllocatorChunkSize = sizeof(T) * ChunkSize; 52 using AllocatorType = Allocator<AllocatorChunkSize>; 53 static_assert(std::is_trivially_destructible<T>::value, 54 "T must be trivially destructible."); 55 56 private: 57 // TODO: Consider co-locating the chunk information with the data in the 58 // Block, as in an intrusive list -- i.e. putting the next and previous 59 // pointer values inside the Block storage. 60 struct Chunk { 61 typename AllocatorType::Block Block; 62 static constexpr size_t Size = N; 63 Chunk *Prev = nullptr; 64 Chunk *Next = nullptr; 65 }; 66 67 static Chunk SentinelChunk; 68 69 AllocatorType *Allocator; 70 Chunk *Head = &SentinelChunk; 71 Chunk *Tail = &SentinelChunk; 72 size_t Size = 0; 73 size_t FreeElements = 0; 74 75 // Here we keep track of chunks in the freelist, to allow us to re-use chunks 76 // when elements are trimmed off the end. 77 Chunk *Freelist = &SentinelChunk; 78 79 Chunk *NewChunk() { 80 // We need to handle the case in which enough elements have been trimmed to 81 // allow us to re-use chunks we've allocated before. For this we look into 82 // the Freelist, to see whether we need to actually allocate new blocks or 83 // just re-use blocks we've already seen before. 84 if (Freelist != &SentinelChunk) { 85 auto *FreeChunk = Freelist; 86 Freelist = FreeChunk->Next; 87 FreeChunk->Next = &SentinelChunk; 88 return FreeChunk; 89 } 90 91 auto Block = Allocator->Allocate(); 92 if (Block.Data == nullptr) 93 return nullptr; 94 // TODO: Maybe use a separate managed allocator for Chunk instances? 95 auto C = reinterpret_cast<Chunk *>(InternalAlloc(sizeof(Chunk))); 96 if (C == nullptr) 97 return nullptr; 98 C->Block = Block; 99 return C; 100 } 101 102 static AllocatorType &GetGlobalAllocator() { 103 static AllocatorType *const GlobalAllocator = [] { 104 AllocatorType *A = reinterpret_cast<AllocatorType *>( 105 InternalAlloc(sizeof(AllocatorType))); 106 new (A) AllocatorType(2 << 10, 0); 107 return A; 108 }(); 109 110 return *GlobalAllocator; 111 } 112 113 Chunk *InitHeadAndTail() { 114 DCHECK_EQ(Head, &SentinelChunk); 115 DCHECK_EQ(Tail, &SentinelChunk); 116 auto Chunk = NewChunk(); 117 if (Chunk == nullptr) 118 return nullptr; 119 Chunk->Prev = &SentinelChunk; 120 Chunk->Next = &SentinelChunk; 121 Head = Chunk; 122 Tail = Chunk; 123 return Chunk; 124 } 125 126 Chunk *AppendNewChunk() { 127 auto Chunk = NewChunk(); 128 if (Chunk == nullptr) 129 return nullptr; 130 Tail->Next = Chunk; 131 Chunk->Prev = Tail; 132 Chunk->Next = &SentinelChunk; 133 Tail = Chunk; 134 return Chunk; 135 } 136 137 // This Iterator models a BidirectionalIterator. 138 template <class U> class Iterator { 139 Chunk *C = nullptr; 140 size_t Offset = 0; 141 142 public: 143 Iterator(Chunk *IC, size_t Off) : C(IC), Offset(Off) {} 144 145 Iterator &operator++() { 146 DCHECK_NE(C, &SentinelChunk); 147 if (++Offset % N) 148 return *this; 149 150 // At this point, we know that Offset % N == 0, so we must advance the 151 // chunk pointer. 152 DCHECK_EQ(Offset % N, 0); 153 C = C->Next; 154 return *this; 155 } 156 157 Iterator &operator--() { 158 DCHECK_NE(C, &SentinelChunk); 159 DCHECK_GT(Offset, 0); 160 161 // We check whether the offset was on a boundary before decrement, to see 162 // whether we need to retreat to the previous chunk. 163 if ((Offset-- % N) == 0) 164 C = C->Prev; 165 return *this; 166 } 167 168 Iterator operator++(int) { 169 Iterator Copy(*this); 170 ++(*this); 171 return Copy; 172 } 173 174 Iterator operator--(int) { 175 Iterator Copy(*this); 176 --(*this); 177 return Copy; 178 } 179 180 template <class V, class W> 181 friend bool operator==(const Iterator<V> &L, const Iterator<W> &R) { 182 return L.C == R.C && L.Offset == R.Offset; 183 } 184 185 template <class V, class W> 186 friend bool operator!=(const Iterator<V> &L, const Iterator<W> &R) { 187 return !(L == R); 188 } 189 190 U &operator*() const { 191 DCHECK_NE(C, &SentinelChunk); 192 return reinterpret_cast<U *>(C->Block.Data)[Offset % N]; 193 } 194 195 U *operator->() const { 196 DCHECK_NE(C, &SentinelChunk); 197 return reinterpret_cast<U *>(C->Block.Data) + (Offset % N); 198 } 199 }; 200 201 public: 202 explicit Array(AllocatorType &A) : Allocator(&A) {} 203 Array() : Array(GetGlobalAllocator()) {} 204 205 Array(const Array &) = delete; 206 Array(Array &&O) NOEXCEPT : Allocator(O.Allocator), 207 Head(O.Head), 208 Tail(O.Tail), 209 Size(O.Size) { 210 O.Head = &SentinelChunk; 211 O.Tail = &SentinelChunk; 212 O.Size = 0; 213 } 214 215 bool empty() const { return Size == 0; } 216 217 AllocatorType &allocator() const { 218 DCHECK_NE(Allocator, nullptr); 219 return *Allocator; 220 } 221 222 size_t size() const { return Size; } 223 224 T *Append(const T &E) { 225 if (UNLIKELY(Head == &SentinelChunk)) 226 if (InitHeadAndTail() == nullptr) 227 return nullptr; 228 229 auto Offset = Size % N; 230 if (UNLIKELY(Size != 0 && Offset == 0)) 231 if (AppendNewChunk() == nullptr) 232 return nullptr; 233 234 auto Position = reinterpret_cast<T *>(Tail->Block.Data) + Offset; 235 *Position = E; 236 ++Size; 237 FreeElements -= FreeElements ? 1 : 0; 238 return Position; 239 } 240 241 template <class... Args> T *AppendEmplace(Args &&... args) { 242 if (UNLIKELY(Head == &SentinelChunk)) 243 if (InitHeadAndTail() == nullptr) 244 return nullptr; 245 246 auto Offset = Size % N; 247 if (UNLIKELY(Size != 0 && Offset == 0)) 248 if (AppendNewChunk() == nullptr) 249 return nullptr; 250 251 auto Position = reinterpret_cast<T *>(Tail->Block.Data) + Offset; 252 // In-place construct at Position. 253 new (Position) T(std::forward<Args>(args)...); 254 ++Size; 255 FreeElements -= FreeElements ? 1 : 0; 256 return Position; 257 } 258 259 T &operator[](size_t Offset) const { 260 DCHECK_LE(Offset, Size); 261 // We need to traverse the array enough times to find the element at Offset. 262 auto C = Head; 263 while (Offset >= N) { 264 C = C->Next; 265 Offset -= N; 266 DCHECK_NE(C, &SentinelChunk); 267 } 268 auto Position = reinterpret_cast<T *>(C->Block.Data) + Offset; 269 return *Position; 270 } 271 272 T &front() const { 273 DCHECK_NE(Head, &SentinelChunk); 274 DCHECK_NE(Size, 0u); 275 return *reinterpret_cast<T *>(Head->Block.Data); 276 } 277 278 T &back() const { 279 DCHECK_NE(Tail, &SentinelChunk); 280 auto Offset = (Size - 1) % N; 281 return *(reinterpret_cast<T *>(Tail->Block.Data) + Offset); 282 } 283 284 template <class Predicate> T *find_element(Predicate P) const { 285 if (empty()) 286 return nullptr; 287 288 auto E = end(); 289 for (auto I = begin(); I != E; ++I) 290 if (P(*I)) 291 return &(*I); 292 293 return nullptr; 294 } 295 296 /// Remove N Elements from the end. This leaves the blocks behind, and not 297 /// require allocation of new blocks for new elements added after trimming. 298 void trim(size_t Elements) { 299 DCHECK_LE(Elements, Size); 300 Size -= Elements; 301 FreeElements += Elements; 302 303 // Here we need to check whether we've cleared enough elements to warrant 304 // putting blocks on to the freelist. We determine whether we need to 305 // right-size the internal list, by keeping track of the number of "free" 306 // elements still in the array. 307 auto ChunksToTrim = FreeElements / N; 308 for (size_t i = 0; i < ChunksToTrim; ++i, FreeElements -= N) { 309 // Put the tail into the Freelist. 310 auto *FreeChunk = Tail; 311 Tail = Tail->Prev; 312 if (Tail == &SentinelChunk) 313 Head = Tail; 314 else 315 Tail->Next = &SentinelChunk; 316 FreeChunk->Next = Freelist; 317 FreeChunk->Prev = Freelist->Prev; 318 Freelist = FreeChunk; 319 } 320 } 321 322 // Provide iterators. 323 Iterator<T> begin() const { return Iterator<T>(Head, 0); } 324 Iterator<T> end() const { return Iterator<T>(Tail, Size); } 325 Iterator<const T> cbegin() const { return Iterator<const T>(Head, 0); } 326 Iterator<const T> cend() const { return Iterator<const T>(Tail, Size); } 327 }; 328 329 // We need to have this storage definition out-of-line so that the compiler can 330 // ensure that storage for the SentinelChunk is defined and has a single 331 // address. 332 template <class T, size_t N> 333 typename Array<T, N>::Chunk Array<T, N>::SentinelChunk; 334 335 } // namespace __xray 336 337 #endif // XRAY_SEGMENTED_ARRAY_H 338