blob: 1c2b7c9b9ec5cda1ea6f216c48113c34c705e472 [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
Carl Shapiro69759ea2011-07-21 18:13:35 -070067 // Blocks the caller until the garbage collector becomes idle.
68 static void WaitForConcurrentGcToComplete();
69
Elliott Hughes92b3b562011-09-08 16:32:26 -070070 static void Lock();
71
72 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -070073
Carl Shapiro58551df2011-07-24 03:09:51 -070074 static const std::vector<Space*>& GetSpaces() {
75 return spaces_;
76 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070077
Brian Carlstroma663ea52011-08-19 23:33:41 -070078 static Space* GetBootSpace() {
79 return boot_space_;
80 }
81
Carl Shapiro58551df2011-07-24 03:09:51 -070082 static HeapBitmap* GetLiveBits() {
83 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070084 }
Carl Shapiro58551df2011-07-24 03:09:51 -070085
86 static HeapBitmap* GetMarkBits() {
87 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070088 }
Carl Shapiro58551df2011-07-24 03:09:51 -070089
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070090 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
91 MemberOffset reference_queue_offset,
92 MemberOffset reference_queueNext_offset,
93 MemberOffset reference_pendingNext_offset,
94 MemberOffset finalizer_reference_zombie_offset) {
95 CHECK_NE(reference_referent_offset.Uint32Value(), 0U);
96 CHECK_NE(reference_queue_offset.Uint32Value(), 0U);
97 CHECK_NE(reference_queueNext_offset.Uint32Value(), 0U);
98 CHECK_NE(reference_pendingNext_offset.Uint32Value(), 0U);
99 CHECK_NE(finalizer_reference_zombie_offset.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700100 reference_referent_offset_ = reference_referent_offset;
101 reference_queue_offset_ = reference_queue_offset;
102 reference_queueNext_offset_ = reference_queueNext_offset;
103 reference_pendingNext_offset_ = reference_pendingNext_offset;
104 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
105 }
106
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700107 static MemberOffset GetReferenceReferentOffset() {
108 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700109 return reference_referent_offset_;
110 }
111
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700112 static MemberOffset GetReferenceQueueOffset() {
113 DCHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700114 return reference_queue_offset_;
115 }
116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 static MemberOffset GetReferenceQueueNextOffset() {
118 DCHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700119 return reference_queueNext_offset_;
120 }
121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700122 static MemberOffset GetReferencePendingNextOffset() {
123 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700124 return reference_pendingNext_offset_;
125 }
126
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700127 static MemberOffset GetFinalizerReferenceZombieOffset() {
128 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700129 return finalizer_reference_zombie_offset_;
130 }
131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132 static void DisableObjectValidation() {
133 // TODO: remove this hack necessary for image writing
134 verify_object_disabled_ = true;
135 }
136
Elliott Hughes92b3b562011-09-08 16:32:26 -0700137 // Callers must hold the heap lock.
138 static void RecordFreeLocked(Space* space, const Object* object);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700139
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700140 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
141 // The call is not needed if NULL is stored in the field.
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700142 static void WriteBarrier(const Object* object) {
143 // TODO: re-enable card marking when concurrent collector is active.
144 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700145
Carl Shapiro58551df2011-07-24 03:09:51 -0700146 private:
147 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700148 static Object* AllocateLocked(size_t num_bytes);
149 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700150
Elliott Hughes92b3b562011-09-08 16:32:26 -0700151 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700152 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153
154 static void CollectGarbageInternal();
155
156 static void GrowForUtilization();
157
Elliott Hughes92b3b562011-09-08 16:32:26 -0700158 static void VerifyObjectLocked(const Object *obj);
159
160 static void VerificationCallback(Object* obj, void *arg);
161
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 static Mutex* lock_;
163
Carl Shapiro58551df2011-07-24 03:09:51 -0700164 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700165
Brian Carlstroma663ea52011-08-19 23:33:41 -0700166 // Space loaded from an image
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700167 // TODO: remove after intern_addr is removed
Brian Carlstroma663ea52011-08-19 23:33:41 -0700168 static Space* boot_space_;
169
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700170 // default Space for allocations
171 static Space* alloc_space_;
172
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173 static HeapBitmap* mark_bitmap_;
174
175 static HeapBitmap* live_bitmap_;
176
Carl Shapiro58551df2011-07-24 03:09:51 -0700177 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178 static size_t maximum_size_;
179
Carl Shapiro58551df2011-07-24 03:09:51 -0700180 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700181 static bool is_gc_running_;
182
Carl Shapiro58551df2011-07-24 03:09:51 -0700183 // Number of bytes allocated. Adjusted after each allocation and
184 // free.
185 static size_t num_bytes_allocated_;
186
187 // Number of objects allocated. Adjusted after each allocation and
188 // free.
189 static size_t num_objects_allocated_;
190
Brian Carlstrom1f870082011-08-23 16:02:11 -0700191 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700193
194 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700196
197 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700199
200 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700202
203 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 static MemberOffset finalizer_reference_zombie_offset_;
205
206 static bool verify_object_disabled_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700207
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
209};
210
Carl Shapiro1fb86202011-06-27 17:43:13 -0700211} // namespace art
212
213#endif // ART_SRC_HEAP_H_