blob: 74d9cca6db7dc5260fe90ea2e9b4d7b5feb6207d [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
Ian Rogers1d54e732013-05-02 21:10:01 -070020
21#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
Ian Rogers1d54e732013-05-02 21:10:01 -070044 uint64_t GetBytesAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070045 return num_bytes_allocated_;
46 }
47
Ian Rogers1d54e732013-05-02 21:10:01 -070048 uint64_t GetObjectsAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070049 return num_objects_allocated_;
50 }
51
52 uint64_t GetTotalBytesAllocated() const {
53 return total_bytes_allocated_;
54 }
55
56 uint64_t GetTotalObjectsAllocated() const {
57 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:
63
64 LargeObjectSpace(const std::string& name);
65
66 // Approximate number of bytes which have been allocated into the space.
67 size_t num_bytes_allocated_;
68 size_t num_objects_allocated_;
69 size_t total_bytes_allocated_;
70 size_t total_objects_allocated_;
71
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070072 friend class Space;
Ian Rogers1d54e732013-05-02 21:10:01 -070073
74 private:
75 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070076};
77
Ian Rogers22a20862013-03-16 16:34:57 -070078// A discontinuous large object space implemented by individual mmap/munmap calls.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070079class LargeObjectMapSpace : public LargeObjectSpace {
80 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070081 // Creates a large object space. Allocations into the large object space use memory maps instead
82 // of malloc.
83 static LargeObjectMapSpace* Create(const std::string& name);
84
85 // Return the storage space required by obj.
Ian Rogers1d54e732013-05-02 21:10:01 -070086 size_t AllocationSize(const mirror::Object* obj);
87 mirror::Object* Alloc(Thread* self, size_t num_bytes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 size_t Free(Thread* self, mirror::Object* ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -070089 void Walk(DlMallocSpace::WalkCallback, void* arg);
Ian Rogersa3dd0b32013-03-19 19:30:59 -070090 // TODO: disabling thread safety analysis as this may be called when we already hold lock_.
Ian Rogers1d54e732013-05-02 21:10:01 -070091 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS;
92
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070093private:
94 LargeObjectMapSpace(const std::string& name);
95 virtual ~LargeObjectMapSpace() {}
96
97 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
Ian Rogers22a20862013-03-16 16:34:57 -070098 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
99 std::vector<mirror::Object*> large_objects_ GUARDED_BY(lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 typedef SafeMap<mirror::Object*, MemMap*> MemMaps;
Ian Rogers22a20862013-03-16 16:34:57 -0700101 MemMaps mem_maps_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700102};
103
Ian Rogers22a20862013-03-16 16:34:57 -0700104// A continuous large object space with a free-list to handle holes.
Ian Rogers1d54e732013-05-02 21:10:01 -0700105// TODO: this implementation is buggy.
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_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::Object* Alloc(Thread* self, size_t num_bytes);
113 size_t Free(Thread* self, mirror::Object* obj);
114 bool Contains(const mirror::Object* obj) const;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700115 void Walk(DlMallocSpace::WalkCallback callback, void* arg);
116
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
137 class Chunk {
138 public:
139 static const size_t kFreeFlag = 0x80000000;
140
141 struct SortBySize {
142 bool operator()(const Chunk* a, const Chunk* b) const {
143 return a->GetSize() < b->GetSize();
144 }
145 };
146
147 bool IsFree() const {
148 return (m_size & kFreeFlag) != 0;
149 }
150
151 void SetSize(size_t size, bool is_free = false) {
152 m_size = size | (is_free ? kFreeFlag : 0);
153 }
154
155 size_t GetSize() const {
156 return m_size & (kFreeFlag - 1);
157 }
158
159 Chunk* GetPrevious() {
160 return m_previous;
161 }
162
163 void SetPrevious(Chunk* previous) {
164 m_previous = previous;
165 DCHECK(m_previous == NULL ||
166 (m_previous != NULL && m_previous + m_previous->GetSize() / kAlignment == this));
167 }
168 private:
169 size_t m_size;
170 Chunk* m_previous;
171 };
172
173 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
Ian Rogers22a20862013-03-16 16:34:57 -0700174 void AddFreeChunk(void* address, size_t size, Chunk* previous) EXCLUSIVE_LOCKS_REQUIRED(lock_);
175 Chunk* ChunkFromAddr(void* address) EXCLUSIVE_LOCKS_REQUIRED(lock_);
176 void* AddrFromChunk(Chunk* chunk) EXCLUSIVE_LOCKS_REQUIRED(lock_);
177 void RemoveFreeChunk(Chunk* chunk) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700178 Chunk* GetNextChunk(Chunk* chunk);
179
180 typedef std::multiset<Chunk*, Chunk::SortBySize> FreeChunks;
Ian Rogers22a20862013-03-16 16:34:57 -0700181 byte* const begin_;
182 byte* const end_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700183 UniquePtr<MemMap> mem_map_;
Ian Rogers22a20862013-03-16 16:34:57 -0700184 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
185 std::vector<Chunk> chunks_ GUARDED_BY(lock_);
186 FreeChunks free_chunks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700187};
188
Ian Rogers1d54e732013-05-02 21:10:01 -0700189} // namespace space
190} // namespace gc
191} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700192
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700193#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_