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