Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
| 4 | #ifndef ART_SRC_HEAP_H_ |
| 5 | #define ART_SRC_HEAP_H_ |
| 6 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 7 | #include "globals.h" |
| 8 | #include "object.h" |
| 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 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 14 | class Space; |
| 15 | class HeapBitmap; |
| 16 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 17 | class Heap { |
| 18 | public: |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | static const size_t kStartupSize = 1 * MB; |
| 20 | |
| 21 | static const size_t kMaximumSize = 16 * MB; |
| 22 | |
| 23 | static bool Init() { |
| 24 | return Init(kStartupSize, kMaximumSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 27 | static bool Init(size_t staring_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 | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 31 | static Object* AllocObject(Class* klass) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 32 | return AllocObject(klass, klass->object_size_); |
| 33 | } |
| 34 | |
| 35 | static Object* AllocObject(Class* klass, size_t num_bytes) { |
| 36 | Object* obj = Allocate(num_bytes); |
| 37 | if (obj != NULL) { |
| 38 | obj->klass_ = klass; |
| 39 | } |
| 40 | return obj; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 43 | static Array* AllocArray(Class* array_class, size_t component_count, size_t component_size) { |
| 44 | size_t size = sizeof(Array) + component_count * component_size; |
| 45 | Array* array = down_cast<Array*>(AllocObject(array_class, size)); |
| 46 | if (array != NULL) { |
| 47 | array->SetLength(component_count); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 48 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 49 | return array; |
| 50 | } |
| 51 | |
| 52 | static ObjectArray* AllocObjectArray(Class* object_array_class, size_t length) { |
| 53 | return down_cast<ObjectArray*>(AllocArray(object_array_class, length, sizeof(uint32_t))); |
| 54 | } |
| 55 | |
| 56 | static CharArray* AllocCharArray(Class* char_array_class, size_t length) { |
| 57 | return down_cast<CharArray*>(AllocArray(char_array_class, length, sizeof(uint16_t))); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 60 | static String* AllocString(Class* java_lang_String) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 61 | return down_cast<String*>(AllocObject(java_lang_String)); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 64 | static String* AllocStringFromModifiedUtf8(Class* java_lang_String, |
| 65 | Class* char_array, |
| 66 | const char* data); |
| 67 | |
| 68 | // Initiates an explicit garbage collection. |
| 69 | static void CollectGarbage(); |
| 70 | |
| 71 | // Blocks the caller until the garbage collector becomes idle. |
| 72 | static void WaitForConcurrentGcToComplete(); |
| 73 | |
| 74 | static Mutex* GetLock() { |
| 75 | return lock_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 76 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 77 | |
| 78 | private: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 79 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 80 | static Object* Allocate(size_t num_bytes); |
| 81 | |
| 82 | static void CollectGarbageInternal(); |
| 83 | |
| 84 | static void GrowForUtilization(); |
| 85 | |
| 86 | static Mutex* lock_; |
| 87 | |
| 88 | static Space* space_; |
| 89 | |
| 90 | static HeapBitmap* mark_bitmap_; |
| 91 | |
| 92 | static HeapBitmap* live_bitmap_; |
| 93 | |
| 94 | static size_t startup_size_; |
| 95 | |
| 96 | static size_t maximum_size_; |
| 97 | |
| 98 | static bool is_gc_running_; |
| 99 | |
| 100 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 101 | }; |
| 102 | |
| 103 | class HeapLock { |
| 104 | public: |
| 105 | HeapLock(Heap* heap) : lock_(heap->GetLock()) { |
| 106 | lock_->Lock(); |
| 107 | } |
| 108 | ~HeapLock() { |
| 109 | lock_->Unlock(); |
| 110 | } |
| 111 | private: |
| 112 | Mutex* lock_; |
| 113 | DISALLOW_COPY_AND_ASSIGN(HeapLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace art |
| 117 | |
| 118 | #endif // ART_SRC_HEAP_H_ |