blob: c25803f9095a254c54d3315335cb489cc42eea5b [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_HEAP_H_
18#define ART_SRC_HEAP_H_
19
Carl Shapiro58551df2011-07-24 03:09:51 -070020#include <vector>
21
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "globals.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "object_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070024#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070025
Elliott Hughes3e465b12011-09-02 18:26:12 -070026#define VERIFY_OBJECT_ENABLED 0
27
Carl Shapiro1fb86202011-06-27 17:43:13 -070028namespace art {
29
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070030class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070031class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070032class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033class Space;
34class HeapBitmap;
35
Carl Shapiro1fb86202011-06-27 17:43:13 -070036class Heap {
37 public:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070038 static const size_t kInitialSize = 4 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070039
Brian Carlstrom27ec9612011-09-19 20:20:38 -070040 static const size_t kMaximumSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Elliott Hughes410c0c82011-09-01 17:58:25 -070042 typedef void (RootVisitor)(const Object* root, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070043
Brian Carlstrom58ae9412011-10-04 00:56:06 -070044 // Create a heap with the requested sizes. The possible empty
45 // image_file_names names specify Spaces to load based on
46 // ImageWriter output.
Elliott Hughesbe759c62011-09-08 19:38:21 -070047 static void Init(size_t starting_size, size_t maximum_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070048 const std::vector<std::string>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070049
Carl Shapiro69759ea2011-07-21 18:13:35 -070050 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070051
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070052 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070053 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070054
Elliott Hughesa2501992011-08-26 19:39:54 -070055 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070056#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070058#else
59 static void VerifyObject(const Object *obj) {}
60#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070061
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070062 // Check sanity of all live references. Requires the heap lock.
63 static void VerifyHeap();
64
Elliott Hughesa2501992011-08-26 19:39:54 -070065 // A weaker test than VerifyObject that doesn't require the heap lock,
66 // and doesn't abort on error, allowing the caller to report more
67 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070068 static bool IsHeapAddress(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070069
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 // Initiates an explicit garbage collection.
71 static void CollectGarbage();
72
Elliott Hughesbf86d042011-08-31 17:53:14 -070073 // Implements java.lang.Runtime.maxMemory.
74 static int64_t GetMaxMemory();
75 // Implements java.lang.Runtime.totalMemory.
76 static int64_t GetTotalMemory();
77 // Implements java.lang.Runtime.freeMemory.
78 static int64_t GetFreeMemory();
79
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070080 // Implements VMDebug.countInstancesOfClass.
81 static int64_t CountInstances(Class* c, bool count_assignable);
82
Elliott Hughes7ede61e2011-09-14 18:18:06 -070083 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
84 static void ClearGrowthLimit() {
85 UNIMPLEMENTED(WARNING);
86 }
87 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
88 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -070089 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070090 }
91 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
92 static void SetTargetHeapUtilization(float target) {
Brian Carlstrom395520e2011-09-25 19:35:00 -070093 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070094 }
Shih-wei Liao7f1caab2011-10-06 12:11:04 -070095 // Sets the maximum number of bytes that the heap is allowed to allocate
96 // from the system. Clamps to the appropriate maximum value.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -070097 static void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070098
Carl Shapiro69759ea2011-07-21 18:13:35 -070099 // Blocks the caller until the garbage collector becomes idle.
100 static void WaitForConcurrentGcToComplete();
101
Elliott Hughes92b3b562011-09-08 16:32:26 -0700102 static void Lock();
103
104 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105
Carl Shapiro58551df2011-07-24 03:09:51 -0700106 static const std::vector<Space*>& GetSpaces() {
107 return spaces_;
108 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700109
Carl Shapiro58551df2011-07-24 03:09:51 -0700110 static HeapBitmap* GetLiveBits() {
111 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700112 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700113
114 static HeapBitmap* GetMarkBits() {
115 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700116 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700117
Elliott Hughesadb460d2011-10-05 17:02:34 -0700118 static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
119 Class* java_lang_ref_ReferenceQueue);
120
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700121 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
122 MemberOffset reference_queue_offset,
123 MemberOffset reference_queueNext_offset,
124 MemberOffset reference_pendingNext_offset,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700125 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700126
Elliott Hughesadb460d2011-10-05 17:02:34 -0700127 static Object* GetReferenceReferent(Object* reference);
128 static void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700129
Elliott Hughesadb460d2011-10-05 17:02:34 -0700130 // Returns true if the reference object has not yet been enqueued.
131 static bool IsEnqueuable(const Object* ref);
132 static void EnqueueReference(Object* ref, Object** list);
133 static void EnqueuePendingReference(Object* ref, Object** list);
134 static Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136 static MemberOffset GetReferencePendingNextOffset() {
137 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700138 return reference_pendingNext_offset_;
139 }
140
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 static MemberOffset GetFinalizerReferenceZombieOffset() {
142 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700143 return finalizer_reference_zombie_offset_;
144 }
145
Elliott Hughes85d15452011-09-16 17:33:01 -0700146 static void EnableObjectValidation() {
147 verify_objects_ = true;
148 }
149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700151 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152 }
153
Elliott Hughes92b3b562011-09-08 16:32:26 -0700154 // Callers must hold the heap lock.
155 static void RecordFreeLocked(Space* space, const Object* object);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700156
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700157 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
158 // The call is not needed if NULL is stored in the field.
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700159 static void WriteBarrier(const Object* object) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700160#ifdef CONCURRENT_GARBAGE_COLLECTOR
161 // TODO: we need card marking for a concurrent collector.
162 UNIMPLEMENTED(FATAL);
163#endif
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700164 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700165
Elliott Hughesadb460d2011-10-05 17:02:34 -0700166 static void AddFinalizerReference(Object* object);
167
Carl Shapiro58551df2011-07-24 03:09:51 -0700168 private:
169 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700170 static Object* AllocateLocked(size_t num_bytes);
171 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700172
Elliott Hughesadb460d2011-10-05 17:02:34 -0700173 // Pushes a list of cleared references out to the managed heap.
174 static void EnqueueClearedReferences(Object** cleared_references);
175
Elliott Hughes92b3b562011-09-08 16:32:26 -0700176 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700177 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178
179 static void CollectGarbageInternal();
180
181 static void GrowForUtilization();
182
Elliott Hughes92b3b562011-09-08 16:32:26 -0700183 static void VerifyObjectLocked(const Object *obj);
184
Brian Carlstrom78128a62011-09-15 17:21:19 -0700185 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700186
Carl Shapiro69759ea2011-07-21 18:13:35 -0700187 static Mutex* lock_;
188
Carl Shapiro58551df2011-07-24 03:09:51 -0700189 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700191 // default Space for allocations
192 static Space* alloc_space_;
193
Carl Shapiro69759ea2011-07-21 18:13:35 -0700194 static HeapBitmap* mark_bitmap_;
195
196 static HeapBitmap* live_bitmap_;
197
Carl Shapiro58551df2011-07-24 03:09:51 -0700198 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 static size_t maximum_size_;
200
Carl Shapiro58551df2011-07-24 03:09:51 -0700201 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202 static bool is_gc_running_;
203
Carl Shapiro58551df2011-07-24 03:09:51 -0700204 // Number of bytes allocated. Adjusted after each allocation and
205 // free.
206 static size_t num_bytes_allocated_;
207
208 // Number of objects allocated. Adjusted after each allocation and
209 // free.
210 static size_t num_objects_allocated_;
211
Elliott Hughesadb460d2011-10-05 17:02:34 -0700212 static Class* java_lang_ref_FinalizerReference_;
213 static Class* java_lang_ref_ReferenceQueue_;
214
Brian Carlstrom1f870082011-08-23 16:02:11 -0700215 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700217
218 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700220
221 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700223
224 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700226
227 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 static MemberOffset finalizer_reference_zombie_offset_;
229
Brian Carlstrom395520e2011-09-25 19:35:00 -0700230 // Target ideal heap utilization ratio
231 static float target_utilization_;
232
Elliott Hughes85d15452011-09-16 17:33:01 -0700233 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700234
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
236};
237
Carl Shapiro1fb86202011-06-27 17:43:13 -0700238} // namespace art
239
240#endif // ART_SRC_HEAP_H_