Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2 | |
| 3 | #ifndef ART_SRC_HEAP_H_ |
| 4 | #define ART_SRC_HEAP_H_ |
| 5 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 8 | #include "globals.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "object_bitmap.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 10 | #include "offsets.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 11 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 12 | #define VERIFY_OBJECT_ENABLED 0 |
| 13 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 14 | namespace art { |
| 15 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 16 | class Class; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 17 | class Mutex; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 18 | class Object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | class Space; |
| 20 | class HeapBitmap; |
| 21 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 22 | class Heap { |
| 23 | public: |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 24 | static const size_t kInitialSize = 64 * MB; // TODO: lower to 4 |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 25 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 26 | static const size_t kMaximumSize = 64 * MB; // TODO: lower to 16 |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 28 | typedef void (RootVisitor)(const Object* root, void* arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 30 | // Create a heap with the requested sizes. The optional boot image may |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 31 | // be NULL, otherwise it is an image filename created by ImageWriter. |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 32 | // image_file_names specifies application images to load. |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 33 | static void Init(size_t starting_size, size_t maximum_size, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 34 | const char* boot_image_file_name, |
| 35 | std::vector<const char*>& image_file_names); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 36 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 37 | static void Destroy(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 38 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 39 | // Allocates and initializes storage for an object instance. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 40 | static Object* AllocObject(Class* klass, size_t num_bytes); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 42 | // Check sanity of given reference. Requires the heap lock. |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 43 | #if VERIFY_OBJECT_ENABLED |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 44 | static void VerifyObject(const Object *obj); |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 45 | #else |
| 46 | static void VerifyObject(const Object *obj) {} |
| 47 | #endif |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 49 | // Check sanity of all live references. Requires the heap lock. |
| 50 | static void VerifyHeap(); |
| 51 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 52 | // 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 Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 55 | static bool IsHeapAddress(const Object* obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 56 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 57 | // Initiates an explicit garbage collection. |
| 58 | static void CollectGarbage(); |
| 59 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 60 | // 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 Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 67 | // 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 81 | // Blocks the caller until the garbage collector becomes idle. |
| 82 | static void WaitForConcurrentGcToComplete(); |
| 83 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 84 | static void Lock(); |
| 85 | |
| 86 | static void Unlock(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 87 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 88 | static const std::vector<Space*>& GetSpaces() { |
| 89 | return spaces_; |
| 90 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 91 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 92 | static Space* GetBootSpace() { |
| 93 | return boot_space_; |
| 94 | } |
| 95 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 96 | static HeapBitmap* GetLiveBits() { |
| 97 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 98 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 99 | |
| 100 | static HeapBitmap* GetMarkBits() { |
| 101 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 102 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 104 | 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 Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 114 | 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 Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 121 | static MemberOffset GetReferenceReferentOffset() { |
| 122 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 123 | return reference_referent_offset_; |
| 124 | } |
| 125 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 126 | static MemberOffset GetReferenceQueueOffset() { |
| 127 | DCHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 128 | return reference_queue_offset_; |
| 129 | } |
| 130 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 131 | static MemberOffset GetReferenceQueueNextOffset() { |
| 132 | DCHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 133 | return reference_queueNext_offset_; |
| 134 | } |
| 135 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 136 | static MemberOffset GetReferencePendingNextOffset() { |
| 137 | DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 138 | return reference_pendingNext_offset_; |
| 139 | } |
| 140 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 141 | static MemberOffset GetFinalizerReferenceZombieOffset() { |
| 142 | DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 143 | return finalizer_reference_zombie_offset_; |
| 144 | } |
| 145 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 146 | static void DisableObjectValidation() { |
| 147 | // TODO: remove this hack necessary for image writing |
| 148 | verify_object_disabled_ = true; |
| 149 | } |
| 150 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 151 | // Callers must hold the heap lock. |
| 152 | static void RecordFreeLocked(Space* space, const Object* object); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 153 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 154 | // Must be called if a field of an Object in the heap changes, and before any GC safe-point. |
| 155 | // The call is not needed if NULL is stored in the field. |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 156 | static void WriteBarrier(const Object* object) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 157 | #ifdef CONCURRENT_GARBAGE_COLLECTOR |
| 158 | // TODO: we need card marking for a concurrent collector. |
| 159 | UNIMPLEMENTED(FATAL); |
| 160 | #endif |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 161 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 162 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 163 | private: |
| 164 | // Allocates uninitialized storage. |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 165 | static Object* AllocateLocked(size_t num_bytes); |
| 166 | static Object* AllocateLocked(Space* space, size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 168 | static void RecordAllocationLocked(Space* space, const Object* object); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 169 | static void RecordImageAllocations(Space* space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 170 | |
| 171 | static void CollectGarbageInternal(); |
| 172 | |
| 173 | static void GrowForUtilization(); |
| 174 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 175 | static void VerifyObjectLocked(const Object *obj); |
| 176 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 177 | static void VerificationCallback(Object* obj, void* arg); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 178 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 179 | static Mutex* lock_; |
| 180 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 181 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 182 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 183 | // Space loaded from an image |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 184 | // TODO: remove after intern_addr is removed |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 185 | static Space* boot_space_; |
| 186 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 187 | // default Space for allocations |
| 188 | static Space* alloc_space_; |
| 189 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 190 | static HeapBitmap* mark_bitmap_; |
| 191 | |
| 192 | static HeapBitmap* live_bitmap_; |
| 193 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 194 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 195 | static size_t maximum_size_; |
| 196 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 197 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 198 | static bool is_gc_running_; |
| 199 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 200 | // Number of bytes allocated. Adjusted after each allocation and |
| 201 | // free. |
| 202 | static size_t num_bytes_allocated_; |
| 203 | |
| 204 | // Number of objects allocated. Adjusted after each allocation and |
| 205 | // free. |
| 206 | static size_t num_objects_allocated_; |
| 207 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 208 | // offset of java.lang.ref.Reference.referent |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 209 | static MemberOffset reference_referent_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 210 | |
| 211 | // offset of java.lang.ref.Reference.queue |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 212 | static MemberOffset reference_queue_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 213 | |
| 214 | // offset of java.lang.ref.Reference.queueNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 215 | static MemberOffset reference_queueNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 216 | |
| 217 | // offset of java.lang.ref.Reference.pendingNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 218 | static MemberOffset reference_pendingNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 219 | |
| 220 | // offset of java.lang.ref.FinalizerReference.zombie |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 221 | static MemberOffset finalizer_reference_zombie_offset_; |
| 222 | |
| 223 | static bool verify_object_disabled_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 224 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 225 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 226 | }; |
| 227 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 228 | } // namespace art |
| 229 | |
| 230 | #endif // ART_SRC_HEAP_H_ |