blob: 6b056a27fa7308c0b370386ffa4dc2f033272c5e [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
Ian Rogers5d76c432011-10-31 21:42:49 -070022#include "card_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070024#include "heap_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070025#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070026
Elliott Hughes3e465b12011-09-02 18:26:12 -070027#define VERIFY_OBJECT_ENABLED 0
28
Carl Shapiro1fb86202011-06-27 17:43:13 -070029namespace art {
30
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070032class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070033class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070034class Space;
Ian Rogers5d4bdc22011-11-02 22:15:43 -070035class Thread;
Carl Shapiro69759ea2011-07-21 18:13:35 -070036class HeapBitmap;
37
Carl Shapiro1fb86202011-06-27 17:43:13 -070038class Heap {
39 public:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070040 static const size_t kInitialSize = 4 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Brian Carlstrom27ec9612011-09-19 20:20:38 -070042 static const size_t kMaximumSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070043
Elliott Hughes410c0c82011-09-01 17:58:25 -070044 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070045 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046
Brian Carlstrom58ae9412011-10-04 00:56:06 -070047 // Create a heap with the requested sizes. The possible empty
48 // image_file_names names specify Spaces to load based on
49 // ImageWriter output.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080050 static void Init(size_t starting_size, size_t maximum_size, size_t growth_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070051 const std::vector<std::string>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070054
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070055 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070056 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070057
Elliott Hughesa2501992011-08-26 19:39:54 -070058 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070059#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070061#else
62 static void VerifyObject(const Object *obj) {}
63#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070064
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070065 // Check sanity of all live references. Requires the heap lock.
66 static void VerifyHeap();
67
Elliott Hughes6a5bd492011-10-28 14:33:57 -070068 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -070069 // and doesn't abort on error, allowing the caller to report more
70 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071 static bool IsHeapAddress(const Object* obj);
Elliott Hughes6a5bd492011-10-28 14:33:57 -070072 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
73 // Requires the heap lock to be held.
74 static bool IsLiveObjectLocked(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070075
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 // Initiates an explicit garbage collection.
77 static void CollectGarbage();
78
Elliott Hughesbf86d042011-08-31 17:53:14 -070079 // Implements java.lang.Runtime.maxMemory.
80 static int64_t GetMaxMemory();
81 // Implements java.lang.Runtime.totalMemory.
82 static int64_t GetTotalMemory();
83 // Implements java.lang.Runtime.freeMemory.
84 static int64_t GetFreeMemory();
85
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070086 // Implements VMDebug.countInstancesOfClass.
87 static int64_t CountInstances(Class* c, bool count_assignable);
88
Elliott Hughes7ede61e2011-09-14 18:18:06 -070089 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
jeffhaoc1160702011-10-27 15:48:45 -070090 static void ClearGrowthLimit();
91
Elliott Hughes7ede61e2011-09-14 18:18:06 -070092 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
93 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -070094 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070095 }
96 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
97 static void SetTargetHeapUtilization(float target) {
Brian Carlstrom395520e2011-09-25 19:35:00 -070098 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070099 }
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700100 // Sets the maximum number of bytes that the heap is allowed to allocate
101 // from the system. Clamps to the appropriate maximum value.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700102 static void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103
Carl Shapiro69759ea2011-07-21 18:13:35 -0700104 // Blocks the caller until the garbage collector becomes idle.
105 static void WaitForConcurrentGcToComplete();
106
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700107 static pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700108 static void Lock();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700109 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700110
Carl Shapiro58551df2011-07-24 03:09:51 -0700111 static const std::vector<Space*>& GetSpaces() {
112 return spaces_;
113 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700114
Carl Shapiro58551df2011-07-24 03:09:51 -0700115 static HeapBitmap* GetLiveBits() {
116 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700117 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700118
119 static HeapBitmap* GetMarkBits() {
120 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700121 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700122
Elliott Hughesadb460d2011-10-05 17:02:34 -0700123 static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
124 Class* java_lang_ref_ReferenceQueue);
125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
127 MemberOffset reference_queue_offset,
128 MemberOffset reference_queueNext_offset,
129 MemberOffset reference_pendingNext_offset,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700130 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700131
Elliott Hughesadb460d2011-10-05 17:02:34 -0700132 static Object* GetReferenceReferent(Object* reference);
133 static void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700134
Elliott Hughesadb460d2011-10-05 17:02:34 -0700135 // Returns true if the reference object has not yet been enqueued.
136 static bool IsEnqueuable(const Object* ref);
137 static void EnqueueReference(Object* ref, Object** list);
138 static void EnqueuePendingReference(Object* ref, Object** list);
139 static Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700140
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 static MemberOffset GetReferencePendingNextOffset() {
142 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700143 return reference_pendingNext_offset_;
144 }
145
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 static MemberOffset GetFinalizerReferenceZombieOffset() {
147 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700148 return finalizer_reference_zombie_offset_;
149 }
150
Elliott Hughes85d15452011-09-16 17:33:01 -0700151 static void EnableObjectValidation() {
152 verify_objects_ = true;
153 }
154
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700156 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 }
158
Elliott Hughes92b3b562011-09-08 16:32:26 -0700159 // Callers must hold the heap lock.
Elliott Hughes307f75d2011-10-12 18:04:40 -0700160 static void RecordFreeLocked(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700161
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700162 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
163 // The call is not needed if NULL is stored in the field.
Ian Rogers5d76c432011-10-31 21:42:49 -0700164 static void WriteBarrierField(const Object* dest, MemberOffset offset, const Object* new_val) {
165 if (!card_marking_disabled_) {
166 card_table_->MarkCard(dest);
167 }
168 }
169
170 // Write barrier for array operations that update many field positions
171 static void WriteBarrierArray(const Object* dest, int pos, size_t len) {
172 if (UNLIKELY(!card_marking_disabled_)) {
173 card_table_->MarkCard(dest);
174 }
175 }
176
177 static CardTable* GetCardTable() {
178 return card_table_;
179 }
180
181 static void DisableCardMarking() {
182 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
183 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700184 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700185
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700186 // dlmalloc_walk_heap-compatible heap walker.
187 static void WalkHeap(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg);
188
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700189 static void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700190
Elliott Hughes7162ad92011-10-27 14:08:42 -0700191 static size_t GetBytesAllocated() { return num_bytes_allocated_; }
192 static size_t GetObjectsAllocated() { return num_objects_allocated_; }
193
Ian Rogers5d76c432011-10-31 21:42:49 -0700194 static Space* GetAllocSpace() {
195 return alloc_space_;
196 }
197
Carl Shapiro58551df2011-07-24 03:09:51 -0700198 private:
199 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700200 static Object* AllocateLocked(size_t num_bytes);
201 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700202
Elliott Hughesadb460d2011-10-05 17:02:34 -0700203 // Pushes a list of cleared references out to the managed heap.
204 static void EnqueueClearedReferences(Object** cleared_references);
205
Elliott Hughes92b3b562011-09-08 16:32:26 -0700206 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700207 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208
209 static void CollectGarbageInternal();
210
211 static void GrowForUtilization();
212
Elliott Hughes92b3b562011-09-08 16:32:26 -0700213 static void VerifyObjectLocked(const Object *obj);
214
Brian Carlstrom78128a62011-09-15 17:21:19 -0700215 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700216
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 static Mutex* lock_;
218
Carl Shapiro58551df2011-07-24 03:09:51 -0700219 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700221 // default Space for allocations
222 static Space* alloc_space_;
223
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224 static HeapBitmap* mark_bitmap_;
225
226 static HeapBitmap* live_bitmap_;
227
Ian Rogers5d76c432011-10-31 21:42:49 -0700228 static CardTable* card_table_;
229
230 // Used by the image writer to disable card marking on copied objects
231 // TODO: remove
232 static bool card_marking_disabled_;
233
Carl Shapiro58551df2011-07-24 03:09:51 -0700234 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 static size_t maximum_size_;
236
jeffhaoc1160702011-10-27 15:48:45 -0700237 // The largest size the heap may grow. This value allows the app to limit the
238 // growth below the maximum size. This is a work around until we can
239 // dynamically set the maximum size. This value can range between the starting
240 // size and the maximum size but should never be set below the current
241 // footprint of the heap.
242 static size_t growth_size_;
243
Carl Shapiro58551df2011-07-24 03:09:51 -0700244 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 static bool is_gc_running_;
246
Carl Shapiro58551df2011-07-24 03:09:51 -0700247 // Number of bytes allocated. Adjusted after each allocation and
248 // free.
249 static size_t num_bytes_allocated_;
250
251 // Number of objects allocated. Adjusted after each allocation and
252 // free.
253 static size_t num_objects_allocated_;
254
Elliott Hughesadb460d2011-10-05 17:02:34 -0700255 static Class* java_lang_ref_FinalizerReference_;
256 static Class* java_lang_ref_ReferenceQueue_;
257
Brian Carlstrom1f870082011-08-23 16:02:11 -0700258 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700260
261 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700263
264 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700266
267 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700269
270 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 static MemberOffset finalizer_reference_zombie_offset_;
272
Brian Carlstrom395520e2011-09-25 19:35:00 -0700273 // Target ideal heap utilization ratio
274 static float target_utilization_;
275
Elliott Hughes85d15452011-09-16 17:33:01 -0700276 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700277
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
279};
280
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700281class ScopedHeapLock {
282 public:
283 ScopedHeapLock() {
284 Heap::Lock();
285 }
286
287 ~ScopedHeapLock() {
288 Heap::Unlock();
289 }
290
291 private:
292 DISALLOW_COPY_AND_ASSIGN(ScopedHeapLock);
293};
294
Carl Shapiro1fb86202011-06-27 17:43:13 -0700295} // namespace art
296
297#endif // ART_SRC_HEAP_H_