1 //===-- sanitizer_allocator_combined.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 // Part of the Sanitizer Allocator.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_ALLOCATOR_H
14 #error This file must be included inside sanitizer_allocator.h
17 // This class implements a complete memory allocator by using two
18 // internal allocators:
19 // PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
20 // When allocating 2^x bytes it should return 2^x aligned chunk.
21 // PrimaryAllocator is used via a local AllocatorCache.
22 // SecondaryAllocator can allocate anything, but is not efficient.
23 template <class PrimaryAllocator, class AllocatorCache,
24 class SecondaryAllocator> // NOLINT
25 class CombinedAllocator {
27 void InitCommon(bool may_return_null, s32 release_to_os_interval_ms) {
28 primary_.Init(release_to_os_interval_ms);
29 atomic_store(&may_return_null_, may_return_null, memory_order_relaxed);
32 void InitLinkerInitialized(
33 bool may_return_null, s32 release_to_os_interval_ms) {
34 secondary_.InitLinkerInitialized(may_return_null);
35 stats_.InitLinkerInitialized();
36 InitCommon(may_return_null, release_to_os_interval_ms);
39 void Init(bool may_return_null, s32 release_to_os_interval_ms) {
40 secondary_.Init(may_return_null);
42 InitCommon(may_return_null, release_to_os_interval_ms);
45 void *Allocate(AllocatorCache *cache, uptr size, uptr alignment,
46 bool cleared = false) {
47 // Returning 0 on malloc(0) may break a lot of code.
50 if (size + alignment < size)
51 return ReturnNullOrDieOnBadRequest();
52 uptr original_size = size;
53 // If alignment requirements are to be fulfilled by the frontend allocator
54 // rather than by the primary or secondary, passing an alignment lower than
55 // or equal to 8 will prevent any further rounding up, as well as the later
58 size = RoundUpTo(size, alignment);
60 bool from_primary = primary_.CanAllocate(size, alignment);
61 // The primary allocator should return a 2^x aligned allocation when
62 // requested 2^x bytes, hence using the rounded up 'size' when being
63 // serviced by the primary (this is no longer true when the primary is
64 // using a non-fixed base address). The secondary takes care of the
65 // alignment without such requirement, and allocating 'size' would use
66 // extraneous memory, so we employ 'original_size'.
68 res = cache->Allocate(&primary_, primary_.ClassID(size));
70 res = secondary_.Allocate(&stats_, original_size, alignment);
72 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
73 // When serviced by the secondary, the chunk comes from a mmap allocation
74 // and will be zero'd out anyway. We only need to clear our the chunk if
75 // it was serviced by the primary, hence using the rounded up 'size'.
76 if (cleared && res && from_primary)
77 internal_bzero_aligned16(res, RoundUpTo(size, 16));
81 bool MayReturnNull() const {
82 return atomic_load(&may_return_null_, memory_order_acquire);
85 void *ReturnNullOrDieOnBadRequest() {
88 ReportAllocatorCannotReturnNull(false);
91 void *ReturnNullOrDieOnOOM() {
94 ReportAllocatorCannotReturnNull(true);
97 void SetMayReturnNull(bool may_return_null) {
98 secondary_.SetMayReturnNull(may_return_null);
99 atomic_store(&may_return_null_, may_return_null, memory_order_release);
102 s32 ReleaseToOSIntervalMs() const {
103 return primary_.ReleaseToOSIntervalMs();
106 void SetReleaseToOSIntervalMs(s32 release_to_os_interval_ms) {
107 primary_.SetReleaseToOSIntervalMs(release_to_os_interval_ms);
110 void Deallocate(AllocatorCache *cache, void *p) {
112 if (primary_.PointerIsMine(p))
113 cache->Deallocate(&primary_, primary_.GetSizeClass(p), p);
115 secondary_.Deallocate(&stats_, p);
118 void *Reallocate(AllocatorCache *cache, void *p, uptr new_size,
121 return Allocate(cache, new_size, alignment);
123 Deallocate(cache, p);
126 CHECK(PointerIsMine(p));
127 uptr old_size = GetActuallyAllocatedSize(p);
128 uptr memcpy_size = Min(new_size, old_size);
129 void *new_p = Allocate(cache, new_size, alignment);
131 internal_memcpy(new_p, p, memcpy_size);
132 Deallocate(cache, p);
136 bool PointerIsMine(void *p) {
137 if (primary_.PointerIsMine(p))
139 return secondary_.PointerIsMine(p);
142 bool FromPrimary(void *p) {
143 return primary_.PointerIsMine(p);
146 void *GetMetaData(const void *p) {
147 if (primary_.PointerIsMine(p))
148 return primary_.GetMetaData(p);
149 return secondary_.GetMetaData(p);
152 void *GetBlockBegin(const void *p) {
153 if (primary_.PointerIsMine(p))
154 return primary_.GetBlockBegin(p);
155 return secondary_.GetBlockBegin(p);
158 // This function does the same as GetBlockBegin, but is much faster.
159 // Must be called with the allocator locked.
160 void *GetBlockBeginFastLocked(void *p) {
161 if (primary_.PointerIsMine(p))
162 return primary_.GetBlockBegin(p);
163 return secondary_.GetBlockBeginFastLocked(p);
166 uptr GetActuallyAllocatedSize(void *p) {
167 if (primary_.PointerIsMine(p))
168 return primary_.GetActuallyAllocatedSize(p);
169 return secondary_.GetActuallyAllocatedSize(p);
172 uptr TotalMemoryUsed() {
173 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
176 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
178 void InitCache(AllocatorCache *cache) {
179 cache->Init(&stats_);
182 void DestroyCache(AllocatorCache *cache) {
183 cache->Destroy(&primary_, &stats_);
186 void SwallowCache(AllocatorCache *cache) {
187 cache->Drain(&primary_);
190 void GetStats(AllocatorStatCounters s) const {
195 primary_.PrintStats();
196 secondary_.PrintStats();
199 // ForceLock() and ForceUnlock() are needed to implement Darwin malloc zone
200 // introspection API.
202 primary_.ForceLock();
203 secondary_.ForceLock();
207 secondary_.ForceUnlock();
208 primary_.ForceUnlock();
211 // Iterate over all existing chunks.
212 // The allocator must be locked when calling this function.
213 void ForEachChunk(ForEachChunkCallback callback, void *arg) {
214 primary_.ForEachChunk(callback, arg);
215 secondary_.ForEachChunk(callback, arg);
219 PrimaryAllocator primary_;
220 SecondaryAllocator secondary_;
221 AllocatorGlobalStats stats_;
222 atomic_uint8_t may_return_null_;