Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 20 | #include "object_callbacks.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 21 | #include "space.h" |
| 22 | |
| 23 | namespace art { |
| 24 | namespace gc { |
| 25 | |
| 26 | namespace collector { |
| 27 | class MarkSweep; |
| 28 | } // namespace collector |
| 29 | |
| 30 | namespace space { |
| 31 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 32 | // A bump pointer space allocates by incrementing a pointer, it doesn't provide a free |
| 33 | // implementation as its intended to be evacuated. |
| 34 | class BumpPointerSpace FINAL : public ContinuousMemMapAllocSpace { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 35 | public: |
| 36 | typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg); |
| 37 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 38 | SpaceType GetType() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 39 | 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); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 46 | static BumpPointerSpace* CreateFromMemMap(const std::string& name, MemMap* mem_map); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | |
| 48 | // Allocate num_bytes, returns nullptr if the space is full. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 49 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 50 | size_t* usable_size) OVERRIDE; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 51 | mirror::Object* AllocNonvirtual(size_t num_bytes); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 52 | mirror::Object* AllocNonvirtualWithoutAccounting(size_t num_bytes); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 53 | |
| 54 | // Return the storage space required by obj. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 55 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE |
| 56 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 57 | return AllocationSizeNonvirtual(obj, usable_size); |
| 58 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 59 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 60 | // NOPS unless we support free lists. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 61 | size_t Free(Thread*, mirror::Object*) OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 65 | size_t FreeList(Thread*, size_t, mirror::Object**) OVERRIDE { |
| 66 | return 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 69 | size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) |
| 70 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 71 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 72 | // Removes the fork time growth limit on capacity, allowing the application to allocate up to the |
| 73 | // maximum reserved size of the heap. |
| 74 | void ClearGrowthLimit() { |
| 75 | growth_end_ = Limit(); |
| 76 | } |
| 77 | |
| 78 | // Override capacity so that we only return the possibly limited capacity |
| 79 | size_t Capacity() const { |
| 80 | return growth_end_ - begin_; |
| 81 | } |
| 82 | |
| 83 | // The total amount of memory reserved for the space. |
| 84 | size_t NonGrowthLimitCapacity() const { |
| 85 | return GetMemMap()->Size(); |
| 86 | } |
| 87 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 88 | accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 92 | accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 93 | return nullptr; |
| 94 | } |
| 95 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 96 | // Reset the space to empty. |
| 97 | void Clear() OVERRIDE LOCKS_EXCLUDED(block_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 98 | |
| 99 | void Dump(std::ostream& os) const; |
| 100 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 101 | void RevokeThreadLocalBuffers(Thread* thread) LOCKS_EXCLUDED(block_lock_); |
| 102 | void RevokeAllThreadLocalBuffers() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, |
| 103 | Locks::thread_list_lock_); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 104 | void AssertThreadLocalBuffersAreRevoked(Thread* thread) LOCKS_EXCLUDED(block_lock_); |
| 105 | void AssertAllThreadLocalBuffersAreRevoked() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, |
| 106 | Locks::thread_list_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 107 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 108 | uint64_t GetBytesAllocated() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 109 | uint64_t GetObjectsAllocated() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 110 | bool IsEmpty() const { |
| 111 | return Begin() == End(); |
| 112 | } |
| 113 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 114 | bool CanMoveObjects() const OVERRIDE { |
| 115 | return true; |
| 116 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 117 | |
| 118 | bool Contains(const mirror::Object* obj) const { |
| 119 | const byte* byte_obj = reinterpret_cast<const byte*>(obj); |
| 120 | return byte_obj >= Begin() && byte_obj < End(); |
| 121 | } |
| 122 | |
| 123 | // TODO: Change this? Mainly used for compacting to a particular region of memory. |
| 124 | BumpPointerSpace(const std::string& name, byte* begin, byte* limit); |
| 125 | |
| 126 | // Return the object which comes after obj, while ensuring alignment. |
| 127 | static mirror::Object* GetNextObject(mirror::Object* obj) |
| 128 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 129 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 130 | // Allocate a new TLAB, returns false if the allocation failed. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 131 | bool AllocNewTlab(Thread* self, size_t bytes); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 132 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 133 | BumpPointerSpace* AsBumpPointerSpace() OVERRIDE { |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 134 | return this; |
| 135 | } |
| 136 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 137 | // Go through all of the blocks and visit the continuous objects. |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 138 | void Walk(ObjectCallback* callback, void* arg) |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 139 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 140 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 141 | accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() OVERRIDE; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 142 | |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 143 | // Object alignment within the space. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 144 | static constexpr size_t kAlignment = 8; |
| 145 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 146 | protected: |
| 147 | BumpPointerSpace(const std::string& name, MemMap* mem_map); |
| 148 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 149 | // Allocate a raw block of bytes. |
| 150 | byte* AllocBlock(size_t bytes) EXCLUSIVE_LOCKS_REQUIRED(block_lock_); |
| 151 | void RevokeThreadLocalBuffersLocked(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(block_lock_); |
| 152 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 153 | // The main block is an unbounded block where objects go when there are no other blocks. This |
| 154 | // enables us to maintain tightly packed objects when you are not using thread local buffers for |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 155 | // allocation. The main block starts at the space Begin(). |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 156 | void UpdateMainBlock() EXCLUSIVE_LOCKS_REQUIRED(block_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 157 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 158 | byte* growth_end_; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 159 | AtomicInteger objects_allocated_; // Accumulated from revoked thread local regions. |
| 160 | AtomicInteger bytes_allocated_; // Accumulated from revoked thread local regions. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 161 | Mutex block_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 162 | // The objects at the start of the space are stored in the main block. The main block doesn't |
| 163 | // have a header, this lets us walk empty spaces which are mprotected. |
| 164 | size_t main_block_size_ GUARDED_BY(block_lock_); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 165 | // The number of blocks in the space, if it is 0 then the space has one long continuous block |
| 166 | // which doesn't have an updated header. |
| 167 | size_t num_blocks_ GUARDED_BY(block_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 168 | |
| 169 | private: |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 170 | struct BlockHeader { |
| 171 | size_t size_; // Size of the block in bytes, does not include the header. |
| 172 | size_t unused_; // Ensures alignment of kAlignment. |
| 173 | }; |
| 174 | |
| 175 | COMPILE_ASSERT(sizeof(BlockHeader) % kAlignment == 0, |
| 176 | continuous_block_must_be_kAlignment_aligned); |
| 177 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 178 | friend class collector::MarkSweep; |
| 179 | DISALLOW_COPY_AND_ASSIGN(BumpPointerSpace); |
| 180 | }; |
| 181 | |
| 182 | } // namespace space |
| 183 | } // namespace gc |
| 184 | } // namespace art |
| 185 | |
| 186 | #endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_ |