blob: 99f655424adc27d43d14af0f4c37a85ed9a9e3a0 [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
Carl Shapiro69759ea2011-07-21 18:13:35 -070036 // Initiates an explicit garbage collection.
37 static void CollectGarbage();
38
39 // Blocks the caller until the garbage collector becomes idle.
40 static void WaitForConcurrentGcToComplete();
41
42 static Mutex* GetLock() {
43 return lock_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070044 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070045
Carl Shapiro58551df2011-07-24 03:09:51 -070046 static const std::vector<Space*>& GetSpaces() {
47 return spaces_;
48 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070049
Carl Shapiro58551df2011-07-24 03:09:51 -070050 static HeapBitmap* GetLiveBits() {
51 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070052 }
Carl Shapiro58551df2011-07-24 03:09:51 -070053
54 static HeapBitmap* GetMarkBits() {
55 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -070056 }
Carl Shapiro58551df2011-07-24 03:09:51 -070057
Carl Shapiro58551df2011-07-24 03:09:51 -070058 private:
59 // Allocates uninitialized storage.
Carl Shapiro69759ea2011-07-21 18:13:35 -070060 static Object* Allocate(size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -070061 static Object* Allocate(Space* space, size_t num_bytes);
62
63 static void RecordAllocation(Space* space, const Object* object);
64 static void RecordFree(Space* space, const Object* object);
Carl Shapiro69759ea2011-07-21 18:13:35 -070065
66 static void CollectGarbageInternal();
67
68 static void GrowForUtilization();
69
70 static Mutex* lock_;
71
Carl Shapiro58551df2011-07-24 03:09:51 -070072 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070073
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 // default Space for allocations
75 static Space* alloc_space_;
76
Carl Shapiro69759ea2011-07-21 18:13:35 -070077 static HeapBitmap* mark_bitmap_;
78
79 static HeapBitmap* live_bitmap_;
80
Carl Shapiro58551df2011-07-24 03:09:51 -070081 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -070082 static size_t maximum_size_;
83
Carl Shapiro58551df2011-07-24 03:09:51 -070084 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 static bool is_gc_running_;
86
Carl Shapiro58551df2011-07-24 03:09:51 -070087 // Number of bytes allocated. Adjusted after each allocation and
88 // free.
89 static size_t num_bytes_allocated_;
90
91 // Number of objects allocated. Adjusted after each allocation and
92 // free.
93 static size_t num_objects_allocated_;
94
Carl Shapiro69759ea2011-07-21 18:13:35 -070095 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
96};
97
98class HeapLock {
99 public:
100 HeapLock(Heap* heap) : lock_(heap->GetLock()) {
101 lock_->Lock();
102 }
103 ~HeapLock() {
104 lock_->Unlock();
105 }
106 private:
107 Mutex* lock_;
108 DISALLOW_COPY_AND_ASSIGN(HeapLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700109};
110
111} // namespace art
112
113#endif // ART_SRC_HEAP_H_