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 | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 22 | static Object* AllocRaw(size_t size, Class* klass) { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 23 | byte* raw = new byte[size](); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 24 | Object* object = reinterpret_cast<Object*>(raw); |
| 25 | object->klass_ = klass; |
| 26 | return object; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 29 | static Object* AllocObject(Class* klass) { |
| 30 | return AllocRaw(klass->object_size_, klass); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 33 | static CharArray* AllocCharArray(Class* char_array, size_t length) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 34 | size_t size = sizeof(Array) + length * sizeof(uint16_t); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 35 | return reinterpret_cast<CharArray*>(AllocRaw(size, char_array)); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 38 | static String* AllocString(Class* java_lang_String) { |
| 39 | return reinterpret_cast<String*>(AllocObject(java_lang_String)); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 42 | static String* AllocStringFromModifiedUtf8(Class* java_lang_String, Class* char_array, const char* data) { |
| 43 | String* string = AllocString(java_lang_String); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 44 | uint32_t count = strlen(data); // TODO |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame^] | 45 | CharArray* array = AllocCharArray(char_array, count); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 46 | string->array_ = array; |
| 47 | string->count_ = count; |
| 48 | return string; |
| 49 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | Heap() {} |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(Heap); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } // namespace art |
| 58 | |
| 59 | #endif // ART_SRC_HEAP_H_ |