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" |
| 10 | #include "thread.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 11 | |
| 12 | namespace art { |
| 13 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 14 | class Class; |
| 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 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | typedef void (RootVistor)(Object* root, void* arg); |
| 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. |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 37 | static void VerifyObject(Object *obj); |
| 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. |
| 42 | static bool IsHeapAddress(Object* obj); |
| 43 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 44 | // Initiates an explicit garbage collection. |
| 45 | static void CollectGarbage(); |
| 46 | |
| 47 | // Blocks the caller until the garbage collector becomes idle. |
| 48 | static void WaitForConcurrentGcToComplete(); |
| 49 | |
| 50 | static Mutex* GetLock() { |
| 51 | return lock_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 52 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 53 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 54 | static const std::vector<Space*>& GetSpaces() { |
| 55 | return spaces_; |
| 56 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 57 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 58 | static Space* GetBootSpace() { |
| 59 | return boot_space_; |
| 60 | } |
| 61 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 62 | static HeapBitmap* GetLiveBits() { |
| 63 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 64 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 65 | |
| 66 | static HeapBitmap* GetMarkBits() { |
| 67 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 68 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 69 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 70 | private: |
| 71 | // Allocates uninitialized storage. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 72 | static Object* Allocate(size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 73 | static Object* Allocate(Space* space, size_t num_bytes); |
| 74 | |
| 75 | static void RecordAllocation(Space* space, const Object* object); |
| 76 | static void RecordFree(Space* space, const Object* object); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 77 | static void RecordImageAllocations(Space* space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 78 | |
| 79 | static void CollectGarbageInternal(); |
| 80 | |
| 81 | static void GrowForUtilization(); |
| 82 | |
| 83 | static Mutex* lock_; |
| 84 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 85 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 86 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 87 | // Space loaded from an image |
| 88 | static Space* boot_space_; |
| 89 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 90 | // default Space for allocations |
| 91 | static Space* alloc_space_; |
| 92 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 93 | static HeapBitmap* mark_bitmap_; |
| 94 | |
| 95 | static HeapBitmap* live_bitmap_; |
| 96 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 97 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 98 | static size_t maximum_size_; |
| 99 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 100 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 101 | static bool is_gc_running_; |
| 102 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 103 | // Number of bytes allocated. Adjusted after each allocation and |
| 104 | // free. |
| 105 | static size_t num_bytes_allocated_; |
| 106 | |
| 107 | // Number of objects allocated. Adjusted after each allocation and |
| 108 | // free. |
| 109 | static size_t num_objects_allocated_; |
| 110 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 111 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 112 | }; |
| 113 | |
| 114 | class HeapLock { |
| 115 | public: |
| 116 | HeapLock(Heap* heap) : lock_(heap->GetLock()) { |
| 117 | lock_->Lock(); |
| 118 | } |
| 119 | ~HeapLock() { |
| 120 | lock_->Unlock(); |
| 121 | } |
| 122 | private: |
| 123 | Mutex* lock_; |
| 124 | DISALLOW_COPY_AND_ASSIGN(HeapLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace art |
| 128 | |
| 129 | #endif // ART_SRC_HEAP_H_ |