blob: b669a869f24901c20c8ed0247b75f1962f2d724d [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
2 * Copyright (C) 2008 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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_HEAP_H_
18#define ART_SRC_HEAP_H_
19
Carl Shapiro58551df2011-07-24 03:09:51 -070020#include <vector>
21
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "globals.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "object_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070024#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070025
Elliott Hughes3e465b12011-09-02 18:26:12 -070026#define VERIFY_OBJECT_ENABLED 0
27
Carl Shapiro1fb86202011-06-27 17:43:13 -070028namespace art {
29
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070030class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070031class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070032class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033class Space;
34class HeapBitmap;
35
Carl Shapiro1fb86202011-06-27 17:43:13 -070036class Heap {
37 public:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070038 static const size_t kInitialSize = 4 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070039
Brian Carlstrom27ec9612011-09-19 20:20:38 -070040 static const size_t kMaximumSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Elliott Hughes410c0c82011-09-01 17:58:25 -070042 typedef void (RootVisitor)(const Object* root, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070043
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044 // Create a heap with the requested sizes. The optional boot image may
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070045 // be NULL, otherwise it is an image filename created by ImageWriter.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070046 // image_file_names specifies application images to load.
Elliott Hughesbe759c62011-09-08 19:38:21 -070047 static void Init(size_t starting_size, size_t maximum_size,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070048 const char* boot_image_file_name,
49 std::vector<const char*>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070052
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070053 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070054 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070055
Elliott Hughesa2501992011-08-26 19:39:54 -070056 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070057#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070059#else
60 static void VerifyObject(const Object *obj) {}
61#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070062
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070063 // Check sanity of all live references. Requires the heap lock.
64 static void VerifyHeap();
65
Elliott Hughesa2501992011-08-26 19:39:54 -070066 // A weaker test than VerifyObject that doesn't require the heap lock,
67 // and doesn't abort on error, allowing the caller to report more
68 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069 static bool IsHeapAddress(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070070
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 // Initiates an explicit garbage collection.
72 static void CollectGarbage();
73
Elliott Hughesbf86d042011-08-31 17:53:14 -070074 // Implements java.lang.Runtime.maxMemory.
75 static int64_t GetMaxMemory();
76 // Implements java.lang.Runtime.totalMemory.
77 static int64_t GetTotalMemory();
78 // Implements java.lang.Runtime.freeMemory.
79 static int64_t GetFreeMemory();
80
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070081 // Implements VMDebug.countInstancesOfClass.
82 static int64_t CountInstances(Class* c, bool count_assignable);
83
Elliott Hughes7ede61e2011-09-14 18:18:06 -070084 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
85 static void ClearGrowthLimit() {
86 UNIMPLEMENTED(WARNING);
87 }
88 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
89 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -070090 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091 }
92 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
93 static void SetTargetHeapUtilization(float target) {
Brian Carlstrom395520e2011-09-25 19:35:00 -070094 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070095 }
96
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 // Blocks the caller until the garbage collector becomes idle.
98 static void WaitForConcurrentGcToComplete();
99
Elliott Hughes92b3b562011-09-08 16:32:26 -0700100 static void Lock();
101
102 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103
Carl Shapiro58551df2011-07-24 03:09:51 -0700104 static const std::vector<Space*>& GetSpaces() {
105 return spaces_;
106 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700107
Brian Carlstroma663ea52011-08-19 23:33:41 -0700108 static Space* GetBootSpace() {
109 return boot_space_;
110 }
111
Carl Shapiro58551df2011-07-24 03:09:51 -0700112 static HeapBitmap* GetLiveBits() {
113 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700114 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700115
116 static HeapBitmap* GetMarkBits() {
117 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700118 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700119
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700120 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
121 MemberOffset reference_queue_offset,
122 MemberOffset reference_queueNext_offset,
123 MemberOffset reference_pendingNext_offset,
124 MemberOffset finalizer_reference_zombie_offset) {
125 CHECK_NE(reference_referent_offset.Uint32Value(), 0U);
126 CHECK_NE(reference_queue_offset.Uint32Value(), 0U);
127 CHECK_NE(reference_queueNext_offset.Uint32Value(), 0U);
128 CHECK_NE(reference_pendingNext_offset.Uint32Value(), 0U);
129 CHECK_NE(finalizer_reference_zombie_offset.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700130 reference_referent_offset_ = reference_referent_offset;
131 reference_queue_offset_ = reference_queue_offset;
132 reference_queueNext_offset_ = reference_queueNext_offset;
133 reference_pendingNext_offset_ = reference_pendingNext_offset;
134 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
135 }
136
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700137 static MemberOffset GetReferenceReferentOffset() {
138 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700139 return reference_referent_offset_;
140 }
141
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 static MemberOffset GetReferenceQueueOffset() {
143 DCHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700144 return reference_queue_offset_;
145 }
146
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700147 static MemberOffset GetReferenceQueueNextOffset() {
148 DCHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700149 return reference_queueNext_offset_;
150 }
151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152 static MemberOffset GetReferencePendingNextOffset() {
153 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700154 return reference_pendingNext_offset_;
155 }
156
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 static MemberOffset GetFinalizerReferenceZombieOffset() {
158 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700159 return finalizer_reference_zombie_offset_;
160 }
161
Elliott Hughes85d15452011-09-16 17:33:01 -0700162 static void EnableObjectValidation() {
163 verify_objects_ = true;
164 }
165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700167 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 }
169
Elliott Hughes92b3b562011-09-08 16:32:26 -0700170 // Callers must hold the heap lock.
171 static void RecordFreeLocked(Space* space, const Object* object);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700172
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700173 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
174 // The call is not needed if NULL is stored in the field.
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700175 static void WriteBarrier(const Object* object) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700176#ifdef CONCURRENT_GARBAGE_COLLECTOR
177 // TODO: we need card marking for a concurrent collector.
178 UNIMPLEMENTED(FATAL);
179#endif
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700180 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700181
Carl Shapiro58551df2011-07-24 03:09:51 -0700182 private:
183 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700184 static Object* AllocateLocked(size_t num_bytes);
185 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700186
Elliott Hughes92b3b562011-09-08 16:32:26 -0700187 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700188 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700189
190 static void CollectGarbageInternal();
191
192 static void GrowForUtilization();
193
Elliott Hughes92b3b562011-09-08 16:32:26 -0700194 static void VerifyObjectLocked(const Object *obj);
195
Brian Carlstrom78128a62011-09-15 17:21:19 -0700196 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700197
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 static Mutex* lock_;
199
Carl Shapiro58551df2011-07-24 03:09:51 -0700200 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201
Brian Carlstroma663ea52011-08-19 23:33:41 -0700202 // Space loaded from an image
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700203 // TODO: remove after intern_addr is removed
Brian Carlstroma663ea52011-08-19 23:33:41 -0700204 static Space* boot_space_;
205
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700206 // default Space for allocations
207 static Space* alloc_space_;
208
Carl Shapiro69759ea2011-07-21 18:13:35 -0700209 static HeapBitmap* mark_bitmap_;
210
211 static HeapBitmap* live_bitmap_;
212
Carl Shapiro58551df2011-07-24 03:09:51 -0700213 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700214 static size_t maximum_size_;
215
Carl Shapiro58551df2011-07-24 03:09:51 -0700216 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 static bool is_gc_running_;
218
Carl Shapiro58551df2011-07-24 03:09:51 -0700219 // Number of bytes allocated. Adjusted after each allocation and
220 // free.
221 static size_t num_bytes_allocated_;
222
223 // Number of objects allocated. Adjusted after each allocation and
224 // free.
225 static size_t num_objects_allocated_;
226
Brian Carlstrom1f870082011-08-23 16:02:11 -0700227 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700229
230 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700232
233 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700235
236 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700238
239 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 static MemberOffset finalizer_reference_zombie_offset_;
241
Brian Carlstrom395520e2011-09-25 19:35:00 -0700242 // Target ideal heap utilization ratio
243 static float target_utilization_;
244
Elliott Hughes85d15452011-09-16 17:33:01 -0700245 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700246
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
248};
249
Carl Shapiro1fb86202011-06-27 17:43:13 -0700250} // namespace art
251
252#endif // ART_SRC_HEAP_H_