• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "space.h"
21 
22 namespace art {
23 
24 namespace mirror {
25 class Object;
26 }
27 
28 namespace gc {
29 
30 namespace collector {
31   class MarkSweep;
32 }  // namespace collector
33 
34 namespace space {
35 
36 // A bump pointer space allocates by incrementing a pointer, it doesn't provide a free
37 // implementation as its intended to be evacuated.
38 class BumpPointerSpace FINAL : public ContinuousMemMapAllocSpace {
39  public:
40   typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
41 
GetType()42   SpaceType GetType() const OVERRIDE {
43     return kSpaceTypeBumpPointerSpace;
44   }
45 
46   // Create a bump pointer space with the requested sizes. The requested base address is not
47   // guaranteed to be granted, if it is required, the caller should call Begin on the returned
48   // space to confirm the request was granted.
49   static BumpPointerSpace* Create(const std::string& name, size_t capacity, uint8_t* requested_begin);
50   static BumpPointerSpace* CreateFromMemMap(const std::string& name, MemMap* mem_map);
51 
52   // Allocate num_bytes, returns null if the space is full.
53   mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
54                         size_t* usable_size, size_t* bytes_tl_bulk_allocated) OVERRIDE;
55   // Thread-unsafe allocation for when mutators are suspended, used by the semispace collector.
56   mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated,
57                                     size_t* usable_size, size_t* bytes_tl_bulk_allocated)
58       OVERRIDE REQUIRES(Locks::mutator_lock_);
59 
60   mirror::Object* AllocNonvirtual(size_t num_bytes);
61   mirror::Object* AllocNonvirtualWithoutAccounting(size_t num_bytes);
62 
63   // Return the storage space required by obj.
AllocationSize(mirror::Object * obj,size_t * usable_size)64   size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE
65       REQUIRES_SHARED(Locks::mutator_lock_) {
66     return AllocationSizeNonvirtual(obj, usable_size);
67   }
68 
69   // NOPS unless we support free lists.
Free(Thread *,mirror::Object *)70   size_t Free(Thread*, mirror::Object*) OVERRIDE {
71     return 0;
72   }
73 
FreeList(Thread *,size_t,mirror::Object **)74   size_t FreeList(Thread*, size_t, mirror::Object**) OVERRIDE {
75     return 0;
76   }
77 
78   size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
79       REQUIRES_SHARED(Locks::mutator_lock_);
80 
81   // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
82   // maximum reserved size of the heap.
ClearGrowthLimit()83   void ClearGrowthLimit() {
84     growth_end_ = Limit();
85   }
86 
87   // Override capacity so that we only return the possibly limited capacity
Capacity()88   size_t Capacity() const {
89     return growth_end_ - begin_;
90   }
91 
92   // The total amount of memory reserved for the space.
NonGrowthLimitCapacity()93   size_t NonGrowthLimitCapacity() const {
94     return GetMemMap()->Size();
95   }
96 
GetLiveBitmap()97   accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
98     return nullptr;
99   }
100 
GetMarkBitmap()101   accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
102     return nullptr;
103   }
104 
105   // Reset the space to empty.
106   void Clear() OVERRIDE REQUIRES(!block_lock_);
107 
108   void Dump(std::ostream& os) const;
109 
110   size_t RevokeThreadLocalBuffers(Thread* thread) REQUIRES(!block_lock_);
111   size_t RevokeAllThreadLocalBuffers()
112       REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_, !block_lock_);
113   void AssertThreadLocalBuffersAreRevoked(Thread* thread) REQUIRES(!block_lock_);
114   void AssertAllThreadLocalBuffersAreRevoked()
115       REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_, !block_lock_);
116 
117   uint64_t GetBytesAllocated() REQUIRES_SHARED(Locks::mutator_lock_)
118       REQUIRES(!*Locks::runtime_shutdown_lock_, !*Locks::thread_list_lock_, !block_lock_);
119   uint64_t GetObjectsAllocated() REQUIRES_SHARED(Locks::mutator_lock_)
120       REQUIRES(!*Locks::runtime_shutdown_lock_, !*Locks::thread_list_lock_, !block_lock_);
IsEmpty()121   bool IsEmpty() const {
122     return Begin() == End();
123   }
124 
CanMoveObjects()125   bool CanMoveObjects() const OVERRIDE {
126     return true;
127   }
128 
Contains(const mirror::Object * obj)129   bool Contains(const mirror::Object* obj) const {
130     const uint8_t* byte_obj = reinterpret_cast<const uint8_t*>(obj);
131     return byte_obj >= Begin() && byte_obj < End();
132   }
133 
134   // TODO: Change this? Mainly used for compacting to a particular region of memory.
135   BumpPointerSpace(const std::string& name, uint8_t* begin, uint8_t* limit);
136 
137   // Return the object which comes after obj, while ensuring alignment.
138   static mirror::Object* GetNextObject(mirror::Object* obj)
139       REQUIRES_SHARED(Locks::mutator_lock_);
140 
141   // Allocate a new TLAB, returns false if the allocation failed.
142   bool AllocNewTlab(Thread* self, size_t bytes) REQUIRES(!block_lock_);
143 
AsBumpPointerSpace()144   BumpPointerSpace* AsBumpPointerSpace() OVERRIDE {
145     return this;
146   }
147 
148   // Go through all of the blocks and visit the continuous objects.
149   template <typename Visitor>
150   ALWAYS_INLINE void Walk(Visitor&& visitor)
151       REQUIRES_SHARED(Locks::mutator_lock_)
152       REQUIRES(!block_lock_);
153 
154   accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() OVERRIDE;
155 
156   // Record objects / bytes freed.
RecordFree(int32_t objects,int32_t bytes)157   void RecordFree(int32_t objects, int32_t bytes) {
158     objects_allocated_.FetchAndSubSequentiallyConsistent(objects);
159     bytes_allocated_.FetchAndSubSequentiallyConsistent(bytes);
160   }
161 
162   void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE
163       REQUIRES_SHARED(Locks::mutator_lock_);
164 
165   // Object alignment within the space.
166   static constexpr size_t kAlignment = 8;
167 
168  protected:
169   BumpPointerSpace(const std::string& name, MemMap* mem_map);
170 
171   // Allocate a raw block of bytes.
172   uint8_t* AllocBlock(size_t bytes) REQUIRES(block_lock_);
173   void RevokeThreadLocalBuffersLocked(Thread* thread) REQUIRES(block_lock_);
174 
175   // The main block is an unbounded block where objects go when there are no other blocks. This
176   // enables us to maintain tightly packed objects when you are not using thread local buffers for
177   // allocation. The main block starts at the space Begin().
178   void UpdateMainBlock() REQUIRES(block_lock_);
179 
180   uint8_t* growth_end_;
181   AtomicInteger objects_allocated_;  // Accumulated from revoked thread local regions.
182   AtomicInteger bytes_allocated_;  // Accumulated from revoked thread local regions.
183   Mutex block_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
184   // The objects at the start of the space are stored in the main block. The main block doesn't
185   // have a header, this lets us walk empty spaces which are mprotected.
186   size_t main_block_size_ GUARDED_BY(block_lock_);
187   // The number of blocks in the space, if it is 0 then the space has one long continuous block
188   // which doesn't have an updated header.
189   size_t num_blocks_ GUARDED_BY(block_lock_);
190 
191  private:
192   struct BlockHeader {
193     size_t size_;  // Size of the block in bytes, does not include the header.
194     size_t unused_;  // Ensures alignment of kAlignment.
195   };
196 
197   static_assert(sizeof(BlockHeader) % kAlignment == 0,
198                 "continuous block must be kAlignment aligned");
199 
200   friend class collector::MarkSweep;
201   DISALLOW_COPY_AND_ASSIGN(BumpPointerSpace);
202 };
203 
204 }  // namespace space
205 }  // namespace gc
206 }  // namespace art
207 
208 #endif  // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_
209