Skip to content

Commit 9bdbaa1

Browse files
committed
Update serialize/deserialize code to set up for contexts table and data segment.
1 parent 40269bf commit 9bdbaa1

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/6model/serialization.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
#define CURRENT_VERSION 1
1717

1818
/* Various sizes (in bytes). */
19-
#define HEADER_SIZE 4 * 11
19+
#define HEADER_SIZE 4 * 14
2020
#define DEP_TABLE_ENTRY_SIZE 8
2121
#define STABLES_TABLE_ENTRY_SIZE 8
2222
#define OBJECTS_TABLE_ENTRY_SIZE 16
2323
#define CLOSURES_TABLE_ENTRY_SIZE 12
24+
#define CONTEXTS_TABLE_ENTRY_SIZE 12
2425

2526
/* Some guesses. */
2627
#define DEFAULT_STABLE_DATA_SIZE 4096
2728
#define STABLES_TABLE_ENTRIES_GUESS 16
2829
#define OBJECT_SIZE_GUESS 8
2930
#define CLOSURES_TABLE_ENTRIES_GUESS 16
31+
#define CONTEXTS_TABLE_ENTRIES_GUESS 4
32+
#define DEFAULT_CONTEXTS_DATA_SIZE 1024
3033

3134
/* Possible reference types we can serialize. */
3235
#define REFVAR_NULL 1
@@ -452,6 +455,8 @@ static STRING * concatenate_outputs(PARROT_INTERP, SerializationWriter *writer)
452455
output_size += writer->root.num_objects * OBJECTS_TABLE_ENTRY_SIZE;
453456
output_size += writer->objects_data_offset;
454457
output_size += writer->root.num_closures * CLOSURES_TABLE_ENTRY_SIZE;
458+
output_size += writer->root.num_contexts * CONTEXTS_TABLE_ENTRY_SIZE;
459+
output_size += writer->contexts_data_offset;
455460

456461
/* Allocate a buffer that size. */
457462
output = mem_sys_allocate(output_size);
@@ -500,6 +505,19 @@ static STRING * concatenate_outputs(PARROT_INTERP, SerializationWriter *writer)
500505
writer->root.num_closures * CLOSURES_TABLE_ENTRY_SIZE);
501506
offset += writer->root.num_closures * CLOSURES_TABLE_ENTRY_SIZE;
502507

508+
/* Put contexts table in place, and set location/rows in header. */
509+
write_int32(output, 44, offset);
510+
write_int32(output, 48, writer->root.num_contexts);
511+
memcpy(output + offset, writer->root.contexts_table,
512+
writer->root.num_contexts * CONTEXTS_TABLE_ENTRY_SIZE);
513+
offset += writer->root.num_contexts * CONTEXTS_TABLE_ENTRY_SIZE;
514+
515+
/* Put contexts data in place. */
516+
write_int32(output, 52, offset);
517+
memcpy(output + offset, writer->root.contexts_data,
518+
writer->contexts_data_offset);
519+
offset += writer->contexts_data_offset;
520+
503521
/* Sanity check. */
504522
if (offset != output_size)
505523
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
@@ -653,6 +671,7 @@ STRING * Serialization_serialize(PARROT_INTERP, PMC *sc, PMC *empty_string_heap)
653671
writer->stables_list = stables;
654672
writer->objects_list = objects;
655673
writer->codes_list = codes;
674+
writer->contexts_list = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
656675
writer->root.string_heap = empty_string_heap;
657676
writer->root.dependent_scs = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
658677

@@ -669,6 +688,10 @@ STRING * Serialization_serialize(PARROT_INTERP, PMC *sc, PMC *empty_string_heap)
669688
writer->root.objects_data = mem_sys_allocate(writer->objects_data_alloc);
670689
writer->closures_table_alloc = CLOSURES_TABLE_ENTRY_SIZE * CLOSURES_TABLE_ENTRIES_GUESS;
671690
writer->root.closures_table = mem_sys_allocate(writer->closures_table_alloc);
691+
writer->contexts_table_alloc = CONTEXTS_TABLE_ENTRY_SIZE * CONTEXTS_TABLE_ENTRIES_GUESS;
692+
writer->root.contexts_table = mem_sys_allocate(writer->contexts_table_alloc);
693+
writer->contexts_data_alloc = DEFAULT_CONTEXTS_DATA_SIZE;
694+
writer->root.contexts_data = mem_sys_allocate(writer->contexts_data_alloc);
672695

673696
/* Populate write functions table. */
674697
writer->write_int = write_int_func;
@@ -1032,7 +1055,7 @@ static void check_and_disect_input(PARROT_INTERP, SerializationReader *reader, S
10321055
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
10331056
"Corruption detected (objects data starts after end of data)");
10341057

1035-
/* Get size and location of STables table. */
1058+
/* Get size and location of closures table. */
10361059
reader->root.closures_table = data + read_int32(data, 36);
10371060
reader->root.num_closures = read_int32(data, 40);
10381061
if (reader->root.closures_table < prov_pos)
@@ -1043,9 +1066,31 @@ static void check_and_disect_input(PARROT_INTERP, SerializationReader *reader, S
10431066
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
10441067
"Corruption detected (Closures table overruns end of data)");
10451068

1069+
/* Get size and location of contexts table. */
1070+
reader->root.contexts_table = data + read_int32(data, 44);
1071+
reader->root.num_contexts = read_int32(data, 48);
1072+
if (reader->root.contexts_table < prov_pos)
1073+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
1074+
"Corruption detected (contexts table starts before closures table ends)");
1075+
prov_pos = reader->root.contexts_table + reader->root.num_contexts * CONTEXTS_TABLE_ENTRY_SIZE;
1076+
if (prov_pos > data_end)
1077+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
1078+
"Corruption detected (contexts table overruns end of data)");
1079+
1080+
/* Get location of contexts data. */
1081+
reader->root.contexts_data = data + read_int32(data, 52);
1082+
if (reader->root.contexts_data < prov_pos)
1083+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
1084+
"Corruption detected (contexts data starts before contexts table ends)");
1085+
prov_pos = reader->root.contexts_data;
1086+
if (prov_pos > data_end)
1087+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
1088+
"Corruption detected (contexts data starts after end of data)");
1089+
10461090
/* Set reading limits for data chunks. */
10471091
reader->stables_data_end = reader->root.objects_table;
10481092
reader->objects_data_end = reader->root.closures_table;
1093+
reader->contexts_data_end = data_end;
10491094
}
10501095

10511096
/* Goes through the dependencies table and resolves the dependencies that it
@@ -1185,6 +1230,7 @@ void Serialization_deserialize(PARROT_INTERP, PMC *sc, PMC *string_heap, PMC *st
11851230
GETATTR_SerializationContext_root_objects(interp, sc, objects);
11861231
reader->stables_list = stables;
11871232
reader->objects_list = objects;
1233+
reader->contexts_list = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
11881234
reader->root.sc = sc;
11891235
reader->root.string_heap = string_heap;
11901236
reader->root.dependent_scs = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);

src/6model/serialization.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ typedef struct {
3434
Parrot_Int4 num_closures;
3535
char *closures_table;
3636

37+
/* The number of contexts (e.g. lexpads), as well as pointers
38+
* to the contexts table and data chunk. */
39+
Parrot_Int4 num_contexts;
40+
char *contexts_table;
41+
char *contexts_data;
42+
3743
/* Array of STRINGs. */
3844
PMC *string_heap;
3945
} SerializationRoot;
@@ -45,19 +51,23 @@ typedef struct SerializationReader {
4551
/* Serialization root data. */
4652
SerializationRoot root;
4753

48-
/* The stables, objects and code refs lists we're deserializing things into. */
54+
/* The stables, objects code refs and contexts lists we're deserializing
55+
* things into. */
4956
PMC *stables_list;
5057
PMC *objects_list;
5158
PMC *codes_list;
59+
PMC *contexts_list;
5260

5361
/* Current offsets for the data chunks (also correspond to the amount of
5462
* data written in to them). */
5563
Parrot_Int4 stables_data_offset;
5664
Parrot_Int4 objects_data_offset;
65+
Parrot_Int4 contexts_data_offset;
5766

58-
/* Limits up to where we can read stables and objects data. */
67+
/* Limits up to where we can read stables, objects and contexts data. */
5968
char *stables_data_end;
6069
char *objects_data_end;
70+
char *contexts_data_end;
6171

6272
/* Where to find details related to the current buffer we're reading from:
6373
* the buffer pointer itself, the current offset and the amount that is
@@ -80,10 +90,12 @@ typedef struct SerializationWriter {
8090
/* Serialization root data. */
8191
SerializationRoot root;
8292

83-
/* The stables, objects and code refs lists we're working through/adding to. */
93+
/* The stables, objects, code refs and contexts lists we're working
94+
* through/adding to. */
8495
PMC *stables_list;
8596
PMC *objects_list;
8697
PMC *codes_list;
98+
PMC *contexts_list;
8799

88100
/* Current position in the stables list and objects list. */
89101
INTVAL stables_list_pos;
@@ -100,11 +112,14 @@ typedef struct SerializationWriter {
100112
Parrot_Int4 objects_table_alloc;
101113
Parrot_Int4 objects_data_alloc;
102114
Parrot_Int4 closures_table_alloc;
115+
Parrot_Int4 contexts_table_alloc;
116+
Parrot_Int4 contexts_data_alloc;
103117

104118
/* Current offsets for the data chunks (also correspond to the amount of
105119
* data written in to them). */
106120
Parrot_Int4 stables_data_offset;
107121
Parrot_Int4 objects_data_offset;
122+
Parrot_Int4 contexts_data_offset;
108123

109124
/* Where to find details related to the current buffer we're writing in
110125
* to: the buffer pointer itself, the current offset and the amount that

0 commit comments

Comments
 (0)