rosalloc_space.h revision 6fac447555dc94a935b78198479cce645c837b89
1/* 2 * Copyright (C) 2013 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_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ 18#define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ 19 20#include "gc/allocator/rosalloc.h" 21#include "malloc_space.h" 22#include "space.h" 23 24namespace art { 25namespace gc { 26 27namespace collector { 28 class MarkSweep; 29} // namespace collector 30 31namespace space { 32 33// An alloc space implemented using a runs-of-slots memory allocator. Not final as may be 34// overridden by a ValgrindMallocSpace. 35class RosAllocSpace : public MallocSpace { 36 public: 37 // Create a RosAllocSpace with the requested sizes. The requested 38 // base address is not guaranteed to be granted, if it is required, 39 // the caller should call Begin on the returned space to confirm the 40 // request was granted. 41 static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit, 42 size_t capacity, byte* requested_begin, bool low_memory_mode); 43 static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name, 44 size_t starting_size, size_t initial_size, 45 size_t growth_limit, size_t capacity, 46 bool low_memory_mode); 47 48 mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated, 49 size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_); 50 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, 51 size_t* usable_size) OVERRIDE { 52 return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size); 53 } 54 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE { 55 return AllocationSizeNonvirtual(obj, usable_size); 56 } 57 size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE 58 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 59 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE 60 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 61 62 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated, 63 size_t* usable_size) { 64 // RosAlloc zeroes memory internally. 65 return AllocCommon(self, num_bytes, bytes_allocated, usable_size); 66 } 67 68 // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held. 69 size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) 70 NO_THREAD_SAFETY_ANALYSIS; 71 72 allocator::RosAlloc* GetRosAlloc() const { 73 return rosalloc_; 74 } 75 76 size_t Trim() OVERRIDE; 77 void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); 78 size_t GetFootprint() OVERRIDE; 79 size_t GetFootprintLimit() OVERRIDE; 80 void SetFootprintLimit(size_t limit) OVERRIDE; 81 82 void Clear() OVERRIDE; 83 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, 84 byte* begin, byte* end, byte* limit, size_t growth_limit); 85 86 uint64_t GetBytesAllocated() OVERRIDE; 87 uint64_t GetObjectsAllocated() OVERRIDE; 88 89 void RevokeThreadLocalBuffers(Thread* thread); 90 void RevokeAllThreadLocalBuffers(); 91 92 // Returns the class of a recently freed object. 93 mirror::Class* FindRecentFreedObject(const mirror::Object* obj); 94 95 bool IsRosAllocSpace() const OVERRIDE { 96 return true; 97 } 98 99 RosAllocSpace* AsRosAllocSpace() OVERRIDE { 100 return this; 101 } 102 103 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) { 104 rosalloc_->Verify(); 105 } 106 107 protected: 108 RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc, 109 byte* begin, byte* end, byte* limit, size_t growth_limit); 110 111 private: 112 mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated, 113 size_t* usable_size); 114 115 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, 116 bool low_memory_mode) OVERRIDE { 117 return CreateRosAlloc(base, morecore_start, initial_size, low_memory_mode); 118 } 119 static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size, 120 bool low_memory_mode); 121 122 void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), 123 void* arg) 124 LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_); 125 126 // Underlying rosalloc. 127 allocator::RosAlloc* const rosalloc_; 128 129 // The rosalloc pointer used for allocation. Equal to rosalloc_ or nullptr after 130 // InvalidateAllocator() is called. 131 allocator::RosAlloc* rosalloc_for_alloc_; 132 133 friend class collector::MarkSweep; 134 135 DISALLOW_COPY_AND_ASSIGN(RosAllocSpace); 136}; 137 138} // namespace space 139} // namespace gc 140} // namespace art 141 142#endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ 143