blob: 2129681210e5da46e7c1d0c326c596ccaaa3b9a2 [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
25// Maps size class to size and back.
26class 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;
56
57 COMPILER_CHECK(kNumClasses <= 256);
58 COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0);
59
60 static uptr Size(uptr size_class) {
61 if (size_class <= u0) return l0 + s0 * (size_class - 0);
62 if (size_class <= u1) return l1 + s1 * (size_class - u0);
63 if (size_class <= u2) return l2 + s2 * (size_class - u1);
64 if (size_class <= u3) return l3 + s3 * (size_class - u2);
65 if (size_class <= u4) return l4 + s4 * (size_class - u3);
66 return 0;
67 }
68 static uptr Class(uptr size) {
69 if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0;
70 if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1;
71 if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2;
72 if (size <= l4) return u2 + (size - l3 + s3 - 1) / s3;
73 if (size <= l5) return u3 + (size - l4 + s4 - 1) / s4;
74 return 0;
75 }
76};
77
78} // namespace __sanitizer
79
80#endif // SANITIZER_ALLOCATOR_H