blob: 72ceffa2feebeaf2b205510fe30b18b95dd231a0 [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
Elliott Hughesa2501992011-08-26 19:39:54 -070036 // Check sanity of given reference. Requires the heap lock.
Ian Rogers408f79a2011-08-23 18:22:33 -070037 static void VerifyObject(Object *obj);
38
Elliott Hughesa2501992011-08-26 19:39:54 -070039 // A weaker test than VerifyObject that doesn't require the heap lock,
40 // and doesn't abort on error, allowing the caller to report more
41 // meaningful diagnostics.
42 static bool IsHeapAddress(Object* obj);
43
Carl Shapiro69759ea2011-07-21 18:13:35 -070044 // Initiates an explicit garbage collection.
45 static void CollectGarbage();
46
Elliott Hughesbf86d042011-08-31 17:53:14 -070047 // Implements java.lang.Runtime.maxMemory.
48 static int64_t GetMaxMemory();
49 // Implements java.lang.Runtime.totalMemory.
50 static int64_t GetTotalMemory();
51 // Implements java.lang.Runtime.freeMemory.
52 static int64_t GetFreeMemory();
53
Carl Shapiro69759ea2011-07-21 18:13:35 -070054 // Blocks the caller until the garbage collector becomes idle.
55 static void WaitForConcurrentGcToComplete();
56
57 static Mutex* GetLock() {
58 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070059 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070060
Carl Shapiro58551df2011-07-24 03:09:51 -070061 static const std::vector<Space*>& GetSpaces() {
62 return spaces_;
63 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070064
Brian Carlstroma663ea52011-08-19 23:33:41 -070065 static Space* GetBootSpace() {
66 return boot_space_;
67 }
68
Carl Shapiro58551df2011-07-24 03:09:51 -070069 static HeapBitmap* GetLiveBits() {
70 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070071 }
Carl Shapiro58551df2011-07-24 03:09:51 -070072
73 static HeapBitmap* GetMarkBits() {
74 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070075 }
Carl Shapiro58551df2011-07-24 03:09:51 -070076
Brian Carlstrom1f870082011-08-23 16:02:11 -070077 static void SetReferenceOffsets(size_t reference_referent_offset,
78 size_t reference_queue_offset,
79 size_t reference_queueNext_offset,
80 size_t reference_pendingNext_offset,
81 size_t finalizer_reference_zombie_offset) {
82 CHECK_NE(reference_referent_offset, 0U);
83 CHECK_NE(reference_queue_offset, 0U);
84 CHECK_NE(reference_queueNext_offset, 0U);
85 CHECK_NE(reference_pendingNext_offset, 0U);
86 CHECK_NE(finalizer_reference_zombie_offset, 0U);
87 reference_referent_offset_ = reference_referent_offset;
88 reference_queue_offset_ = reference_queue_offset;
89 reference_queueNext_offset_ = reference_queueNext_offset;
90 reference_pendingNext_offset_ = reference_pendingNext_offset;
91 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
92 }
93
94 static size_t GetReferenceReferentOffset() {
95 DCHECK_NE(reference_referent_offset_, 0U);
96 return reference_referent_offset_;
97 }
98
99 static size_t GetReferenceQueueOffset() {
100 DCHECK_NE(reference_queue_offset_, 0U);
101 return reference_queue_offset_;
102 }
103
104 static size_t GetReferenceQueueNextOffset() {
105 DCHECK_NE(reference_queueNext_offset_, 0U);
106 return reference_queueNext_offset_;
107 }
108
109 static size_t GetReferencePendingNextOffset() {
110 DCHECK_NE(reference_pendingNext_offset_, 0U);
111 return reference_pendingNext_offset_;
112 }
113
114 static size_t GetFinalizerReferenceZombieOffset() {
115 DCHECK_NE(finalizer_reference_zombie_offset_, 0U);
116 return finalizer_reference_zombie_offset_;
117 }
118
Carl Shapiro58551df2011-07-24 03:09:51 -0700119 private:
120 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700122 static Object* Allocate(Space* space, size_t num_bytes);
123
124 static void RecordAllocation(Space* space, const Object* object);
125 static void RecordFree(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700126 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700127
128 static void CollectGarbageInternal();
129
130 static void GrowForUtilization();
131
132 static Mutex* lock_;
133
Carl Shapiro58551df2011-07-24 03:09:51 -0700134 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135
Brian Carlstroma663ea52011-08-19 23:33:41 -0700136 // Space loaded from an image
137 static Space* boot_space_;
138
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700139 // default Space for allocations
140 static Space* alloc_space_;
141
Carl Shapiro69759ea2011-07-21 18:13:35 -0700142 static HeapBitmap* mark_bitmap_;
143
144 static HeapBitmap* live_bitmap_;
145
Carl Shapiro58551df2011-07-24 03:09:51 -0700146 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 static size_t maximum_size_;
148
Carl Shapiro58551df2011-07-24 03:09:51 -0700149 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150 static bool is_gc_running_;
151
Carl Shapiro58551df2011-07-24 03:09:51 -0700152 // Number of bytes allocated. Adjusted after each allocation and
153 // free.
154 static size_t num_bytes_allocated_;
155
156 // Number of objects allocated. Adjusted after each allocation and
157 // free.
158 static size_t num_objects_allocated_;
159
Brian Carlstrom1f870082011-08-23 16:02:11 -0700160 // offset of java.lang.ref.Reference.referent
161 static size_t reference_referent_offset_;
162
163 // offset of java.lang.ref.Reference.queue
164 static size_t reference_queue_offset_;
165
166 // offset of java.lang.ref.Reference.queueNext
167 static size_t reference_queueNext_offset_;
168
169 // offset of java.lang.ref.Reference.pendingNext
170 static size_t reference_pendingNext_offset_;
171
172 // offset of java.lang.ref.FinalizerReference.zombie
173 static size_t finalizer_reference_zombie_offset_;
174
Carl Shapiro69759ea2011-07-21 18:13:35 -0700175 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
176};
177
178class HeapLock {
179 public:
180 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
181 lock_->Lock();
182 }
183 ~HeapLock() {
184 lock_->Unlock();
185 }
186 private:
187 Mutex* lock_;
188 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700189};
190
191} // namespace art
192
193#endif // ART_SRC_HEAP_H_