blob: c5293147d47e03bd0de9d14324fb7076f6fae230 [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
20#include "gc/allocator/dlmalloc.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070021#include "malloc_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#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.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070034class DlMallocSpace : public MallocSpace {
Ian Rogers1d54e732013-05-02 21:10:01 -070035 public:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070036 // Create a DlMallocSpace with the requested sizes. The requested
Ian Rogers1d54e732013-05-02 21:10:01 -070037 // base address is not guaranteed to be granted, if it is required,
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070038 // the caller should call Begin on the returned space to confirm the
39 // request was granted.
Ian Rogers1d54e732013-05-02 21:10:01 -070040 static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
41 size_t capacity, byte* requested_begin);
42
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070043 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
44 size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070045 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
Ian Rogers1d54e732013-05-02 21:10:01 -070046 virtual size_t AllocationSize(const mirror::Object* obj);
47 virtual size_t Free(Thread* self, mirror::Object* ptr);
48 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs);
49
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070050 mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated);
51
52 size_t AllocationSizeNonvirtual(const mirror::Object* obj) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070053 void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
54 return mspace_usable_size(obj_ptr) + kChunkOverhead;
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070055 }
56
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070057#ifndef NDEBUG
58 // Override only in the debug build.
59 void CheckMoreCoreForPrecondition();
60#endif
Ian Rogers1d54e732013-05-02 21:10:01 -070061
62 void* GetMspace() const {
63 return mspace_;
64 }
65
Ian Rogers1d54e732013-05-02 21:10:01 -070066 size_t Trim();
67
68 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
69 // in use, indicated by num_bytes equaling zero.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070070 void Walk(WalkCallback callback, void* arg) LOCKS_EXCLUDED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070071
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -070072 // Returns the number of bytes that the space has currently obtained from the system. This is
73 // greater or equal to the amount of live data in the space.
74 size_t GetFootprint();
75
Ian Rogers1d54e732013-05-02 21:10:01 -070076 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
77 size_t GetFootprintLimit();
78
79 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
80 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
81 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
82 void SetFootprintLimit(size_t limit);
83
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070084 MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
85 byte* begin, byte* end, byte* limit, size_t growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -070086
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070087 uint64_t GetBytesAllocated();
88 uint64_t GetObjectsAllocated();
89 uint64_t GetTotalBytesAllocated() {
90 return GetBytesAllocated() + total_bytes_freed_;
Ian Rogers1d54e732013-05-02 21:10:01 -070091 }
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070092 uint64_t GetTotalObjectsAllocated() {
93 return GetObjectsAllocated() + total_objects_freed_;
Ian Rogers1d54e732013-05-02 21:10:01 -070094 }
95
Mathieu Chartier0f72e412013-09-06 16:40:01 -070096 // Returns the class of a recently freed object.
97 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
98
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070099 virtual void InvalidateAllocator() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800100 mspace_for_alloc_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700101 }
102
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700103 virtual bool IsDlMallocSpace() const {
104 return true;
105 }
106 virtual DlMallocSpace* AsDlMallocSpace() {
107 return this;
108 }
109
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 protected:
111 DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700112 byte* limit, size_t growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -0700113
114 private:
115 size_t InternalAllocationSize(const mirror::Object* obj);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116
117 mirror::Object* AllocWithoutGrowthLocked(Thread* self, size_t num_bytes, size_t* bytes_allocated)
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700118 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700119
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800120 void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, bool /*low_memory_mode*/) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700121 return CreateMspace(base, morecore_start, initial_size);
122 }
123 static void* CreateMspace(void* base, size_t morecore_start, size_t initial_size);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700124
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700125 // Approximate number of bytes and objects which have been deallocated in the space.
126 size_t total_bytes_freed_;
127 size_t total_objects_freed_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700128
Ian Rogers1d54e732013-05-02 21:10:01 -0700129 // The boundary tag overhead.
130 static const size_t kChunkOverhead = kWordSize;
131
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 // Underlying malloc space
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800133 void* const mspace_;
134
135 // A mspace pointer used for allocation. Equals to what mspace_
136 // points to or nullptr after InvalidateAllocator() is called.
137 void* mspace_for_alloc_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700138
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 friend class collector::MarkSweep;
140
141 DISALLOW_COPY_AND_ASSIGN(DlMallocSpace);
142};
143
144} // namespace space
145} // namespace gc
146} // namespace art
147
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700148#endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_