blob: 8cd50880db363dc40a97e27708e6117725481012 [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
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;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070098 std::vector<mirror::Object*,
99 accounting::GCAllocator<mirror::Object*> > large_objects_ GUARDED_BY(lock_);
100 typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>,
101 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.
Ian Rogers1d54e732013-05-02 21:10:01 -0700106// TODO: this implementation is buggy.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700107class FreeListSpace : public LargeObjectSpace {
108 public:
109 virtual ~FreeListSpace();
110 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
111
Ian Rogers22a20862013-03-16 16:34:57 -0700112 size_t AllocationSize(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::Object* Alloc(Thread* self, size_t num_bytes);
114 size_t Free(Thread* self, mirror::Object* obj);
115 bool Contains(const mirror::Object* obj) const;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700116 void Walk(DlMallocSpace::WalkCallback callback, void* arg);
117
Ian Rogers22a20862013-03-16 16:34:57 -0700118 // Address at which the space begins.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119 byte* Begin() const {
120 return begin_;
121 }
122
123 // Address at which the space ends, which may vary as the space is filled.
124 byte* End() const {
125 return end_;
126 }
127
128 // Current size of space
129 size_t Size() const {
130 return End() - Begin();
131 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 void Dump(std::ostream& os) const;
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700134
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700135 private:
136 static const size_t kAlignment = kPageSize;
137
138 class Chunk {
139 public:
140 static const size_t kFreeFlag = 0x80000000;
141
142 struct SortBySize {
143 bool operator()(const Chunk* a, const Chunk* b) const {
144 return a->GetSize() < b->GetSize();
145 }
146 };
147
148 bool IsFree() const {
149 return (m_size & kFreeFlag) != 0;
150 }
151
152 void SetSize(size_t size, bool is_free = false) {
153 m_size = size | (is_free ? kFreeFlag : 0);
154 }
155
156 size_t GetSize() const {
157 return m_size & (kFreeFlag - 1);
158 }
159
160 Chunk* GetPrevious() {
161 return m_previous;
162 }
163
164 void SetPrevious(Chunk* previous) {
165 m_previous = previous;
166 DCHECK(m_previous == NULL ||
167 (m_previous != NULL && m_previous + m_previous->GetSize() / kAlignment == this));
168 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700169
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700170 private:
171 size_t m_size;
172 Chunk* m_previous;
173 };
174
175 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
Ian Rogers22a20862013-03-16 16:34:57 -0700176 void AddFreeChunk(void* address, size_t size, Chunk* previous) EXCLUSIVE_LOCKS_REQUIRED(lock_);
177 Chunk* ChunkFromAddr(void* address) EXCLUSIVE_LOCKS_REQUIRED(lock_);
178 void* AddrFromChunk(Chunk* chunk) EXCLUSIVE_LOCKS_REQUIRED(lock_);
179 void RemoveFreeChunk(Chunk* chunk) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700180 Chunk* GetNextChunk(Chunk* chunk);
181
182 typedef std::multiset<Chunk*, Chunk::SortBySize> FreeChunks;
Ian Rogers22a20862013-03-16 16:34:57 -0700183 byte* const begin_;
184 byte* const end_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700185 UniquePtr<MemMap> mem_map_;
Ian Rogers22a20862013-03-16 16:34:57 -0700186 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
187 std::vector<Chunk> chunks_ GUARDED_BY(lock_);
188 FreeChunks free_chunks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700189};
190
Ian Rogers1d54e732013-05-02 21:10:01 -0700191} // namespace space
192} // namespace gc
193} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700194
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700195#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_