Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Store REPR data offset in STables data also.
This makes it much easier for anything that wants to try and work out
sizing/layout before deserializing objects.
  • Loading branch information
jnthn committed Mar 19, 2013
1 parent efb432b commit 5400ed8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/serialization_format.markdown
Expand Up @@ -80,6 +80,13 @@ contains the following items.
| the data for this STable has been serialized |
| 32-bit integer |
+---------------------------------------------------------+
| Offset from the start of the STable data chunk where |
| the REPR data for this STable has been serialized (you |
| can get there by reading everything from the previous |
| offset, but it may not be efficient if you want to get |
| an idea of the object size first) |
| 32-bit integer |
+---------------------------------------------------------+

## STables Data
The STable is serialized just by a sequence of primitives, in the
Expand Down
15 changes: 11 additions & 4 deletions src/6model/serialization.c
Expand Up @@ -19,13 +19,13 @@

/* Version of the serialization format that we are currently at and lowest
* version we support. */
#define CURRENT_VERSION 3
#define CURRENT_VERSION 4
#define MIN_VERSION 1

/* Various sizes (in bytes). */
#define HEADER_SIZE 4 * 16
#define DEP_TABLE_ENTRY_SIZE 8
#define STABLES_TABLE_ENTRY_SIZE 8
#define STABLES_TABLE_ENTRY_SIZE (version == 4 ? 12 : 8)
#define OBJECTS_TABLE_ENTRY_SIZE 16
#define CLOSURES_TABLE_ENTRY_SIZE 24
#define CONTEXTS_TABLE_ENTRY_SIZE 16
Expand Down Expand Up @@ -596,6 +596,7 @@ static STRING * concatenate_outputs(PARROT_INTERP, SerializationWriter *writer)
char *output_b64 = NULL;
Parrot_Int4 output_size = 0;
Parrot_Int4 offset = 0;
Parrot_Int4 version = writer->root.version;

/* Calculate total size. */
output_size += HEADER_SIZE;
Expand Down Expand Up @@ -698,6 +699,7 @@ static STRING * concatenate_outputs(PARROT_INTERP, SerializationWriter *writer)
* its representation data also. */
static void serialize_stable(PARROT_INTERP, SerializationWriter *writer, PMC *st_pmc) {
STable *st = STABLE_STRUCT(st_pmc);
Parrot_Int4 version = writer->root.version;
INTVAL i;

/* Ensure there's space in the STables table; grow if not. */
Expand Down Expand Up @@ -754,6 +756,9 @@ static void serialize_stable(PARROT_INTERP, SerializationWriter *writer, PMC *st
write_ref_func(interp, writer, st->container_spec->fetch_method);
}

/* Store offset we save REPR data at. */
write_int32(writer->root.stables_table, offset + 8, writer->stables_data_offset);

/* If the REPR has a function to serialize representation data, call it. */
if (st->REPR->serialize_repr_data)
st->REPR->serialize_repr_data(interp, st, writer);
Expand Down Expand Up @@ -966,6 +971,7 @@ STRING * Serialization_serialize(PARROT_INTERP, PMC *sc, PMC *empty_string_heap)
PMC *codes = PMCNULL;
STRING *result = STRINGNULL;
Parrot_Int4 sc_elems = (Parrot_Int4)VTABLE_elements(interp, sc);
Parrot_Int4 version = CURRENT_VERSION;

/* Set up writer with some initial settings. */
SerializationWriter *writer = mem_allocate_zeroed_typed(SerializationWriter);
Expand Down Expand Up @@ -1316,6 +1322,7 @@ static STable * read_stable_ref_func(PARROT_INTERP, SerializationReader *reader)
* the reader data structure more fully. */
static void check_and_disect_input(PARROT_INTERP, SerializationReader *reader, STRING *data_str) {
/* Grab data from string. */
Parrot_Int4 version;
size_t data_len;
char *data_b64 = (char *)Parrot_str_to_cstring(interp, data_str);
char *data = (char *)base64_decode(data_b64, &data_len);
Expand All @@ -1333,8 +1340,7 @@ static void check_and_disect_input(PARROT_INTERP, SerializationReader *reader, S
if (data_len < 4)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Serialized data too short to read a version number (< 4 bytes)");
reader->root.version = read_int32(data, 0);
/*if (reader->root.version != CURRENT_VERSION)*/
reader->root.version = version = read_int32(data, 0);
if (reader->root.version < MIN_VERSION || reader->root.version > CURRENT_VERSION)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Unsupported serialization format version %d (current version is %d)",
Expand Down Expand Up @@ -1570,6 +1576,7 @@ static void attach_context_outer(PARROT_INTERP, SerializationReader *reader, INT
/* Deserializes a single STable, along with its REPR data. */
static void deserialize_stable(PARROT_INTERP, SerializationReader *reader, INTVAL i, PMC *st_pmc) {
STable *st = STABLE_STRUCT(st_pmc);
Parrot_Int4 version = reader->root.version;

/* Calculate location of STable's table row. */
char *st_table_row = reader->root.stables_table + i * STABLES_TABLE_ENTRY_SIZE;
Expand Down

0 comments on commit 5400ed8

Please sign in to comment.