1 //===-- tsan_dense_alloc.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // A DenseSlabAlloc is a freelist-based allocator of fixed-size objects.
13 // DenseSlabAllocCache is a thread-local cache for DenseSlabAlloc.
14 // The only difference with traditional slab allocators is that DenseSlabAlloc
15 // allocates/free indices of objects and provide a functionality to map
16 // the index onto the real pointer. The index is u32, that is, 2 times smaller
17 // than uptr (hense the Dense prefix).
18 //===----------------------------------------------------------------------===//
19 #ifndef TSAN_DENSE_ALLOC_H
20 #define TSAN_DENSE_ALLOC_H
22 #include "sanitizer_common/sanitizer_common.h"
23 #include "tsan_defs.h"
24 #include "tsan_mutex.h"
28 class DenseSlabAllocCache {
29 static const uptr kSize = 128;
33 template<typename T, uptr kL1Size, uptr kL2Size> friend class DenseSlabAlloc;
36 template<typename T, uptr kL1Size, uptr kL2Size>
37 class DenseSlabAlloc {
39 typedef DenseSlabAllocCache Cache;
40 typedef typename Cache::IndexT IndexT;
43 // Check that kL1Size and kL2Size are sane.
44 CHECK_EQ(kL1Size & (kL1Size - 1), 0);
45 CHECK_EQ(kL2Size & (kL2Size - 1), 0);
46 CHECK_GE(1ull << (sizeof(IndexT) * 8), kL1Size * kL2Size);
47 // Check that it makes sense to use the dense alloc.
48 CHECK_GE(sizeof(T), sizeof(IndexT));
49 internal_memset(map_, 0, sizeof(map_));
55 for (uptr i = 0; i < kL1Size; i++) {
57 UnmapOrDie(map_[i], kL2Size * sizeof(T));
61 IndexT Alloc(Cache *c) {
64 return c->cache[--c->pos];
67 void Free(Cache *c, IndexT idx) {
69 if (c->pos == Cache::kSize)
71 c->cache[c->pos++] = idx;
76 DCHECK_LE(idx, kL1Size * kL2Size);
77 return &map_[idx / kL2Size][idx % kL2Size];
80 void FlushCache(Cache *c) {
81 SpinMutexLock lock(&mtx_);
83 IndexT idx = c->cache[--c->pos];
84 *(IndexT*)Map(idx) = freelist_;
89 void InitCache(Cache *c) {
91 internal_memset(c->cache, 0, sizeof(c->cache));
100 void Refill(Cache *c) {
101 SpinMutexLock lock(&mtx_);
102 if (freelist_ == 0) {
103 if (fillpos_ == kL1Size) {
104 Printf("ThreadSanitizer: DenseSlabAllocator overflow. Dying.\n");
107 T *batch = (T*)MmapOrDie(kL2Size * sizeof(T), "DenseSlabAllocator");
108 // Reserve 0 as invalid index.
109 IndexT start = fillpos_ == 0 ? 1 : 0;
110 for (IndexT i = start; i < kL2Size; i++) {
112 *(IndexT*)(batch + i) = i + 1 + fillpos_ * kL2Size;
114 *(IndexT*)(batch + kL2Size - 1) = 0;
115 freelist_ = fillpos_ * kL2Size + start;
116 map_[fillpos_++] = batch;
118 for (uptr i = 0; i < Cache::kSize / 2 && freelist_ != 0; i++) {
119 IndexT idx = freelist_;
120 c->cache[c->pos++] = idx;
121 freelist_ = *(IndexT*)Map(idx);
125 void Drain(Cache *c) {
126 SpinMutexLock lock(&mtx_);
127 for (uptr i = 0; i < Cache::kSize / 2; i++) {
128 IndexT idx = c->cache[--c->pos];
129 *(IndexT*)Map(idx) = freelist_;
135 } // namespace __tsan
137 #endif // TSAN_DENSE_ALLOC_H