blob: a15673884a29ed1216df1f6bf6e9b5a4a4dea1e4 [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
Ian Rogers6fac4472014-02-25 17:01:10 -080033// An alloc space implemented using a runs-of-slots memory allocator. Not final as may be
34// overridden by a ValgrindMallocSpace.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070035class 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,
Mathieu Chartier31f44142014-04-08 14:40:03 -070042 size_t capacity, byte* requested_begin, bool low_memory_mode,
43 bool can_move_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080044 static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name,
45 size_t starting_size, size_t initial_size,
46 size_t growth_limit, size_t capacity,
Mathieu Chartier31f44142014-04-08 14:40:03 -070047 bool low_memory_mode, bool can_move_objects);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070048
Ian Rogers6fac4472014-02-25 17:01:10 -080049 mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
50 size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_);
51 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
52 size_t* usable_size) OVERRIDE {
53 return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size);
54 }
55 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
56 return AllocationSizeNonvirtual(obj, usable_size);
57 }
58 size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080060 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070062
Ian Rogers6fac4472014-02-25 17:01:10 -080063 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
64 size_t* usable_size) {
65 // RosAlloc zeroes memory internally.
66 return AllocCommon(self, num_bytes, bytes_allocated, usable_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070067 }
68
Ian Rogers6fac4472014-02-25 17:01:10 -080069 // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
70 size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
71 NO_THREAD_SAFETY_ANALYSIS;
72
73 allocator::RosAlloc* GetRosAlloc() const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070074 return rosalloc_;
75 }
76
Ian Rogers6fac4472014-02-25 17:01:10 -080077 size_t Trim() OVERRIDE;
78 void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
79 size_t GetFootprint() OVERRIDE;
80 size_t GetFootprintLimit() OVERRIDE;
81 void SetFootprintLimit(size_t limit) OVERRIDE;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070082
Ian Rogers6fac4472014-02-25 17:01:10 -080083 void Clear() OVERRIDE;
Mathieu Chartier31f44142014-04-08 14:40:03 -070084
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070085 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
Mathieu Chartier31f44142014-04-08 14:40:03 -070086 byte* begin, byte* end, byte* limit, size_t growth_limit,
87 bool can_move_objects) OVERRIDE;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070088
Ian Rogers6fac4472014-02-25 17:01:10 -080089 uint64_t GetBytesAllocated() OVERRIDE;
90 uint64_t GetObjectsAllocated() OVERRIDE;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070091
92 void RevokeThreadLocalBuffers(Thread* thread);
93 void RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -070094 void AssertAllThreadLocalBuffersAreRevoked();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070095
96 // Returns the class of a recently freed object.
97 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
98
Ian Rogers6fac4472014-02-25 17:01:10 -080099 bool IsRosAllocSpace() const OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100 return true;
101 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800102
103 RosAllocSpace* AsRosAllocSpace() OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700104 return this;
105 }
106
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800107 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
108 rosalloc_->Verify();
109 }
110
Mathieu Chartier661974a2014-01-09 11:23:53 -0800111 virtual ~RosAllocSpace();
112
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700113 protected:
114 RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700115 byte* begin, byte* end, byte* limit, size_t growth_limit, bool can_move_objects,
116 size_t starting_size, size_t initial_size, bool low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700117
118 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800119 mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated,
120 size_t* usable_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700121
Ian Rogers6fac4472014-02-25 17:01:10 -0800122 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800123 size_t maximum_size, bool low_memory_mode) OVERRIDE {
124 return CreateRosAlloc(base, morecore_start, initial_size, maximum_size, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700125 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800126 static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800127 size_t maximum_size, bool low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700128
129 void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700130 void* arg, bool do_null_callback_at_end)
131 LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
132 void InspectAllRosAllocWithSuspendAll(
133 void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
134 void* arg, bool do_null_callback_at_end)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700135 LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
136
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700137 // Underlying rosalloc.
Mathieu Chartier31f44142014-04-08 14:40:03 -0700138 allocator::RosAlloc* rosalloc_;
139
140 const bool low_memory_mode_;
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800141
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700142 friend class collector::MarkSweep;
143
144 DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
145};
146
147} // namespace space
148} // namespace gc
149} // namespace art
150
151#endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_