Game Development Reference
In-Depth Information
for(Object* object : objects)
{
int size = object->getMeta()->getSize();
diskOffsets[object] = buffer
start;
memcpy(buffer, object, size);
buffer += size;
}
Listing 7.1. Creating memory image.
7.4.1 Pointers
For reasonably complex data, pointers are required to connect different objects. In
memory, these pointers are direct memory references but on disk this connection is
much more dicult.
To solve this problem, after serialization, all pointers need to become offsets.
This is most easily done in multiple passes: The first pass is to copy all of the target
objects into a separate memory buffer. From here, all of the objects are contiguous
in memory and are representative of what will be serialized to disk. Next, a second
pass must be made through all of the objects to find all of the internal pointers.
These pointers must first be transformed to point at the newly copied data and
then be converted into an offset from the beginning of the memory block.
// Pointer fixup
for(Object* object : objects)
{
for(int offset : object->getMeta()->getOffsets())
{
void** ptr = diskOffsets[object] + offset;
*ptr = diskOffsets[*ptr];
}
}
7.4.2 Virtual Tables
The C++ standard does not specify how virtual functions must be defined, but
most implementations rely on a pointer to the virtual table. The location of this
pointer is implementation-specific, but because this format only has to work for a
specific build of a game, that constraint is not a significant problem. There are a
number of different ways to find the virtual table inside an object, but the easiest
method is to simply allocate a number of different objects and inspect the memory
Search WWH ::




Custom Search