Body Copy
List of: Discussion Topic
Subjects: SAT Save and Restore
Contents: Kernel

The process of copying a body is identical to that of save and restore, except that it omits the step of saving an intermediate representation of the body to secondary storage. The algorithms make use of the class ENTITY_LIST, which is a variable length associative array implementation used by many of the ACIS algorithms. ENTITY_LIST contains pointers to ENTITYs.

An ENTITY_LIST has four methods that pertain to copying a body:

add Adds an ENTITY to the list if not already there and always return the index.

lookup Searches for an ENTITY in the list and returns the index.

init Prepares to walk the list.

next Returns the next undeleted item.

The following is the algorithm, with pseudo code, for copying a body.


1. Set the version information so that the pointers are arranged correctly:


restore_major_version = get_major_version ();

restore_minor_version = get_minor_version ();

restore_version_number =


PORTMANTEAU(restore_major_version,



restore_minor_version);


2. Create an empty ENTITY_LIST to hold a pointer to each data structure in the body:


ENTITY_LIST list;

list.add(entities_to_be_copied);


Seed the list with all entities to be copied.


3. For each entity in the list, add each of its ENTITY pointers to the list:


list.init();

for(int num_ents; ++num_ents) {


ENTITY* this_ent = list.next();


if(this_ent == NULL)


break;


this_ent->copy_scan(list);

}


4. Continue to scan until all pointers are added to the list.


For example, the copy_scan method for BODY adds the LUMP pointer to the list:


list.add(lump());


5. Allocate an array of ENTITY pointers to hold copies of each data structure:


ENTITY** array = new ENTITY* [num_ents];


6. Copy each entity, replacing every pointer in the copy with an index:


for(int i = 0; i < num_ents; i++)


array[i] = list[i]->copy_data(list);


7. Copy the LUMP pointer to a new body:


new_body->lump_ptr = (LUMP*)list.lookup(lump());


The copy_data method for BODY, for example, looks up the LUMP pointer in the list and inserts the index in the LUMP pointer of the copy. The LUMP index is cast to a LUMP* to satisfy the C++ compiler.


8. For each entity in the array, replace the pointer indices with pointers corresponding to the new entities:


for(int i = 0; i < num_ents; i++)


array[i]->fix_pointers(array);


9. The fix_pointers method for BODY finds the LUMP pointer in the list from the index:


set_lump((LUMP*)read_array(array, (int)lump()));


The copied body in array[0] is returned and the array is returned to free storage.
PDF/KERN/10SAVE.PDF
HTM/DATA/KERN/KERN/10SAVE/0002.HTM