blob: f7ae5a543f4a5912089d6fc6ce52a92cc2f784d9 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
4#ifndef ART_SRC_HEAP_H_
5#define ART_SRC_HEAP_H_
6
7#include "src/globals.h"
8#include "src/object.h"
9
10namespace art {
11
12class Heap {
13 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -070014 static Heap* Create() {
15 Heap* new_heap = new Heap();
16 // TODO: should return NULL if the heap could not be created.
17 return new_heap;
18 }
19
20 ~Heap() {}
21
Brian Carlstroma0808032011-07-18 00:39:23 -070022 static Object* AllocRaw(size_t size, Class* klass) {
Brian Carlstroma7f4f482011-07-17 17:01:34 -070023 byte* raw = new byte[size]();
Brian Carlstroma0808032011-07-18 00:39:23 -070024 Object* object = reinterpret_cast<Object*>(raw);
25 object->klass_ = klass;
26 return object;
Brian Carlstroma7f4f482011-07-17 17:01:34 -070027 }
28
Brian Carlstroma0808032011-07-18 00:39:23 -070029 static Object* AllocObject(Class* klass) {
30 return AllocRaw(klass->object_size_, klass);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070031 }
32
Brian Carlstroma0808032011-07-18 00:39:23 -070033 static CharArray* AllocCharArray(Class* char_array, size_t length) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070034 size_t size = sizeof(Array) + length * sizeof(uint16_t);
Brian Carlstroma0808032011-07-18 00:39:23 -070035 return reinterpret_cast<CharArray*>(AllocRaw(size, char_array));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070036 }
37
Brian Carlstroma0808032011-07-18 00:39:23 -070038 static String* AllocString(Class* java_lang_String) {
39 return reinterpret_cast<String*>(AllocObject(java_lang_String));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070040 }
41
Brian Carlstroma0808032011-07-18 00:39:23 -070042 static String* AllocStringFromModifiedUtf8(Class* java_lang_String, Class* char_array, const char* data) {
43 String* string = AllocString(java_lang_String);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070044 uint32_t count = strlen(data); // TODO
Brian Carlstroma0808032011-07-18 00:39:23 -070045 CharArray* array = AllocCharArray(char_array, count);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070046 string->array_ = array;
47 string->count_ = count;
48 return string;
49 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
51 private:
52 Heap() {}
53
54 DISALLOW_COPY_AND_ASSIGN(Heap);
Carl Shapiro1fb86202011-06-27 17:43:13 -070055};
56
57} // namespace art
58
59#endif // ART_SRC_HEAP_H_