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 | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 27 | static bool Init(size_t starting_size, size_t maximum_size); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 28 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | static void Destroy(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 31 | // Allocates and initializes storage for an object instance. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 32 | static Object* AllocObject(Class* klass, size_t num_bytes); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 34 | // Initiates an explicit garbage collection. |
| 35 | static void CollectGarbage(); |
| 36 | |
| 37 | // Blocks the caller until the garbage collector becomes idle. |
| 38 | static void WaitForConcurrentGcToComplete(); |
| 39 | |
| 40 | static Mutex* GetLock() { |
| 41 | return lock_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 42 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 43 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 44 | static const std::vector<Space*>& GetSpaces() { |
| 45 | return spaces_; |
| 46 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 47 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 48 | static HeapBitmap* GetLiveBits() { |
| 49 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 50 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 51 | |
| 52 | static HeapBitmap* GetMarkBits() { |
| 53 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 54 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 55 | |
| 56 | static size_t GetMaximumSize() { |
| 57 | return maximum_size_; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | // Allocates uninitialized storage. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 62 | static Object* Allocate(size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 63 | static Object* Allocate(Space* space, size_t num_bytes); |
| 64 | |
| 65 | static void RecordAllocation(Space* space, const Object* object); |
| 66 | static void RecordFree(Space* space, const Object* object); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 67 | |
| 68 | static void CollectGarbageInternal(); |
| 69 | |
| 70 | static void GrowForUtilization(); |
| 71 | |
| 72 | static Mutex* lock_; |
| 73 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 74 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 75 | |
| 76 | static HeapBitmap* mark_bitmap_; |
| 77 | |
| 78 | static HeapBitmap* live_bitmap_; |
| 79 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 80 | // The startup size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 81 | static size_t startup_size_; |
| 82 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 83 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 84 | static size_t maximum_size_; |
| 85 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 86 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 87 | static bool is_gc_running_; |
| 88 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 89 | // Number of bytes allocated. Adjusted after each allocation and |
| 90 | // free. |
| 91 | static size_t num_bytes_allocated_; |
| 92 | |
| 93 | // Number of objects allocated. Adjusted after each allocation and |
| 94 | // free. |
| 95 | static size_t num_objects_allocated_; |
| 96 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 97 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 98 | }; |
| 99 | |
| 100 | class HeapLock { |
| 101 | public: |
| 102 | HeapLock(Heap* heap) : lock_(heap->GetLock()) { |
| 103 | lock_->Lock(); |
| 104 | } |
| 105 | ~HeapLock() { |
| 106 | lock_->Unlock(); |
| 107 | } |
| 108 | private: |
| 109 | Mutex* lock_; |
| 110 | DISALLOW_COPY_AND_ASSIGN(HeapLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace art |
| 114 | |
| 115 | #endif // ART_SRC_HEAP_H_ |