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 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 67 | // Blocks the caller until the garbage collector becomes idle. |
| 68 | static void WaitForConcurrentGcToComplete(); |
| 69 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 70 | static void Lock(); |
| 71 | |
| 72 | static void Unlock(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 73 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 74 | static const std::vector<Space*>& GetSpaces() { |
| 75 | return spaces_; |
| 76 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 78 | static Space* GetBootSpace() { |
| 79 | return boot_space_; |
| 80 | } |
| 81 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 82 | static HeapBitmap* GetLiveBits() { |
| 83 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 84 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 85 | |
| 86 | static HeapBitmap* GetMarkBits() { |
| 87 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 88 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 89 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 90 | 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 Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 100 | 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 Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 107 | static MemberOffset GetReferenceReferentOffset() { |
| 108 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 109 | return reference_referent_offset_; |
| 110 | } |
| 111 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 112 | static MemberOffset GetReferenceQueueOffset() { |
| 113 | DCHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 114 | return reference_queue_offset_; |
| 115 | } |
| 116 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 117 | static MemberOffset GetReferenceQueueNextOffset() { |
| 118 | DCHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 119 | return reference_queueNext_offset_; |
| 120 | } |
| 121 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 122 | static MemberOffset GetReferencePendingNextOffset() { |
| 123 | DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 124 | return reference_pendingNext_offset_; |
| 125 | } |
| 126 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 127 | static MemberOffset GetFinalizerReferenceZombieOffset() { |
| 128 | DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 129 | return finalizer_reference_zombie_offset_; |
| 130 | } |
| 131 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 132 | static void DisableObjectValidation() { |
| 133 | // TODO: remove this hack necessary for image writing |
| 134 | verify_object_disabled_ = true; |
| 135 | } |
| 136 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 137 | // Callers must hold the heap lock. |
| 138 | static void RecordFreeLocked(Space* space, const Object* object); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 140 | // 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 Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame^] | 142 | static void WriteBarrier(const Object* object) { |
| 143 | // TODO: re-enable card marking when concurrent collector is active. |
| 144 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 145 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 146 | private: |
| 147 | // Allocates uninitialized storage. |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 148 | static Object* AllocateLocked(size_t num_bytes); |
| 149 | static Object* AllocateLocked(Space* space, size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 151 | static void RecordAllocationLocked(Space* space, const Object* object); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 152 | static void RecordImageAllocations(Space* space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 153 | |
| 154 | static void CollectGarbageInternal(); |
| 155 | |
| 156 | static void GrowForUtilization(); |
| 157 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 158 | static void VerifyObjectLocked(const Object *obj); |
| 159 | |
| 160 | static void VerificationCallback(Object* obj, void *arg); |
| 161 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 162 | static Mutex* lock_; |
| 163 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 164 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 166 | // Space loaded from an image |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 167 | // TODO: remove after intern_addr is removed |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 168 | static Space* boot_space_; |
| 169 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 170 | // default Space for allocations |
| 171 | static Space* alloc_space_; |
| 172 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 173 | static HeapBitmap* mark_bitmap_; |
| 174 | |
| 175 | static HeapBitmap* live_bitmap_; |
| 176 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 177 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 178 | static size_t maximum_size_; |
| 179 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 180 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 181 | static bool is_gc_running_; |
| 182 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 183 | // 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 Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 191 | // offset of java.lang.ref.Reference.referent |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 192 | static MemberOffset reference_referent_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 193 | |
| 194 | // offset of java.lang.ref.Reference.queue |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 195 | static MemberOffset reference_queue_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 196 | |
| 197 | // offset of java.lang.ref.Reference.queueNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 198 | static MemberOffset reference_queueNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 199 | |
| 200 | // offset of java.lang.ref.Reference.pendingNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 201 | static MemberOffset reference_pendingNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 202 | |
| 203 | // offset of java.lang.ref.FinalizerReference.zombie |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 204 | static MemberOffset finalizer_reference_zombie_offset_; |
| 205 | |
| 206 | static bool verify_object_disabled_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 207 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 208 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 209 | }; |
| 210 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 211 | } // namespace art |
| 212 | |
| 213 | #endif // ART_SRC_HEAP_H_ |