blob: 1e567786b2a29d9d56a2bda6159d290a28f31ad4 [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 Carlstrom4a289ed2011-08-16 17:17:49 -070027 // Create a heap with the requested sizes. optional boot image may
28 // be NULL, otherwise it is an image filename created by ImageWriter.
29 static bool Init(size_t starting_size, size_t maximum_size, const char* boot_image_file_name);
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
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070033 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070034 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070035
Ian Rogers408f79a2011-08-23 18:22:33 -070036 // Check sanity of given reference
37 static void VerifyObject(Object *obj);
38
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
Brian Carlstroma663ea52011-08-19 23:33:41 -070053 static Space* GetBootSpace() {
54 return boot_space_;
55 }
56
Carl Shapiro58551df2011-07-24 03:09:51 -070057 static HeapBitmap* GetLiveBits() {
58 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070059 }
Carl Shapiro58551df2011-07-24 03:09:51 -070060
61 static HeapBitmap* GetMarkBits() {
62 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070063 }
Carl Shapiro58551df2011-07-24 03:09:51 -070064
Carl Shapiro58551df2011-07-24 03:09:51 -070065 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);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070072 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -070073
74 static void CollectGarbageInternal();
75
76 static void GrowForUtilization();
77
78 static Mutex* lock_;
79
Carl Shapiro58551df2011-07-24 03:09:51 -070080 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
Brian Carlstroma663ea52011-08-19 23:33:41 -070082 // Space loaded from an image
83 static Space* boot_space_;
84
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070085 // default Space for allocations
86 static Space* alloc_space_;
87
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 static HeapBitmap* mark_bitmap_;
89
90 static HeapBitmap* live_bitmap_;
91
Carl Shapiro58551df2011-07-24 03:09:51 -070092 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 static size_t maximum_size_;
94
Carl Shapiro58551df2011-07-24 03:09:51 -070095 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -070096 static bool is_gc_running_;
97
Carl Shapiro58551df2011-07-24 03:09:51 -070098 // Number of bytes allocated. Adjusted after each allocation and
99 // free.
100 static size_t num_bytes_allocated_;
101
102 // Number of objects allocated. Adjusted after each allocation and
103 // free.
104 static size_t num_objects_allocated_;
105
Carl Shapiro69759ea2011-07-21 18:13:35 -0700106 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
107};
108
109class HeapLock {
110 public:
111 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
112 lock_->Lock();
113 }
114 ~HeapLock() {
115 lock_->Unlock();
116 }
117 private:
118 Mutex* lock_;
119 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700120};
121
122} // namespace art
123
124#endif // ART_SRC_HEAP_H_