blob: ef889d42c2bf6868d71cacfa7229551f7a5bc6e5 [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
2 * Copyright (C) 2012 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070019
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070020#include "gc/accounting/gc_allocator.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "dlmalloc_space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "safe_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25#include <set>
26#include <vector>
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070027
28namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070031
Ian Rogers22a20862013-03-16 16:34:57 -070032// Abstraction implemented by all large object spaces.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070033class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace {
34 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070035 virtual SpaceType GetType() const {
36 return kSpaceTypeLargeObjectSpace;
37 }
38
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070039 virtual void SwapBitmaps();
40 virtual void CopyLiveToMarked();
41 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0;
42 virtual ~LargeObjectSpace() {}
43
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070044 uint64_t GetBytesAllocated() {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070045 return num_bytes_allocated_;
46 }
47
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070048 uint64_t GetObjectsAllocated() {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070049 return num_objects_allocated_;
50 }
51
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070052 uint64_t GetTotalBytesAllocated() {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070053 return total_bytes_allocated_;
54 }
55
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070056 uint64_t GetTotalObjectsAllocated() {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070057 return total_objects_allocated_;
58 }
59
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070061
62 protected:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070063 explicit LargeObjectSpace(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070064
65 // Approximate number of bytes which have been allocated into the space.
66 size_t num_bytes_allocated_;
67 size_t num_objects_allocated_;
68 size_t total_bytes_allocated_;
69 size_t total_objects_allocated_;
70
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070071 friend class Space;
Ian Rogers1d54e732013-05-02 21:10:01 -070072
73 private:
74 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070075};
76
Ian Rogers22a20862013-03-16 16:34:57 -070077// A discontinuous large object space implemented by individual mmap/munmap calls.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070078class LargeObjectMapSpace : public LargeObjectSpace {
79 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070080 // Creates a large object space. Allocations into the large object space use memory maps instead
81 // of malloc.
82 static LargeObjectMapSpace* Create(const std::string& name);
83
84 // Return the storage space required by obj.
Ian Rogers1d54e732013-05-02 21:10:01 -070085 size_t AllocationSize(const mirror::Object* obj);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070086 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 size_t Free(Thread* self, mirror::Object* ptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070088 void Walk(DlMallocSpace::WalkCallback, void* arg) LOCKS_EXCLUDED(lock_);
Ian Rogersa3dd0b32013-03-19 19:30:59 -070089 // TODO: disabling thread safety analysis as this may be called when we already hold lock_.
Ian Rogers1d54e732013-05-02 21:10:01 -070090 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS;
91
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070092 private:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070093 explicit LargeObjectMapSpace(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070094 virtual ~LargeObjectMapSpace() {}
95
96 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
Ian Rogers22a20862013-03-16 16:34:57 -070097 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070098 std::vector<mirror::Object*,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070099 accounting::GcAllocator<mirror::Object*> > large_objects_ GUARDED_BY(lock_);
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700100 typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101 accounting::GcAllocator<std::pair<const mirror::Object*, MemMap*> > > MemMaps;
Ian Rogers22a20862013-03-16 16:34:57 -0700102 MemMaps mem_maps_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700103};
104
Ian Rogers22a20862013-03-16 16:34:57 -0700105// A continuous large object space with a free-list to handle holes.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700106class FreeListSpace : public LargeObjectSpace {
107 public:
108 virtual ~FreeListSpace();
109 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
110
Ian Rogers22a20862013-03-16 16:34:57 -0700111 size_t AllocationSize(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700112 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 size_t Free(Thread* self, mirror::Object* obj);
114 bool Contains(const mirror::Object* obj) const;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700115 void Walk(DlMallocSpace::WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700116
Ian Rogers22a20862013-03-16 16:34:57 -0700117 // Address at which the space begins.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118 byte* Begin() const {
119 return begin_;
120 }
121
122 // Address at which the space ends, which may vary as the space is filled.
123 byte* End() const {
124 return end_;
125 }
126
127 // Current size of space
128 size_t Size() const {
129 return End() - Begin();
130 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700131
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 void Dump(std::ostream& os) const;
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700133
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700134 private:
135 static const size_t kAlignment = kPageSize;
136
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700137 class AllocationHeader {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700138 public:
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700139 // Returns the allocation size, includes the header.
140 size_t AllocationSize() const {
141 return alloc_size_;
142 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700143
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700144 // Updates the allocation size in the header, the allocation size includes the header itself.
145 void SetAllocationSize(size_t size) {
146 DCHECK(IsAligned<kPageSize>(size));
147 alloc_size_ = size;
148 }
149
150 bool IsFree() const {
151 return AllocationSize() == 0;
152 }
153
154 // Returns the previous free allocation header by using the prev_free_ member to figure out
155 // where it is. If prev free is 0 then we just return ourself.
156 AllocationHeader* GetPrevFreeAllocationHeader() {
157 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) - prev_free_);
158 }
159
160 // Returns the address of the object associated with this allocation header.
161 mirror::Object* GetObjectAddress() {
162 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
163 }
164
165 // Returns the next allocation header after the object associated with this allocation header.
166 AllocationHeader* GetNextAllocationHeader() {
167 DCHECK_NE(alloc_size_, 0U);
168 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) + alloc_size_);
169 }
170
171 // Returns how many free bytes there is before the block.
172 size_t GetPrevFree() const {
173 return prev_free_;
174 }
175
176 // Update the size of the free block prior to the allocation.
177 void SetPrevFree(size_t prev_free) {
178 DCHECK(IsAligned<kPageSize>(prev_free));
179 prev_free_ = prev_free;
180 }
181
182 // Finds and returns the next non free allocation header after ourself.
183 // TODO: Optimize, currently O(n) for n free following pages.
184 AllocationHeader* GetNextNonFree();
185
186 // Used to implement best fit object allocation. Each allocation has an AllocationHeader which
187 // contains the size of the previous free block preceding it. Implemented in such a way that we
188 // can also find the iterator for any allocation header pointer.
189 class SortByPrevFree {
190 public:
191 bool operator()(const AllocationHeader* a, const AllocationHeader* b) const {
192 if (a->GetPrevFree() < b->GetPrevFree()) return true;
193 if (a->GetPrevFree() > b->GetPrevFree()) return false;
194 if (a->AllocationSize() < b->AllocationSize()) return true;
195 if (a->AllocationSize() > b->AllocationSize()) return false;
196 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700197 }
198 };
199
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700200 private:
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700201 // Contains the size of the previous free block, if 0 then the memory preceding us is an
202 // allocation.
203 size_t prev_free_;
204
205 // Allocation size of this object, 0 means that the allocation header is free memory.
206 size_t alloc_size_;
207
208 friend class FreeListSpace;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700209 };
210
211 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700212
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700213 // Removes header from the free blocks set by finding the corresponding iterator and erasing it.
214 void RemoveFreePrev(AllocationHeader* header) EXCLUSIVE_LOCKS_REQUIRED(lock_);
215
216 // Finds the allocation header corresponding to obj.
217 AllocationHeader* GetAllocationHeader(const mirror::Object* obj);
218
219 typedef std::set<AllocationHeader*, AllocationHeader::SortByPrevFree,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 accounting::GcAllocator<AllocationHeader*> > FreeBlocks;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700221
Ian Rogers22a20862013-03-16 16:34:57 -0700222 byte* const begin_;
223 byte* const end_;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700224
225 // There is not footer for any allocations at the end of the space, so we keep track of how much
226 // free space there is at the end manually.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700227 UniquePtr<MemMap> mem_map_;
Ian Rogers22a20862013-03-16 16:34:57 -0700228 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700229 size_t free_end_ GUARDED_BY(lock_);
230 FreeBlocks free_blocks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700231};
232
Ian Rogers1d54e732013-05-02 21:10:01 -0700233} // namespace space
234} // namespace gc
235} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700236
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700237#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_