2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2012,2018 Google, Inc.
4 * Copyright © 2019 Facebook, Inc.
6 * This is part of HarfBuzz, a text shaping library.
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 * Red Hat Author(s): Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
28 * Facebook Author(s): Behdad Esfahbod
31 #ifndef HB_SERIALIZE_HH
32 #define HB_SERIALIZE_HH
39 #ifdef HB_EXPERIMENTAL_API
40 #include "hb-subset-repacker.h"
47 enum hb_serialize_error_t {
48 HB_SERIALIZE_ERROR_NONE = 0x00000000u,
49 HB_SERIALIZE_ERROR_OTHER = 0x00000001u,
50 HB_SERIALIZE_ERROR_OFFSET_OVERFLOW = 0x00000002u,
51 HB_SERIALIZE_ERROR_OUT_OF_ROOM = 0x00000004u,
52 HB_SERIALIZE_ERROR_INT_OVERFLOW = 0x00000008u,
53 HB_SERIALIZE_ERROR_ARRAY_OVERFLOW = 0x00000010u
55 HB_MARK_AS_FLAG_T (hb_serialize_error_t);
57 struct hb_serialize_context_t
59 typedef unsigned objidx_t;
62 Head, /* Relative to the current object head (default). */
63 Tail, /* Relative to the current object tail after packed. */
64 Absolute /* Absolute: from the start of the serialize buffer. */
73 virtual_links.fini ();
76 object_t () = default;
78 #ifdef HB_EXPERIMENTAL_API
79 object_t (const hb_object_t &o)
84 real_links.alloc (o.num_real_links, true);
85 for (unsigned i = 0 ; i < o.num_real_links; i++)
86 real_links.push (o.real_links[i]);
88 virtual_links.alloc (o.num_virtual_links, true);
89 for (unsigned i = 0; i < o.num_virtual_links; i++)
90 virtual_links.push (o.virtual_links[i]);
94 friend void swap (object_t& a, object_t& b)
96 hb_swap (a.head, b.head);
97 hb_swap (a.tail, b.tail);
98 hb_swap (a.next, b.next);
99 hb_swap (a.real_links, b.real_links);
100 hb_swap (a.virtual_links, b.virtual_links);
103 bool operator == (const object_t &o) const
105 // Virtual links aren't considered for equality since they don't affect the functionality
107 return (tail - head == o.tail - o.head)
108 && (real_links.length == o.real_links.length)
109 && 0 == hb_memcmp (head, o.head, tail - head)
110 && real_links.as_bytes () == o.real_links.as_bytes ();
112 uint32_t hash () const
114 // Virtual links aren't considered for equality since they don't affect the functionality
116 return hb_bytes_t (head, hb_min (128, tail - head)).hash () ^
117 real_links.as_bytes ().hash ();
123 unsigned is_signed: 1;
131 #ifdef HB_EXPERIMENTAL_API
132 link_t (const hb_link_t &o)
137 position = o.position;
143 HB_INTERNAL static int cmp (const void* a, const void* b)
145 int cmp = ((const link_t*)a)->position - ((const link_t*)b)->position;
148 return ((const link_t*)a)->objidx - ((const link_t*)b)->objidx;
154 hb_vector_t<link_t> real_links;
155 hb_vector_t<link_t> virtual_links;
158 auto all_links () const HB_AUTO_RETURN
159 (( hb_concat (this->real_links, this->virtual_links) ));
160 auto all_links_writer () HB_AUTO_RETURN
161 (( hb_concat (this->real_links.writer (), this->virtual_links.writer ()) ));
168 object_t *current; // Just for sanity check
169 unsigned num_real_links;
170 unsigned num_virtual_links;
171 hb_serialize_error_t errors;
174 snapshot_t snapshot ()
178 current ? current->real_links.length : 0,
179 current ? current->virtual_links.length : 0,
184 hb_serialize_context_t (void *start_, unsigned int size) :
185 start ((char *) start_),
189 ~hb_serialize_context_t () { fini (); }
193 for (object_t *_ : ++hb_iter (packed)) _->fini ();
195 this->packed_map.fini ();
200 current = current->next;
205 bool in_error () const { return bool (errors); }
207 bool successful () const { return !bool (errors); }
209 HB_NODISCARD bool ran_out_of_room () const { return errors & HB_SERIALIZE_ERROR_OUT_OF_ROOM; }
210 HB_NODISCARD bool offset_overflow () const { return errors & HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; }
211 HB_NODISCARD bool only_offset_overflow () const { return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; }
212 HB_NODISCARD bool only_overflow () const
214 return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW
215 || errors == HB_SERIALIZE_ERROR_INT_OVERFLOW
216 || errors == HB_SERIALIZE_ERROR_ARRAY_OVERFLOW;
219 void reset (void *start_, unsigned int size)
221 start = (char*) start_;
229 this->errors = HB_SERIALIZE_ERROR_NONE;
230 this->head = this->start;
231 this->tail = this->end;
232 this->zerocopy = nullptr;
233 this->debug_depth = 0;
236 this->packed.push (nullptr);
237 this->packed_map.init ();
240 bool check_success (bool success,
241 hb_serialize_error_t err_type = HB_SERIALIZE_ERROR_OTHER)
244 && (success || err (err_type));
247 template <typename T1, typename T2>
248 bool check_equal (T1 &&v1, T2 &&v2, hb_serialize_error_t err_type)
250 if ((long long) v1 != (long long) v2)
252 return err (err_type);
257 template <typename T1, typename T2>
258 bool check_assign (T1 &v1, T2 &&v2, hb_serialize_error_t err_type)
259 { return check_equal (v1 = v2, v2, err_type); }
261 template <typename T> bool propagate_error (T &&obj)
262 { return check_success (!hb_deref (obj).in_error ()); }
264 template <typename T1, typename... Ts> bool propagate_error (T1 &&o1, Ts&&... os)
265 { return propagate_error (std::forward<T1> (o1)) &&
266 propagate_error (std::forward<Ts> (os)...); }
268 /* To be called around main operation. */
269 template <typename Type=char>
270 __attribute__((returns_nonnull))
271 Type *start_serialize ()
273 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
274 "start [%p..%p] (%lu bytes)",
275 this->start, this->end,
276 (unsigned long) (this->end - this->start));
279 return push<Type> ();
281 void end_serialize ()
283 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
284 "end [%p..%p] serialized %u bytes; %s",
285 this->start, this->end,
286 (unsigned) (this->head - this->start),
287 successful () ? "successful" : "UNSUCCESSFUL");
289 propagate_error (packed, packed_map);
291 if (unlikely (!current)) return;
292 if (unlikely (in_error()))
294 // Offset overflows that occur before link resolution cannot be handled
295 // by repacking, so set a more general error.
296 if (offset_overflow ()) err (HB_SERIALIZE_ERROR_OTHER);
300 assert (!current->next);
302 /* Only "pack" if there exist other objects... Otherwise, don't bother.
304 if (packed.length <= 1)
312 template <typename Type = void>
313 __attribute__((returns_nonnull))
316 if (unlikely (in_error ())) return start_embed<Type> ();
318 object_t *obj = object_pool.alloc ();
320 check_success (false);
328 return start_embed<Type> ();
332 object_t *obj = current;
333 if (unlikely (!obj)) return;
334 // Allow cleanup when we've error'd out on int overflows which don't compromise
335 // the serializer state.
336 if (unlikely (in_error() && !only_overflow ())) return;
338 current = current->next;
339 revert (zerocopy ? zerocopy : obj->head, obj->tail);
342 object_pool.release (obj);
345 /* Set share to false when an object is unlikely shareable with others
346 * so not worth an attempt, or a contiguous table is serialized as
347 * multiple consecutive objects in the reverse order so can't be shared.
349 objidx_t pop_pack (bool share=true)
351 object_t *obj = current;
352 if (unlikely (!obj)) return 0;
353 // Allow cleanup when we've error'd out on int overflows which don't compromise
354 // the serializer state.
355 if (unlikely (in_error() && !only_overflow ())) return 0;
357 current = current->next;
360 assert (obj->head <= obj->tail);
361 unsigned len = obj->tail - obj->head;
362 head = zerocopy ? zerocopy : obj->head; /* Rewind head. */
363 bool was_zerocopy = zerocopy;
368 assert (!obj->real_links.length);
369 assert (!obj->virtual_links.length);
377 hash = hb_hash (obj);
378 objidx = packed_map.get_with_hash (obj, hash);
381 merge_virtual_links (obj, objidx);
389 assert (tail == obj->head);
391 memmove (tail, obj->head, len);
394 obj->tail = tail + len;
398 if (unlikely (!propagate_error (packed)))
400 /* Obj wasn't successfully added to packed, so clean it up otherwise its
401 * links will be leaked. When we use constructor/destructors properly, we
402 * can remove these. */
407 objidx = packed.length - 1;
409 if (share) packed_map.set_with_hash (obj, hash, objidx);
410 propagate_error (packed_map);
415 void revert (snapshot_t snap)
417 // Overflows that happened after the snapshot will be erased by the revert.
418 if (unlikely (in_error () && !only_overflow ())) return;
419 assert (snap.current == current);
422 current->real_links.shrink (snap.num_real_links);
423 current->virtual_links.shrink (snap.num_virtual_links);
425 errors = snap.errors;
426 revert (snap.head, snap.tail);
429 void revert (char *snap_head,
432 if (unlikely (in_error ())) return;
433 assert (snap_head <= head);
434 assert (tail <= snap_tail);
437 discard_stale_objects ();
440 void discard_stale_objects ()
442 if (unlikely (in_error ())) return;
443 while (packed.length > 1 &&
444 packed.tail ()->head < tail)
446 packed_map.del (packed.tail ());
447 assert (!packed.tail ()->next);
448 packed.tail ()->fini ();
451 if (packed.length > 1)
452 assert (packed.tail ()->head == tail);
455 // Adds a virtual link from the current object to objidx. A virtual link is not associated with
456 // an actual offset field. They are solely used to enforce ordering constraints between objects.
457 // Adding a virtual link from object a to object b will ensure that object b is always packed after
458 // object a in the final serialized order.
460 // This is useful in certain situations where there needs to be a specific ordering in the
461 // final serialization. Such as when platform bugs require certain orderings, or to provide
462 // guidance to the repacker for better offset overflow resolution.
463 void add_virtual_link (objidx_t objidx)
465 if (unlikely (in_error ())) return;
472 auto& link = *current->virtual_links.push ();
473 if (current->virtual_links.in_error ())
474 err (HB_SERIALIZE_ERROR_OTHER);
477 link.objidx = objidx;
484 template <typename T>
485 void add_link (T &ofs, objidx_t objidx,
486 whence_t whence = Head,
489 if (unlikely (in_error ())) return;
495 assert (current->head <= (const char *) &ofs);
497 auto& link = *current->real_links.push ();
498 if (current->real_links.in_error ())
499 err (HB_SERIALIZE_ERROR_OTHER);
501 link.width = sizeof (T);
502 link.objidx = objidx;
503 if (unlikely (!sizeof (T)))
505 // This link is not associated with an actual offset and exists merely to enforce
506 // an ordering constraint.
514 link.is_signed = std::is_signed<hb_unwrap_type (T)>::value;
515 link.whence = (unsigned) whence;
516 link.position = (const char *) &ofs - current->head;
520 unsigned to_bias (const void *base) const
522 if (unlikely (in_error ())) return 0;
525 assert (current->head <= (const char *) base);
526 return (const char *) base - current->head;
529 void resolve_links ()
531 if (unlikely (in_error ())) return;
534 assert (packed.length > 1);
536 for (const object_t* parent : ++hb_iter (packed))
537 for (const object_t::link_t &link : parent->real_links)
539 const object_t* child = packed[link.objidx];
540 if (unlikely (!child)) { err (HB_SERIALIZE_ERROR_OTHER); return; }
542 switch ((whence_t) link.whence) {
543 case Head: offset = child->head - parent->head; break;
544 case Tail: offset = child->head - parent->tail; break;
545 case Absolute: offset = (head - start) + (child->head - tail); break;
548 assert (offset >= link.bias);
552 assert (link.width == 2 || link.width == 4);
554 assign_offset<int32_t> (parent, link, offset);
556 assign_offset<int16_t> (parent, link, offset);
560 assert (link.width == 2 || link.width == 3 || link.width == 4);
562 assign_offset<uint32_t> (parent, link, offset);
563 else if (link.width == 3)
564 assign_offset<uint32_t, 3> (parent, link, offset);
566 assign_offset<uint16_t> (parent, link, offset);
571 unsigned int length () const
573 if (unlikely (!current)) return 0;
574 return this->head - current->head;
577 void align (unsigned int alignment)
579 unsigned int l = length () % alignment;
581 (void) allocate_size<void> (alignment - l);
584 template <typename Type = void>
585 __attribute__((returns_nonnull))
586 Type *start_embed (const Type *obj HB_UNUSED = nullptr) const
587 { return reinterpret_cast<Type *> (this->head); }
588 template <typename Type>
589 __attribute__((returns_nonnull))
590 Type *start_embed (const Type &obj) const
591 { return start_embed (std::addressof (obj)); }
593 bool err (hb_serialize_error_t err_type)
595 return !bool ((errors = (errors | err_type)));
598 bool start_zerocopy (size_t size)
600 if (unlikely (in_error ())) return false;
602 if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size)))
604 err (HB_SERIALIZE_ERROR_OUT_OF_ROOM);
608 assert (!this->zerocopy);
609 this->zerocopy = this->head;
611 assert (this->current->head == this->head);
612 this->current->head = this->current->tail = this->head = this->tail - size;
616 template <typename Type>
618 Type *allocate_size (size_t size, bool clear = true)
620 if (unlikely (in_error ())) return nullptr;
622 if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size)))
624 err (HB_SERIALIZE_ERROR_OUT_OF_ROOM);
628 hb_memset (this->head, 0, size);
629 char *ret = this->head;
631 return reinterpret_cast<Type *> (ret);
634 template <typename Type>
635 Type *allocate_min ()
636 { return this->allocate_size<Type> (Type::min_size); }
638 template <typename Type>
640 Type *embed (const Type *obj)
642 unsigned int size = obj->get_size ();
643 Type *ret = this->allocate_size<Type> (size, false);
644 if (unlikely (!ret)) return nullptr;
645 hb_memcpy (ret, obj, size);
648 template <typename Type>
650 Type *embed (const Type &obj)
651 { return embed (std::addressof (obj)); }
652 char *embed (const char *obj, unsigned size)
654 char *ret = this->allocate_size<char> (size, false);
655 if (unlikely (!ret)) return nullptr;
656 hb_memcpy (ret, obj, size);
660 template <typename Type, typename ...Ts> auto
661 _copy (const Type &src, hb_priority<1>, Ts&&... ds) HB_RETURN
662 (Type *, src.copy (this, std::forward<Ts> (ds)...))
664 template <typename Type> auto
665 _copy (const Type &src, hb_priority<0>) -> decltype (&(hb_declval<Type> () = src))
667 Type *ret = this->allocate_size<Type> (sizeof (Type));
668 if (unlikely (!ret)) return nullptr;
673 /* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data
674 * instead of hb_memcpy(). */
675 template <typename Type, typename ...Ts>
676 Type *copy (const Type &src, Ts&&... ds)
677 { return _copy (src, hb_prioritize, std::forward<Ts> (ds)...); }
678 template <typename Type, typename ...Ts>
679 Type *copy (const Type *src, Ts&&... ds)
680 { return copy (*src, std::forward<Ts> (ds)...); }
682 template<typename Iterator,
683 hb_requires (hb_is_iterator (Iterator)),
685 void copy_all (Iterator it, Ts&&... ds)
686 { for (decltype (*it) _ : it) copy (_, std::forward<Ts> (ds)...); }
688 template <typename Type>
689 hb_serialize_context_t& operator << (const Type &obj) & { embed (obj); return *this; }
691 template <typename Type>
692 Type *extend_size (Type *obj, size_t size, bool clear = true)
694 if (unlikely (in_error ())) return nullptr;
696 assert (this->start <= (char *) obj);
697 assert ((char *) obj <= this->head);
698 assert ((size_t) (this->head - (char *) obj) <= size);
699 if (unlikely (((char *) obj + size < (char *) obj) ||
700 !this->allocate_size<Type> (((char *) obj) + size - this->head, clear))) return nullptr;
701 return reinterpret_cast<Type *> (obj);
703 template <typename Type>
704 Type *extend_size (Type &obj, size_t size, bool clear = true)
705 { return extend_size (std::addressof (obj), size, clear); }
707 template <typename Type>
708 Type *extend_min (Type *obj) { return extend_size (obj, obj->min_size); }
709 template <typename Type>
710 Type *extend_min (Type &obj) { return extend_min (std::addressof (obj)); }
712 template <typename Type, typename ...Ts>
713 Type *extend (Type *obj, Ts&&... ds)
714 { return extend_size (obj, obj->get_size (std::forward<Ts> (ds)...)); }
715 template <typename Type, typename ...Ts>
716 Type *extend (Type &obj, Ts&&... ds)
717 { return extend (std::addressof (obj), std::forward<Ts> (ds)...); }
719 /* Output routines. */
720 hb_bytes_t copy_bytes () const
722 assert (successful ());
723 /* Copy both items from head side and tail side... */
724 unsigned int len = (this->head - this->start)
725 + (this->end - this->tail);
727 // If len is zero don't hb_malloc as the memory won't get properly
729 if (!len) return hb_bytes_t ();
731 char *p = (char *) hb_malloc (len);
732 if (unlikely (!p)) return hb_bytes_t ();
734 hb_memcpy (p, this->start, this->head - this->start);
735 hb_memcpy (p + (this->head - this->start), this->tail, this->end - this->tail);
736 return hb_bytes_t (p, len);
738 template <typename Type>
740 { return reinterpret_cast<Type *> ((char *) copy_bytes ().arrayZ); }
741 hb_blob_t *copy_blob () const
743 hb_bytes_t b = copy_bytes ();
744 return hb_blob_create (b.arrayZ, b.length,
745 HB_MEMORY_MODE_WRITABLE,
746 (char *) b.arrayZ, hb_free);
749 const hb_vector_t<object_t *>& object_graph() const
753 template <typename T, unsigned Size = sizeof (T)>
754 void assign_offset (const object_t* parent, const object_t::link_t &link, unsigned offset)
756 auto &off = * ((BEInt<T, Size> *) (parent->head + link.position));
758 check_assign (off, offset, HB_SERIALIZE_ERROR_OFFSET_OVERFLOW);
762 char *start, *head, *tail, *end, *zerocopy;
763 unsigned int debug_depth;
764 hb_serialize_error_t errors;
768 void merge_virtual_links (const object_t* from, objidx_t to_idx) {
769 object_t* to = packed[to_idx];
770 for (const auto& l : from->virtual_links) {
771 to->virtual_links.push (l);
775 /* Object memory pool. */
776 hb_pool_t<object_t> object_pool;
778 /* Stack of currently under construction objects. */
781 /* Stack of packed objects. Object 0 is always nil object. */
782 hb_vector_t<object_t *> packed;
784 /* Map view of packed objects. */
785 hb_hashmap_t<const object_t *, objidx_t> packed_map;
788 #endif /* HB_SERIALIZE_HH */