blob: 6ce2db74323ab9a4a23df9f942666af753bf5cc2 [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"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "object_bitmap.h"
11#include "thread.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070012
13namespace art {
14
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070015class Class;
16class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070017class Space;
18class HeapBitmap;
19
Carl Shapiro1fb86202011-06-27 17:43:13 -070020class Heap {
21 public:
Brian Carlstrom4a96b602011-07-26 16:40:23 -070022 static const size_t kStartupSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070023
Brian Carlstrom4a96b602011-07-26 16:40:23 -070024 static const size_t kMaximumSize = 64 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026 typedef void (RootVistor)(Object* root, void* arg);
27
Carl Shapiro69759ea2011-07-21 18:13:35 -070028 static bool Init() {
29 return Init(kStartupSize, kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070030 }
31
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 static bool Init(size_t staring_size, size_t maximum_size);
Carl Shapiro61e019d2011-07-14 16:53:09 -070033
Carl Shapiro69759ea2011-07-21 18:13:35 -070034 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070035
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070036 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070037 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070038
Carl Shapiro69759ea2011-07-21 18:13:35 -070039 // Initiates an explicit garbage collection.
40 static void CollectGarbage();
41
42 // Blocks the caller until the garbage collector becomes idle.
43 static void WaitForConcurrentGcToComplete();
44
45 static Mutex* GetLock() {
46 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070047 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070048
Carl Shapiro58551df2011-07-24 03:09:51 -070049 static const std::vector<Space*>& GetSpaces() {
50 return spaces_;
51 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Carl Shapiro58551df2011-07-24 03:09:51 -070053 static HeapBitmap* GetLiveBits() {
54 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070055 }
Carl Shapiro58551df2011-07-24 03:09:51 -070056
57 static HeapBitmap* GetMarkBits() {
58 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070059 }
Carl Shapiro58551df2011-07-24 03:09:51 -070060
61 static size_t GetMaximumSize() {
62 return maximum_size_;
63 }
64
65 private:
66 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -070067 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -070068 static Object* Allocate(Space* space, size_t num_bytes);
69
70 static void RecordAllocation(Space* space, const Object* object);
71 static void RecordFree(Space* space, const Object* object);
Carl Shapiro69759ea2011-07-21 18:13:35 -070072
73 static void CollectGarbageInternal();
74
75 static void GrowForUtilization();
76
77 static Mutex* lock_;
78
Carl Shapiro58551df2011-07-24 03:09:51 -070079 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070080
81 static HeapBitmap* mark_bitmap_;
82
83 static HeapBitmap* live_bitmap_;
84
Carl Shapiro58551df2011-07-24 03:09:51 -070085 // The startup size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070086 static size_t startup_size_;
87
Carl Shapiro58551df2011-07-24 03:09:51 -070088 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 static size_t maximum_size_;
90
Carl Shapiro58551df2011-07-24 03:09:51 -070091 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 static bool is_gc_running_;
93
Carl Shapiro58551df2011-07-24 03:09:51 -070094 // Number of bytes allocated. Adjusted after each allocation and
95 // free.
96 static size_t num_bytes_allocated_;
97
98 // Number of objects allocated. Adjusted after each allocation and
99 // free.
100 static size_t num_objects_allocated_;
101
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
103};
104
105class HeapLock {
106 public:
107 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
108 lock_->Lock();
109 }
110 ~HeapLock() {
111 lock_->Unlock();
112 }
113 private:
114 Mutex* lock_;
115 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700116};
117
118} // namespace art
119
120#endif // ART_SRC_HEAP_H_