blob: 2edd3e2facaeac67afa419b614cea57a98aab505 [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
20#include "space.h"
21
22namespace art {
23namespace gc {
24
25namespace collector {
26 class MarkSweep;
27} // namespace collector
28
29namespace space {
30
31// A bump pointer space is a space where objects may be allocated and garbage collected.
32class BumpPointerSpace : public ContinuousMemMapAllocSpace {
33 public:
34 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
35
36 SpaceType GetType() const {
37 return kSpaceTypeBumpPointerSpace;
38 }
39
40 // Create a bump pointer space with the requested sizes. The requested base address is not
41 // guaranteed to be granted, if it is required, the caller should call Begin on the returned
42 // space to confirm the request was granted.
43 static BumpPointerSpace* Create(const std::string& name, size_t capacity, byte* requested_begin);
44
45 // Allocate num_bytes, returns nullptr if the space is full.
46 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
47 mirror::Object* AllocNonvirtual(size_t num_bytes);
48
49 // Return the storage space required by obj.
50 virtual size_t AllocationSize(const mirror::Object* obj)
51 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
52
53 // Nos unless we support free lists.
54 virtual size_t Free(Thread*, mirror::Object*) {
55 return 0;
56 }
57 virtual size_t FreeList(Thread*, size_t, mirror::Object**) {
58 return 0;
59 }
60
61 size_t AllocationSizeNonvirtual(const mirror::Object* obj)
62 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
63 return obj->SizeOf();
64 }
65
66 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
67 // maximum reserved size of the heap.
68 void ClearGrowthLimit() {
69 growth_end_ = Limit();
70 }
71
72 // Override capacity so that we only return the possibly limited capacity
73 size_t Capacity() const {
74 return growth_end_ - begin_;
75 }
76
77 // The total amount of memory reserved for the space.
78 size_t NonGrowthLimitCapacity() const {
79 return GetMemMap()->Size();
80 }
81
82 accounting::SpaceBitmap* GetLiveBitmap() const {
83 return nullptr;
84 }
85
86 accounting::SpaceBitmap* GetMarkBitmap() const {
87 return nullptr;
88 }
89
90 // Clear the memory and reset the pointer to the start of the space.
91 void Clear();
92
93 void Dump(std::ostream& os) const;
94
95 uint64_t GetBytesAllocated() {
96 return Size();
97 }
98
99 uint64_t GetObjectsAllocated() {
100 return num_objects_allocated_;
101 }
102
103 uint64_t GetTotalBytesAllocated() {
104 return total_bytes_allocated_;
105 }
106
107 uint64_t GetTotalObjectsAllocated() {
108 return total_objects_allocated_;
109 }
110
111 bool Contains(const mirror::Object* obj) const {
112 const byte* byte_obj = reinterpret_cast<const byte*>(obj);
113 return byte_obj >= Begin() && byte_obj < End();
114 }
115
116 // TODO: Change this? Mainly used for compacting to a particular region of memory.
117 BumpPointerSpace(const std::string& name, byte* begin, byte* limit);
118
119 // Return the object which comes after obj, while ensuring alignment.
120 static mirror::Object* GetNextObject(mirror::Object* obj)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
Mathieu Chartier7410f292013-11-24 13:17:35 -0800123 virtual BumpPointerSpace* AsBumpPointerSpace() {
124 return this;
125 }
126
127 // Object alignment within the space.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800128 static constexpr size_t kAlignment = 8;
129
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 protected:
131 BumpPointerSpace(const std::string& name, MemMap* mem_map);
132
133 size_t InternalAllocationSize(const mirror::Object* obj);
134 mirror::Object* AllocWithoutGrowthLocked(size_t num_bytes, size_t* bytes_allocated)
135 EXCLUSIVE_LOCKS_REQUIRED(lock_);
136
137 // Approximate number of bytes which have been allocated into the space.
138 AtomicInteger num_objects_allocated_;
139 AtomicInteger total_bytes_allocated_;
140 AtomicInteger total_objects_allocated_;
141
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142 byte* growth_end_;
143
144 private:
145 friend class collector::MarkSweep;
146 DISALLOW_COPY_AND_ASSIGN(BumpPointerSpace);
147};
148
149} // namespace space
150} // namespace gc
151} // namespace art
152
153#endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_