blob: f35e3f85714d33c6f5290810fcfdca11051cd0ed [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// 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
7#include "src/globals.h"
8#include "src/object.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07009#include "src/object_bitmap.h"
10#include "src/thread.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070011
12namespace art {
13
Carl Shapiro69759ea2011-07-21 18:13:35 -070014class Space;
15class HeapBitmap;
16
Carl Shapiro1fb86202011-06-27 17:43:13 -070017class Heap {
18 public:
Carl Shapiro69759ea2011-07-21 18:13:35 -070019 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 Shapiro61e019d2011-07-14 16:53:09 -070025 }
26
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 static bool Init(size_t staring_size, size_t maximum_size);
Carl Shapiro61e019d2011-07-14 16:53:09 -070028
Carl Shapiro69759ea2011-07-21 18:13:35 -070029 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070030
Brian Carlstroma0808032011-07-18 00:39:23 -070031 static Object* AllocObject(Class* klass) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 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 Carlstroma7f4f482011-07-17 17:01:34 -070041 }
42
Brian Carlstroma0808032011-07-18 00:39:23 -070043 static CharArray* AllocCharArray(Class* char_array, size_t length) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070044 size_t size = sizeof(Array) + length * sizeof(uint16_t);
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 Object* new_array = AllocObject(char_array, size);
46 if (new_array != NULL) {
47 char_array->klass_ = char_array;
48 }
49 return down_cast<CharArray*>(new_array);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070050 }
51
Brian Carlstroma0808032011-07-18 00:39:23 -070052 static String* AllocString(Class* java_lang_String) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 return down_cast<String*>(AllocObject(java_lang_String));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070054 }
55
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 static String* AllocStringFromModifiedUtf8(Class* java_lang_String,
57 Class* char_array,
58 const char* data);
59
60 // Initiates an explicit garbage collection.
61 static void CollectGarbage();
62
63 // Blocks the caller until the garbage collector becomes idle.
64 static void WaitForConcurrentGcToComplete();
65
66 static Mutex* GetLock() {
67 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070068 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070069
70 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070071
Carl Shapiro69759ea2011-07-21 18:13:35 -070072 static Object* Allocate(size_t num_bytes);
73
74 static void CollectGarbageInternal();
75
76 static void GrowForUtilization();
77
78 static Mutex* lock_;
79
80 static Space* space_;
81
82 static HeapBitmap* mark_bitmap_;
83
84 static HeapBitmap* live_bitmap_;
85
86 static size_t startup_size_;
87
88 static size_t maximum_size_;
89
90 static bool is_gc_running_;
91
92 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
93};
94
95class HeapLock {
96 public:
97 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
98 lock_->Lock();
99 }
100 ~HeapLock() {
101 lock_->Unlock();
102 }
103 private:
104 Mutex* lock_;
105 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106};
107
108} // namespace art
109
110#endif // ART_SRC_HEAP_H_