blob: e12ef4aa7179dceef8bdc75ae2d4f70662dcb8d8 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro1fb86202011-06-27 17:43:13 -07002
3#ifndef ART_SRC_HEAP_H_
4#define ART_SRC_HEAP_H_
5
Carl Shapiro58551df2011-07-24 03:09:51 -07006#include <vector>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "globals.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "object_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070010#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070011
Elliott Hughes3e465b12011-09-02 18:26:12 -070012#define VERIFY_OBJECT_ENABLED 0
13
Carl Shapiro1fb86202011-06-27 17:43:13 -070014namespace art {
15
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070016class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070017class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070018class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070019class Space;
20class HeapBitmap;
21
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class Heap {
23 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070024 static const size_t kInitialSize = 64 * MB; // TODO: lower to 4
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070026 static const size_t kMaximumSize = 64 * MB; // TODO: lower to 16
Carl Shapiro69759ea2011-07-21 18:13:35 -070027
Elliott Hughes410c0c82011-09-01 17:58:25 -070028 typedef void (RootVisitor)(const Object* root, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070029
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030 // Create a heap with the requested sizes. The optional boot image may
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031 // be NULL, otherwise it is an image filename created by ImageWriter.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070032 // image_file_names specifies application images to load.
Elliott Hughesbe759c62011-09-08 19:38:21 -070033 static void Init(size_t starting_size, size_t maximum_size,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070034 const char* boot_image_file_name,
35 std::vector<const char*>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070036
Carl Shapiro69759ea2011-07-21 18:13:35 -070037 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070038
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070039 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070040 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070041
Elliott Hughesa2501992011-08-26 19:39:54 -070042 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070043#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070045#else
46 static void VerifyObject(const Object *obj) {}
47#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070048
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070049 // Check sanity of all live references. Requires the heap lock.
50 static void VerifyHeap();
51
Elliott Hughesa2501992011-08-26 19:39:54 -070052 // A weaker test than VerifyObject that doesn't require the heap lock,
53 // and doesn't abort on error, allowing the caller to report more
54 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070055 static bool IsHeapAddress(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070056
Carl Shapiro69759ea2011-07-21 18:13:35 -070057 // Initiates an explicit garbage collection.
58 static void CollectGarbage();
59
Elliott Hughesbf86d042011-08-31 17:53:14 -070060 // Implements java.lang.Runtime.maxMemory.
61 static int64_t GetMaxMemory();
62 // Implements java.lang.Runtime.totalMemory.
63 static int64_t GetTotalMemory();
64 // Implements java.lang.Runtime.freeMemory.
65 static int64_t GetFreeMemory();
66
Elliott Hughes7ede61e2011-09-14 18:18:06 -070067 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
68 static void ClearGrowthLimit() {
69 UNIMPLEMENTED(WARNING);
70 }
71 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
72 static float GetTargetHeapUtilization() {
73 UNIMPLEMENTED(WARNING);
74 return 0.0f;
75 }
76 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
77 static void SetTargetHeapUtilization(float target) {
78 UNIMPLEMENTED(WARNING);
79 }
80
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 // Blocks the caller until the garbage collector becomes idle.
82 static void WaitForConcurrentGcToComplete();
83
Elliott Hughes92b3b562011-09-08 16:32:26 -070084 static void Lock();
85
86 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -070087
Carl Shapiro58551df2011-07-24 03:09:51 -070088 static const std::vector<Space*>& GetSpaces() {
89 return spaces_;
90 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070091
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 static Space* GetBootSpace() {
93 return boot_space_;
94 }
95
Carl Shapiro58551df2011-07-24 03:09:51 -070096 static HeapBitmap* GetLiveBits() {
97 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070098 }
Carl Shapiro58551df2011-07-24 03:09:51 -070099
100 static HeapBitmap* GetMarkBits() {
101 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700102 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700103
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
105 MemberOffset reference_queue_offset,
106 MemberOffset reference_queueNext_offset,
107 MemberOffset reference_pendingNext_offset,
108 MemberOffset finalizer_reference_zombie_offset) {
109 CHECK_NE(reference_referent_offset.Uint32Value(), 0U);
110 CHECK_NE(reference_queue_offset.Uint32Value(), 0U);
111 CHECK_NE(reference_queueNext_offset.Uint32Value(), 0U);
112 CHECK_NE(reference_pendingNext_offset.Uint32Value(), 0U);
113 CHECK_NE(finalizer_reference_zombie_offset.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700114 reference_referent_offset_ = reference_referent_offset;
115 reference_queue_offset_ = reference_queue_offset;
116 reference_queueNext_offset_ = reference_queueNext_offset;
117 reference_pendingNext_offset_ = reference_pendingNext_offset;
118 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
119 }
120
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700121 static MemberOffset GetReferenceReferentOffset() {
122 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700123 return reference_referent_offset_;
124 }
125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126 static MemberOffset GetReferenceQueueOffset() {
127 DCHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700128 return reference_queue_offset_;
129 }
130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700131 static MemberOffset GetReferenceQueueNextOffset() {
132 DCHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700133 return reference_queueNext_offset_;
134 }
135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136 static MemberOffset GetReferencePendingNextOffset() {
137 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700138 return reference_pendingNext_offset_;
139 }
140
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 static MemberOffset GetFinalizerReferenceZombieOffset() {
142 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700143 return finalizer_reference_zombie_offset_;
144 }
145
Elliott Hughes85d15452011-09-16 17:33:01 -0700146 static void EnableObjectValidation() {
147 verify_objects_ = true;
148 }
149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700151 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152 }
153
Elliott Hughes92b3b562011-09-08 16:32:26 -0700154 // Callers must hold the heap lock.
155 static void RecordFreeLocked(Space* space, const Object* object);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700156
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700157 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
158 // The call is not needed if NULL is stored in the field.
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700159 static void WriteBarrier(const Object* object) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700160#ifdef CONCURRENT_GARBAGE_COLLECTOR
161 // TODO: we need card marking for a concurrent collector.
162 UNIMPLEMENTED(FATAL);
163#endif
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700164 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700165
Carl Shapiro58551df2011-07-24 03:09:51 -0700166 private:
167 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700168 static Object* AllocateLocked(size_t num_bytes);
169 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700170
Elliott Hughes92b3b562011-09-08 16:32:26 -0700171 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700172 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173
174 static void CollectGarbageInternal();
175
176 static void GrowForUtilization();
177
Elliott Hughes92b3b562011-09-08 16:32:26 -0700178 static void VerifyObjectLocked(const Object *obj);
179
Brian Carlstrom78128a62011-09-15 17:21:19 -0700180 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700181
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182 static Mutex* lock_;
183
Carl Shapiro58551df2011-07-24 03:09:51 -0700184 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185
Brian Carlstroma663ea52011-08-19 23:33:41 -0700186 // Space loaded from an image
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700187 // TODO: remove after intern_addr is removed
Brian Carlstroma663ea52011-08-19 23:33:41 -0700188 static Space* boot_space_;
189
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700190 // default Space for allocations
191 static Space* alloc_space_;
192
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193 static HeapBitmap* mark_bitmap_;
194
195 static HeapBitmap* live_bitmap_;
196
Carl Shapiro58551df2011-07-24 03:09:51 -0700197 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 static size_t maximum_size_;
199
Carl Shapiro58551df2011-07-24 03:09:51 -0700200 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 static bool is_gc_running_;
202
Carl Shapiro58551df2011-07-24 03:09:51 -0700203 // Number of bytes allocated. Adjusted after each allocation and
204 // free.
205 static size_t num_bytes_allocated_;
206
207 // Number of objects allocated. Adjusted after each allocation and
208 // free.
209 static size_t num_objects_allocated_;
210
Brian Carlstrom1f870082011-08-23 16:02:11 -0700211 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700213
214 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700216
217 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700219
220 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700222
223 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 static MemberOffset finalizer_reference_zombie_offset_;
225
Elliott Hughes85d15452011-09-16 17:33:01 -0700226 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700227
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
229};
230
Carl Shapiro1fb86202011-06-27 17:43:13 -0700231} // namespace art
232
233#endif // ART_SRC_HEAP_H_