85 #ifndef r_code_object_h
86 #define r_code_object_h
90 #include <unordered_set>
92 #include "resized_vector.h"
94 #include "replicode_defs.h"
96 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
100 using namespace core;
111 virtual void write(word32 *data) = 0;
112 virtual void read(word32 *data) = 0;
113 virtual void trace(std::ostream& out)
const = 0;
124 void write(word32 *data)
override;
125 void read(word32 *data)
override;
126 uint32 get_size()
const;
127 void trace(std::ostream& out)
const override;
128 #ifdef WITH_DETAIL_OID
138 static uint32 lastOID_;
144 #ifdef WITH_DETAIL_OID
152 void write(word32 *data)
override;
153 void read(word32 *data)
override;
155 void trace(std::ostream& out)
const override;
166 Atom code_[VIEW_CODE_MAX_SIZE];
168 Code *references_[2];
171 _View() : object_(NULL) {
173 references_[0] = references_[1] = NULL;
178 for (uint16 i = 0; i < source->code_.size(); ++i)
179 code_[i] = source->code_[i];
180 references_[0] = references_[1] = NULL;
186 Atom &code(uint16 i) {
return code_[i]; }
187 Atom code(uint16 i)
const {
return code_[i]; }
197 SyncMode get_sync()
const {
return (SyncMode)(uint32)code_[VIEW_SYNC].asFloat(); }
198 Timestamp get_ijt()
const {
return Utils::GetTimestamp(code_ + code_[VIEW_IJT].asIndex()); }
199 void set_ijt(Timestamp ijt) { Utils::SetTimestamp(code_ + code_[VIEW_IJT].asIndex(), ijt); }
203 size_t operator ()(
_View *v)
const {
204 return (
size_t)(
Code *)v->references_[0];
210 bool operator ()(
const _View *lhs,
const _View *rhs)
const {
211 return lhs->references_[0] == rhs->references_[0];
217 bool operator ()(
const _View *lhs,
const _View *rhs)
const {
218 return lhs->get_ijt() < rhs->get_ijt();
226 static const int32 null_storage_index = -1;
227 static const uint32 CodeMarkersInitialSize = 8;
229 int32 storage_index_;
233 for (uint16 i = 0; i < source->code_.size(); ++i)
234 code(i) = source->code_[i];
235 set_oid(source->oid_);
239 return new V(source,
this);
242 void set_strorage_index(int32 i) { storage_index_ = i; }
243 bool is_registered()
const {
return storage_index_ > null_storage_index; }
244 int32 get_storage_index()
const {
return storage_index_; }
246 virtual uint32 get_oid()
const = 0;
247 virtual void set_oid(uint32 oid) = 0;
249 virtual Atom &code(uint16 i) = 0;
250 virtual const Atom &code(uint16 i)
const = 0;
251 virtual uint16 code_size()
const = 0;
252 virtual void resize_code(uint16 new_size) = 0;
253 bool includes(
Atom a)
const {
254 for (uint16 i = 0; i < code_size(); ++i) {
260 virtual void set_reference(uint16 i,
Code *
object) = 0;
261 virtual Code *get_reference(uint16 i)
const = 0;
262 virtual uint16 references_size()
const = 0;
263 virtual void clear_references() = 0;
264 virtual void set_references(std::vector<
P<Code> > &new_references) = 0;
266 virtual bool is_compact()
const {
return false; }
267 virtual bool is_invalidated() {
return false; }
268 virtual bool invalidate() {
return false; }
271 std::unordered_set<_View *, _View::Hash, _View::Equal> views_;
275 virtual void acq_views() {}
276 virtual void rel_views() {}
277 virtual void acq_markers() {}
278 virtual void rel_markers() {}
280 virtual float32 get_psln_thr() {
return 1; }
282 Code() : storage_index_(null_storage_index) { markers_.reserve(CodeMarkersInitialSize); }
285 virtual void mod(uint16 , float32 ) {};
286 virtual void set(uint16 , float32 ) {};
287 virtual _View *get_view(
Code* ,
bool ) {
return NULL; }
288 virtual void add_reference(
Code* ) {}
289 void remove_marker(
Code *m) {
301 atom.trace(context, out);
302 if (atom.getDescriptor() == Atom::R_PTR) {
303 if (atom.asIndex() < references_size()) {
304 out <<
" -> " << get_reference(atom.asIndex())->get_oid();
305 #ifdef WITH_DETAIL_OID
306 out <<
"(" << get_reference(atom.asIndex())->get_detail_oid() <<
")";
310 out <<
" (unassigned) ";
317 void trace(std::ostream& out)
const {
321 for (uint16 i = 0; i < code_size(); ++i) {
324 trace(i, out, context);
327 out <<
"OID: " << get_oid();
328 #ifdef WITH_DETAIL_OID
329 out <<
"(" << get_detail_oid() <<
")";
334 void trace()
const { trace(std::cout); }
340 std::ostringstream out;
350 std::ostringstream out;
351 trace(i, out, context);
361 void r_trace(std::ostream& out)
const;
363 void r_trace()
const { r_trace(std::cout); }
369 std::ostringstream out;
392 return Code::build_view<_View>(source);
395 uint32 get_oid()
const override {
return oid_; }
396 void set_oid(uint32 oid)
override { oid_ = oid; }
398 Atom &code(uint16 i)
override {
return code_[i]; }
399 const Atom &code(uint16 i)
const override {
return code_[i]; }
400 uint16 code_size()
const override {
402 return (uint16)code_.size();
404 void resize_code(uint16 new_size)
override { code_.resize(new_size); }
405 void set_reference(uint16 i,
Code *
object)
override { references_[i] = object; }
406 Code *get_reference(uint16 i)
const override {
return references_[i]; }
407 uint16 references_size()
const override {
409 return (uint16)references_.size();
411 void clear_references()
override { references_.clear(); }
412 void set_references(std::vector<
P<Code> > &new_references)
override {
414 for (
size_t i = 0; i < new_references.size(); ++i)
415 references_.push_back(new_references[i]);
417 void add_reference(
Code *
object)
override { references_.push_back(
object); }
422 static Mem *singleton_;
428 virtual void delete_object(
Code *
object) = 0;