blob: 3721604da5fda037b502ef4837a88727a5b70729 [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "globals.h"
8#include "object.h"
9#include "object_bitmap.h"
10#include "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 Carlstrom578bbdc2011-07-21 14:07:47 -070043 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 Shapiro69759ea2011-07-21 18:13:35 -070048 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070049 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 Shapiro5fafe2b2011-07-09 15:34:41 -070058 }
59
Brian Carlstroma0808032011-07-18 00:39:23 -070060 static String* AllocString(Class* java_lang_String) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070061 return down_cast<String*>(AllocObject(java_lang_String));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070062 }
63
Carl Shapiro69759ea2011-07-21 18:13:35 -070064 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 Shapiro5fafe2b2011-07-09 15:34:41 -070076 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070077
78 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070079
Carl Shapiro69759ea2011-07-21 18:13:35 -070080 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
103class 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 Shapiro1fb86202011-06-27 17:43:13 -0700114};
115
116} // namespace art
117
118#endif // ART_SRC_HEAP_H_