blob: 3ab5df4cab6156f11c11eb2f49a76d7e85528d65 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
2 * Copyright (C) 2013 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
17#ifndef ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_
19
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080020#include "object_callbacks.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070021#include "space.h"
22
23namespace art {
24namespace gc {
25
26namespace collector {
27 class MarkSweep;
28} // namespace collector
29
30namespace space {
31
Ian Rogers6fac4472014-02-25 17:01:10 -080032// A bump pointer space allocates by incrementing a pointer, it doesn't provide a free
33// implementation as its intended to be evacuated.
34class BumpPointerSpace FINAL : public ContinuousMemMapAllocSpace {
Mathieu Chartier590fee92013-09-13 13:46:47 -070035 public:
36 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
37
Ian Rogers6fac4472014-02-25 17:01:10 -080038 SpaceType GetType() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070039 return kSpaceTypeBumpPointerSpace;
40 }
41
42 // Create a bump pointer space with the requested sizes. The requested base address is not
43 // guaranteed to be granted, if it is required, the caller should call Begin on the returned
44 // space to confirm the request was granted.
45 static BumpPointerSpace* Create(const std::string& name, size_t capacity, byte* requested_begin);
46
47 // Allocate num_bytes, returns nullptr if the space is full.
Ian Rogers6fac4472014-02-25 17:01:10 -080048 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
49 size_t* usable_size) OVERRIDE;
Mathieu Chartier590fee92013-09-13 13:46:47 -070050 mirror::Object* AllocNonvirtual(size_t num_bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080051 mirror::Object* AllocNonvirtualWithoutAccounting(size_t num_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -070052
53 // Return the storage space required by obj.
Ian Rogers6fac4472014-02-25 17:01:10 -080054 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE
55 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
56 return AllocationSizeNonvirtual(obj, usable_size);
57 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070058
Mathieu Chartier692fafd2013-11-29 17:24:40 -080059 // NOPS unless we support free lists.
Ian Rogers6fac4472014-02-25 17:01:10 -080060 size_t Free(Thread*, mirror::Object*) OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 return 0;
62 }
63
Ian Rogers6fac4472014-02-25 17:01:10 -080064 size_t FreeList(Thread*, size_t, mirror::Object**) OVERRIDE {
65 return 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -070066 }
67
Ian Rogers6fac4472014-02-25 17:01:10 -080068 size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
72 // maximum reserved size of the heap.
73 void ClearGrowthLimit() {
74 growth_end_ = Limit();
75 }
76
77 // Override capacity so that we only return the possibly limited capacity
78 size_t Capacity() const {
79 return growth_end_ - begin_;
80 }
81
82 // The total amount of memory reserved for the space.
83 size_t NonGrowthLimitCapacity() const {
84 return GetMemMap()->Size();
85 }
86
Ian Rogers6fac4472014-02-25 17:01:10 -080087 accounting::SpaceBitmap* GetLiveBitmap() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070088 return nullptr;
89 }
90
Ian Rogers6fac4472014-02-25 17:01:10 -080091 accounting::SpaceBitmap* GetMarkBitmap() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070092 return nullptr;
93 }
94
Mathieu Chartier15d34022014-02-26 17:16:38 -080095 // Madvise the memory back to the OS.
96 void Clear() OVERRIDE;
97
98 // Reset the pointer to the start of the space.
99 void Reset() OVERRIDE LOCKS_EXCLUDED(block_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700100
101 void Dump(std::ostream& os) const;
102
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800103 void RevokeThreadLocalBuffers(Thread* thread) LOCKS_EXCLUDED(block_lock_);
104 void RevokeAllThreadLocalBuffers() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_,
105 Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700106 void AssertThreadLocalBuffersAreRevoked(Thread* thread) LOCKS_EXCLUDED(block_lock_);
107 void AssertAllThreadLocalBuffersAreRevoked() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_,
108 Locks::thread_list_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700109
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800110 uint64_t GetBytesAllocated() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111 uint64_t GetObjectsAllocated() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800112 bool IsEmpty() const {
113 return Begin() == End();
114 }
115
Mathieu Chartier590fee92013-09-13 13:46:47 -0700116
117 bool Contains(const mirror::Object* obj) const {
118 const byte* byte_obj = reinterpret_cast<const byte*>(obj);
119 return byte_obj >= Begin() && byte_obj < End();
120 }
121
122 // TODO: Change this? Mainly used for compacting to a particular region of memory.
123 BumpPointerSpace(const std::string& name, byte* begin, byte* limit);
124
125 // Return the object which comes after obj, while ensuring alignment.
126 static mirror::Object* GetNextObject(mirror::Object* obj)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800129 // Allocate a new TLAB, returns false if the allocation failed.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800130 bool AllocNewTlab(Thread* self, size_t bytes);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800131
Ian Rogers6fac4472014-02-25 17:01:10 -0800132 BumpPointerSpace* AsBumpPointerSpace() OVERRIDE {
Mathieu Chartier7410f292013-11-24 13:17:35 -0800133 return this;
134 }
135
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800136 // Go through all of the blocks and visit the continuous objects.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800137 void Walk(ObjectCallback* callback, void* arg)
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139
Ian Rogers6fac4472014-02-25 17:01:10 -0800140 accounting::SpaceBitmap::SweepCallback* GetSweepCallback() OVERRIDE;
141
Mathieu Chartier7410f292013-11-24 13:17:35 -0800142 // Object alignment within the space.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800143 static constexpr size_t kAlignment = 8;
144
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145 protected:
146 BumpPointerSpace(const std::string& name, MemMap* mem_map);
147
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800148 // Allocate a raw block of bytes.
149 byte* AllocBlock(size_t bytes) EXCLUSIVE_LOCKS_REQUIRED(block_lock_);
150 void RevokeThreadLocalBuffersLocked(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(block_lock_);
151
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800152 // The main block is an unbounded block where objects go when there are no other blocks. This
153 // enables us to maintain tightly packed objects when you are not using thread local buffers for
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800154 // allocation. The main block starts at the space Begin().
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800155 void UpdateMainBlock() EXCLUSIVE_LOCKS_REQUIRED(block_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700156
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157 byte* growth_end_;
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800158 AtomicInteger objects_allocated_; // Accumulated from revoked thread local regions.
159 AtomicInteger bytes_allocated_; // Accumulated from revoked thread local regions.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800160 Mutex block_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartierfc4c27e2014-02-11 11:05:41 -0800161 // The objects at the start of the space are stored in the main block. The main block doesn't
162 // have a header, this lets us walk empty spaces which are mprotected.
163 size_t main_block_size_ GUARDED_BY(block_lock_);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 // The number of blocks in the space, if it is 0 then the space has one long continuous block
165 // which doesn't have an updated header.
166 size_t num_blocks_ GUARDED_BY(block_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167
168 private:
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 struct BlockHeader {
170 size_t size_; // Size of the block in bytes, does not include the header.
171 size_t unused_; // Ensures alignment of kAlignment.
172 };
173
174 COMPILE_ASSERT(sizeof(BlockHeader) % kAlignment == 0,
175 continuous_block_must_be_kAlignment_aligned);
176
Mathieu Chartier590fee92013-09-13 13:46:47 -0700177 friend class collector::MarkSweep;
178 DISALLOW_COPY_AND_ASSIGN(BumpPointerSpace);
179};
180
181} // namespace space
182} // namespace gc
183} // namespace art
184
185#endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_