blob: 4cd5a6d0a779acde1f3d2a768e6d7954e49285c2 [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001/*
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,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080041 size_t capacity, byte* requested_begin, bool low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080042 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);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046
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);
Ian Rogersef7d42f2014-01-06 12:55:46 -080050 virtual size_t AllocationSize(mirror::Object* obj);
51 virtual size_t Free(Thread* self, mirror::Object* ptr)
52 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
53 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs)
54 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070055
56 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated);
57
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 size_t AllocationSizeNonvirtual(mirror::Object* obj)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070059 NO_THREAD_SAFETY_ANALYSIS {
60 // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
61 void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
62 // obj is a valid object. Use its class in the header to get the size.
63 size_t size = obj->SizeOf();
64 size_t size_by_size = rosalloc_->UsableSize(size);
65 if (kIsDebugBuild) {
66 size_t size_by_ptr = rosalloc_->UsableSize(obj_ptr);
67 if (size_by_size != size_by_ptr) {
68 LOG(INFO) << "Found a bad sized obj of size " << size
69 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
70 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
71 }
72 DCHECK_EQ(size_by_size, size_by_ptr);
73 }
74 return size_by_size;
75 }
76
77 art::gc::allocator::RosAlloc* GetRosAlloc() {
78 return rosalloc_;
79 }
80
81 size_t Trim();
82 void Walk(WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_);
83 size_t GetFootprint();
84 size_t GetFootprintLimit();
85 void SetFootprintLimit(size_t limit);
86
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080087 virtual void Clear();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070088 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
89 byte* begin, byte* end, byte* limit, size_t growth_limit);
90
91 uint64_t GetBytesAllocated();
92 uint64_t GetObjectsAllocated();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070093
94 void RevokeThreadLocalBuffers(Thread* thread);
95 void RevokeAllThreadLocalBuffers();
96
97 // Returns the class of a recently freed object.
98 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
99
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100 virtual bool IsRosAllocSpace() const {
101 return true;
102 }
103 virtual RosAllocSpace* AsRosAllocSpace() {
104 return this;
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:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700112 mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated);
113
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800114 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, bool low_memory_mode) {
115 return CreateRosAlloc(base, morecore_start, initial_size, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800117 static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
118 bool low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700119
120 void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
121 void* arg)
122 LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
123
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700124 // Underlying rosalloc.
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800125 art::gc::allocator::RosAlloc* const rosalloc_;
126
127 // A rosalloc pointer used for allocation. Equals to what rosalloc_
128 // points to or nullptr after InvalidateAllocator() is called.
129 art::gc::allocator::RosAlloc* rosalloc_for_alloc_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700130
131 friend class collector::MarkSweep;
132
133 DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
134};
135
136} // namespace space
137} // namespace gc
138} // namespace art
139
140#endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_