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" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 13 | class Class; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 14 | class Mutex; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 15 | class Object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | class Space; |
| 17 | class HeapBitmap; |
| 18 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 19 | class Heap { |
| 20 | public: |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 21 | static const size_t kInitialSize = 16 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 23 | static const size_t kMaximumSize = 64 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 25 | typedef void (RootVisitor)(const Object* root, void* arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 26 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 27 | // Create a heap with the requested sizes. optional boot image may |
| 28 | // be NULL, otherwise it is an image filename created by ImageWriter. |
| 29 | static bool Init(size_t starting_size, size_t maximum_size, const char* boot_image_file_name); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 30 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 31 | static void Destroy(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 33 | // Allocates and initializes storage for an object instance. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 34 | static Object* AllocObject(Class* klass, size_t num_bytes); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 36 | // Check sanity of given reference. Requires the heap lock. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 37 | static void VerifyObject(const Object *obj); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 39 | // A weaker test than VerifyObject that doesn't require the heap lock, |
| 40 | // and doesn't abort on error, allowing the caller to report more |
| 41 | // meaningful diagnostics. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 42 | static bool IsHeapAddress(const Object* obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 43 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 44 | // Initiates an explicit garbage collection. |
| 45 | static void CollectGarbage(); |
| 46 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 47 | // Implements java.lang.Runtime.maxMemory. |
| 48 | static int64_t GetMaxMemory(); |
| 49 | // Implements java.lang.Runtime.totalMemory. |
| 50 | static int64_t GetTotalMemory(); |
| 51 | // Implements java.lang.Runtime.freeMemory. |
| 52 | static int64_t GetFreeMemory(); |
| 53 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | // Blocks the caller until the garbage collector becomes idle. |
| 55 | static void WaitForConcurrentGcToComplete(); |
| 56 | |
| 57 | static Mutex* GetLock() { |
| 58 | return lock_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 59 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 60 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 61 | static const std::vector<Space*>& GetSpaces() { |
| 62 | return spaces_; |
| 63 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 64 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 65 | static Space* GetBootSpace() { |
| 66 | return boot_space_; |
| 67 | } |
| 68 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 69 | static HeapBitmap* GetLiveBits() { |
| 70 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 71 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 72 | |
| 73 | static HeapBitmap* GetMarkBits() { |
| 74 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 75 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 76 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 77 | static void SetReferenceOffsets(size_t reference_referent_offset, |
| 78 | size_t reference_queue_offset, |
| 79 | size_t reference_queueNext_offset, |
| 80 | size_t reference_pendingNext_offset, |
| 81 | size_t finalizer_reference_zombie_offset) { |
| 82 | CHECK_NE(reference_referent_offset, 0U); |
| 83 | CHECK_NE(reference_queue_offset, 0U); |
| 84 | CHECK_NE(reference_queueNext_offset, 0U); |
| 85 | CHECK_NE(reference_pendingNext_offset, 0U); |
| 86 | CHECK_NE(finalizer_reference_zombie_offset, 0U); |
| 87 | reference_referent_offset_ = reference_referent_offset; |
| 88 | reference_queue_offset_ = reference_queue_offset; |
| 89 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 90 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 91 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 92 | } |
| 93 | |
| 94 | static size_t GetReferenceReferentOffset() { |
| 95 | DCHECK_NE(reference_referent_offset_, 0U); |
| 96 | return reference_referent_offset_; |
| 97 | } |
| 98 | |
| 99 | static size_t GetReferenceQueueOffset() { |
| 100 | DCHECK_NE(reference_queue_offset_, 0U); |
| 101 | return reference_queue_offset_; |
| 102 | } |
| 103 | |
| 104 | static size_t GetReferenceQueueNextOffset() { |
| 105 | DCHECK_NE(reference_queueNext_offset_, 0U); |
| 106 | return reference_queueNext_offset_; |
| 107 | } |
| 108 | |
| 109 | static size_t GetReferencePendingNextOffset() { |
| 110 | DCHECK_NE(reference_pendingNext_offset_, 0U); |
| 111 | return reference_pendingNext_offset_; |
| 112 | } |
| 113 | |
| 114 | static size_t GetFinalizerReferenceZombieOffset() { |
| 115 | DCHECK_NE(finalizer_reference_zombie_offset_, 0U); |
| 116 | return finalizer_reference_zombie_offset_; |
| 117 | } |
| 118 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 119 | private: |
| 120 | // Allocates uninitialized storage. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 121 | static Object* Allocate(size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 122 | static Object* Allocate(Space* space, size_t num_bytes); |
| 123 | |
| 124 | static void RecordAllocation(Space* space, const Object* object); |
| 125 | static void RecordFree(Space* space, const Object* object); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 126 | static void RecordImageAllocations(Space* space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 127 | |
| 128 | static void CollectGarbageInternal(); |
| 129 | |
| 130 | static void GrowForUtilization(); |
| 131 | |
| 132 | static Mutex* lock_; |
| 133 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 134 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 135 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 136 | // Space loaded from an image |
| 137 | static Space* boot_space_; |
| 138 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 139 | // default Space for allocations |
| 140 | static Space* alloc_space_; |
| 141 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 142 | static HeapBitmap* mark_bitmap_; |
| 143 | |
| 144 | static HeapBitmap* live_bitmap_; |
| 145 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 146 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 147 | static size_t maximum_size_; |
| 148 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 149 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 150 | static bool is_gc_running_; |
| 151 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 152 | // Number of bytes allocated. Adjusted after each allocation and |
| 153 | // free. |
| 154 | static size_t num_bytes_allocated_; |
| 155 | |
| 156 | // Number of objects allocated. Adjusted after each allocation and |
| 157 | // free. |
| 158 | static size_t num_objects_allocated_; |
| 159 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 160 | // offset of java.lang.ref.Reference.referent |
| 161 | static size_t reference_referent_offset_; |
| 162 | |
| 163 | // offset of java.lang.ref.Reference.queue |
| 164 | static size_t reference_queue_offset_; |
| 165 | |
| 166 | // offset of java.lang.ref.Reference.queueNext |
| 167 | static size_t reference_queueNext_offset_; |
| 168 | |
| 169 | // offset of java.lang.ref.Reference.pendingNext |
| 170 | static size_t reference_pendingNext_offset_; |
| 171 | |
| 172 | // offset of java.lang.ref.FinalizerReference.zombie |
| 173 | static size_t finalizer_reference_zombie_offset_; |
| 174 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 175 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 176 | }; |
| 177 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 178 | } // namespace art |
| 179 | |
| 180 | #endif // ART_SRC_HEAP_H_ |