blob: 8e34fd0c1375c6f7a71d3e5608ba930dbd8c22d7 [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_MALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_
19
20#include "space.h"
21
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -080022#include <valgrind.h>
23#include <memcheck/memcheck.h>
24
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025namespace art {
26namespace gc {
27
28namespace collector {
29 class MarkSweep;
30} // namespace collector
31
32namespace space {
33
Mathieu Chartiera1602f22014-01-13 17:19:19 -080034class ZygoteSpace;
35
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070036// TODO: Remove define macro
37#define CHECK_MEMORY_CALL(call, args, what) \
38 do { \
39 int rc = call args; \
40 if (UNLIKELY(rc != 0)) { \
41 errno = rc; \
42 PLOG(FATAL) << # call << " failed for " << what; \
43 } \
44 } while (false)
45
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046// A common parent of DlMallocSpace and RosAllocSpace.
47class MallocSpace : public ContinuousMemMapAllocSpace {
48 public:
49 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
50
51 SpaceType GetType() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080052 return kSpaceTypeMallocSpace;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070053 }
54
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070055 // Allocate num_bytes allowing the underlying space to grow.
Ian Rogers6fac4472014-02-25 17:01:10 -080056 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
57 size_t* bytes_allocated, size_t* usable_size) = 0;
58 // Allocate num_bytes without allowing the underlying space to grow.
59 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
60 size_t* usable_size) = 0;
61 // Return the storage space required by obj. If usable_size isn't nullptr then it is set to the
62 // amount of the storage space that may be used by obj.
63 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -080064 virtual size_t Free(Thread* self, mirror::Object* ptr)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
66 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070068
69#ifndef NDEBUG
70 virtual void CheckMoreCoreForPrecondition() {} // to be overridden in the debug build.
71#else
72 void CheckMoreCoreForPrecondition() {} // no-op in the non-debug build.
73#endif
74
75 void* MoreCore(intptr_t increment);
76
77 // Hands unused pages back to the system.
78 virtual size_t Trim() = 0;
79
80 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
81 // in use, indicated by num_bytes equaling zero.
82 virtual void Walk(WalkCallback callback, void* arg) = 0;
83
84 // Returns the number of bytes that the space has currently obtained from the system. This is
85 // greater or equal to the amount of live data in the space.
86 virtual size_t GetFootprint() = 0;
87
88 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
89 virtual size_t GetFootprintLimit() = 0;
90
91 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
92 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
93 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
94 virtual void SetFootprintLimit(size_t limit) = 0;
95
96 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
97 // maximum reserved size of the heap.
98 void ClearGrowthLimit() {
99 growth_limit_ = NonGrowthLimitCapacity();
100 }
101
102 // Override capacity so that we only return the possibly limited capacity
103 size_t Capacity() const {
104 return growth_limit_;
105 }
106
107 // The total amount of memory reserved for the alloc space.
108 size_t NonGrowthLimitCapacity() const {
109 return GetMemMap()->Size();
110 }
111
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700112 void Dump(std::ostream& os) const;
113
114 void SetGrowthLimit(size_t growth_limit);
115
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116 virtual MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
117 byte* begin, byte* end, byte* limit, size_t growth_limit) = 0;
118
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800119 // Splits ourself into a zygote space and new malloc space which has our unused memory. When true,
120 // the low memory mode argument specifies that the heap wishes the created space to be more
121 // aggressive in releasing unused pages. Invalidates the space its called on.
122 ZygoteSpace* CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode,
123 MallocSpace** out_malloc_space) NO_THREAD_SAFETY_ANALYSIS;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700124 virtual uint64_t GetBytesAllocated() = 0;
125 virtual uint64_t GetObjectsAllocated() = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700126
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700127 // Returns the class of a recently freed object.
128 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
129
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700130 protected:
131 MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800132 byte* limit, size_t growth_limit, bool create_bitmaps = true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700133
134 static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
135 size_t* growth_limit, size_t* capacity, byte* requested_begin);
136
Ian Rogers6fac4472014-02-25 17:01:10 -0800137 // When true the low memory mode argument specifies that the heap wishes the created allocator to
138 // be more aggressive in releasing unused pages.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800139 virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
140 bool low_memory_mode) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700141
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 void RegisterRecentFree(mirror::Object* ptr)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
144 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700145
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800146 virtual accounting::SpaceBitmap::SweepCallback* GetSweepCallback() {
147 return &SweepCallback;
148 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700149
150 // Recent allocation buffer.
151 static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0;
152 static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1;
153 std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount];
154 size_t recent_free_pos_;
155
156 static size_t bitmap_index_;
157
158 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
159 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
160
161 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
162 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
163 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
164 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
165 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
166 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
167 // one time by a call to ClearGrowthLimit.
168 size_t growth_limit_;
169
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800170 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800173
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700174 DISALLOW_COPY_AND_ASSIGN(MallocSpace);
175};
176
177} // namespace space
178} // namespace gc
179} // namespace art
180
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800181#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_