blob: 444a561eeca40ca68c35a5dd3d8eacf460ecf709 [file] [log] [blame]
Kostya Serebryany6e26fa92012-06-21 10:04:36 +00001//===-- sanitizer_allocator64.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// Specialized allocator which works only in 64-bit address space.
10// To be used by ThreadSanitizer, MemorySanitizer and possibly other tools.
11// The main feature of this allocator is that the header is located far away
12// from the user memory region, so that the tool does not use extra shadow
13// for the header.
14//
15// Status: not yet ready.
16//===----------------------------------------------------------------------===//
17#ifndef SANITIZER_ALLOCATOR_H
18#define SANITIZER_ALLOCATOR_H
19
20#include "sanitizer_common.h"
21#include "sanitizer_internal_defs.h"
22
23namespace __sanitizer {
24
Kostya Serebryany5b014152012-06-22 13:00:50 +000025// Maps size class id to size and back.
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000026class DefaultSizeClassMap {
27 private:
28 // Here we use a spline composed of 5 polynomials of oder 1.
29 // The first size class is l0, then the classes go with step s0
30 // untill they reach l1, after which they go with step s1 and so on.
31 // Steps should be powers of two for cheap division.
32 // The size of the last size class should be a power of two.
33 // There should be at most 256 size classes.
34 static const uptr l0 = 1 << 4;
35 static const uptr l1 = 1 << 9;
36 static const uptr l2 = 1 << 12;
37 static const uptr l3 = 1 << 15;
38 static const uptr l4 = 1 << 18;
39 static const uptr l5 = 1 << 21;
40
41 static const uptr s0 = 1 << 4;
42 static const uptr s1 = 1 << 6;
43 static const uptr s2 = 1 << 9;
44 static const uptr s3 = 1 << 12;
45 static const uptr s4 = 1 << 15;
46
47 static const uptr u0 = 0 + (l1 - l0) / s0;
48 static const uptr u1 = u0 + (l2 - l1) / s1;
49 static const uptr u2 = u1 + (l3 - l2) / s2;
50 static const uptr u3 = u2 + (l4 - l3) / s3;
51 static const uptr u4 = u3 + (l5 - l4) / s4;
52
53 public:
54 static const uptr kNumClasses = u4 + 1;
55 static const uptr kMaxSize = l5;
Kostya Serebryany5b014152012-06-22 13:00:50 +000056 static const uptr kMinSize = l0;
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000057
58 COMPILER_CHECK(kNumClasses <= 256);
59 COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0);
60
Kostya Serebryany5b014152012-06-22 13:00:50 +000061 static uptr Size(uptr class_id) {
62 if (class_id <= u0) return l0 + s0 * (class_id - 0);
63 if (class_id <= u1) return l1 + s1 * (class_id - u0);
64 if (class_id <= u2) return l2 + s2 * (class_id - u1);
65 if (class_id <= u3) return l3 + s3 * (class_id - u2);
66 if (class_id <= u4) return l4 + s4 * (class_id - u3);
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000067 return 0;
68 }
Kostya Serebryany5b014152012-06-22 13:00:50 +000069 static uptr ClassID(uptr size) {
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000070 if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0;
71 if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1;
72 if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2;
73 if (size <= l4) return u2 + (size - l3 + s3 - 1) / s3;
74 if (size <= l5) return u3 + (size - l4 + s4 - 1) / s4;
75 return 0;
76 }
77};
78
Kostya Serebryany5b014152012-06-22 13:00:50 +000079// Space: a portion of address space of kSpaceSize bytes starting at
80// a fixed address (kSpaceBeg). Both constants are powers of two and
81// kSpaceBeg is kSpaceSize-aligned.
82//
83// Region: a part of Space dedicated to a single size class.
84// There are kNumClasses Regions of equal size.
85//
86// UserChunk: a piece of memory returned to user.
87// MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk.
88//
89// A Region looks like this:
90// UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1
91template <const uptr kSpaceBeg, const uptr kSpaceSize,
92 const uptr kMetadataSize, class SizeClassMap>
93class SizeClassAllocator64 {
94 public:
95 void Init() {
96 CHECK_EQ(AllocBeg(), reinterpret_cast<uptr>(MmapFixedNoReserve(
97 AllocBeg(), AllocSize())));
98 }
Kostya Serebryany278ccda2012-06-22 16:13:28 +000099 NOINLINE
Kostya Serebryany5b014152012-06-22 13:00:50 +0000100 void *Allocate(uptr size) {
101 CHECK_LE(size, SizeClassMap::kMaxSize);
102 return AllocateBySizeClass(SizeClassMap::ClassID(size));
103 }
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000104 NOINLINE
Kostya Serebryany5b014152012-06-22 13:00:50 +0000105 void Deallocate(void *p) {
106 DeallocateBySizeClass(p, GetSizeClass(p));
107 }
108 bool PointerIsMine(void *p) {
109 return reinterpret_cast<uptr>(p) / kSpaceSize == kSpaceBeg / kSpaceSize;
110 }
111 uptr GetSizeClass(void *p) {
112 return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses;
113 }
114
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000115 uptr GetMetaData(void *p) {
116 uptr class_id = GetSizeClass(p);
117 uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), class_id);
118 return kSpaceBeg + (kRegionSize * (class_id + 1)) -
119 (1 + chunk_idx) * kMetadataSize;
120 }
121
Kostya Serebryany5b014152012-06-22 13:00:50 +0000122 uptr TotalMemoryUsedIncludingFreeLists() {
123 uptr res = 0;
124 for (uptr i = 0; i < kNumClasses; i++)
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000125 res += GetRegionInfo(i)->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000126 return res;
127 }
128
129 // Test-only.
130 void TestOnlyUnmap() {
131 UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
132 }
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000133
Kostya Serebryany5b014152012-06-22 13:00:50 +0000134 private:
135 static const uptr kNumClasses = 256; // Power of two <= 256
136 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
137 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000138 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000139 // Populate the free list with at most this number of bytes at once
140 // or with one element if its size is greater.
141 static const uptr kPopulateSize = 1 << 18;
142
143 struct LifoListNode {
144 LifoListNode *next;
145 };
146
147 struct RegionInfo {
148 uptr mutex; // FIXME
149 LifoListNode *free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000150 uptr allocated_user; // Bytes allocated for user memory.
151 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000152 char padding[kCacheLineSize -
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000153 sizeof(mutex) - sizeof(free_list) -
154 sizeof(allocated_user) - sizeof(allocated_meta)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000155 };
156 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
157
158 uptr AdditionalSize() { return sizeof(RegionInfo) * kNumClasses; }
159 uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
160 uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
161
162 RegionInfo *GetRegionInfo(uptr class_id) {
163 CHECK_LT(class_id, kNumClasses);
164 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
165 return &regions[-1 - class_id];
166 }
167
168 void PushLifoList(LifoListNode **list, LifoListNode *node) {
169 node->next = *list;
170 *list = node;
171 }
172
173 LifoListNode *PopLifoList(LifoListNode **list) {
174 LifoListNode *res = *list;
175 *list = (*list)->next;
176 return res;
177 }
178
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000179 uptr GetChunkIdx(uptr chunk, uptr class_id) {
180 u32 offset = chunk % kRegionSize;
181 // Here we divide by a non-constant. This is costly.
182 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
183 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
184 return offset / (u32)SizeClassMap::Size(class_id);
185 }
186
Kostya Serebryany5b014152012-06-22 13:00:50 +0000187 LifoListNode *PopulateFreeList(uptr class_id, RegionInfo *region) {
188 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000189 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000190 uptr end_idx = beg_idx + kPopulateSize;
191 LifoListNode *res = 0;
192 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
193 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000194 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000195 do { // do-while loop because we need to put at least one item.
196 uptr p = region_beg + idx;
197 PushLifoList(&res, reinterpret_cast<LifoListNode*>(p));
198 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000199 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000200 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000201 region->allocated_user += idx - beg_idx;
202 region->allocated_meta += i * kMetadataSize;
203 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000204 return res;
205 }
206
207 void *AllocateBySizeClass(uptr class_id) {
208 CHECK_LT(class_id, kNumClasses);
209 RegionInfo *region = GetRegionInfo(class_id);
210 // FIXME: Lock region->mutex;
211 if (!region->free_list) {
212 region->free_list = PopulateFreeList(class_id, region);
213 }
214 CHECK_NE(region->free_list, 0);
215 LifoListNode *node = PopLifoList(&region->free_list);
216 return reinterpret_cast<void*>(node);
217 }
218
219 void DeallocateBySizeClass(void *p, uptr class_id) {
220 RegionInfo *region = GetRegionInfo(class_id);
221 // FIXME: Lock region->mutex;
222 PushLifoList(&region->free_list, reinterpret_cast<LifoListNode*>(p));
223 }
224};
225
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000226} // namespace __sanitizer
227
228#endif // SANITIZER_ALLOCATOR_H