replicode
image.h
Go to the documentation of this file.
1 // image.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef r_code_image_h
32 #define r_code_image_h
33 
34 #include "CoreLibrary/dll.h"
35 
36 #include <fstream>
37 
38 
39 using namespace std;
40 
41 namespace r_comp {
42 class Image;
43 }
44 
45 namespace r_code {
46 
47 // An image contains the following:
48 // - sizes of what follows
49 // - object map: list of indexes (4 bytes) of objects in the code segment
50 // - code segment: list of objects
51 // - object:
52 // - size of the code (number of atoms)
53 // - size of the reference set (number of pointers)
54 // - size of the marker set (number of pointers)
55 // - size of the view set (number of views)
56 // - code: indexes in internal pointers are relative to the beginning of the object, indexes in reference pointers are relative to the beginning of the reference set
57 // - reference set:
58 // - number of pointers
59 // - pointers to the relocation segment, i.e. indexes of relocation entries
60 // - marker set:
61 // - number of pointers
62 // - pointers to the relocation segment, i.e. indexes of relocation entries
63 // - view set: list of views
64 // - view:
65 // - size of the code (number of atoms)
66 // - size of the reference set (number of pointers)
67 // - list of atoms
68 // - reference set:
69 // - pointers to the relocation segment, i.e. indexes of relocation entries
70 //
71 // RAM layout of Image::data [sizes in word32]:
72 //
73 // data[0]: index of first object in data (i.e. def_size+map_size) [1]
74 // ... ...
75 // data[map_size-1]: index of last object in data [1]
76 // data[map_size]: first word32 of code segment [1]
77 // ... ...
78 // data[map_size+code_size-1]: last word32 of code segment [1]
79 //
80 // I is the implementation class; prototype:
81 // class ImageImpl{
82 // protected:
83 // uin64 get_timestamp() const;
84 // uint64_t map_size() const;
85 // uint64_t code_size() const;
86 // word32 *data(); // [object map|code segment|reloc segment]
87 // public:
88 // void *operator new(size_t,uint64_t data_size);
89 // ImageImpl(uint64_t map_size,uint64_t code_size);
90 // ~ImageImpl();
91 // };
92 
93 template<class I> class Image:
94  public I {
95  friend class r_comp::Image;
96 public:
97  static Image<I> *Build(uint64_t timestamp, size_t map_size, size_t code_size, size_t names_size);
98 // file IO
99  static Image<I> *Read(ifstream &stream);
100  static void Write(Image<I> *image, ofstream &stream);
101 
102  Image();
103  ~Image();
104 
105  size_t get_size() const; // size of data in word32
106  size_t getObjectCount() const;
107  uintptr_t *getObject(size_t i); // points to the code size of the object; the first atom is at getObject()+2
108  uintptr_t *getCodeSegment(); // equals getObject(0)
109  size_t getCodeSegmentSize() const;
110 
111  void trace() const;
112 };
113 
114 // utilities
115 size_t dll_export GetSize(const std::string &s); // returns the number of word32 needed to encode the string
116 void dll_export Write(uintptr_t *data, const std::string &s);
117 void dll_export Read(uintptr_t *data, std::string &s);
118 }
119 
120 
121 #include "image.tpl.cpp"
122 
123 
124 #endif
Definition: segments.h:119
#define dll_export
Definition: dll.h:44
Image()
Definition: segments.cpp:406
Definition: atom.cpp:36
Definition: image.h:93
~Image()
Definition: segments.cpp:409
uint64_t timestamp
Definition: segments.h:135
size_t GetSize(const std::string &s)
Definition: image.cpp:36
void Read(uintptr_t *data, std::string &s)
Definition: image.cpp:53
void Write(uintptr_t *data, const std::string &s)
Definition: image.cpp:44
Definition: image.h:41