blob: 6ea10ad0a48de6252e7ebde08d4353ceedad4b0f [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
33// be overridden by a ValgrindMallocSpace.
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,
39 size_t growth_limit, size_t capacity);
40
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070041 // Create a DlMallocSpace with the requested sizes. The requested
Ian Rogers1d54e732013-05-02 21:10:01 -070042 // base address is not guaranteed to be granted, if it is required,
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070043 // the caller should call Begin on the returned space to confirm the
44 // request was granted.
Ian Rogers1d54e732013-05-02 21:10:01 -070045 static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
46 size_t capacity, byte* requested_begin);
47
Ian Rogers6fac4472014-02-25 17:01:10 -080048 // Virtual to allow ValgrindMallocSpace to intercept.
49 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
50 size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_);
51 // Virtual to allow ValgrindMallocSpace to intercept.
52 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
53 size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_) {
54 return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070055 }
Ian Rogers6fac4472014-02-25 17:01:10 -080056 // Virtual to allow ValgrindMallocSpace to intercept.
57 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
58 return AllocationSizeNonvirtual(obj, usable_size);
59 }
60 // Virtual to allow ValgrindMallocSpace to intercept.
61 virtual size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE
62 LOCKS_EXCLUDED(lock_)
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64 // Virtual to allow ValgrindMallocSpace to intercept.
65 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE
66 LOCKS_EXCLUDED(lock_)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68
69 // DlMallocSpaces don't have thread local state.
70 void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE {
71 }
72 void RevokeAllThreadLocalBuffers() OVERRIDE {
73 }
74
75 // Faster non-virtual allocation path.
76 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
77 size_t* usable_size) LOCKS_EXCLUDED(lock_);
78
79 // Faster non-virtual allocation size path.
80 size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070081
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070082#ifndef NDEBUG
83 // Override only in the debug build.
84 void CheckMoreCoreForPrecondition();
85#endif
Ian Rogers1d54e732013-05-02 21:10:01 -070086
87 void* GetMspace() const {
88 return mspace_;
89 }
90
Ian Rogers6fac4472014-02-25 17:01:10 -080091 size_t Trim() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -070092
93 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
94 // in use, indicated by num_bytes equaling zero.
Ian Rogers6fac4472014-02-25 17:01:10 -080095 void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070096
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -070097 // Returns the number of bytes that the space has currently obtained from the system. This is
98 // greater or equal to the amount of live data in the space.
Ian Rogers6fac4472014-02-25 17:01:10 -080099 size_t GetFootprint() OVERRIDE;
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700100
Ian Rogers1d54e732013-05-02 21:10:01 -0700101 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
Ian Rogers6fac4472014-02-25 17:01:10 -0800102 size_t GetFootprintLimit() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700103
104 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
105 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
106 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
Ian Rogers6fac4472014-02-25 17:01:10 -0800107 void SetFootprintLimit(size_t limit) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700108
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700109 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
110 byte* begin, byte* end, byte* limit, size_t growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
Ian Rogers6fac4472014-02-25 17:01:10 -0800112 uint64_t GetBytesAllocated() OVERRIDE;
113 uint64_t GetObjectsAllocated() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700114
Ian Rogers6fac4472014-02-25 17:01:10 -0800115 void Clear() OVERRIDE;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700116
Ian Rogers6fac4472014-02-25 17:01:10 -0800117 bool IsDlMallocSpace() const OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700118 return true;
119 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800120
121 DlMallocSpace* AsDlMallocSpace() OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700122 return this;
123 }
124
Ian Rogers1d54e732013-05-02 21:10:01 -0700125 protected:
126 DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127 byte* limit, size_t growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -0700128
129 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated,
131 size_t* usable_size)
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700132 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700133
Ian Rogers6fac4472014-02-25 17:01:10 -0800134 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
135 bool /*low_memory_mode*/) OVERRIDE {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700136 return CreateMspace(base, morecore_start, initial_size);
137 }
138 static void* CreateMspace(void* base, size_t morecore_start, size_t initial_size);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700139
Ian Rogers1d54e732013-05-02 21:10:01 -0700140 // The boundary tag overhead.
141 static const size_t kChunkOverhead = kWordSize;
142
Ian Rogers6fac4472014-02-25 17:01:10 -0800143 // Underlying malloc space.
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800144 void* const mspace_;
145
Ian Rogers6fac4472014-02-25 17:01:10 -0800146 // An mspace pointer used for allocation. Equals mspace_ or nullptr after InvalidateAllocator()
147 // is called.
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800148 void* mspace_for_alloc_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700149
Ian Rogers1d54e732013-05-02 21:10:01 -0700150 friend class collector::MarkSweep;
151
152 DISALLOW_COPY_AND_ASSIGN(DlMallocSpace);
153};
154
155} // namespace space
156} // namespace gc
157} // namespace art
158
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700159#endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_