blob: eab757a13ed53af119080bfb41e96d426b3ede95 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070020#include "malloc_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "space.h"
22
23namespace art {
24namespace gc {
25
26namespace collector {
27 class MarkSweep;
28} // namespace collector
29
30namespace space {
31
Ian Rogers6fac4472014-02-25 17:01:10 -080032// An alloc space is a space where objects may be allocated and garbage collected. Not final as may
Evgenii Stepanov1e133742015-05-20 12:30:59 -070033// be overridden by a MemoryToolMallocSpace.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070034class DlMallocSpace : public MallocSpace {
Ian Rogers1d54e732013-05-02 21:10:01 -070035 public:
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080036 // Create a DlMallocSpace from an existing mem_map.
37 static DlMallocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name,
38 size_t starting_size, size_t initial_size,
Mathieu Chartier31f44142014-04-08 14:40:03 -070039 size_t growth_limit, size_t capacity,
40 bool can_move_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080041
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070042 // Create a DlMallocSpace with the requested sizes. The requested
Ian Rogers1d54e732013-05-02 21:10:01 -070043 // base address is not guaranteed to be granted, if it is required,
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070044 // the caller should call Begin on the returned space to confirm the
45 // request was granted.
Ian Rogers1d54e732013-05-02 21:10:01 -070046 static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
Ian Rogers13735952014-10-08 12:43:28 -070047 size_t capacity, uint8_t* requested_begin, bool can_move_objects);
Ian Rogers1d54e732013-05-02 21:10:01 -070048
Evgenii Stepanov1e133742015-05-20 12:30:59 -070049 // Virtual to allow MemoryToolMallocSpace to intercept.
Ian Rogers6fac4472014-02-25 17:01:10 -080050 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070051 size_t* usable_size,
52 size_t* bytes_tl_bulk_allocated)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 OVERRIDE REQUIRES(!lock_);
Evgenii Stepanov1e133742015-05-20 12:30:59 -070054 // Virtual to allow MemoryToolMallocSpace to intercept.
Ian Rogers6fac4472014-02-25 17:01:10 -080055 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070056 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
Mathieu Chartier90443472015-07-16 20:32:27 -070057 OVERRIDE REQUIRES(!lock_) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070058 return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size,
59 bytes_tl_bulk_allocated);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070060 }
Evgenii Stepanov1e133742015-05-20 12:30:59 -070061 // Virtual to allow MemoryToolMallocSpace to intercept.
Ian Rogers6fac4472014-02-25 17:01:10 -080062 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
63 return AllocationSizeNonvirtual(obj, usable_size);
64 }
Evgenii Stepanov1e133742015-05-20 12:30:59 -070065 // Virtual to allow MemoryToolMallocSpace to intercept.
Ian Rogers6fac4472014-02-25 17:01:10 -080066 virtual size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -070067 REQUIRES(!lock_)
68 SHARED_REQUIRES(Locks::mutator_lock_);
Evgenii Stepanov1e133742015-05-20 12:30:59 -070069 // Virtual to allow MemoryToolMallocSpace to intercept.
Ian Rogers6fac4472014-02-25 17:01:10 -080070 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -070071 REQUIRES(!lock_)
72 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080073
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070074 size_t MaxBytesBulkAllocatedFor(size_t num_bytes) OVERRIDE {
75 return num_bytes;
Ian Rogers6fac4472014-02-25 17:01:10 -080076 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070077
78 // DlMallocSpaces don't have thread local state.
79 size_t RevokeThreadLocalBuffers(art::Thread*) OVERRIDE {
80 return 0U;
81 }
82 size_t RevokeAllThreadLocalBuffers() OVERRIDE {
83 return 0U;
Ian Rogers6fac4472014-02-25 17:01:10 -080084 }
85
86 // Faster non-virtual allocation path.
87 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070088 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
Mathieu Chartier90443472015-07-16 20:32:27 -070089 REQUIRES(!lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080090
91 // Faster non-virtual allocation size path.
92 size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070093
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070094#ifndef NDEBUG
95 // Override only in the debug build.
96 void CheckMoreCoreForPrecondition();
97#endif
Ian Rogers1d54e732013-05-02 21:10:01 -070098
99 void* GetMspace() const {
100 return mspace_;
101 }
102
Ian Rogers6fac4472014-02-25 17:01:10 -0800103 size_t Trim() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700104
105 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
106 // in use, indicated by num_bytes equaling zero.
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 void Walk(WalkCallback callback, void* arg) OVERRIDE REQUIRES(!lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700108
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700109 // Returns the number of bytes that the space has currently obtained from the system. This is
110 // greater or equal to the amount of live data in the space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800111 size_t GetFootprint() OVERRIDE;
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700112
Ian Rogers1d54e732013-05-02 21:10:01 -0700113 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
Ian Rogers6fac4472014-02-25 17:01:10 -0800114 size_t GetFootprintLimit() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700115
116 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
117 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
118 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
Ian Rogers6fac4472014-02-25 17:01:10 -0800119 void SetFootprintLimit(size_t limit) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700120
Andreas Gamped7576322014-10-24 22:13:45 -0700121 MallocSpace* CreateInstance(MemMap* mem_map, const std::string& name, void* allocator,
Ian Rogers13735952014-10-08 12:43:28 -0700122 uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700123 bool can_move_objects);
Ian Rogers1d54e732013-05-02 21:10:01 -0700124
Ian Rogers6fac4472014-02-25 17:01:10 -0800125 uint64_t GetBytesAllocated() OVERRIDE;
126 uint64_t GetObjectsAllocated() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700127
Mathieu Chartier31f44142014-04-08 14:40:03 -0700128 virtual void Clear() OVERRIDE;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700129
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 bool IsDlMallocSpace() const OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700131 return true;
132 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800133
134 DlMallocSpace* AsDlMallocSpace() OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700135 return this;
136 }
137
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700138 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700140
Ian Rogers1d54e732013-05-02 21:10:01 -0700141 protected:
Andreas Gamped7576322014-10-24 22:13:45 -0700142 DlMallocSpace(MemMap* mem_map, size_t initial_size, const std::string& name, void* mspace,
143 uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit,
144 bool can_move_objects, size_t starting_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700145
146 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800147 mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700148 size_t* usable_size,
149 size_t* bytes_tl_bulk_allocated)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 REQUIRES(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700151
Ian Rogers6fac4472014-02-25 17:01:10 -0800152 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800153 size_t /*maximum_size*/, bool /*low_memory_mode*/) OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700154 return CreateMspace(base, morecore_start, initial_size);
155 }
156 static void* CreateMspace(void* base, size_t morecore_start, size_t initial_size);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700157
Ian Rogers1d54e732013-05-02 21:10:01 -0700158 // The boundary tag overhead.
Ian Rogers13735952014-10-08 12:43:28 -0700159 static const size_t kChunkOverhead = sizeof(intptr_t);
Ian Rogers1d54e732013-05-02 21:10:01 -0700160
Ian Rogers6fac4472014-02-25 17:01:10 -0800161 // Underlying malloc space.
Mathieu Chartier31f44142014-04-08 14:40:03 -0700162 void* mspace_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700163
Ian Rogers1d54e732013-05-02 21:10:01 -0700164 friend class collector::MarkSweep;
165
166 DISALLOW_COPY_AND_ASSIGN(DlMallocSpace);
167};
168
169} // namespace space
170} // namespace gc
171} // namespace art
172
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700173#endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_