rosalloc_space.h revision e6da9af8dfe0a3e3fbc2be700554f6478380e7b9
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);
50cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual size_t AllocationSize(const mirror::Object* obj);
51cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual size_t Free(Thread* self, mirror::Object* ptr);
52cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs);
53cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
54cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated);
55cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
56cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t AllocationSizeNonvirtual(const mirror::Object* obj)
57cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      NO_THREAD_SAFETY_ANALYSIS {
58cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
59cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
60cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    // obj is a valid object. Use its class in the header to get the size.
61cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    size_t size = obj->SizeOf();
62cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    size_t size_by_size = rosalloc_->UsableSize(size);
63cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    if (kIsDebugBuild) {
64cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      size_t size_by_ptr = rosalloc_->UsableSize(obj_ptr);
65cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if (size_by_size != size_by_ptr) {
66cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        LOG(INFO) << "Found a bad sized obj of size " << size
67cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                  << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
68cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                  << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
69cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      }
70cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      DCHECK_EQ(size_by_size, size_by_ptr);
71cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    }
72cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return size_by_size;
73cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
74cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
75cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  art::gc::allocator::RosAlloc* GetRosAlloc() {
76cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return rosalloc_;
77cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
78cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
79cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t Trim();
80cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void Walk(WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_);
81cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t GetFootprint();
82cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  size_t GetFootprintLimit();
83cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void SetFootprintLimit(size_t limit);
84cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
85e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier  virtual void Clear();
86cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
87cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                              byte* begin, byte* end, byte* limit, size_t growth_limit);
88cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
89cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  uint64_t GetBytesAllocated();
90cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  uint64_t GetObjectsAllocated();
91cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
92cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeThreadLocalBuffers(Thread* thread);
93cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeAllThreadLocalBuffers();
94cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
95cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Returns the class of a recently freed object.
96cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
97cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
98cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual void InvalidateAllocator() {
994ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi    rosalloc_for_alloc_ = NULL;
100cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
101cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
102cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual bool IsRosAllocSpace() const {
103cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return true;
104cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
105cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  virtual RosAllocSpace* AsRosAllocSpace() {
106cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return this;
107cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
108cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
109cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi protected:
110cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc,
111cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                byte* begin, byte* end, byte* limit, size_t growth_limit);
112cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
113cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi private:
114cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated);
115cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
116573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi  void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, bool low_memory_mode) {
117573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi    return CreateRosAlloc(base, morecore_start, initial_size, low_memory_mode);
118cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
119573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi  static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
120573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi                                             bool low_memory_mode);
121cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
122cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
123cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi                          void* arg)
124cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
125cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
126cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Underlying rosalloc.
1274ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  art::gc::allocator::RosAlloc* const rosalloc_;
1284ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi
1294ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  // A rosalloc pointer used for allocation. Equals to what rosalloc_
1304ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  // points to or nullptr after InvalidateAllocator() is called.
1314ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi  art::gc::allocator::RosAlloc* rosalloc_for_alloc_;
132cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
133cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  friend class collector::MarkSweep;
134cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
135cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
136cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi};
137cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
138cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace space
139cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace gc
140cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace art
141cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
142cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
143