rosalloc_space.h revision a4adbfd44032d70e166e6f18096bbbed05a990ba
1cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi/*
2cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Copyright (C) 2013 The Android Open Source Project
3cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
4cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Licensed under the Apache License, Version 2.0 (the "License");
5cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * you may not use this file except in compliance with the License.
6cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * You may obtain a copy of the License at
7cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
8cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *      http://www.apache.org/licenses/LICENSE-2.0
9cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
10cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Unless required by applicable law or agreed to in writing, software
11cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * distributed under the License is distributed on an "AS IS" BASIS,
12cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * See the License for the specific language governing permissions and
14cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * limitations under the License.
15cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi */
16cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
17cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#ifndef ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
18cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
19cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
20cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "gc/allocator/rosalloc.h"
21cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "malloc_space.h"
22cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "space.h"
23cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
24cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace art {
25cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace gc {
26cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
27cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace collector {
28cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  class MarkSweep;
29cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace collector
30cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
31cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace space {
32cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
33cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi// An alloc space is a space where objects may be allocated and garbage collected.
34cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchiclass RosAllocSpace : public MallocSpace {
35cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi public:
36cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Create a RosAllocSpace with the requested sizes. The requested
37cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // base address is not guaranteed to be granted, if it is required,
38cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // the caller should call Begin on the returned space to confirm the
39cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // request was granted.
40cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
41573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi                               size_t capacity, byte* requested_begin, bool low_memory_mode);
42e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier  static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name,
43e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier                                         size_t starting_size, size_t initial_size,
44e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier                                         size_t growth_limit, size_t capacity,
45e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier                                         bool low_memory_mode);
46cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
47cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
48cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                                          size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
49cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
50ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  virtual size_t AllocationSize(mirror::Object* obj);
51ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  virtual size_t Free(Thread* self, mirror::Object* ptr)
52ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
53ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs)
54ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
56cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated);
57cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
58ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  size_t AllocationSizeNonvirtual(mirror::Object* obj)
59cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      NO_THREAD_SAFETY_ANALYSIS {
60cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
61cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
62cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    // obj is a valid object. Use its class in the header to get the size.
63cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    size_t size = obj->SizeOf();
64cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    size_t size_by_size = rosalloc_->UsableSize(size);
65cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    if (kIsDebugBuild) {
66cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      size_t size_by_ptr = rosalloc_->UsableSize(obj_ptr);
67cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if (size_by_size != size_by_ptr) {
68cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        LOG(INFO) << "Found a bad sized obj of size " << size
69cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                  << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
70cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                  << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
71cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      }
72cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      DCHECK_EQ(size_by_size, size_by_ptr);
73cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    }
74cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return size_by_size;
75cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
76cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
77cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  art::gc::allocator::RosAlloc* GetRosAlloc() {
78cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return rosalloc_;
79cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
80cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
81cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t Trim();
82cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void Walk(WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_);
83cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t GetFootprint();
84cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t GetFootprintLimit();
85cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void SetFootprintLimit(size_t limit);
86cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
87e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier  virtual void Clear();
88cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
89cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                              byte* begin, byte* end, byte* limit, size_t growth_limit);
90cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
91cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  uint64_t GetBytesAllocated();
92cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  uint64_t GetObjectsAllocated();
93cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
94cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeThreadLocalBuffers(Thread* thread);
95cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeAllThreadLocalBuffers();
96cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
97cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Returns the class of a recently freed object.
98cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
99cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
100cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual bool IsRosAllocSpace() const {
101cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return true;
102cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
103cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual RosAllocSpace* AsRosAllocSpace() {
104cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return this;
105cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
106cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
107a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi  void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
108a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi    rosalloc_->Verify();
109a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi  }
110a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi
111cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi protected:
112cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc,
113cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                byte* begin, byte* end, byte* limit, size_t growth_limit);
114cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
115cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi private:
116cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated);
117cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
118573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi  void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, bool low_memory_mode) {
119573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi    return CreateRosAlloc(base, morecore_start, initial_size, low_memory_mode);
120cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
121573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi  static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
122573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi                                             bool low_memory_mode);
123cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
124cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
125cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                          void* arg)
126cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
127cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
128cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Underlying rosalloc.
1294ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  art::gc::allocator::RosAlloc* const rosalloc_;
1304ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi
1314ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  // A rosalloc pointer used for allocation. Equals to what rosalloc_
1324ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  // points to or nullptr after InvalidateAllocator() is called.
1334ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  art::gc::allocator::RosAlloc* rosalloc_for_alloc_;
134cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
135cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  friend class collector::MarkSweep;
136cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
137cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
138cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi};
139cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
140cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace space
141cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace gc
142cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace art
143cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
144cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
145