rosalloc_space.h revision e6da9af8dfe0a3e3fbc2be700554f6478380e7b9
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 is a space where objects may be allocated and garbage collected. 34class RosAllocSpace : public MallocSpace { 35 public: 36 // Create a RosAllocSpace with the requested sizes. The requested 37 // base address is not guaranteed to be granted, if it is required, 38 // the caller should call Begin on the returned space to confirm the 39 // request was granted. 40 static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit, 41 size_t capacity, byte* requested_begin, bool low_memory_mode); 42 static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name, 43 size_t starting_size, size_t initial_size, 44 size_t growth_limit, size_t capacity, 45 bool low_memory_mode); 46 47 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, 48 size_t* bytes_allocated) LOCKS_EXCLUDED(lock_); 49 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated); 50 virtual size_t AllocationSize(const mirror::Object* obj); 51 virtual size_t Free(Thread* self, mirror::Object* ptr); 52 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs); 53 54 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated); 55 56 size_t AllocationSizeNonvirtual(const mirror::Object* obj) 57 NO_THREAD_SAFETY_ANALYSIS { 58 // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held. 59 void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj)); 60 // obj is a valid object. Use its class in the header to get the size. 61 size_t size = obj->SizeOf(); 62 size_t size_by_size = rosalloc_->UsableSize(size); 63 if (kIsDebugBuild) { 64 size_t size_by_ptr = rosalloc_->UsableSize(obj_ptr); 65 if (size_by_size != size_by_ptr) { 66 LOG(INFO) << "Found a bad sized obj of size " << size 67 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec 68 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr; 69 } 70 DCHECK_EQ(size_by_size, size_by_ptr); 71 } 72 return size_by_size; 73 } 74 75 art::gc::allocator::RosAlloc* GetRosAlloc() { 76 return rosalloc_; 77 } 78 79 size_t Trim(); 80 void Walk(WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_); 81 size_t GetFootprint(); 82 size_t GetFootprintLimit(); 83 void SetFootprintLimit(size_t limit); 84 85 virtual void Clear(); 86 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, 87 byte* begin, byte* end, byte* limit, size_t growth_limit); 88 89 uint64_t GetBytesAllocated(); 90 uint64_t GetObjectsAllocated(); 91 92 void RevokeThreadLocalBuffers(Thread* thread); 93 void RevokeAllThreadLocalBuffers(); 94 95 // Returns the class of a recently freed object. 96 mirror::Class* FindRecentFreedObject(const mirror::Object* obj); 97 98 virtual void InvalidateAllocator() { 99 rosalloc_for_alloc_ = NULL; 100 } 101 102 virtual bool IsRosAllocSpace() const { 103 return true; 104 } 105 virtual RosAllocSpace* AsRosAllocSpace() { 106 return this; 107 } 108 109 protected: 110 RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc, 111 byte* begin, byte* end, byte* limit, size_t growth_limit); 112 113 private: 114 mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated); 115 116 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, bool low_memory_mode) { 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 art::gc::allocator::RosAlloc* const rosalloc_; 128 129 // A rosalloc pointer used for allocation. Equals to what rosalloc_ 130 // points to or nullptr after InvalidateAllocator() is called. 131 art::gc::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