blob: ebd86a57b466adea090f8689a552b25b8de0be66 [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
6#include <cstddef>
7#include <stdint.h>
8
9#include "mem_map.h"
10#include "object.h"
11#include "os.h"
12#include "scoped_ptr.h"
13
14namespace art {
15
Brian Carlstrom4e777d42011-08-15 13:53:52 -070016// Write a Space built during compilation for use during execution.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070017class ImageWriter {
18
19 public:
Brian Carlstrom4e777d42011-08-15 13:53:52 -070020 ImageWriter() : image_top_(0), image_base_(NULL) {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021 bool Write(Space* space, const char* filename, byte* image_base);
22 ~ImageWriter() {};
23
24 private:
25
26 bool Init(Space* space);
27
28 // we use the lock word to store the offset of the object in the image
29 void SetImageOffset(Object* object, size_t offset) {
30 DCHECK(object != NULL);
31 DCHECK(object->monitor_ == NULL); // should be no lock
32 DCHECK_NE(0U, offset);
33 object->monitor_ = reinterpret_cast<Monitor*>(offset);
34 }
35 size_t GetImageOffset(const Object* object) {
36 DCHECK(object != NULL);
37 size_t offset = reinterpret_cast<size_t>(object->monitor_);
38 DCHECK_NE(0U, offset);
39 return offset;
40 }
41 Object* GetImageAddress(const Object* object) {
42 if (object == NULL) {
43 return NULL;
44 }
45 return reinterpret_cast<Object*>(image_base_ + GetImageOffset(object));
46 }
47
48 void CalculateNewObjectOffsets();
Brian Carlstrom4873d462011-08-21 15:23:39 -070049 static void CalculateNewObjectOffsetsCallback(Object* obj, void *arg);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050
51 void CopyAndFixupObjects();
Brian Carlstrom4873d462011-08-21 15:23:39 -070052 static void CopyAndFixupObjectsCallback(Object* obj, void *arg);
53 void FixupClass(const Class* orig, Class* copy);
54 void FixupMethod(const Method* orig, Method* copy);
55 void FixupField(const Field* orig, Field* copy);
56 void FixupObject(const Object* orig, Object* copy);
57 void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy);
58 void FixupInstanceFields(const Object* orig, Object* copy);
59 void FixupStaticFields(const Class* orig, Class* copy);
60 void FixupFields(const Object* orig, Object* copy, uint32_t ref_offsets, bool is_static);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061
62 // memory mapped for generating the image
Brian Carlstrom4e777d42011-08-15 13:53:52 -070063 scoped_ptr<MemMap> image_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070064
Brian Carlstrom4e777d42011-08-15 13:53:52 -070065 // Offset to the free space in image_
66 size_t image_top_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070067
68 // Target base address for the output image
69 byte* image_base_;
70};
71
72} // namespace art
73
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074#endif // ART_SRC_IMAGE_WRITER_H_