replicode
object.h
Go to the documentation of this file.
1 // object.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_object_h
32 #define r_code_object_h
33 
34 #include "atom.h"
35 #include "vector.h"
36 #include "list.h"
37 #include "replicode_defs.h"
38 #include "utils.h"
39 
40 #include "CoreLibrary/base.h"
41 #include <unordered_set>
42 
43 using namespace core;
44 
45 namespace r_code {
46 
47 // I/O from/to r_code::Image ////////////////////////////////////////////////////////////////////////
48 
50 public:
53  virtual ~ImageObject() {}
54 
55  virtual void write(uintptr_t *data) = 0;
56  virtual void read(uintptr_t *data) = 0;
57  virtual void trace() = 0;
58 };
59 
60 class View;
61 
63  public ImageObject {
64 public:
65  SysView();
66  SysView(View *source);
67 
68  void write(uintptr_t *data);
69  void read(uintptr_t *data);
70  size_t get_size() const;
71  void trace();
72 };
73 
74 class Code;
75 
77  public ImageObject {
78 private:
79  static uintptr_t LastOID;
80 public:
81  r_code::vector<uintptr_t> markers; // indexes in the relocation segment
83 
84  uintptr_t oid;
85 
86  SysObject();
87  SysObject(Code *source);
88  ~SysObject();
89 
90  void write(uintptr_t *data);
91  void read(uintptr_t *data);
92  size_t get_size();
93  void trace();
94 };
95 
96 // Interfaces for r_exec classes ////////////////////////////////////////////////////////////////////////
97 
98 class Object;
99 
101  public _Object {
102 private:
103  uint16_t index; // for unpacking: index is the index of the view in the SysObject.
104 protected:
105  Atom _code[VIEW_CODE_MAX_SIZE]; // dimensioned to hold the largest view (group view): head atom, iptr to ijt, sln, res, rptr to grp, rptr to org, vis, cov, 3 atoms for ijt's timestamp; oid is the last word32 (not an atom).
106 public:
107  Code *references[2]; // does not include the viewed object; no smart pointer here (a view is held by a group and holds a ref to said group in references[0]).
108  P<Code> object; // viewed object.
109 
110  View(): object(NULL) {
111 
112  references[0] = references[1] = NULL;
113  }
114 
115  View(SysView *source, Code *object) {
116 
117  for (uint16_t i = 0; i < source->code.size(); ++i)
118  _code[i] = source->code[i];
119  references[0] = references[1] = NULL;
120  this->object = object;
121  }
122 
123  virtual ~View() {}
124 
125  Atom &code(uint16_t i) {
126  return _code[i];
127  }
128  Atom code(uint16_t i) const {
129  return _code[i];
130  }
131 
132  typedef enum {
133  SYNC_ONCE = 0,
134  SYNC_PERIODIC = 1,
135  SYNC_HOLD = 2,
136  SYNC_AXIOM = 3,
137  SYNC_ONCE_AXIOM = 4
138  } SyncMode;
139 
140  SyncMode get_sync() const {
141  return (SyncMode)(uint64_t)_code[VIEW_SYNC];
142  }
143  uint64_t get_ijt() const {
144  return Utils::GetTimestamp(_code + _code[VIEW_IJT].asIndex());
145  }
146  void set_ijt(uint64_t ijt) {
147  Utils::SetTimestamp(_code + _code[VIEW_IJT].asIndex(), ijt);
148  }
149 
150  class Hash {
151  public:
152  size_t operator()(View *v) const {
153  return (size_t)(Code *)v->references[0]; // i.e. the group the view belongs to.
154  }
155  };
156 
157  class Equal {
158  public:
159  bool operator()(const View *lhs, const View *rhs) const {
160  return lhs->references[0] == rhs->references[0];
161  }
162  };
163 
164  class Less {
165  public:
166  bool operator()(const View *lhs, const View *rhs) const {
167  return lhs->get_ijt() < rhs->get_ijt();
168  }
169  };
170 };
171 
173  public _Object {
174 public:
175  static const int64_t null_storage_index = -1;
176  static const uint64_t CodeMarkersInitialSize = 8;
177 protected:
178  int64_t storage_index; // -1: not sored; >0 index of the object in a vector-based container.
179 
180  void load(SysObject *source) {
181 
182  for (uint16_t i = 0; i < source->code.size(); ++i)
183  code(i) = source->code[i];
184  set_oid(source->oid);
185  }
186  template<class V> View *build_view(SysView *source) {
187 
188  return new V(source, this);
189  }
190 public:
191  void set_stroage_index(int64_t i) {
192  storage_index = i;
193  }
194  bool is_registered() const {
195  return storage_index > null_storage_index;
196  }
197  int64_t get_storage_index() const {
198  return storage_index;
199  }
200 
201  virtual uint64_t get_oid() const = 0;
202  virtual void set_oid(uint64_t oid) = 0;
203 
204  virtual Atom &code(uint16_t i) = 0;
205  virtual Atom &code(uint16_t i) const = 0;
206  virtual uint16_t code_size() const = 0;
207  virtual void resize_code(uint16_t new_size) = 0;
208  virtual void set_reference(uint16_t i, Code *object) = 0;
209  virtual Code *get_reference(uint16_t i) const = 0;
210  virtual uint16_t references_size() const = 0;
211  virtual void clear_references() = 0;
212  virtual void set_references(std::vector<P<Code> > &new_references) = 0;
213 
214  virtual bool is_compact() const {
215  return false;
216  }
217  virtual bool is_invalidated() {
218  return false;
219  }
220  virtual bool invalidate() {
221  return false;
222  }
223 
225  std::unordered_set<View *, View::Hash, View::Equal> views; // indexed by groups.
226 
227  virtual View *build_view(SysView *source) = 0;
228 
229  virtual void acq_views() {}
230  virtual void rel_views() {}
231  virtual void acq_markers() {}
232  virtual void rel_markers() {}
233 
234  virtual double get_psln_thr() {
235  return 1;
236  }
237 
238  Code(): storage_index(null_storage_index) {
239  markers.reserve(CodeMarkersInitialSize);
240  }
241  virtual ~Code() {}
242 
243  virtual void mod(uint16_t member_index, double value) {};
244  virtual void set(uint16_t member_index, double value) {};
245  virtual View *get_view(Code *group, bool lock) {
246  return NULL;
247  }
248  virtual void add_reference(Code *object) const {} // called only on local objects.
249  void remove_marker(Code *m) {
250 
251  acq_markers();
252  markers.remove(m);
253  rel_markers();
254  }
255 
256  void trace() const {
257 
258  std::cout << "--------\n";
259  for (uint16_t i = 0; i < code_size(); ++i) {
260 
261  std::cout << i << "\t";
262  code(i).trace();
263  std::cout << std::endl;
264  }
265  std::cout << "OID: " << get_oid() << std::endl;
266  }
267 };
268 
269 // Implementation for local objects (non distributed).
271  public Code {
272 protected:
276 public:
277  LObject(): Code() {}
278  LObject(SysObject *source): Code() {
279 
280  load(source);
281  }
282  virtual ~LObject() {}
283 
284  View *build_view(SysView *source) {
285 
286  return Code::build_view<View>(source);
287  }
288 
289  uint64_t get_oid() const {
290  return _oid;
291  }
292  void set_oid(uint64_t oid) {
293  _oid = oid;
294  }
295 
296  Atom &code(uint16_t i) {
297  return _code[i];
298  }
299  Atom &code(uint16_t i) const {
300  return (*_code.as_std())[i];
301  }
302  uint16_t code_size() const {
303  return _code.size();
304  }
305  void resize_code(uint16_t new_size) {
306  _code.as_std()->resize(new_size);
307  }
308  void set_reference(uint16_t i, Code *object) {
309  _references[i] = object;
310  }
311  Code *get_reference(uint16_t i) const {
312  return (*_references.as_std())[i];
313  }
314  uint16_t references_size() const {
315  return _references.size();
316  }
318  _references.as_std()->clear();
319  }
320  void set_references(std::vector<P<Code> > &new_references) {
321  (*_references.as_std()) = new_references;
322  }
323  void add_reference(Code *object) const {
324  _references.as_std()->push_back(object);
325  }
326 };
327 
329 protected:
330  static Mem *Singleton;
331  Mem();
332 public:
333  static Mem *Get();
334 
335  virtual Code *build_object(SysObject *source) const = 0;
336  virtual void delete_object(Code *object) = 0;
337 };
338 }
339 
340 
341 #endif
virtual ~LObject()
Definition: object.h:282
virtual ~View()
Definition: object.h:123
uint64_t get_ijt() const
Definition: object.h:143
virtual bool invalidate()
Definition: object.h:220
void load(SysObject *source)
Definition: object.h:180
#define VIEW_SYNC
Definition: replicode_defs.h:41
void set_stroage_index(int64_t i)
Definition: object.h:191
void reserve(uint64_t size)
Definition: list.h:149
bool operator()(const View *lhs, const View *rhs) const
Definition: object.h:159
Definition: base.h:75
void set_ijt(uint64_t ijt)
Definition: object.h:146
Atom & code(uint16_t i) const
Definition: object.h:299
View * build_view(SysView *source)
Definition: object.h:186
Atom code(uint16_t i) const
Definition: object.h:128
#define VIEW_CODE_MAX_SIZE
Definition: replicode_defs.h:38
size_t operator()(View *v) const
Definition: object.h:152
virtual void add_reference(Code *object) const
Definition: object.h:248
Definition: base.cpp:35
uint16_t references_size() const
Definition: object.h:314
#define dll_export
Definition: dll.h:44
Definition: object.h:328
Code * references[2]
Definition: object.h:107
void set_reference(uint16_t i, Code *object)
Definition: object.h:308
static Mem * Singleton
Definition: object.h:330
Definition: atom.cpp:36
LObject(SysObject *source)
Definition: object.h:278
bool operator()(const View *lhs, const View *rhs) const
Definition: object.h:166
void set_references(std::vector< P< Code > > &new_references)
Definition: object.h:320
void remove_marker(Code *m)
Definition: object.h:249
void set_oid(uint64_t oid)
Definition: object.h:292
virtual void mod(uint16_t member_index, double value)
Definition: object.h:243
virtual bool is_invalidated()
Definition: object.h:217
Definition: object.h:49
uint16_t code_size() const
Definition: object.h:302
Definition: object.h:157
r_code::vector< Atom > code
Definition: object.h:51
SyncMode
Definition: object.h:132
Definition: object.h:270
r_code::vector< SysView * > views
Definition: object.h:82
Atom & code(uint16_t i)
Definition: object.h:296
#define VIEW_IJT
Definition: replicode_defs.h:42
LObject()
Definition: object.h:277
int64_t storage_index
Definition: object.h:178
virtual void acq_views()
Definition: object.h:229
P< Code > object
Definition: object.h:108
std::vector< T > * as_std() const
Definition: vector.h:58
uintptr_t oid
Definition: object.h:84
void remove(const T &t)
Definition: list.h:300
SyncMode get_sync() const
Definition: object.h:140
virtual ~Code()
Definition: object.h:241
r_code::vector< uintptr_t > references
Definition: object.h:52
virtual void rel_views()
Definition: object.h:230
Definition: object.h:150
View * build_view(SysView *source)
Definition: object.h:284
void resize_code(uint16_t new_size)
Definition: object.h:305
Definition: atom.h:45
virtual bool is_compact() const
Definition: object.h:214
uint64_t _oid
Definition: object.h:273
static uintptr_t LastOID
Definition: object.h:79
Definition: object.h:76
uint64_t get_oid() const
Definition: object.h:289
size_t size() const
Definition: vector.h:44
Definition: object.h:100
Atom & code(uint16_t i)
Definition: object.h:125
Definition: list.h:42
virtual ~ImageObject()
Definition: object.h:53
Definition: object.h:172
View()
Definition: object.h:110
virtual double get_psln_thr()
Definition: object.h:234
void trace() const
Definition: object.h:256
Definition: object.h:164
virtual void rel_markers()
Definition: object.h:232
virtual void set(uint16_t member_index, double value)
Definition: object.h:244
virtual void acq_markers()
Definition: object.h:231
Code()
Definition: object.h:238
void clear_references()
Definition: object.h:317
Definition: object.h:62
uint16_t index
Definition: object.h:103
bool is_registered() const
Definition: object.h:194
std::unordered_set< View *, View::Hash, View::Equal > views
Definition: object.h:225
r_code::list< Code * > markers
Definition: object.h:224
int64_t get_storage_index() const
Definition: object.h:197
r_code::vector< Atom > _code
Definition: object.h:274
void add_reference(Code *object) const
Definition: object.h:323
virtual View * get_view(Code *group, bool lock)
Definition: object.h:245
View(SysView *source, Code *object)
Definition: object.h:115
r_code::vector< P< Code > > _references
Definition: object.h:275
Code * get_reference(uint16_t i) const
Definition: object.h:311
r_code::vector< uintptr_t > markers
Definition: object.h:81