Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | |
| 21 | #include "dlmalloc_space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "safe_map.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | |
| 25 | #include <set> |
| 26 | #include <vector> |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | namespace gc { |
| 30 | namespace space { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 31 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 32 | // Abstraction implemented by all large object spaces. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 33 | class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace { |
| 34 | public: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 35 | virtual SpaceType GetType() const { |
| 36 | return kSpaceTypeLargeObjectSpace; |
| 37 | } |
| 38 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 39 | virtual void SwapBitmaps(); |
| 40 | virtual void CopyLiveToMarked(); |
| 41 | virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0; |
| 42 | virtual ~LargeObjectSpace() {} |
| 43 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 44 | uint64_t GetBytesAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 45 | return num_bytes_allocated_; |
| 46 | } |
| 47 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | uint64_t GetObjectsAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 49 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 61 | |
| 62 | protected: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 63 | explicit LargeObjectSpace(const std::string& name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 64 | |
| 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 Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 71 | friend class Space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 77 | // A discontinuous large object space implemented by individual mmap/munmap calls. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 78 | class LargeObjectMapSpace : public LargeObjectSpace { |
| 79 | public: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 80 | // 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 Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 85 | size_t AllocationSize(const mirror::Object* obj); |
| 86 | mirror::Object* Alloc(Thread* self, size_t num_bytes); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | size_t Free(Thread* self, mirror::Object* ptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 88 | void Walk(DlMallocSpace::WalkCallback, void* arg); |
Ian Rogers | a3dd0b3 | 2013-03-19 19:30:59 -0700 | [diff] [blame] | 89 | // TODO: disabling thread safety analysis as this may be called when we already hold lock_. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 90 | bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS; |
| 91 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 92 | private: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 93 | explicit LargeObjectMapSpace(const std::string& name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 94 | virtual ~LargeObjectMapSpace() {} |
| 95 | |
| 96 | // Used to ensure mutual exclusion when the allocation spaces data structures are being modified. |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 97 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 98 | std::vector<mirror::Object*> large_objects_ GUARDED_BY(lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | typedef SafeMap<mirror::Object*, MemMap*> MemMaps; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 100 | MemMaps mem_maps_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 103 | // A continuous large object space with a free-list to handle holes. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 104 | // TODO: this implementation is buggy. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 105 | class FreeListSpace : public LargeObjectSpace { |
| 106 | public: |
| 107 | virtual ~FreeListSpace(); |
| 108 | static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity); |
| 109 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 110 | size_t AllocationSize(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | 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 Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 114 | void Walk(DlMallocSpace::WalkCallback callback, void* arg); |
| 115 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 116 | // Address at which the space begins. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 117 | 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 Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 130 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 131 | void Dump(std::ostream& os) const; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 132 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 133 | 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 Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 167 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 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 Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 174 | 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 Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 178 | Chunk* GetNextChunk(Chunk* chunk); |
| 179 | |
| 180 | typedef std::multiset<Chunk*, Chunk::SortBySize> FreeChunks; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 181 | byte* const begin_; |
| 182 | byte* const end_; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 183 | UniquePtr<MemMap> mem_map_; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 184 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 185 | std::vector<Chunk> chunks_ GUARDED_BY(lock_); |
| 186 | FreeChunks free_chunks_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 189 | } // namespace space |
| 190 | } // namespace gc |
| 191 | } // namespace art |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 193 | #endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |