blob: 09c55ec566523cc08d1b524835ac86f992cd5ef0 [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:
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);
86 mirror::Object* Alloc(Thread* self, size_t num_bytes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 size_t Free(Thread* self, mirror::Object* ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -070088 void Walk(DlMallocSpace::WalkCallback, void* arg);
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;
98 std::vector<mirror::Object*> large_objects_ GUARDED_BY(lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 typedef SafeMap<mirror::Object*, MemMap*> MemMaps;
Ian Rogers22a20862013-03-16 16:34:57 -0700100 MemMaps mem_maps_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101};
102
Ian Rogers22a20862013-03-16 16:34:57 -0700103// A continuous large object space with a free-list to handle holes.
Ian Rogers1d54e732013-05-02 21:10:01 -0700104// TODO: this implementation is buggy.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700105class FreeListSpace : public LargeObjectSpace {
106 public:
107 virtual ~FreeListSpace();
108 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
109
Ian Rogers22a20862013-03-16 16:34:57 -0700110 size_t AllocationSize(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 mirror::Object* Alloc(Thread* self, size_t num_bytes);
112 size_t Free(Thread* self, mirror::Object* obj);
113 bool Contains(const mirror::Object* obj) const;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700114 void Walk(DlMallocSpace::WalkCallback callback, void* arg);
115
Ian Rogers22a20862013-03-16 16:34:57 -0700116 // Address at which the space begins.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700117 byte* Begin() const {
118 return begin_;
119 }
120
121 // Address at which the space ends, which may vary as the space is filled.
122 byte* End() const {
123 return end_;
124 }
125
126 // Current size of space
127 size_t Size() const {
128 return End() - Begin();
129 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700130
Ian Rogers1d54e732013-05-02 21:10:01 -0700131 void Dump(std::ostream& os) const;
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700132
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700133 private:
134 static const size_t kAlignment = kPageSize;
135
136 class Chunk {
137 public:
138 static const size_t kFreeFlag = 0x80000000;
139
140 struct SortBySize {
141 bool operator()(const Chunk* a, const Chunk* b) const {
142 return a->GetSize() < b->GetSize();
143 }
144 };
145
146 bool IsFree() const {
147 return (m_size & kFreeFlag) != 0;
148 }
149
150 void SetSize(size_t size, bool is_free = false) {
151 m_size = size | (is_free ? kFreeFlag : 0);
152 }
153
154 size_t GetSize() const {
155 return m_size & (kFreeFlag - 1);
156 }
157
158 Chunk* GetPrevious() {
159 return m_previous;
160 }
161
162 void SetPrevious(Chunk* previous) {
163 m_previous = previous;
164 DCHECK(m_previous == NULL ||
165 (m_previous != NULL && m_previous + m_previous->GetSize() / kAlignment == this));
166 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700167
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700168 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_