[serialize] Flesh out packing

This commit is contained in:
Behdad Esfahbod 2019-03-30 18:14:30 -07:00
parent 357c7c611c
commit 9a19b885f9
1 changed files with 34 additions and 24 deletions

View File

@ -42,7 +42,12 @@ struct hb_serialize_context_t
{ {
typedef unsigned objidx_t; typedef unsigned objidx_t;
struct object_t struct range_t
{
char *head, *tail;
};
struct object_t : range_t
{ {
void fini () { links.fini (); } void fini () { links.fini (); }
@ -53,16 +58,10 @@ struct hb_serialize_context_t
objidx_t objidx; objidx_t objidx;
}; };
char *head;
unsigned length;
hb_vector_t<link_t> links; hb_vector_t<link_t> links;
}; };
struct snapshot_t range_t snapshot () { range_t s = {head, tail} ; return s; }
{
char *head, *tail;
};
snapshot_t snapshot () { snapshot_t s = {head, tail} ; return s; }
hb_serialize_context_t (void *start_, unsigned int size) hb_serialize_context_t (void *start_, unsigned int size)
@ -82,8 +81,8 @@ struct hb_serialize_context_t
this->tail = this->end; this->tail = this->end;
this->debug_depth = 0; this->debug_depth = 0;
this->current.resize (0); this->current.reset ();
this->packed.resize (0); this->packed.reset ();
this->packed.push ()->head = this->end; this->packed.push ()->head = this->end;
this->packed_map.reset (); this->packed_map.reset ();
} }
@ -119,6 +118,9 @@ struct hb_serialize_context_t
this->start, this->end, this->start, this->end,
(unsigned) (this->head - this->start), (unsigned) (this->head - this->start),
this->successful ? "successful" : "UNSUCCESSFUL"); this->successful ? "successful" : "UNSUCCESSFUL");
/* TODO Propagate errors. */
assert (current.length == 1); assert (current.length == 1);
return pop_pack (); return pop_pack ();
} }
@ -126,7 +128,10 @@ struct hb_serialize_context_t
template <typename Type> template <typename Type>
Type *push () Type *push ()
{ {
current.push (snapshot ()); object_t obj;
obj.head = head;
obj.tail = tail;
current.push (obj);
return start_embed<Type> (); return start_embed<Type> ();
} }
void pop_discard () void pop_discard ()
@ -135,23 +140,27 @@ struct hb_serialize_context_t
} }
objidx_t pop_pack () objidx_t pop_pack ()
{ {
snapshot_t snap = current.pop (); object_t obj = current.pop ();
char *s = snap.head; unsigned len = head - obj.head;
char *e = head;
unsigned l = e - s;
tail -= l; tail -= len;
memmove (tail, s, l); memmove (tail, obj.head, len);
head = obj.head;
/* TODO... */ obj.head = tail;
packed.push (); obj.tail = tail + len;
head = snap.head; packed.push (obj);
return 0;
/* TODO Handle error. */
if (unlikely (packed.in_error ()))
return 0;
return packed.length - 1;
} }
void revert (snapshot_t snap) void revert (range_t snap)
{ {
assert (snap.head <= head); assert (snap.head <= head);
assert (tail <= snap.tail); assert (tail <= snap.tail);
@ -274,8 +283,9 @@ struct hb_serialize_context_t
private: private:
/* Stack of currently under construction object locations. */ /* Stack of currently under construction objects. */
hb_vector_t<snapshot_t> current; /* Note. We store the "end - tail" distance in the length member of these. */
hb_vector_t<object_t> current;
/* Stack of packed objects. Object 0 is always nil object. */ /* Stack of packed objects. Object 0 is always nil object. */
hb_vector_t<object_t> packed; hb_vector_t<object_t> packed;