blob: 9c3dce97a091dcb960b4eca9dcbedc095ef2aee1 [file] [log] [blame]
Sergey Matveev866abfb2013-05-20 10:54:00 +00001//=-- lsan_allocator.h ----------------------------------------------------===//
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 LeakSanitizer.
11// Allocator for standalone LSan.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LSAN_ALLOCATOR_H
16#define LSAN_ALLOCATOR_H
17
Francis Riccid668a012017-03-27 14:07:50 +000018#include "sanitizer_common/sanitizer_allocator.h"
Sergey Matveev866abfb2013-05-20 10:54:00 +000019#include "sanitizer_common/sanitizer_common.h"
20#include "sanitizer_common/sanitizer_internal_defs.h"
Francis Riccid668a012017-03-27 14:07:50 +000021#include "lsan_common.h"
Sergey Matveev866abfb2013-05-20 10:54:00 +000022
23namespace __lsan {
24
25void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
26 bool cleared);
27void Deallocate(void *p);
28void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
29 uptr alignment);
Sergey Matveev08347ca2014-08-26 14:28:28 +000030uptr GetMallocUsableSize(const void *p);
Sergey Matveev866abfb2013-05-20 10:54:00 +000031
32template<typename Callable>
33void ForEachChunk(const Callable &callback);
34
35void GetAllocatorCacheRange(uptr *begin, uptr *end);
36void AllocatorThreadFinish();
37void InitializeAllocator();
38
Francis Riccid668a012017-03-27 14:07:50 +000039struct ChunkMetadata {
40 u8 allocated : 8; // Must be first.
41 ChunkTag tag : 2;
42#if SANITIZER_WORDSIZE == 64
43 uptr requested_size : 54;
44#else
45 uptr requested_size : 32;
46 uptr padding : 22;
47#endif
48 u32 stack_trace_id;
49};
50
51#if defined(__mips64) || defined(__aarch64__) || defined(__i386__)
52static const uptr kRegionSizeLog = 20;
53static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
54typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
55typedef CompactSizeClassMap SizeClassMap;
56typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
57 sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
58 PrimaryAllocator;
59#elif defined(__x86_64__)
60struct AP64 { // Allocator64 parameters. Deliberately using a short name.
61 static const uptr kSpaceBeg = 0x600000000000ULL;
62 static const uptr kSpaceSize = 0x40000000000ULL; // 4T.
63 static const uptr kMetadataSize = sizeof(ChunkMetadata);
64 typedef DefaultSizeClassMap SizeClassMap;
65 typedef NoOpMapUnmapCallback MapUnmapCallback;
66 static const uptr kFlags = 0;
67};
68
69typedef SizeClassAllocator64<AP64> PrimaryAllocator;
70#endif
71typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
72
73AllocatorCache *GetAllocatorCache();
Sergey Matveev866abfb2013-05-20 10:54:00 +000074} // namespace __lsan
75
76#endif // LSAN_ALLOCATOR_H