blob: 4aff139fff7012fc9f09b6b48ce282a756c76ebd [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
Carl Shapiro58551df2011-07-24 03:09:51 -07007#include <vector>
8
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
10#include "object.h"
11#include "object_bitmap.h"
12#include "thread.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070013
14namespace art {
15
Carl Shapiro69759ea2011-07-21 18:13:35 -070016class Space;
17class HeapBitmap;
18
Carl Shapiro1fb86202011-06-27 17:43:13 -070019class Heap {
20 public:
Carl Shapiro69759ea2011-07-21 18:13:35 -070021 static const size_t kStartupSize = 1 * MB;
22
23 static const size_t kMaximumSize = 16 * MB;
24
25 static bool Init() {
26 return Init(kStartupSize, kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070027 }
28
Carl Shapiro69759ea2011-07-21 18:13:35 -070029 static bool Init(size_t staring_size, size_t maximum_size);
Carl Shapiro61e019d2011-07-14 16:53:09 -070030
Carl Shapiro69759ea2011-07-21 18:13:35 -070031 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070032
Carl Shapiro58551df2011-07-24 03:09:51 -070033 // Allocates and initializes storage for a class instance.
34 static Object* AllocObject(Class* klass);
Carl Shapiro69759ea2011-07-21 18:13:35 -070035
Carl Shapiro58551df2011-07-24 03:09:51 -070036 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070037
Carl Shapiro58551df2011-07-24 03:09:51 -070038 static Array* AllocArray(Class* array_class,
39 size_t component_count,
40 size_t component_size) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041 size_t size = sizeof(Array) + component_count * component_size;
42 Array* array = down_cast<Array*>(AllocObject(array_class, size));
43 if (array != NULL) {
44 array->SetLength(component_count);
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070046 return array;
47 }
48
Carl Shapiro58551df2011-07-24 03:09:51 -070049 static ObjectArray* AllocObjectArray(Class* object_array_class,
50 size_t length) {
51 return down_cast<ObjectArray*>(AllocArray(object_array_class,
52 length,
53 sizeof(uint32_t)));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070054 }
55
56 static CharArray* AllocCharArray(Class* char_array_class, size_t length) {
Carl Shapiro58551df2011-07-24 03:09:51 -070057 return down_cast<CharArray*>(AllocArray(char_array_class,
58 length,
59 sizeof(uint16_t)));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070060 }
61
Brian Carlstroma0808032011-07-18 00:39:23 -070062 static String* AllocString(Class* java_lang_String) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070063 return down_cast<String*>(AllocObject(java_lang_String));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070064 }
65
Carl Shapiro69759ea2011-07-21 18:13:35 -070066 static String* AllocStringFromModifiedUtf8(Class* java_lang_String,
67 Class* char_array,
68 const char* data);
69
70 // Initiates an explicit garbage collection.
71 static void CollectGarbage();
72
73 // Blocks the caller until the garbage collector becomes idle.
74 static void WaitForConcurrentGcToComplete();
75
76 static Mutex* GetLock() {
77 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070078 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070079
Carl Shapiro58551df2011-07-24 03:09:51 -070080 static const std::vector<Space*>& GetSpaces() {
81 return spaces_;
82 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070083
Carl Shapiro58551df2011-07-24 03:09:51 -070084 static HeapBitmap* GetLiveBits() {
85 return live_bitmap_;
86 };
87
88 static HeapBitmap* GetMarkBits() {
89 return mark_bitmap_;
90 };
91
92 static size_t GetMaximumSize() {
93 return maximum_size_;
94 }
95
96 private:
97 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -070098 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -070099 static Object* Allocate(Space* space, size_t num_bytes);
100
101 static void RecordAllocation(Space* space, const Object* object);
102 static void RecordFree(Space* space, const Object* object);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103
104 static void CollectGarbageInternal();
105
106 static void GrowForUtilization();
107
108 static Mutex* lock_;
109
Carl Shapiro58551df2011-07-24 03:09:51 -0700110 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111
112 static HeapBitmap* mark_bitmap_;
113
114 static HeapBitmap* live_bitmap_;
115
Carl Shapiro58551df2011-07-24 03:09:51 -0700116 // The startup size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 static size_t startup_size_;
118
Carl Shapiro58551df2011-07-24 03:09:51 -0700119 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700120 static size_t maximum_size_;
121
Carl Shapiro58551df2011-07-24 03:09:51 -0700122 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700123 static bool is_gc_running_;
124
Carl Shapiro58551df2011-07-24 03:09:51 -0700125 // Number of bytes allocated. Adjusted after each allocation and
126 // free.
127 static size_t num_bytes_allocated_;
128
129 // Number of objects allocated. Adjusted after each allocation and
130 // free.
131 static size_t num_objects_allocated_;
132
Carl Shapiro69759ea2011-07-21 18:13:35 -0700133 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
134};
135
136class HeapLock {
137 public:
138 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
139 lock_->Lock();
140 }
141 ~HeapLock() {
142 lock_->Unlock();
143 }
144 private:
145 Mutex* lock_;
146 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700147};
148
149} // namespace art
150
151#endif // ART_SRC_HEAP_H_