Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_DLMALLOC_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 20 | #include "malloc_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include "space.h" |
| 22 | |
| 23 | namespace art { |
| 24 | namespace gc { |
| 25 | |
| 26 | namespace collector { |
| 27 | class MarkSweep; |
| 28 | } // namespace collector |
| 29 | |
| 30 | namespace space { |
| 31 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 32 | // An alloc space is a space where objects may be allocated and garbage collected. Not final as may |
| 33 | // be overridden by a ValgrindMallocSpace. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 34 | class DlMallocSpace : public MallocSpace { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 35 | public: |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 36 | // Create a DlMallocSpace from an existing mem_map. |
| 37 | static DlMallocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name, |
| 38 | size_t starting_size, size_t initial_size, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 39 | size_t growth_limit, size_t capacity, |
| 40 | bool can_move_objects); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 41 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 42 | // Create a DlMallocSpace with the requested sizes. The requested |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | // base address is not guaranteed to be granted, if it is required, |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 44 | // the caller should call Begin on the returned space to confirm the |
| 45 | // request was granted. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 47 | size_t capacity, uint8_t* requested_begin, bool can_move_objects); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 49 | // Virtual to allow ValgrindMallocSpace to intercept. |
| 50 | virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 51 | size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_); |
| 52 | // Virtual to allow ValgrindMallocSpace to intercept. |
| 53 | virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 54 | size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_) { |
| 55 | return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 56 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 57 | // Virtual to allow ValgrindMallocSpace to intercept. |
| 58 | virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE { |
| 59 | return AllocationSizeNonvirtual(obj, usable_size); |
| 60 | } |
| 61 | // Virtual to allow ValgrindMallocSpace to intercept. |
| 62 | virtual size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE |
| 63 | LOCKS_EXCLUDED(lock_) |
| 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 65 | // Virtual to allow ValgrindMallocSpace to intercept. |
| 66 | virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE |
| 67 | LOCKS_EXCLUDED(lock_) |
| 68 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 69 | |
| 70 | // DlMallocSpaces don't have thread local state. |
| 71 | void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE { |
| 72 | } |
| 73 | void RevokeAllThreadLocalBuffers() OVERRIDE { |
| 74 | } |
| 75 | |
| 76 | // Faster non-virtual allocation path. |
| 77 | mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 78 | size_t* usable_size) LOCKS_EXCLUDED(lock_); |
| 79 | |
| 80 | // Faster non-virtual allocation size path. |
| 81 | size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 82 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 83 | #ifndef NDEBUG |
| 84 | // Override only in the debug build. |
| 85 | void CheckMoreCoreForPrecondition(); |
| 86 | #endif |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | |
| 88 | void* GetMspace() const { |
| 89 | return mspace_; |
| 90 | } |
| 91 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 92 | size_t Trim() OVERRIDE; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 93 | |
| 94 | // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be |
| 95 | // in use, indicated by num_bytes equaling zero. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 96 | void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 97 | |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 98 | // Returns the number of bytes that the space has currently obtained from the system. This is |
| 99 | // greater or equal to the amount of live data in the space. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 100 | size_t GetFootprint() OVERRIDE; |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 101 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 102 | // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 103 | size_t GetFootprintLimit() OVERRIDE; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 104 | |
| 105 | // Set the maximum number of bytes that the heap is allowed to obtain from the system via |
| 106 | // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When |
| 107 | // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 108 | void SetFootprintLimit(size_t limit) OVERRIDE; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 109 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 110 | MallocSpace* CreateInstance(MemMap* mem_map, const std::string& name, void* allocator, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 111 | uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 112 | bool can_move_objects); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 113 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 114 | uint64_t GetBytesAllocated() OVERRIDE; |
| 115 | uint64_t GetObjectsAllocated() OVERRIDE; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 116 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 117 | virtual void Clear() OVERRIDE; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 118 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 119 | bool IsDlMallocSpace() const OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 122 | |
| 123 | DlMallocSpace* AsDlMallocSpace() OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 124 | return this; |
| 125 | } |
| 126 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 127 | void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE |
| 128 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 129 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 130 | protected: |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 131 | DlMallocSpace(MemMap* mem_map, size_t initial_size, const std::string& name, void* mspace, |
| 132 | uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit, |
| 133 | bool can_move_objects, size_t starting_size); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 134 | |
| 135 | private: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 136 | mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 137 | size_t* usable_size) |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 138 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 139 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 140 | void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 141 | size_t /*maximum_size*/, bool /*low_memory_mode*/) OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 142 | return CreateMspace(base, morecore_start, initial_size); |
| 143 | } |
| 144 | static void* CreateMspace(void* base, size_t morecore_start, size_t initial_size); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 145 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 146 | // The boundary tag overhead. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 147 | static const size_t kChunkOverhead = sizeof(intptr_t); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 148 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 149 | // Underlying malloc space. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 150 | void* mspace_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 151 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 152 | friend class collector::MarkSweep; |
| 153 | |
| 154 | DISALLOW_COPY_AND_ASSIGN(DlMallocSpace); |
| 155 | }; |
| 156 | |
| 157 | } // namespace space |
| 158 | } // namespace gc |
| 159 | } // namespace art |
| 160 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 161 | #endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_ |