blob: 1059fd7664f0b4de2152fdeada5076931af0de53 [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
26 static bool Init() {
27 return Init(kStartupSize, kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070028 }
29
Carl Shapiro69759ea2011-07-21 18:13:35 -070030 static bool Init(size_t staring_size, size_t maximum_size);
Carl Shapiro61e019d2011-07-14 16:53:09 -070031
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070033
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070034 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070035 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070036
Carl Shapiro69759ea2011-07-21 18:13:35 -070037 // Initiates an explicit garbage collection.
38 static void CollectGarbage();
39
40 // Blocks the caller until the garbage collector becomes idle.
41 static void WaitForConcurrentGcToComplete();
42
43 static Mutex* GetLock() {
44 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070045 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070046
Carl Shapiro58551df2011-07-24 03:09:51 -070047 static const std::vector<Space*>& GetSpaces() {
48 return spaces_;
49 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Carl Shapiro58551df2011-07-24 03:09:51 -070051 static HeapBitmap* GetLiveBits() {
52 return live_bitmap_;
53 };
54
55 static HeapBitmap* GetMarkBits() {
56 return mark_bitmap_;
57 };
58
59 static size_t GetMaximumSize() {
60 return maximum_size_;
61 }
62
63 private:
64 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -070065 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -070066 static Object* Allocate(Space* space, size_t num_bytes);
67
68 static void RecordAllocation(Space* space, const Object* object);
69 static void RecordFree(Space* space, const Object* object);
Carl Shapiro69759ea2011-07-21 18:13:35 -070070
71 static void CollectGarbageInternal();
72
73 static void GrowForUtilization();
74
75 static Mutex* lock_;
76
Carl Shapiro58551df2011-07-24 03:09:51 -070077 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070078
79 static HeapBitmap* mark_bitmap_;
80
81 static HeapBitmap* live_bitmap_;
82
Carl Shapiro58551df2011-07-24 03:09:51 -070083 // The startup size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 static size_t startup_size_;
85
Carl Shapiro58551df2011-07-24 03:09:51 -070086 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 static size_t maximum_size_;
88
Carl Shapiro58551df2011-07-24 03:09:51 -070089 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -070090 static bool is_gc_running_;
91
Carl Shapiro58551df2011-07-24 03:09:51 -070092 // Number of bytes allocated. Adjusted after each allocation and
93 // free.
94 static size_t num_bytes_allocated_;
95
96 // Number of objects allocated. Adjusted after each allocation and
97 // free.
98 static size_t num_objects_allocated_;
99
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 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_