blob: 247c6bad6d2e152d18e03909702eec28a17bbdab [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro1fb86202011-06-27 17:43:13 -07002
3#ifndef ART_SRC_HEAP_H_
4#define ART_SRC_HEAP_H_
5
Carl Shapiro58551df2011-07-24 03:09:51 -07006#include <vector>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "globals.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "object_bitmap.h"
10#include "thread.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070011
12namespace art {
13
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070014class Class;
15class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070016class Space;
17class HeapBitmap;
18
Carl Shapiro1fb86202011-06-27 17:43:13 -070019class Heap {
20 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070021 static const size_t kInitialSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
Brian Carlstrom4a96b602011-07-26 16:40:23 -070023 static const size_t kMaximumSize = 64 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
Brian Carlstrom7e93b502011-08-04 14:16:22 -070025 typedef void (RootVistor)(Object* root, void* arg);
26
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027 static bool Init(size_t starting_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 Carlstroma40f9bc2011-07-26 21:26:07 -070031 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070032 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070033
Carl Shapiro69759ea2011-07-21 18:13:35 -070034 // Initiates an explicit garbage collection.
35 static void CollectGarbage();
36
37 // Blocks the caller until the garbage collector becomes idle.
38 static void WaitForConcurrentGcToComplete();
39
40 static Mutex* GetLock() {
41 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070042 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070043
Carl Shapiro58551df2011-07-24 03:09:51 -070044 static const std::vector<Space*>& GetSpaces() {
45 return spaces_;
46 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070047
Carl Shapiro58551df2011-07-24 03:09:51 -070048 static HeapBitmap* GetLiveBits() {
49 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070050 }
Carl Shapiro58551df2011-07-24 03:09:51 -070051
52 static HeapBitmap* GetMarkBits() {
53 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070054 }
Carl Shapiro58551df2011-07-24 03:09:51 -070055
56 static size_t GetMaximumSize() {
57 return maximum_size_;
58 }
59
60 private:
61 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -070063 static Object* Allocate(Space* space, size_t num_bytes);
64
65 static void RecordAllocation(Space* space, const Object* object);
66 static void RecordFree(Space* space, const Object* object);
Carl Shapiro69759ea2011-07-21 18:13:35 -070067
68 static void CollectGarbageInternal();
69
70 static void GrowForUtilization();
71
72 static Mutex* lock_;
73
Carl Shapiro58551df2011-07-24 03:09:51 -070074 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070075
76 static HeapBitmap* mark_bitmap_;
77
78 static HeapBitmap* live_bitmap_;
79
Carl Shapiro58551df2011-07-24 03:09:51 -070080 // The startup size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 static size_t startup_size_;
82
Carl Shapiro58551df2011-07-24 03:09:51 -070083 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 static size_t maximum_size_;
85
Carl Shapiro58551df2011-07-24 03:09:51 -070086 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 static bool is_gc_running_;
88
Carl Shapiro58551df2011-07-24 03:09:51 -070089 // Number of bytes allocated. Adjusted after each allocation and
90 // free.
91 static size_t num_bytes_allocated_;
92
93 // Number of objects allocated. Adjusted after each allocation and
94 // free.
95 static size_t num_objects_allocated_;
96
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
98};
99
100class HeapLock {
101 public:
102 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
103 lock_->Lock();
104 }
105 ~HeapLock() {
106 lock_->Unlock();
107 }
108 private:
109 Mutex* lock_;
110 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700111};
112
113} // namespace art
114
115#endif // ART_SRC_HEAP_H_