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