AERA
segments.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #include "segments.h"
86 
87 #include <iostream>
88 
89 using namespace std;
90 using namespace std::chrono;
91 using namespace r_code;
92 
93 namespace r_comp {
94 
95 Reference::Reference() {
96 }
97 
98 Reference::Reference(const uint16 i, const Class &c, const Class &cc) : index_(i), class_(c), cast_class_(cc) {
99 }
100 
102 
103 Metadata::Metadata() {
104 }
105 
106 Class *Metadata::get_class(std::string &class_name) {
107 
108  unordered_map<std::string, Class>::iterator it = classes_.find(class_name);
109  if (it != classes_.end())
110  return &it->second;
111  return NULL;
112 }
113 
114 Class *Metadata::get_class(uint16 opcode) {
115 
116  return &classes_by_opcodes_[opcode];
117 }
118 
119 void Metadata::write(word32 *data) {
120 
121  data[0] = classes_by_opcodes_.size();
122  uint32 i;
123  uint32 offset = 1;
124  for (i = 0; i < classes_by_opcodes_.size(); ++i) {
125 
126  classes_by_opcodes_[i].write(data + offset);
127  offset += classes_by_opcodes_[i].get_size();
128  }
129 
130  data[offset++] = classes_.size();
131  unordered_map<std::string, Class>::iterator it = classes_.begin();
132  for (; it != classes_.end(); ++it) {
133 
134  r_code::Write(data + offset, it->first);
135  offset += r_code::GetSize(it->first);
136  data[offset] = it->second.atom_.asOpcode();
137  offset++;
138  }
139 
140  data[offset++] = sys_classes_.size();
141  it = sys_classes_.begin();
142  for (; it != sys_classes_.end(); ++it) {
143 
144  r_code::Write(data + offset, it->first);
145  offset += r_code::GetSize(it->first);
146  data[offset] = it->second.atom_.asOpcode();
147  offset++;
148  }
149 
150  data[offset++] = class_names_.size();
151  for (i = 0; i < class_names_.size(); ++i) {
152 
153  r_code::Write(data + offset, class_names_[i]);
154  offset += r_code::GetSize(class_names_[i]);
155  }
156 
157  data[offset++] = operator_names_.size();
158  for (i = 0; i < operator_names_.size(); ++i) {
159 
160  r_code::Write(data + offset, operator_names_[i]);
161  offset += r_code::GetSize(operator_names_[i]);
162  }
163 
164  data[offset++] = function_names_.size();
165  for (i = 0; i < function_names_.size(); ++i) {
166 
167  r_code::Write(data + offset, function_names_[i]);
168  offset += r_code::GetSize(function_names_[i]);
169  }
170 }
171 
172 void Metadata::read(word32 *data, uint32 size) {
173 
174  uint16 class_count = data[0];
175  uint16 i;
176  uint16 offset = 1;
177  for (i = 0; i < class_count; ++i) {
178 
179  Class c;
180  c.read(data + offset);
181  classes_by_opcodes_.push_back(c);
182  offset += c.get_size();
183  }
184 
185  uint16 classes_count = data[offset++];
186  for (i = 0; i < classes_count; ++i) {
187 
188  std::string s;
189  r_code::Read(data + offset, s);
190  offset += r_code::GetSize(s);
191  classes_[s] = classes_by_opcodes_[data[offset++]];
192  }
193 
194  uint16 sys_classes_count = data[offset++];
195  for (i = 0; i < sys_classes_count; ++i) {
196 
197  std::string s;
198  r_code::Read(data + offset, s);
199  offset += r_code::GetSize(s);
200  sys_classes_[s] = classes_by_opcodes_[data[offset++]];
201  }
202 
203  uint16 class_names_count = data[offset++];
204  for (i = 0; i < class_names_count; ++i) {
205 
206  std::string s;
207  r_code::Read(data + offset, s);
208  class_names_.push_back(s);
209  offset += r_code::GetSize(s);
210  }
211 
212  uint16 operator_names_count = data[offset++];
213  for (i = 0; i < operator_names_count; ++i) {
214 
215  std::string s;
216  r_code::Read(data + offset, s);
217  operator_names_.push_back(s);
218  offset += r_code::GetSize(s);
219  }
220 
221  uint16 function_names_count = data[offset++];
222  for (i = 0; i < function_names_count; ++i) {
223 
224  std::string s;
225  r_code::Read(data + offset, s);
226  function_names_.push_back(s);
227  offset += r_code::GetSize(s);
228  }
229 }
230 
231 uint32 Metadata::get_size() {
232 
233  return get_class_array_size() +
234  get_classes_size() +
235  get_sys_classes_size() +
236  get_class_names_size() +
237  get_operator_names_size() +
238  get_function_names_size();
239 }
240 
241 // RAM layout:
242 // - class array
243 // - number of elements
244 // - list of classes:
245 // - atom
246 // - string (str_opcode)
247 // - return type
248 // - usage
249 // - things to read:
250 // - number of elements
251 // - list of structure members:
252 // - ID of a Compiler::*_Read function
253 // - return type
254 // - string (_class)
255 // - iteration
256 // - classes:
257 // - number of elements
258 // - list of pairs:
259 // - string
260 // - index of a class in the class array
261 // - sys_classes:
262 // - number of elements
263 // - list of pairs:
264 // - string
265 // - index of a class in the class array
266 // - class names:
267 // - number of elements
268 // - list of strings
269 // - operator names:
270 // - number of elements
271 // - list of strings
272 // - function names:
273 // - number of elements
274 // - list of strings
275 //
276 // String layout:
277 // - size in word32
278 // - list of words: contain the charaacters; the last one is \0; some of the least significant bytes of the last word my be empty
279 
280 uint32 Metadata::get_class_array_size() {
281 
282  uint32 size = 1; // size of the array
283  for (uint32 i = 0; i < classes_by_opcodes_.size(); ++i)
284  size += classes_by_opcodes_[i].get_size();
285  return size;
286 }
287 
288 uint32 Metadata::get_classes_size() {
289 
290  uint32 size = 1; // size of the hash table
291  unordered_map<std::string, Class>::iterator it = classes_.begin();
292  for (; it != classes_.end(); ++it)
293  size += r_code::GetSize(it->first) + 1; // +1: index to the class in the class array
294  return size;
295 }
296 
297 uint32 Metadata::get_sys_classes_size() {
298 
299  uint32 size = 1; // size of the hash table
300  unordered_map<std::string, Class>::iterator it = sys_classes_.begin();
301  for (; it != sys_classes_.end(); ++it)
302  size += r_code::GetSize(it->first) + 1; // +1: index to the class in the class array
303  return size;
304 }
305 
306 uint32 Metadata::get_class_names_size() {
307 
308  uint32 size = 1; // size of the vector
309  for (uint32 i = 0; i < class_names_.size(); ++i)
310  size += r_code::GetSize(class_names_[i]);
311  return size;
312 }
313 
314 uint32 Metadata::get_operator_names_size() {
315 
316  uint32 size = 1; // size of the vector
317  for (uint32 i = 0; i < operator_names_.size(); ++i)
318  size += r_code::GetSize(operator_names_[i]);
319  return size;
320 }
321 
322 uint32 Metadata::get_function_names_size() {
323 
324  uint32 size = 1; // size of the vector
325  for (uint32 i = 0; i < function_names_.size(); ++i)
326  size += r_code::GetSize(function_names_[i]);
327  return size;
328 }
329 
331 
332 void ObjectMap::shift(uint16 offset) {
333 
334  for (uint32 i = 0; i < objects_.size(); ++i)
335  objects_[i] += offset;
336 }
337 
338 void ObjectMap::write(word32 *data) {
339 
340  for (uint32 i = 0; i < objects_.size(); ++i)
341  data[i] = objects_[i];
342 }
343 
344 void ObjectMap::read(word32 *data, uint32 size) {
345 
346  for (uint16 i = 0; i < size; ++i)
347  objects_.push_back(data[i]);
348 }
349 
350 uint32 ObjectMap::get_size() const {
351 
352  return objects_.size();
353 }
354 
356 
357 CodeSegment::~CodeSegment() {
358 
359  for (uint32 i = 0; i < objects_.size(); ++i)
360  delete objects_[i];
361 }
362 
363 void CodeSegment::write(word32 *data) {
364 
365  uint32 offset = 0;
366  for (uint32 i = 0; i < objects_.size(); ++i) {
367 
368  objects_[i]->write(data + offset);
369  offset += objects_[i]->get_size();
370  }
371 }
372 
373 void CodeSegment::read(word32 *data, uint16 object_count) {
374 
375  uint16 offset = 0;
376  for (uint16 i = 0; i < object_count; ++i) {
377 
378  SysObject *o = new SysObject();
379  o->read(data + offset);
380  objects_.push_back(o);
381  offset += o->get_size();
382  }
383 }
384 
385 uint32 CodeSegment::get_size() {
386 
387  uint32 size = 0;
388  for (uint32 i = 0; i < objects_.size(); ++i)
389  size += objects_[i]->get_size();
390  return size;
391 }
392 
394 
395 // Format:
396 // number of entries
397 // list of entries (one per user-defined symbol)
398 // oid
399 // symbol length
400 // symbol characters
401 
402 ObjectNames::~ObjectNames() {
403 }
404 
405 void ObjectNames::write(word32 *data) {
406 
407  data[0] = symbols_.size();
408 
409  uint32 index = 1;
410 
411  unordered_map<uint32, std::string>::const_iterator n;
412  for (n = symbols_.begin(); n != symbols_.end(); ++n) {
413 
414  data[index] = n->first;
415  uint32 symbol_length = n->second.length() + 1; // add a trailing null character (for reading).
416  uint32 _symbol_length = symbol_length / 4;
417  uint32 __symbol_length = symbol_length % 4;
418  if (__symbol_length)
419  ++_symbol_length;
420  data[index + 1] = _symbol_length;
421  memcpy(data + index + 2, n->second.c_str(), symbol_length);
422  index += _symbol_length + 2;
423  }
424 }
425 
426 void ObjectNames::read(word32 *data) {
427 
428  uint32 symbol_count = data[0];
429  uint32 index = 1;
430  for (uint32 i = 0; i < symbol_count; ++i) {
431 
432  uint32 oid = data[index];
433  uint32 symbol_length = data[index + 1]; // number of words needed to store all the characters.
434  std::string symbol((char *)(data + index + 2));
435  symbols_[oid] = symbol;
436 
437  index += symbol_length + 2;
438  }
439 }
440 
441 uint32 ObjectNames::get_size() {
442 
443  uint32 size = 1; // size of symbols_.
444 
445  unordered_map<uint32, std::string>::const_iterator n;
446  for (n = symbols_.begin(); n != symbols_.end(); ++n) {
447 
448  size += 2; // oid and symbol's length.
449  uint32 symbol_length = n->second.length() + 1;
450  uint32 _symbol_length = symbol_length / 4;
451  uint32 __symbol_length = symbol_length % 4;
452  if (__symbol_length)
453  ++_symbol_length;
454  size += _symbol_length; // characters packed in 32 bits words.
455  }
456 
457  return size;
458 }
459 
460 uint32 ObjectNames::findSymbol(const std::string& name) {
461  for (auto entry = symbols_.begin(); entry != symbols_.end(); ++entry) {
462  if (entry->second == name)
463  return entry->first;
464  }
465 
466  return UNDEFINED_OID;
467 }
468 
470 
471 Image::Image() : map_offset_(0), timestamp_(seconds(0)) {
472 }
473 
474 Image::~Image() {
475 }
476 
477 void Image::add_sys_object(SysObject *object, std::string name) {
478 
479  add_sys_object(object);
480  if (!name.empty())
481  object_names_.symbols_[object->oid_] = name;
482 }
483 
484 void Image::add_sys_object(SysObject *object) {
485 
486  code_segment_.objects_.push_back(object);
487  object_map_.objects_.push_back(map_offset_);
488  map_offset_ += object->get_size();
489 }
490 
491 void Image::add_objects(r_code::list<P<r_code::Code> > &objects, bool include_invalidated) {
492 
493  r_code::list<P<r_code::Code> >::const_iterator o;
494  for (o = objects.begin(); o != objects.end(); ++o) {
495 
496  if (include_invalidated || !(*o)->is_invalidated())
497  add_object(*o, include_invalidated);
498  }
499 
500  build_references();
501 }
502 
503 void Image::add_objects(r_code::list<P<r_code::Code> > &objects, vector<SysObject *> &imported_objects) {
504 
505  r_code::list<P<r_code::Code> >::const_iterator o;
506  for (o = objects.begin(); o != objects.end(); ++o)
507  add_object(*o, imported_objects);
508 
509  build_references();
510 }
511 
512 inline uint32 Image::get_reference_count(const Code *object) const {
513 
514  switch (object->code(0).getDescriptor()) { // ignore the last reference as it is the unpacked version of the object.
515  case Atom::MODEL:
516  return object->references_size() - MDL_HIDDEN_REFS;
517  case Atom::COMPOSITE_STATE:
518  return object->references_size() - CST_HIDDEN_REFS;
519  default:
520  return object->references_size();
521  }
522 }
523 
524 void Image::add_object(Code *object, bool include_invalidated) {
525 
526  unordered_map<Code *, uint16>::iterator it = ptrs_to_indices_.find(object);
527  if (it != ptrs_to_indices_.end()) // object already there.
528  return;
529 
530  // Add the references first so that they are defined when referenced.
531  uint16 reference_count = get_reference_count(object);
532  for (uint16 i = 0; i < reference_count; ++i) { // follow reference pointers and recurse.
533 
534  Code *reference = object->get_reference(i);
535  if (include_invalidated || reference->get_oid() == UNDEFINED_OID ||
536  reference->is_invalidated()) // the referenced object is not in the image and will not be added otherwise.
537  add_object(reference, include_invalidated);
538  }
539 
540  uint16 object_index;
541  ptrs_to_indices_[object] = object_index = code_segment_.objects_.size();
542  SysObject *sys_object = new SysObject(object);
543  add_sys_object(sys_object);
544 
545  // Temporarily store the memory address of object in sys_object->references_. This will be
546  // recovered in build_references.
547 #if defined ARCH_32
548  uint32 _object = (uint32)object;
549  sys_object->references_[0] = (_object & 0x0000FFFF);
550  sys_object->references_[1] = (_object >> 16);
551 #elif defined ARCH_64
552  uint64 _object = (uint64)object;
553  sys_object->references_[0] = _object & 0x0000FFFF;
554  sys_object->references_[1] = (_object >> 16) & 0x0000FFFF;
555  sys_object->references_[2] = (_object >> 32) & 0x0000FFFF;
556  sys_object->references_[3] = (_object >> 48) & 0x0000FFFF;
557 #endif
558 }
559 
560 SysObject *Image::add_object(Code *object, vector<SysObject *> &imported_objects) {
561 
562  unordered_map<Code *, uint16>::iterator it = ptrs_to_indices_.find(object);
563  if (it != ptrs_to_indices_.end()) // object already there.
564  return code_segment_.objects_[it->second];
565 
566  uint16 object_index;
567  ptrs_to_indices_[object] = object_index = code_segment_.objects_.size();
568  SysObject *sys_object = new SysObject(object);
569  add_sys_object(sys_object);
570 
571  uint16 reference_count = get_reference_count(object);
572  for (uint16 i = 0; i < reference_count; ++i) { // follow the object's reference pointers and recurse.
573 
574  Code *reference = object->get_reference(i);
575  if (reference->get_oid() == UNDEFINED_OID) // the referenced object is not in the image and will not be added otherwise.
576  add_object(reference, imported_objects);
577  else { // add the referenced object if not present in the list.
578 
579  unordered_map<Code *, uint16>::iterator it = ptrs_to_indices_.find(reference);
580  if (it == ptrs_to_indices_.end()) {
581 
582  SysObject *sys_ref = add_object(reference, imported_objects);
583  imported_objects.push_back(sys_ref);
584  }
585  }
586  }
587 
588  object->acq_views();
589  unordered_set<_View *, _View::Hash, _View::Equal>::const_iterator v;
590  for (v = object->views_.begin(); v != object->views_.end(); ++v) { // follow the view's reference pointers and recurse.
591 
592  for (uint8 j = 0; j < 2; ++j) { // 2 refs maximum per view; may be NULL.
593 
594  Code *reference = (*v)->references_[j];
595  if (reference) {
596 
597  unordered_map<r_code::Code *, uint16>::const_iterator index = ptrs_to_indices_.find(reference);
598  if (index == ptrs_to_indices_.end()) {
599 
600  SysObject *sys_ref = add_object(reference, imported_objects);
601  imported_objects.push_back(sys_ref);
602  }
603  }
604  }
605  }
606  object->rel_views();
607 
608  // Temporarily store the memory address of object in sys_object->references_. This will be
609  // recovered in build_references.
610 #if defined ARCH_32
611  uint32 _object = (uint32)object;
612  sys_object->references_[0] = (_object & 0x0000FFFF);
613  sys_object->references_[1] = (_object >> 16);
614 #elif defined ARCH_64
615  uint64 _object = (uint64)object;
616  sys_object->references_[0] = _object & 0x0000FFFF;
617  sys_object->references_[1] = (_object >> 16) & 0x0000FFFF;
618  sys_object->references_[2] = (_object >> 32) & 0x0000FFFF;
619  sys_object->references_[3] = (_object >> 48) & 0x0000FFFF;
620 #endif
621 
622  return sys_object;
623 }
624 
625 void Image::build_references() {
626 
627  Code *object;
628  SysObject *sys_object;
629  for (uint32 i = 0; i < code_segment_.objects_.size(); ++i) {
630 
631  sys_object = code_segment_.objects_[i];
632  // The memory address of the original object was stored in sys_object->references_, so recover it.
633 #if defined ARCH_32
634  uint32 _object = sys_object->references_[0];
635  _object |= (sys_object->references_[1] << 16);
636  object = (Code *)_object;
637 #elif defined ARCH_64
638  uint64 _object = sys_object->references_[0];
639  _object |= ((uint64)(sys_object->references_[1]) << 16);
640  _object |= ((uint64)(sys_object->references_[2]) << 32);
641  _object |= ((uint64)(sys_object->references_[3]) << 48);
642  object = (Code *)_object;
643 #endif
644  sys_object->references_.clear();
645  build_references(sys_object, object);
646  }
647 }
648 
649 void Image::build_references(SysObject *sys_object, Code *object) {
650 
651  // Translate pointers into indices: valuate the sys_object's references to object, incl. sys_object's view references.
652  uint16 i;
653  uint16 referenced_object_index;
654  uint16 reference_count = get_reference_count(object);
655  for (i = 0; i < reference_count; ++i) {
656 
657  unordered_map<r_code::Code *, uint16>::const_iterator index = ptrs_to_indices_.find(object->get_reference(i));
658  if (index != ptrs_to_indices_.end()) {
659 
660  referenced_object_index = index->second;
661  sys_object->references_.push_back(referenced_object_index);
662  }
663  }
664 
665  object->acq_views();
666  unordered_set<_View *, _View::Hash, _View::Equal>::const_iterator v;
667  for (i = 0, v = object->views_.begin(); v != object->views_.end(); ++i, ++v) {
668 
669  for (uint8 j = 0; j < 2; ++j) { // 2 refs maximum per view; may be NULL.
670 
671  Code *reference = (*v)->references_[j];
672  if (reference) {
673 
674  unordered_map<r_code::Code *, uint16>::const_iterator index = ptrs_to_indices_.find(reference);
675  if (index != ptrs_to_indices_.end()) {
676 
677  referenced_object_index = index->second;
678  sys_object->views_[i]->references_[j] = referenced_object_index;
679  }
680  }
681  }
682  }
683  object->rel_views();
684 }
685 
686 void Image::get_objects(Mem *mem, resized_vector<Code *> &ram_objects) {
687 
688  for (uint16 i = 0; i < code_segment_.objects_.size(); ++i)
689  ram_objects[i] = mem->build_object(code_segment_.objects_[i]);
690  unpack_objects(ram_objects);
691 }
692 
693 void Image::unpack_objects(resized_vector<Code *> &ram_objects) {
694 
695  // For each object, translate its reference indices into pointers; build its views; for each view translate its reference indices into pointers.
696  for (uint16 i = 0; i < code_segment_.objects_.size(); ++i) {
697 
698  SysObject *sys_object = code_segment_.objects_[i];
699  Code *ram_object = ram_objects[i];
700 
701  for (uint16 j = 0; j < sys_object->views_.size(); ++j) {
702 
703  SysView *sys_v = sys_object->views_[j];
704  _View *v = ram_object->build_view(sys_v);
705  for (uint16 k = 0; k < sys_v->references_.size(); ++k)
706  v->references_[k] = ram_objects[sys_v->references_[k]];
707 
708  ram_object->views_.insert(v);
709  }
710 
711  for (uint16 j = 0; j < sys_object->references_.size(); ++j)
712  ram_object->set_reference(j, ram_objects[sys_object->references_[j]]);
713  }
714 }
715 }
r_code::resized_vector
Definition: resized_vector.h:137
r_code::_View
Definition: r_code/object.h:164
r_comp::Image::add_objects
void add_objects(r_code::list< P< r_code::Code > > &objects, bool include_invalidated=false)
Definition: segments.cpp:491
r_code::SysView
Definition: r_code/object.h:119
r_code::SysObject
Definition: r_code/object.h:136
core::P< r_code::Code >
r_code::Code
Definition: r_code/object.h:224
r_code::Mem
Definition: r_code/object.h:420
r_code::list
Definition: list.h:99