blob: b9f7937dd15508099ce7cb6d482a6232a88ea2e0 [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 Carlstrom934486c2011-07-12 23:42:50 -070022 static Class* AllocClass(DexFile* dex_file) {
Carl Shapiro565f5072011-07-10 13:39:43 -070023 byte* raw = new byte[sizeof(Class)]();
Brian Carlstrom934486c2011-07-12 23:42:50 -070024 Class* klass = reinterpret_cast<Class*>(raw);
25 klass->dex_file_ = dex_file;
26 return klass;
Carl Shapiro1fb86202011-06-27 17:43:13 -070027 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070028
Brian Carlstroma7f4f482011-07-17 17:01:34 -070029 static StaticField* AllocStaticField() {
30 size_t size = sizeof(StaticField);
31 byte* raw = new byte[size]();
32 return reinterpret_cast<StaticField*>(raw);
33 }
34
35 static InstanceField* AllocInstanceField() {
36 size_t size = sizeof(InstanceField);
37 byte* raw = new byte[size]();
38 return reinterpret_cast<InstanceField*>(raw);
39 }
40
41 static Method* AllocMethod() {
42 size_t size = sizeof(Method);
43 byte* raw = new byte[size]();
44 return reinterpret_cast<Method*>(raw);
45 }
46
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070047 static CharArray* AllocCharArray(size_t length) {
48 size_t size = sizeof(Array) + length * sizeof(uint16_t);
49 byte* raw = new byte[size]();
50 return reinterpret_cast<CharArray*>(raw);
51 }
52
53 static String* AllocString() {
54 size_t size = sizeof(String);
55 byte* raw = new byte[size]();
56 return reinterpret_cast<String*>(raw);
57 }
58
59 static String* AllocStringFromModifiedUtf8(const char* data) {
60 String* string = AllocString();
61 uint32_t count = strlen(data); // TODO
62 CharArray* array = AllocCharArray(count);
63 string->array_ = array;
64 string->count_ = count;
65 return string;
66 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070067
68 private:
69 Heap() {}
70
71 DISALLOW_COPY_AND_ASSIGN(Heap);
Carl Shapiro1fb86202011-06-27 17:43:13 -070072};
73
74} // namespace art
75
76#endif // ART_SRC_HEAP_H_