blob: c34dbcc30c2d0c57b4d245bedd8106e78d9b896e [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
17#ifndef ART_SRC_GC_LARGE_OBJECT_SPACE_H_
18#define ART_SRC_GC_LARGE_OBJECT_SPACE_H_
19
20#include "space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "safe_map.h"
22
23#include <set>
24#include <vector>
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070025
26namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027class SpaceSetMap;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070028
29class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace {
30 public:
31 virtual bool CanAllocateInto() const {
32 return true;
33 }
34
35 virtual bool IsCompactible() const {
36 return true;
37 }
38
39 virtual SpaceType GetType() const {
40 return kSpaceTypeLargeObjectSpace;
41 }
42
43 virtual SpaceSetMap* GetLiveObjects() const {
44 return live_objects_.get();
45 }
46
47 virtual SpaceSetMap* GetMarkObjects() const {
48 return mark_objects_.get();
49 }
50
51 virtual void SwapBitmaps();
52 virtual void CopyLiveToMarked();
53 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0;
54 virtual ~LargeObjectSpace() {}
55
56 uint64_t GetNumBytesAllocated() const {
57 return num_bytes_allocated_;
58 }
59
60 uint64_t GetNumObjectsAllocated() const {
61 return num_objects_allocated_;
62 }
63
64 uint64_t GetTotalBytesAllocated() const {
65 return total_bytes_allocated_;
66 }
67
68 uint64_t GetTotalObjectsAllocated() const {
69 return total_objects_allocated_;
70 }
71
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070073
74 protected:
75
76 LargeObjectSpace(const std::string& name);
77
78 // Approximate number of bytes which have been allocated into the space.
79 size_t num_bytes_allocated_;
80 size_t num_objects_allocated_;
81 size_t total_bytes_allocated_;
82 size_t total_objects_allocated_;
83
84 UniquePtr<SpaceSetMap> live_objects_;
85 UniquePtr<SpaceSetMap> mark_objects_;
86
87 friend class Space;
88};
89
90class LargeObjectMapSpace : public LargeObjectSpace {
91 public:
92
93 // Creates a large object space. Allocations into the large object space use memory maps instead
94 // of malloc.
95 static LargeObjectMapSpace* Create(const std::string& name);
96
97 // Return the storage space required by obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 virtual size_t AllocationSize(const mirror::Object* obj);
99 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes);
100 size_t Free(Thread* self, mirror::Object* ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101 virtual void Walk(DlMallocSpace::WalkCallback, void* arg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 virtual bool Contains(const mirror::Object* obj) const;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700103private:
104 LargeObjectMapSpace(const std::string& name);
105 virtual ~LargeObjectMapSpace() {}
106
107 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
108 mutable Mutex lock_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 std::vector<mirror::Object*> large_objects_;
110 typedef SafeMap<mirror::Object*, MemMap*> MemMaps;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700111 MemMaps mem_maps_;
112};
113
114class FreeListSpace : public LargeObjectSpace {
115 public:
116 virtual ~FreeListSpace();
117 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
118
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 size_t AllocationSize(const mirror::Object* obj);
120 mirror::Object* Alloc(Thread* self, size_t num_bytes);
121 size_t Free(Thread* self, mirror::Object* obj);
122 bool Contains(const mirror::Object* obj) const;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700123 void Walk(DlMallocSpace::WalkCallback callback, void* arg);
124
125 // Address at which the space begins
126 byte* Begin() const {
127 return begin_;
128 }
129
130 // Address at which the space ends, which may vary as the space is filled.
131 byte* End() const {
132 return end_;
133 }
134
135 // Current size of space
136 size_t Size() const {
137 return End() - Begin();
138 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700139
140 virtual void Dump(std::ostream& os) const;
141
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700142 private:
143 static const size_t kAlignment = kPageSize;
144
145 class Chunk {
146 public:
147 static const size_t kFreeFlag = 0x80000000;
148
149 struct SortBySize {
150 bool operator()(const Chunk* a, const Chunk* b) const {
151 return a->GetSize() < b->GetSize();
152 }
153 };
154
155 bool IsFree() const {
156 return (m_size & kFreeFlag) != 0;
157 }
158
159 void SetSize(size_t size, bool is_free = false) {
160 m_size = size | (is_free ? kFreeFlag : 0);
161 }
162
163 size_t GetSize() const {
164 return m_size & (kFreeFlag - 1);
165 }
166
167 Chunk* GetPrevious() {
168 return m_previous;
169 }
170
171 void SetPrevious(Chunk* previous) {
172 m_previous = previous;
173 DCHECK(m_previous == NULL ||
174 (m_previous != NULL && m_previous + m_previous->GetSize() / kAlignment == this));
175 }
176 private:
177 size_t m_size;
178 Chunk* m_previous;
179 };
180
181 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
182 void AddFreeChunk(void* address, size_t size, Chunk* previous);
183 Chunk* ChunkFromAddr(void* address);
184 void* AddrFromChunk(Chunk* chunk);
185 void RemoveFreeChunk(Chunk* chunk);
186 Chunk* GetNextChunk(Chunk* chunk);
187
188 typedef std::multiset<Chunk*, Chunk::SortBySize> FreeChunks;
189 byte* begin_;
190 byte* end_;
191 UniquePtr<MemMap> mem_map_;
192 Mutex lock_;
193 std::vector<Chunk> chunks_;
194 FreeChunks free_chunks_;
195};
196
197}
198
199#endif // ART_SRC_GC_LARGE_OBJECT_SPACE_H_