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