blob: ac4a557aad7b0ea689b7e119a3f1ccaf9f166b78 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_IMAGE_H_
4#define ART_SRC_IMAGE_H_
5
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
16class ImageWriter {
17
18 public:
19 ImageWriter() : top_(0), image_base_(NULL) {};
20 bool Write(Space* space, const char* filename, byte* image_base);
21 ~ImageWriter() {};
22
23 private:
24
25 bool Init(Space* space);
26
27 // we use the lock word to store the offset of the object in the image
28 void SetImageOffset(Object* object, size_t offset) {
29 DCHECK(object != NULL);
30 DCHECK(object->monitor_ == NULL); // should be no lock
31 DCHECK_NE(0U, offset);
32 object->monitor_ = reinterpret_cast<Monitor*>(offset);
33 }
34 size_t GetImageOffset(const Object* object) {
35 DCHECK(object != NULL);
36 size_t offset = reinterpret_cast<size_t>(object->monitor_);
37 DCHECK_NE(0U, offset);
38 return offset;
39 }
40 Object* GetImageAddress(const Object* object) {
41 if (object == NULL) {
42 return NULL;
43 }
44 return reinterpret_cast<Object*>(image_base_ + GetImageOffset(object));
45 }
46
47 void CalculateNewObjectOffsets();
48 static void CalculateNewObjectOffsetsCallback(Object *obj, void *arg);
49
50 void CopyAndFixupObjects();
51 static void CopyAndFixupObjectsCallback(Object *obj, void *arg);
52 void FixupObject(Object* orig, Object* copy);
53 void FixupObjectArray(ObjectArray<Object>* orig, ObjectArray<Object>* copy);
54 void FixupInstanceFields(Object* orig, Object* copy);
55
56 // memory mapped for generating the image
57 scoped_ptr<MemMap> mem_map_;
58
59 // Offset to the free space in mem_map_
60 size_t top_;
61
62 // Target base address for the output image
63 byte* image_base_;
64};
65
66} // namespace art
67
68#endif // ART_SRC_IMAGE_H_