blob: 55044b34e5f37368b895055502e1658858adf9ac [file] [log] [blame]
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#ifndef ART_RUNTIME_BASE_SCOPED_ARENA_ALLOCATOR_H_
18#define ART_RUNTIME_BASE_SCOPED_ARENA_ALLOCATOR_H_
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000019
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "arena_allocator.h"
21#include "debug_stack.h"
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000022#include "globals.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080023#include "logging.h"
24#include "macros.h"
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000025
26namespace art {
27
28class ArenaStack;
29class ScopedArenaAllocator;
30
31template <typename T>
32class ScopedArenaAllocatorAdapter;
33
Mathieu Chartier7b05e172015-10-15 17:47:48 -070034// Tag associated with each allocation to help prevent double free.
35enum class ArenaFreeTag : uint8_t {
36 // Allocation is used and has not yet been destroyed.
37 kUsed,
38 // Allocation has been destroyed.
39 kFree,
40};
41
42static constexpr size_t kArenaAlignment = 8;
43
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000044// Holds a list of Arenas for use by ScopedArenaAllocator stack.
Vladimir Markofda04322015-11-11 18:45:50 +000045// The memory is returned to the ArenaPool when the ArenaStack is destroyed.
Vladimir Marko2a408a32015-09-18 14:11:00 +010046class ArenaStack : private DebugStackRefCounter, private ArenaAllocatorMemoryTool {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000047 public:
48 explicit ArenaStack(ArenaPool* arena_pool);
49 ~ArenaStack();
50
Vladimir Marko2a408a32015-09-18 14:11:00 +010051 using ArenaAllocatorMemoryTool::IsRunningOnMemoryTool;
52 using ArenaAllocatorMemoryTool::MakeDefined;
53 using ArenaAllocatorMemoryTool::MakeUndefined;
54 using ArenaAllocatorMemoryTool::MakeInaccessible;
55
Vladimir Marko53b6afc2014-03-21 14:21:20 +000056 void Reset();
57
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000058 size_t PeakBytesAllocated() {
59 return PeakStats()->BytesAllocated();
60 }
61
62 MemStats GetPeakStats() const;
63
Mathieu Chartier7b05e172015-10-15 17:47:48 -070064 // Return the arena tag associated with a pointer.
65 static ArenaFreeTag& ArenaTagForAllocation(void* ptr) {
66 DCHECK(kIsDebugBuild) << "Only debug builds have tags";
67 return *(reinterpret_cast<ArenaFreeTag*>(ptr) - 1);
68 }
69
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000070 private:
71 struct Peak;
72 struct Current;
73 template <typename Tag> struct TaggedStats : ArenaAllocatorStats { };
74 struct StatsAndPool : TaggedStats<Peak>, TaggedStats<Current> {
75 explicit StatsAndPool(ArenaPool* arena_pool) : pool(arena_pool) { }
76 ArenaPool* const pool;
77 };
78
79 ArenaAllocatorStats* PeakStats() {
80 return static_cast<TaggedStats<Peak>*>(&stats_and_pool_);
81 }
82
83 ArenaAllocatorStats* CurrentStats() {
84 return static_cast<TaggedStats<Current>*>(&stats_and_pool_);
85 }
86
87 // Private - access via ScopedArenaAllocator or ScopedArenaAllocatorAdapter.
88 void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE {
Vladimir Marko2a408a32015-09-18 14:11:00 +010089 if (UNLIKELY(IsRunningOnMemoryTool())) {
90 return AllocWithMemoryTool(bytes, kind);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000091 }
Mathieu Chartier7b05e172015-10-15 17:47:48 -070092 // Add kArenaAlignment for the free or used tag. Required to preserve alignment.
93 size_t rounded_bytes = RoundUp(bytes + (kIsDebugBuild ? kArenaAlignment : 0u), kArenaAlignment);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000094 uint8_t* ptr = top_ptr_;
95 if (UNLIKELY(static_cast<size_t>(top_end_ - ptr) < rounded_bytes)) {
96 ptr = AllocateFromNextArena(rounded_bytes);
97 }
98 CurrentStats()->RecordAlloc(bytes, kind);
99 top_ptr_ = ptr + rounded_bytes;
Mathieu Chartier7b05e172015-10-15 17:47:48 -0700100 if (kIsDebugBuild) {
101 ptr += kArenaAlignment;
102 ArenaTagForAllocation(ptr) = ArenaFreeTag::kUsed;
103 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104 return ptr;
105 }
106
107 uint8_t* AllocateFromNextArena(size_t rounded_bytes);
108 void UpdatePeakStatsAndRestore(const ArenaAllocatorStats& restore_stats);
109 void UpdateBytesAllocated();
Vladimir Marko2a408a32015-09-18 14:11:00 +0100110 void* AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000111
112 StatsAndPool stats_and_pool_;
113 Arena* bottom_arena_;
114 Arena* top_arena_;
115 uint8_t* top_ptr_;
116 uint8_t* top_end_;
117
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000118 friend class ScopedArenaAllocator;
119 template <typename T>
120 friend class ScopedArenaAllocatorAdapter;
121
122 DISALLOW_COPY_AND_ASSIGN(ArenaStack);
123};
124
Vladimir Markofda04322015-11-11 18:45:50 +0000125// Fast single-threaded allocator. Allocated chunks are _not_ guaranteed to be zero-initialized.
126//
127// Unlike the ArenaAllocator, ScopedArenaAllocator is intended for relatively short-lived
128// objects and allows nesting multiple allocators. Only the top allocator can be used but
129// once it's destroyed, its memory can be reused by the next ScopedArenaAllocator on the
130// stack. This is facilitated by returning the memory to the ArenaStack.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000131class ScopedArenaAllocator
132 : private DebugStackReference, private DebugStackRefCounter, private ArenaAllocatorStats {
133 public:
134 // Create a ScopedArenaAllocator directly on the ArenaStack when the scope of
135 // the allocator is not exactly a C++ block scope. For example, an optimization
136 // pass can create the scoped allocator in Start() and destroy it in End().
137 static ScopedArenaAllocator* Create(ArenaStack* arena_stack) {
138 void* addr = arena_stack->Alloc(sizeof(ScopedArenaAllocator), kArenaAllocMisc);
139 ScopedArenaAllocator* allocator = new(addr) ScopedArenaAllocator(arena_stack);
140 allocator->mark_ptr_ = reinterpret_cast<uint8_t*>(addr);
141 return allocator;
142 }
143
144 explicit ScopedArenaAllocator(ArenaStack* arena_stack);
145 ~ScopedArenaAllocator();
146
147 void Reset();
148
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000149 void* Alloc(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000150 DebugStackReference::CheckTop();
151 return arena_stack_->Alloc(bytes, kind);
152 }
153
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000154 template <typename T>
Vladimir Markof6a35de2016-03-21 12:01:50 +0000155 T* Alloc(ArenaAllocKind kind = kArenaAllocMisc) {
156 return AllocArray<T>(1, kind);
157 }
158
159 template <typename T>
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000160 T* AllocArray(size_t length, ArenaAllocKind kind = kArenaAllocMisc) {
161 return static_cast<T*>(Alloc(length * sizeof(T), kind));
162 }
163
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100164 // Get adapter for use in STL containers. See scoped_arena_containers.h .
165 ScopedArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000166
167 // Allow a delete-expression to destroy but not deallocate allocators created by Create().
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100168 static void operator delete(void* ptr ATTRIBUTE_UNUSED) {}
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000169
170 private:
171 ArenaStack* const arena_stack_;
172 Arena* mark_arena_;
173 uint8_t* mark_ptr_;
174 uint8_t* mark_end_;
175
Vladimir Marko3d2ec352014-10-10 15:39:11 +0100176 void DoReset();
177
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000178 template <typename T>
179 friend class ScopedArenaAllocatorAdapter;
180
181 DISALLOW_COPY_AND_ASSIGN(ScopedArenaAllocator);
182};
183
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000184} // namespace art
185
Mathieu Chartierb666f482015-02-18 14:33:14 -0800186#endif // ART_RUNTIME_BASE_SCOPED_ARENA_ALLOCATOR_H_