Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 3 | #ifndef ART_SRC_IMAGE_WRITER_H_ |
| 4 | #define ART_SRC_IMAGE_WRITER_H_ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 5 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 8 | #include <cstddef> |
| 9 | |
| 10 | #include "UniquePtr.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 11 | #include "mem_map.h" |
| 12 | #include "object.h" |
| 13 | #include "os.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 17 | // Write a Space built during compilation for use during execution. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 18 | class ImageWriter { |
| 19 | |
| 20 | public: |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 21 | ImageWriter() : image_top_(0), image_base_(NULL) {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 22 | bool Write(Space* space, const char* filename, byte* image_base); |
| 23 | ~ImageWriter() {}; |
| 24 | |
| 25 | private: |
| 26 | |
| 27 | bool Init(Space* space); |
| 28 | |
| 29 | // we use the lock word to store the offset of the object in the image |
| 30 | void SetImageOffset(Object* object, size_t offset) { |
| 31 | DCHECK(object != NULL); |
| 32 | DCHECK(object->monitor_ == NULL); // should be no lock |
| 33 | DCHECK_NE(0U, offset); |
| 34 | object->monitor_ = reinterpret_cast<Monitor*>(offset); |
| 35 | } |
| 36 | size_t GetImageOffset(const Object* object) { |
| 37 | DCHECK(object != NULL); |
| 38 | size_t offset = reinterpret_cast<size_t>(object->monitor_); |
| 39 | DCHECK_NE(0U, offset); |
| 40 | return offset; |
| 41 | } |
| 42 | Object* GetImageAddress(const Object* object) { |
| 43 | if (object == NULL) { |
| 44 | return NULL; |
| 45 | } |
| 46 | return reinterpret_cast<Object*>(image_base_ + GetImageOffset(object)); |
| 47 | } |
| 48 | |
| 49 | void CalculateNewObjectOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 50 | static void CalculateNewObjectOffsetsCallback(Object* obj, void *arg); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 51 | |
| 52 | void CopyAndFixupObjects(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 53 | static void CopyAndFixupObjectsCallback(Object* obj, void *arg); |
| 54 | void FixupClass(const Class* orig, Class* copy); |
| 55 | void FixupMethod(const Method* orig, Method* copy); |
| 56 | void FixupField(const Field* orig, Field* copy); |
| 57 | void FixupObject(const Object* orig, Object* copy); |
| 58 | void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy); |
| 59 | void FixupInstanceFields(const Object* orig, Object* copy); |
| 60 | void FixupStaticFields(const Class* orig, Class* copy); |
| 61 | void FixupFields(const Object* orig, Object* copy, uint32_t ref_offsets, bool is_static); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 62 | |
| 63 | // memory mapped for generating the image |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 64 | UniquePtr<MemMap> image_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 65 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 66 | // Offset to the free space in image_ |
| 67 | size_t image_top_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 68 | |
| 69 | // Target base address for the output image |
| 70 | byte* image_base_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace art |
| 74 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 75 | #endif // ART_SRC_IMAGE_WRITER_H_ |