blob: 80012bcef60ad843eeacbae315a9550397c4dc6b [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07003#ifndef ART_SRC_IMAGE_WRITER_H_
4#define ART_SRC_IMAGE_WRITER_H_
Brian Carlstromdb4d5402011-08-09 12:18:28 -07005
Brian Carlstromdb4d5402011-08-09 12:18:28 -07006#include <stdint.h>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include <cstddef>
9
10#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070011#include "mem_map.h"
12#include "object.h"
13#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070014#include "space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015
16namespace art {
17
Brian Carlstrom4e777d42011-08-15 13:53:52 -070018// Write a Space built during compilation for use during execution.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019class ImageWriter {
20
21 public:
Brian Carlstrom4e777d42011-08-15 13:53:52 -070022 ImageWriter() : image_top_(0), image_base_(NULL) {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070023 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 Carlstrom4873d462011-08-21 15:23:39 -070051 static void CalculateNewObjectOffsetsCallback(Object* obj, void *arg);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052
53 void CopyAndFixupObjects();
Brian Carlstrom4873d462011-08-21 15:23:39 -070054 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 Carlstromdb4d5402011-08-09 12:18:28 -070063
64 // memory mapped for generating the image
Elliott Hughes90a33692011-08-30 13:27:07 -070065 UniquePtr<MemMap> image_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066
Brian Carlstrom4e777d42011-08-15 13:53:52 -070067 // Offset to the free space in image_
68 size_t image_top_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069
70 // Target base address for the output image
71 byte* image_base_;
72};
73
74} // namespace art
75
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070076#endif // ART_SRC_IMAGE_WRITER_H_