1 /* 2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc. 3 * Copyright © 2012,2018 Google, Inc. 4 * Copyright © 2019 Facebook, Inc. 5 * 6 * This is part of HarfBuzz, a text shaping library. 7 * 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. 13 * 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 18 * DAMAGE. 19 * 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. 25 * 26 * Red Hat Author(s): Behdad Esfahbod 27 * Google Author(s): Behdad Esfahbod 28 * Facebook Author(s): Behdad Esfahbod 29 */ 30 31 #ifndef HB_SERIALIZE_HH 32 #define HB_SERIALIZE_HH 33 34 #include "hb.hh" 35 #include "hb-blob.hh" 36 #include "hb-map.hh" 37 #include "hb-pool.hh" 38 39 #ifdef HB_EXPERIMENTAL_API 40 #include "hb-subset-repacker.h" 41 #endif 42 43 /* 44 * Serialize 45 */ 46 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 54 }; 55 HB_MARK_AS_FLAG_T (hb_serialize_error_t); 56 57 struct hb_serialize_context_t 58 { 59 typedef unsigned objidx_t; 60 61 enum whence_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. */ 65 }; 66 67 68 69 struct object_t 70 { finihb_serialize_context_t::object_t71 void fini () { 72 real_links.fini (); 73 virtual_links.fini (); 74 } 75 76 object_t () = default; 77 78 #ifdef HB_EXPERIMENTAL_API object_thb_serialize_context_t::object_t79 object_t (const hb_object_t &o) 80 { 81 head = o.head; 82 tail = o.tail; 83 next = nullptr; 84 real_links.alloc (o.num_real_links); 85 for (unsigned i = 0 ; i < o.num_real_links; i++) 86 real_links.push (o.real_links[i]); 87 88 virtual_links.alloc (o.num_virtual_links); 89 for (unsigned i = 0; i < o.num_virtual_links; i++) 90 virtual_links.push (o.virtual_links[i]); 91 } 92 #endif 93 swaphb_serialize_context_t94 friend void swap (object_t& a, object_t& b) 95 { 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); 101 } 102 operator ==hb_serialize_context_t::object_t103 bool operator == (const object_t &o) const 104 { 105 // Virtual links aren't considered for equality since they don't affect the functionality 106 // of the object. 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 (); 111 } hashhb_serialize_context_t::object_t112 uint32_t hash () const 113 { 114 // Virtual links aren't considered for equality since they don't affect the functionality 115 // of the object. 116 return hb_bytes_t (head, tail - head).hash () ^ 117 real_links.as_bytes ().hash (); 118 } 119 120 struct link_t 121 { 122 unsigned width: 3; 123 unsigned is_signed: 1; 124 unsigned whence: 2; 125 unsigned bias : 26; 126 unsigned position; 127 objidx_t objidx; 128 129 link_t () = default; 130 131 #ifdef HB_EXPERIMENTAL_API link_thb_serialize_context_t::object_t::link_t132 link_t (const hb_link_t &o) 133 { 134 width = o.width; 135 is_signed = 0; 136 whence = 0; 137 position = o.position; 138 bias = 0; 139 objidx = o.objidx; 140 } 141 #endif 142 cmphb_serialize_context_t::object_t::link_t143 HB_INTERNAL static int cmp (const void* a, const void* b) 144 { 145 int cmp = ((const link_t*)a)->position - ((const link_t*)b)->position; 146 if (cmp) return cmp; 147 148 return ((const link_t*)a)->objidx - ((const link_t*)b)->objidx; 149 } 150 }; 151 152 char *head; 153 char *tail; 154 hb_vector_t<link_t> real_links; 155 hb_vector_t<link_t> virtual_links; 156 object_t *next; 157 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 ()) )); 162 }; 163 164 struct snapshot_t 165 { 166 char *head; 167 char *tail; 168 object_t *current; // Just for sanity check 169 unsigned num_real_links; 170 unsigned num_virtual_links; 171 hb_serialize_error_t errors; 172 }; 173 snapshothb_serialize_context_t174 snapshot_t snapshot () 175 { return snapshot_t { 176 head, tail, current, current->real_links.length, current->virtual_links.length, errors }; } 177 hb_serialize_context_thb_serialize_context_t178 hb_serialize_context_t (void *start_, unsigned int size) : 179 start ((char *) start_), 180 end (start + size), 181 current (nullptr) 182 { reset (); } ~hb_serialize_context_thb_serialize_context_t183 ~hb_serialize_context_t () { fini (); } 184 finihb_serialize_context_t185 void fini () 186 { 187 for (object_t *_ : ++hb_iter (packed)) _->fini (); 188 packed.fini (); 189 this->packed_map.fini (); 190 191 while (current) 192 { 193 auto *_ = current; 194 current = current->next; 195 _->fini (); 196 } 197 } 198 in_errorhb_serialize_context_t199 bool in_error () const { return bool (errors); } 200 successfulhb_serialize_context_t201 bool successful () const { return !bool (errors); } 202 ran_out_of_roomhb_serialize_context_t203 HB_NODISCARD bool ran_out_of_room () const { return errors & HB_SERIALIZE_ERROR_OUT_OF_ROOM; } offset_overflowhb_serialize_context_t204 HB_NODISCARD bool offset_overflow () const { return errors & HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; } only_offset_overflowhb_serialize_context_t205 HB_NODISCARD bool only_offset_overflow () const { return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; } only_overflowhb_serialize_context_t206 HB_NODISCARD bool only_overflow () const 207 { 208 return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW 209 || errors == HB_SERIALIZE_ERROR_INT_OVERFLOW 210 || errors == HB_SERIALIZE_ERROR_ARRAY_OVERFLOW; 211 } 212 resethb_serialize_context_t213 void reset (void *start_, unsigned int size) 214 { 215 start = (char*) start_; 216 end = start + size; 217 reset (); 218 current = nullptr; 219 } 220 resethb_serialize_context_t221 void reset () 222 { 223 this->errors = HB_SERIALIZE_ERROR_NONE; 224 this->head = this->start; 225 this->tail = this->end; 226 this->zerocopy = nullptr; 227 this->debug_depth = 0; 228 229 fini (); 230 this->packed.push (nullptr); 231 this->packed_map.init (); 232 } 233 check_successhb_serialize_context_t234 bool check_success (bool success, 235 hb_serialize_error_t err_type = HB_SERIALIZE_ERROR_OTHER) 236 { 237 return successful () 238 && (success || err (err_type)); 239 } 240 241 template <typename T1, typename T2> check_equalhb_serialize_context_t242 bool check_equal (T1 &&v1, T2 &&v2, hb_serialize_error_t err_type) 243 { 244 if ((long long) v1 != (long long) v2) 245 { 246 return err (err_type); 247 } 248 return true; 249 } 250 251 template <typename T1, typename T2> check_assignhb_serialize_context_t252 bool check_assign (T1 &v1, T2 &&v2, hb_serialize_error_t err_type) 253 { return check_equal (v1 = v2, v2, err_type); } 254 propagate_errorhb_serialize_context_t255 template <typename T> bool propagate_error (T &&obj) 256 { return check_success (!hb_deref (obj).in_error ()); } 257 propagate_errorhb_serialize_context_t258 template <typename T1, typename... Ts> bool propagate_error (T1 &&o1, Ts&&... os) 259 { return propagate_error (std::forward<T1> (o1)) && 260 propagate_error (std::forward<Ts> (os)...); } 261 262 /* To be called around main operation. */ 263 template <typename Type> start_serializehb_serialize_context_t264 Type *start_serialize () 265 { 266 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1, 267 "start [%p..%p] (%lu bytes)", 268 this->start, this->end, 269 (unsigned long) (this->end - this->start)); 270 271 assert (!current); 272 return push<Type> (); 273 } end_serializehb_serialize_context_t274 void end_serialize () 275 { 276 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1, 277 "end [%p..%p] serialized %u bytes; %s", 278 this->start, this->end, 279 (unsigned) (this->head - this->start), 280 successful () ? "successful" : "UNSUCCESSFUL"); 281 282 propagate_error (packed, packed_map); 283 284 if (unlikely (!current)) return; 285 if (unlikely (in_error())) 286 { 287 // Offset overflows that occur before link resolution cannot be handled 288 // by repacking, so set a more general error. 289 if (offset_overflow ()) err (HB_SERIALIZE_ERROR_OTHER); 290 return; 291 } 292 293 assert (!current->next); 294 295 /* Only "pack" if there exist other objects... Otherwise, don't bother. 296 * Saves a move. */ 297 if (packed.length <= 1) 298 return; 299 300 pop_pack (false); 301 302 resolve_links (); 303 } 304 305 template <typename Type = void> pushhb_serialize_context_t306 Type *push () 307 { 308 if (unlikely (in_error ())) return start_embed<Type> (); 309 310 object_t *obj = object_pool.alloc (); 311 if (unlikely (!obj)) 312 check_success (false); 313 else 314 { 315 obj->head = head; 316 obj->tail = tail; 317 obj->next = current; 318 current = obj; 319 } 320 return start_embed<Type> (); 321 } pop_discardhb_serialize_context_t322 void pop_discard () 323 { 324 object_t *obj = current; 325 if (unlikely (!obj)) return; 326 if (unlikely (in_error() && !only_overflow ())) return; 327 328 current = current->next; 329 revert (zerocopy ? zerocopy : obj->head, obj->tail); 330 zerocopy = nullptr; 331 obj->fini (); 332 object_pool.release (obj); 333 } 334 335 /* Set share to false when an object is unlikely shareable with others 336 * so not worth an attempt, or a contiguous table is serialized as 337 * multiple consecutive objects in the reverse order so can't be shared. 338 */ pop_packhb_serialize_context_t339 objidx_t pop_pack (bool share=true) 340 { 341 object_t *obj = current; 342 if (unlikely (!obj)) return 0; 343 if (unlikely (in_error())) return 0; 344 345 current = current->next; 346 obj->tail = head; 347 obj->next = nullptr; 348 assert (obj->head <= obj->tail); 349 unsigned len = obj->tail - obj->head; 350 head = zerocopy ? zerocopy : obj->head; /* Rewind head. */ 351 bool was_zerocopy = zerocopy; 352 zerocopy = nullptr; 353 354 if (!len) 355 { 356 assert (!obj->real_links.length); 357 assert (!obj->virtual_links.length); 358 return 0; 359 } 360 361 objidx_t objidx; 362 uint32_t hash = 0; 363 if (share) 364 { 365 hash = hb_hash (obj); 366 objidx = packed_map.get_with_hash (obj, hash); 367 if (objidx) 368 { 369 merge_virtual_links (obj, objidx); 370 obj->fini (); 371 return objidx; 372 } 373 } 374 375 tail -= len; 376 if (was_zerocopy) 377 assert (tail == obj->head); 378 else 379 memmove (tail, obj->head, len); 380 381 obj->head = tail; 382 obj->tail = tail + len; 383 384 packed.push (obj); 385 386 if (unlikely (!propagate_error (packed))) 387 { 388 /* Obj wasn't successfully added to packed, so clean it up otherwise its 389 * links will be leaked. When we use constructor/destructors properly, we 390 * can remove these. */ 391 obj->fini (); 392 return 0; 393 } 394 395 objidx = packed.length - 1; 396 397 if (share) packed_map.set_with_hash (obj, hash, objidx); 398 propagate_error (packed_map); 399 400 return objidx; 401 } 402 reverthb_serialize_context_t403 void revert (snapshot_t snap) 404 { 405 // Overflows that happened after the snapshot will be erased by the revert. 406 if (unlikely (in_error () && !only_overflow ())) return; 407 assert (snap.current == current); 408 current->real_links.shrink (snap.num_real_links); 409 current->virtual_links.shrink (snap.num_virtual_links); 410 errors = snap.errors; 411 revert (snap.head, snap.tail); 412 } 413 reverthb_serialize_context_t414 void revert (char *snap_head, 415 char *snap_tail) 416 { 417 if (unlikely (in_error ())) return; 418 assert (snap_head <= head); 419 assert (tail <= snap_tail); 420 head = snap_head; 421 tail = snap_tail; 422 discard_stale_objects (); 423 } 424 discard_stale_objectshb_serialize_context_t425 void discard_stale_objects () 426 { 427 if (unlikely (in_error ())) return; 428 while (packed.length > 1 && 429 packed.tail ()->head < tail) 430 { 431 packed_map.del (packed.tail ()); 432 assert (!packed.tail ()->next); 433 packed.tail ()->fini (); 434 packed.pop (); 435 } 436 if (packed.length > 1) 437 assert (packed.tail ()->head == tail); 438 } 439 440 // Adds a virtual link from the current object to objidx. A virtual link is not associated with 441 // an actual offset field. They are solely used to enforce ordering constraints between objects. 442 // Adding a virtual link from object a to object b will ensure that object b is always packed after 443 // object a in the final serialized order. 444 // 445 // This is useful in certain situations where there needs to be a specific ordering in the 446 // final serialization. Such as when platform bugs require certain orderings, or to provide 447 // guidance to the repacker for better offset overflow resolution. add_virtual_linkhb_serialize_context_t448 void add_virtual_link (objidx_t objidx) 449 { 450 if (unlikely (in_error ())) return; 451 452 if (!objidx) 453 return; 454 455 assert (current); 456 457 auto& link = *current->virtual_links.push (); 458 if (current->virtual_links.in_error ()) 459 err (HB_SERIALIZE_ERROR_OTHER); 460 461 link.width = 0; 462 link.objidx = objidx; 463 link.is_signed = 0; 464 link.whence = 0; 465 link.position = 0; 466 link.bias = 0; 467 } 468 469 template <typename T> add_linkhb_serialize_context_t470 void add_link (T &ofs, objidx_t objidx, 471 whence_t whence = Head, 472 unsigned bias = 0) 473 { 474 if (unlikely (in_error ())) return; 475 476 if (!objidx) 477 return; 478 479 assert (current); 480 assert (current->head <= (const char *) &ofs); 481 482 auto& link = *current->real_links.push (); 483 if (current->real_links.in_error ()) 484 err (HB_SERIALIZE_ERROR_OTHER); 485 486 link.width = sizeof (T); 487 link.objidx = objidx; 488 if (unlikely (!sizeof (T))) 489 { 490 // This link is not associated with an actual offset and exists merely to enforce 491 // an ordering constraint. 492 link.is_signed = 0; 493 link.whence = 0; 494 link.position = 0; 495 link.bias = 0; 496 return; 497 } 498 499 link.is_signed = std::is_signed<hb_unwrap_type (T)>::value; 500 link.whence = (unsigned) whence; 501 link.position = (const char *) &ofs - current->head; 502 link.bias = bias; 503 } 504 to_biashb_serialize_context_t505 unsigned to_bias (const void *base) const 506 { 507 if (unlikely (in_error ())) return 0; 508 if (!base) return 0; 509 assert (current); 510 assert (current->head <= (const char *) base); 511 return (const char *) base - current->head; 512 } 513 resolve_linkshb_serialize_context_t514 void resolve_links () 515 { 516 if (unlikely (in_error ())) return; 517 518 assert (!current); 519 assert (packed.length > 1); 520 521 for (const object_t* parent : ++hb_iter (packed)) 522 for (const object_t::link_t &link : parent->real_links) 523 { 524 const object_t* child = packed[link.objidx]; 525 if (unlikely (!child)) { err (HB_SERIALIZE_ERROR_OTHER); return; } 526 unsigned offset = 0; 527 switch ((whence_t) link.whence) { 528 case Head: offset = child->head - parent->head; break; 529 case Tail: offset = child->head - parent->tail; break; 530 case Absolute: offset = (head - start) + (child->head - tail); break; 531 } 532 533 assert (offset >= link.bias); 534 offset -= link.bias; 535 if (link.is_signed) 536 { 537 assert (link.width == 2 || link.width == 4); 538 if (link.width == 4) 539 assign_offset<int32_t> (parent, link, offset); 540 else 541 assign_offset<int16_t> (parent, link, offset); 542 } 543 else 544 { 545 assert (link.width == 2 || link.width == 3 || link.width == 4); 546 if (link.width == 4) 547 assign_offset<uint32_t> (parent, link, offset); 548 else if (link.width == 3) 549 assign_offset<uint32_t, 3> (parent, link, offset); 550 else 551 assign_offset<uint16_t> (parent, link, offset); 552 } 553 } 554 } 555 lengthhb_serialize_context_t556 unsigned int length () const 557 { 558 if (unlikely (!current)) return 0; 559 return this->head - current->head; 560 } 561 alignhb_serialize_context_t562 void align (unsigned int alignment) 563 { 564 unsigned int l = length () % alignment; 565 if (l) 566 allocate_size<void> (alignment - l); 567 } 568 569 template <typename Type = void> start_embedhb_serialize_context_t570 Type *start_embed (const Type *obj HB_UNUSED = nullptr) const 571 { return reinterpret_cast<Type *> (this->head); } 572 template <typename Type> start_embedhb_serialize_context_t573 Type *start_embed (const Type &obj) const 574 { return start_embed (std::addressof (obj)); } 575 errhb_serialize_context_t576 bool err (hb_serialize_error_t err_type) 577 { 578 return !bool ((errors = (errors | err_type))); 579 } 580 start_zerocopyhb_serialize_context_t581 bool start_zerocopy (size_t size) 582 { 583 if (unlikely (in_error ())) return false; 584 585 if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size))) 586 { 587 err (HB_SERIALIZE_ERROR_OUT_OF_ROOM); 588 return false; 589 } 590 591 assert (!this->zerocopy); 592 this->zerocopy = this->head; 593 594 assert (this->current->head == this->head); 595 this->current->head = this->current->tail = this->head = this->tail - size; 596 return true; 597 } 598 599 template <typename Type> allocate_sizehb_serialize_context_t600 Type *allocate_size (size_t size, bool clear = true) 601 { 602 if (unlikely (in_error ())) return nullptr; 603 604 if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size))) 605 { 606 err (HB_SERIALIZE_ERROR_OUT_OF_ROOM); 607 return nullptr; 608 } 609 if (clear) 610 hb_memset (this->head, 0, size); 611 char *ret = this->head; 612 this->head += size; 613 return reinterpret_cast<Type *> (ret); 614 } 615 616 template <typename Type> allocate_minhb_serialize_context_t617 Type *allocate_min () 618 { return this->allocate_size<Type> (Type::min_size); } 619 620 template <typename Type> embedhb_serialize_context_t621 Type *embed (const Type *obj) 622 { 623 unsigned int size = obj->get_size (); 624 Type *ret = this->allocate_size<Type> (size, false); 625 if (unlikely (!ret)) return nullptr; 626 hb_memcpy (ret, obj, size); 627 return ret; 628 } 629 template <typename Type> embedhb_serialize_context_t630 Type *embed (const Type &obj) 631 { return embed (std::addressof (obj)); } 632 633 template <typename Type, typename ...Ts> auto _copyhb_serialize_context_t634 _copy (const Type &src, hb_priority<1>, Ts&&... ds) HB_RETURN 635 (Type *, src.copy (this, std::forward<Ts> (ds)...)) 636 637 template <typename Type> auto 638 _copy (const Type &src, hb_priority<0>) -> decltype (&(hb_declval<Type> () = src)) 639 { 640 Type *ret = this->allocate_size<Type> (sizeof (Type)); 641 if (unlikely (!ret)) return nullptr; 642 *ret = src; 643 return ret; 644 } 645 646 /* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data 647 * instead of hb_memcpy(). */ 648 template <typename Type, typename ...Ts> copyhb_serialize_context_t649 Type *copy (const Type &src, Ts&&... ds) 650 { return _copy (src, hb_prioritize, std::forward<Ts> (ds)...); } 651 template <typename Type, typename ...Ts> copyhb_serialize_context_t652 Type *copy (const Type *src, Ts&&... ds) 653 { return copy (*src, std::forward<Ts> (ds)...); } 654 655 template<typename Iterator, 656 hb_requires (hb_is_iterator (Iterator)), 657 typename ...Ts> copy_allhb_serialize_context_t658 void copy_all (Iterator it, Ts&&... ds) 659 { for (decltype (*it) _ : it) copy (_, std::forward<Ts> (ds)...); } 660 661 template <typename Type> operator <<hb_serialize_context_t662 hb_serialize_context_t& operator << (const Type &obj) & { embed (obj); return *this; } 663 664 template <typename Type> extend_sizehb_serialize_context_t665 Type *extend_size (Type *obj, size_t size, bool clear = true) 666 { 667 if (unlikely (in_error ())) return nullptr; 668 669 assert (this->start <= (char *) obj); 670 assert ((char *) obj <= this->head); 671 assert ((size_t) (this->head - (char *) obj) <= size); 672 if (unlikely (((char *) obj + size < (char *) obj) || 673 !this->allocate_size<Type> (((char *) obj) + size - this->head, clear))) return nullptr; 674 return reinterpret_cast<Type *> (obj); 675 } 676 template <typename Type> extend_sizehb_serialize_context_t677 Type *extend_size (Type &obj, size_t size, bool clear = true) 678 { return extend_size (std::addressof (obj), size, clear); } 679 680 template <typename Type> extend_minhb_serialize_context_t681 Type *extend_min (Type *obj) { return extend_size (obj, obj->min_size); } 682 template <typename Type> extend_minhb_serialize_context_t683 Type *extend_min (Type &obj) { return extend_min (std::addressof (obj)); } 684 685 template <typename Type, typename ...Ts> extendhb_serialize_context_t686 Type *extend (Type *obj, Ts&&... ds) 687 { return extend_size (obj, obj->get_size (std::forward<Ts> (ds)...)); } 688 template <typename Type, typename ...Ts> extendhb_serialize_context_t689 Type *extend (Type &obj, Ts&&... ds) 690 { return extend (std::addressof (obj), std::forward<Ts> (ds)...); } 691 692 /* Output routines. */ copy_byteshb_serialize_context_t693 hb_bytes_t copy_bytes () const 694 { 695 assert (successful ()); 696 /* Copy both items from head side and tail side... */ 697 unsigned int len = (this->head - this->start) 698 + (this->end - this->tail); 699 700 // If len is zero don't hb_malloc as the memory won't get properly 701 // cleaned up later. 702 if (!len) return hb_bytes_t (); 703 704 char *p = (char *) hb_malloc (len); 705 if (unlikely (!p)) return hb_bytes_t (); 706 707 hb_memcpy (p, this->start, this->head - this->start); 708 hb_memcpy (p + (this->head - this->start), this->tail, this->end - this->tail); 709 return hb_bytes_t (p, len); 710 } 711 template <typename Type> copyhb_serialize_context_t712 Type *copy () const 713 { return reinterpret_cast<Type *> ((char *) copy_bytes ().arrayZ); } copy_blobhb_serialize_context_t714 hb_blob_t *copy_blob () const 715 { 716 hb_bytes_t b = copy_bytes (); 717 return hb_blob_create (b.arrayZ, b.length, 718 HB_MEMORY_MODE_WRITABLE, 719 (char *) b.arrayZ, hb_free); 720 } 721 object_graphhb_serialize_context_t722 const hb_vector_t<object_t *>& object_graph() const 723 { return packed; } 724 725 private: 726 template <typename T, unsigned Size = sizeof (T)> assign_offsethb_serialize_context_t727 void assign_offset (const object_t* parent, const object_t::link_t &link, unsigned offset) 728 { 729 auto &off = * ((BEInt<T, Size> *) (parent->head + link.position)); 730 assert (0 == off); 731 check_assign (off, offset, HB_SERIALIZE_ERROR_OFFSET_OVERFLOW); 732 } 733 734 public: 735 char *start, *head, *tail, *end, *zerocopy; 736 unsigned int debug_depth; 737 hb_serialize_error_t errors; 738 739 private: 740 merge_virtual_linkshb_serialize_context_t741 void merge_virtual_links (const object_t* from, objidx_t to_idx) { 742 object_t* to = packed[to_idx]; 743 for (const auto& l : from->virtual_links) { 744 to->virtual_links.push (l); 745 } 746 } 747 748 /* Object memory pool. */ 749 hb_pool_t<object_t> object_pool; 750 751 /* Stack of currently under construction objects. */ 752 object_t *current; 753 754 /* Stack of packed objects. Object 0 is always nil object. */ 755 hb_vector_t<object_t *> packed; 756 757 /* Map view of packed objects. */ 758 hb_hashmap_t<const object_t *, objidx_t> packed_map; 759 }; 760 761 #endif /* HB_SERIALIZE_HH */ 762