Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // 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 | |
| 10 | namespace art { |
| 11 | |
| 12 | class Heap { |
| 13 | public: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 14 | 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 Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 22 | static Class* AllocClass(DexFile* dex_file) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 23 | byte* raw = new byte[sizeof(Class)](); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 24 | Class* klass = reinterpret_cast<Class*>(raw); |
| 25 | klass->dex_file_ = dex_file; |
| 26 | return klass; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 27 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 28 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame^] | 29 | 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 Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 47 | 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 Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | Heap() {} |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(Heap); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace art |
| 75 | |
| 76 | #endif // ART_SRC_HEAP_H_ |