blob: 42993732673f2268c39301ccc36e1b3fa4ae8787 [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 Carlstromdb4d5402011-08-09 12:18:28 -070014
15namespace art {
16
Brian Carlstrom4e777d42011-08-15 13:53:52 -070017// Write a Space built during compilation for use during execution.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018class ImageWriter {
19
20 public:
Brian Carlstrom4e777d42011-08-15 13:53:52 -070021 ImageWriter() : image_top_(0), image_base_(NULL) {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022 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 Carlstrom4873d462011-08-21 15:23:39 -070050 static void CalculateNewObjectOffsetsCallback(Object* obj, void *arg);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051
52 void CopyAndFixupObjects();
Brian Carlstrom4873d462011-08-21 15:23:39 -070053 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 Carlstromdb4d5402011-08-09 12:18:28 -070062
63 // memory mapped for generating the image
Elliott Hughes90a33692011-08-30 13:27:07 -070064 UniquePtr<MemMap> image_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065
Brian Carlstrom4e777d42011-08-15 13:53:52 -070066 // Offset to the free space in image_
67 size_t image_top_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070068
69 // Target base address for the output image
70 byte* image_base_;
71};
72
73} // namespace art
74
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075#endif // ART_SRC_IMAGE_WRITER_H_