sanitizer_addrhashmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //===-- sanitizer_addrhashmap.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. // Concurrent uptr->T hashmap.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ADDRHASHMAP_H
  13. #define SANITIZER_ADDRHASHMAP_H
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_mutex.h"
  16. #include "sanitizer_atomic.h"
  17. #include "sanitizer_allocator_internal.h"
  18. namespace __sanitizer {
  19. // Concurrent uptr->T hashmap.
  20. // T must be a POD type, kSize is preferably a prime but can be any number.
  21. // Usage example:
  22. //
  23. // typedef AddrHashMap<uptr, 11> Map;
  24. // Map m;
  25. // {
  26. // Map::Handle h(&m, addr);
  27. // use h.operator->() to access the data
  28. // if h.created() then the element was just created, and the current thread
  29. // has exclusive access to it
  30. // otherwise the current thread has only read access to the data
  31. // }
  32. // {
  33. // Map::Handle h(&m, addr, true);
  34. // this will remove the data from the map in Handle dtor
  35. // the current thread has exclusive access to the data
  36. // if !h.exists() then the element never existed
  37. // }
  38. // {
  39. // Map::Handle h(&m, addr, false, true);
  40. // this will create a new element or return a handle to an existing element
  41. // if !h.created() this thread does *not* have exclusive access to the data
  42. // }
  43. template<typename T, uptr kSize>
  44. class AddrHashMap {
  45. private:
  46. struct Cell {
  47. atomic_uintptr_t addr;
  48. T val;
  49. };
  50. struct AddBucket {
  51. uptr cap;
  52. uptr size;
  53. Cell cells[1]; // variable len
  54. };
  55. static const uptr kBucketSize = 3;
  56. struct Bucket {
  57. Mutex mtx;
  58. atomic_uintptr_t add;
  59. Cell cells[kBucketSize];
  60. };
  61. public:
  62. AddrHashMap();
  63. class Handle {
  64. public:
  65. Handle(AddrHashMap<T, kSize> *map, uptr addr);
  66. Handle(AddrHashMap<T, kSize> *map, uptr addr, bool remove);
  67. Handle(AddrHashMap<T, kSize> *map, uptr addr, bool remove, bool create);
  68. ~Handle();
  69. T *operator->();
  70. T &operator*();
  71. const T &operator*() const;
  72. bool created() const;
  73. bool exists() const;
  74. private:
  75. friend AddrHashMap<T, kSize>;
  76. AddrHashMap<T, kSize> *map_;
  77. Bucket *bucket_;
  78. Cell *cell_;
  79. uptr addr_;
  80. uptr addidx_;
  81. bool created_;
  82. bool remove_;
  83. bool create_;
  84. };
  85. typedef void (*ForEachCallback)(const uptr key, const T &val, void *arg);
  86. // ForEach acquires a lock on each bucket while iterating over
  87. // elements. Note that this only ensures that the structure of the hashmap is
  88. // unchanged, there may be a data race to the element itself.
  89. void ForEach(ForEachCallback cb, void *arg);
  90. private:
  91. friend class Handle;
  92. Bucket *table_;
  93. void acquire(Handle *h);
  94. void release(Handle *h);
  95. uptr calcHash(uptr addr);
  96. };
  97. template <typename T, uptr kSize>
  98. void AddrHashMap<T, kSize>::ForEach(ForEachCallback cb, void *arg) {
  99. for (uptr n = 0; n < kSize; n++) {
  100. Bucket *bucket = &table_[n];
  101. ReadLock lock(&bucket->mtx);
  102. for (uptr i = 0; i < kBucketSize; i++) {
  103. Cell *c = &bucket->cells[i];
  104. uptr addr1 = atomic_load(&c->addr, memory_order_acquire);
  105. if (addr1 != 0)
  106. cb(addr1, c->val, arg);
  107. }
  108. // Iterate over any additional cells.
  109. if (AddBucket *add =
  110. (AddBucket *)atomic_load(&bucket->add, memory_order_acquire)) {
  111. for (uptr i = 0; i < add->size; i++) {
  112. Cell *c = &add->cells[i];
  113. uptr addr1 = atomic_load(&c->addr, memory_order_acquire);
  114. if (addr1 != 0)
  115. cb(addr1, c->val, arg);
  116. }
  117. }
  118. }
  119. }
  120. template<typename T, uptr kSize>
  121. AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr) {
  122. map_ = map;
  123. addr_ = addr;
  124. remove_ = false;
  125. create_ = true;
  126. map_->acquire(this);
  127. }
  128. template<typename T, uptr kSize>
  129. AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr,
  130. bool remove) {
  131. map_ = map;
  132. addr_ = addr;
  133. remove_ = remove;
  134. create_ = true;
  135. map_->acquire(this);
  136. }
  137. template<typename T, uptr kSize>
  138. AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr,
  139. bool remove, bool create) {
  140. map_ = map;
  141. addr_ = addr;
  142. remove_ = remove;
  143. create_ = create;
  144. map_->acquire(this);
  145. }
  146. template<typename T, uptr kSize>
  147. AddrHashMap<T, kSize>::Handle::~Handle() {
  148. map_->release(this);
  149. }
  150. template <typename T, uptr kSize>
  151. T *AddrHashMap<T, kSize>::Handle::operator->() {
  152. return &cell_->val;
  153. }
  154. template <typename T, uptr kSize>
  155. const T &AddrHashMap<T, kSize>::Handle::operator*() const {
  156. return cell_->val;
  157. }
  158. template <typename T, uptr kSize>
  159. T &AddrHashMap<T, kSize>::Handle::operator*() {
  160. return cell_->val;
  161. }
  162. template<typename T, uptr kSize>
  163. bool AddrHashMap<T, kSize>::Handle::created() const {
  164. return created_;
  165. }
  166. template<typename T, uptr kSize>
  167. bool AddrHashMap<T, kSize>::Handle::exists() const {
  168. return cell_ != nullptr;
  169. }
  170. template<typename T, uptr kSize>
  171. AddrHashMap<T, kSize>::AddrHashMap() {
  172. table_ = (Bucket*)MmapOrDie(kSize * sizeof(table_[0]), "AddrHashMap");
  173. }
  174. template <typename T, uptr kSize>
  175. void AddrHashMap<T, kSize>::acquire(Handle *h)
  176. SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  177. uptr addr = h->addr_;
  178. uptr hash = calcHash(addr);
  179. Bucket *b = &table_[hash];
  180. h->created_ = false;
  181. h->addidx_ = -1U;
  182. h->bucket_ = b;
  183. h->cell_ = nullptr;
  184. // If we want to remove the element, we need exclusive access to the bucket,
  185. // so skip the lock-free phase.
  186. if (h->remove_)
  187. goto locked;
  188. retry:
  189. // First try to find an existing element w/o read mutex.
  190. CHECK(!h->remove_);
  191. // Check the embed cells.
  192. for (uptr i = 0; i < kBucketSize; i++) {
  193. Cell *c = &b->cells[i];
  194. uptr addr1 = atomic_load(&c->addr, memory_order_acquire);
  195. if (addr1 == addr) {
  196. h->cell_ = c;
  197. return;
  198. }
  199. }
  200. // Check the add cells with read lock.
  201. if (atomic_load(&b->add, memory_order_relaxed)) {
  202. b->mtx.ReadLock();
  203. AddBucket *add = (AddBucket*)atomic_load(&b->add, memory_order_relaxed);
  204. for (uptr i = 0; i < add->size; i++) {
  205. Cell *c = &add->cells[i];
  206. uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
  207. if (addr1 == addr) {
  208. h->addidx_ = i;
  209. h->cell_ = c;
  210. return;
  211. }
  212. }
  213. b->mtx.ReadUnlock();
  214. }
  215. locked:
  216. // Re-check existence under write lock.
  217. // Embed cells.
  218. b->mtx.Lock();
  219. for (uptr i = 0; i < kBucketSize; i++) {
  220. Cell *c = &b->cells[i];
  221. uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
  222. if (addr1 == addr) {
  223. if (h->remove_) {
  224. h->cell_ = c;
  225. return;
  226. }
  227. b->mtx.Unlock();
  228. goto retry;
  229. }
  230. }
  231. // Add cells.
  232. AddBucket *add = (AddBucket*)atomic_load(&b->add, memory_order_relaxed);
  233. if (add) {
  234. for (uptr i = 0; i < add->size; i++) {
  235. Cell *c = &add->cells[i];
  236. uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
  237. if (addr1 == addr) {
  238. if (h->remove_) {
  239. h->addidx_ = i;
  240. h->cell_ = c;
  241. return;
  242. }
  243. b->mtx.Unlock();
  244. goto retry;
  245. }
  246. }
  247. }
  248. // The element does not exist, no need to create it if we want to remove.
  249. if (h->remove_ || !h->create_) {
  250. b->mtx.Unlock();
  251. return;
  252. }
  253. // Now try to create it under the mutex.
  254. h->created_ = true;
  255. // See if we have a free embed cell.
  256. for (uptr i = 0; i < kBucketSize; i++) {
  257. Cell *c = &b->cells[i];
  258. uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
  259. if (addr1 == 0) {
  260. h->cell_ = c;
  261. return;
  262. }
  263. }
  264. // Store in the add cells.
  265. if (!add) {
  266. // Allocate a new add array.
  267. const uptr kInitSize = 64;
  268. add = (AddBucket*)InternalAlloc(kInitSize);
  269. internal_memset(add, 0, kInitSize);
  270. add->cap = (kInitSize - sizeof(*add)) / sizeof(add->cells[0]) + 1;
  271. add->size = 0;
  272. atomic_store(&b->add, (uptr)add, memory_order_relaxed);
  273. }
  274. if (add->size == add->cap) {
  275. // Grow existing add array.
  276. uptr oldsize = sizeof(*add) + (add->cap - 1) * sizeof(add->cells[0]);
  277. uptr newsize = oldsize * 2;
  278. AddBucket *add1 = (AddBucket*)InternalAlloc(newsize);
  279. internal_memset(add1, 0, newsize);
  280. add1->cap = (newsize - sizeof(*add)) / sizeof(add->cells[0]) + 1;
  281. add1->size = add->size;
  282. internal_memcpy(add1->cells, add->cells, add->size * sizeof(add->cells[0]));
  283. InternalFree(add);
  284. atomic_store(&b->add, (uptr)add1, memory_order_relaxed);
  285. add = add1;
  286. }
  287. // Store.
  288. uptr i = add->size++;
  289. Cell *c = &add->cells[i];
  290. CHECK_EQ(atomic_load(&c->addr, memory_order_relaxed), 0);
  291. h->addidx_ = i;
  292. h->cell_ = c;
  293. }
  294. template <typename T, uptr kSize>
  295. void AddrHashMap<T, kSize>::release(Handle *h)
  296. SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  297. if (!h->cell_)
  298. return;
  299. Bucket *b = h->bucket_;
  300. Cell *c = h->cell_;
  301. uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
  302. if (h->created_) {
  303. // Denote completion of insertion.
  304. CHECK_EQ(addr1, 0);
  305. // After the following store, the element becomes available
  306. // for lock-free reads.
  307. atomic_store(&c->addr, h->addr_, memory_order_release);
  308. b->mtx.Unlock();
  309. } else if (h->remove_) {
  310. // Denote that the cell is empty now.
  311. CHECK_EQ(addr1, h->addr_);
  312. atomic_store(&c->addr, 0, memory_order_release);
  313. // See if we need to compact the bucket.
  314. AddBucket *add = (AddBucket *)atomic_load(&b->add, memory_order_relaxed);
  315. if (h->addidx_ == -1U) {
  316. // Removed from embed array, move an add element into the freed cell.
  317. if (add && add->size != 0) {
  318. uptr last = --add->size;
  319. Cell *c1 = &add->cells[last];
  320. c->val = c1->val;
  321. uptr addr1 = atomic_load(&c1->addr, memory_order_relaxed);
  322. atomic_store(&c->addr, addr1, memory_order_release);
  323. atomic_store(&c1->addr, 0, memory_order_release);
  324. }
  325. } else {
  326. // Removed from add array, compact it.
  327. uptr last = --add->size;
  328. Cell *c1 = &add->cells[last];
  329. if (c != c1) {
  330. *c = *c1;
  331. atomic_store(&c1->addr, 0, memory_order_relaxed);
  332. }
  333. }
  334. if (add && add->size == 0) {
  335. // FIXME(dvyukov): free add?
  336. }
  337. b->mtx.Unlock();
  338. } else {
  339. CHECK_EQ(addr1, h->addr_);
  340. if (h->addidx_ != -1U)
  341. b->mtx.ReadUnlock();
  342. }
  343. }
  344. template<typename T, uptr kSize>
  345. uptr AddrHashMap<T, kSize>::calcHash(uptr addr) {
  346. addr += addr << 10;
  347. addr ^= addr >> 6;
  348. return addr % kSize;
  349. }
  350. } // namespace __sanitizer
  351. #endif // SANITIZER_ADDRHASHMAP_H