From 9aebfb418222427a324d911b32abfc11256ca9e3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 18 Dec 2018 13:22:17 -0500 Subject: [PATCH] [serialize] Streamline error propagation --- src/hb-buffer.hh | 2 ++ src/hb-machinery.hh | 35 +++++++++++++++++++++++++--------- src/hb-map.hh | 2 ++ src/hb-ot-layout-common.hh | 4 ++-- src/hb-ot-layout-gsub-table.hh | 4 ++-- src/hb-set.hh | 2 ++ src/hb-subset.cc | 2 +- 7 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 0ac88c0fa..8a3170ca9 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -137,6 +137,8 @@ struct hb_buffer_t /* Methods */ + bool in_error () const { return !successful; } + void allocate_var (unsigned int start, unsigned int count) { #ifndef HB_NDEBUG diff --git a/src/hb-machinery.hh b/src/hb-machinery.hh index a3bd3be4a..c02032cf9 100644 --- a/src/hb-machinery.hh +++ b/src/hb-machinery.hh @@ -508,14 +508,31 @@ struct hb_serialize_context_t reset (); } + bool in_error () const { return !this->successful; } + void reset () { - this->ran_out_of_room = false; + this->successful = true; this->head = this->start; this->debug_depth = 0; } - bool err (bool e) { return this->ran_out_of_room = this->ran_out_of_room || e; } + bool propagate_error (bool e) + { return this->successful = this->successful && e; } + template bool propagate_error (const T &obj) + { return this->successful = this->successful && !obj.in_error (); } + template bool propagate_error (const T *obj) + { return this->successful = this->successful && !obj->in_error (); } + template bool propagate_error (T1 &o1, T2 &o2) + { return propagate_error (o1) && propagate_error (o2); } + template bool propagate_error (T1 *o1, T2 *o2) + { return propagate_error (o1) && propagate_error (o2); } + template + bool propagate_error (T1 &o1, T2 &o2, T3 &o3) + { return propagate_error (o1) && propagate_error (o2, o3); } + template + bool propagate_error (T1 *o1, T2 *o2, T3 *o3) + { return propagate_error (o1) && propagate_error (o2, o3); } /* To be called around main operation. */ template @@ -534,7 +551,7 @@ struct hb_serialize_context_t "end [%p..%p] serialized %d bytes; %s", this->start, this->end, (int) (this->head - this->start), - this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room"); + this->successful ? "successful" : "UNSUCCESSFUL"); } unsigned int length () const { return this->head - this->start; } @@ -556,8 +573,8 @@ struct hb_serialize_context_t template Type *allocate_size (unsigned int size) { - if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { - this->ran_out_of_room = true; + if (unlikely (!this->successful || this->end - this->head < ptrdiff_t (size))) { + this->successful = false; return nullptr; } memset (this->head, 0, size); @@ -602,7 +619,7 @@ struct hb_serialize_context_t template Type *copy () const { - assert (!this->ran_out_of_room); + assert (this->successful); unsigned int len = this->head - this->start; void *p = malloc (len); if (p) @@ -611,7 +628,7 @@ struct hb_serialize_context_t } hb_bytes_t copy_bytes () const { - assert (!this->ran_out_of_room); + assert (this->successful); unsigned int len = this->head - this->start; void *p = malloc (len); if (p) @@ -622,7 +639,7 @@ struct hb_serialize_context_t } hb_blob_t *copy_blob () const { - assert (!this->ran_out_of_room); + assert (this->successful); return hb_blob_create (this->start, this->head - this->start, HB_MEMORY_MODE_DUPLICATE, @@ -632,7 +649,7 @@ struct hb_serialize_context_t public: unsigned int debug_depth; char *start, *end, *head; - bool ran_out_of_room; + bool successful; }; diff --git a/src/hb-map.hh b/src/hb-map.hh index ec8de2a69..3b119daa8 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -90,6 +90,8 @@ struct hb_map_t fini_shallow (); } + bool in_error () const { return !successful; } + bool resize () { if (unlikely (!successful)) return false; diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index d60991880..ac739204b 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1254,7 +1254,7 @@ struct ClassDefFormat1 glyphs.push()->set (glyph_map[g]); klasses.push()->set (value); } - c->serializer->err (glyphs.in_error () || klasses.in_error ()); + c->serializer->propagate_error (glyphs, klasses); hb_supplier_t glyphs_supplier (glyphs); hb_supplier_t klasses_supplier (klasses); @@ -1413,7 +1413,7 @@ struct ClassDefFormat2 klasses.push ()->set (value); } } - c->serializer->err (glyphs.in_error () || klasses.in_error ()); + c->serializer->propagate_error (glyphs, klasses); hb_supplier_t glyphs_supplier (glyphs); hb_supplier_t klasses_supplier (klasses); diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index d6e9e9d4d..fb59921a2 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -120,7 +120,7 @@ struct SingleSubstFormat1 from.push ()->set (glyph_map[iter.get_glyph ()]); to.push ()->set (glyph_map[(iter.get_glyph () + delta) & 0xFFFF]); } - c->serializer->err (from.in_error () || to.in_error ()); + c->serializer->propagate_error (from, to); hb_supplier_t from_supplier (from); hb_supplier_t to_supplier (to); @@ -225,7 +225,7 @@ struct SingleSubstFormat2 from.push ()->set (glyph_map[iter.get_glyph ()]); to.push ()->set (glyph_map[substitute[iter.get_coverage ()]]); } - c->serializer->err (from.in_error () || to.in_error ()); + c->serializer->propagate_error (from, to); hb_supplier_t from_supplier (from); hb_supplier_t to_supplier (to); diff --git a/src/hb-set.hh b/src/hb-set.hh index eb325a612..19596880e 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -213,6 +213,8 @@ struct hb_set_t fini_shallow (); } + bool in_error () const { return !successful; } + bool resize (unsigned int count) { if (unlikely (!successful)) return false; diff --git a/src/hb-subset.cc b/src/hb-subset.cc index 9ce0a72e2..37e7cec2e 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc @@ -83,7 +83,7 @@ _subset2 (hb_subset_plan_t *plan) hb_serialize_context_t serializer ((void *) buf, buf_size); hb_subset_context_t c (plan, &serializer); result = table->subset (&c); - if (serializer.ran_out_of_room) + if (serializer.in_error ()) { buf_size += (buf_size >> 1) + 32; DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);