2019-03-30 22:53:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2007,2008,2009,2010 Red Hat, Inc.
|
|
|
|
* Copyright © 2012,2018 Google, Inc.
|
2019-04-03 07:42:22 +02:00
|
|
|
* Copyright © 2019 Facebook, Inc.
|
2019-03-30 22:53:54 +01:00
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
2019-04-03 07:42:22 +02:00
|
|
|
* Facebook Author(s): Behdad Esfahbod
|
2019-03-30 22:53:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_SERIALIZE_HH
|
|
|
|
#define HB_SERIALIZE_HH
|
|
|
|
|
|
|
|
#include "hb.hh"
|
|
|
|
#include "hb-blob.hh"
|
2019-03-31 01:51:26 +01:00
|
|
|
#include "hb-map.hh"
|
2019-04-03 07:42:22 +02:00
|
|
|
#include "hb-pool.hh"
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2022-02-02 19:30:34 +01:00
|
|
|
#ifdef HB_EXPERIMENTAL_API
|
|
|
|
#include "hb-subset-repacker.h"
|
|
|
|
#endif
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Serialize
|
|
|
|
*/
|
|
|
|
|
2021-03-18 18:51:26 +01:00
|
|
|
enum hb_serialize_error_t {
|
|
|
|
HB_SERIALIZE_ERROR_NONE = 0x00000000u,
|
|
|
|
HB_SERIALIZE_ERROR_OTHER = 0x00000001u,
|
|
|
|
HB_SERIALIZE_ERROR_OFFSET_OVERFLOW = 0x00000002u,
|
|
|
|
HB_SERIALIZE_ERROR_OUT_OF_ROOM = 0x00000004u,
|
|
|
|
HB_SERIALIZE_ERROR_INT_OVERFLOW = 0x00000008u,
|
|
|
|
HB_SERIALIZE_ERROR_ARRAY_OVERFLOW = 0x00000010u
|
2021-03-17 23:53:10 +01:00
|
|
|
};
|
2021-03-18 18:51:26 +01:00
|
|
|
HB_MARK_AS_FLAG_T (hb_serialize_error_t);
|
2021-03-17 23:53:10 +01:00
|
|
|
|
2019-03-30 22:53:54 +01:00
|
|
|
struct hb_serialize_context_t
|
|
|
|
{
|
2019-03-31 01:26:35 +01:00
|
|
|
typedef unsigned objidx_t;
|
|
|
|
|
2020-02-19 21:52:18 +01:00
|
|
|
enum whence_t {
|
|
|
|
Head, /* Relative to the current object head (default). */
|
|
|
|
Tail, /* Relative to the current object tail after packed. */
|
|
|
|
Absolute /* Absolute: from the start of the serialize buffer. */
|
|
|
|
};
|
|
|
|
|
2021-03-17 23:53:10 +01:00
|
|
|
|
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
struct object_t
|
2019-03-31 01:26:35 +01:00
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
void fini () {
|
|
|
|
real_links.fini ();
|
|
|
|
virtual_links.fini ();
|
|
|
|
}
|
2019-03-31 01:26:35 +01:00
|
|
|
|
2022-02-02 19:30:34 +01:00
|
|
|
object_t () = default;
|
2022-05-19 23:25:21 +02:00
|
|
|
|
2022-02-02 19:30:34 +01:00
|
|
|
#ifdef HB_EXPERIMENTAL_API
|
|
|
|
object_t (const hb_object_t &o)
|
|
|
|
{
|
|
|
|
head = o.head;
|
|
|
|
tail = o.tail;
|
|
|
|
next = nullptr;
|
2022-12-31 20:27:13 +01:00
|
|
|
real_links.alloc (o.num_real_links, true);
|
2022-02-02 19:30:34 +01:00
|
|
|
for (unsigned i = 0 ; i < o.num_real_links; i++)
|
|
|
|
real_links.push (o.real_links[i]);
|
|
|
|
|
2022-12-31 20:27:13 +01:00
|
|
|
virtual_links.alloc (o.num_virtual_links, true);
|
2022-02-02 19:30:34 +01:00
|
|
|
for (unsigned i = 0; i < o.num_virtual_links; i++)
|
|
|
|
virtual_links.push (o.virtual_links[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-19 23:25:21 +02:00
|
|
|
friend void swap (object_t& a, object_t& b)
|
|
|
|
{
|
|
|
|
hb_swap (a.head, b.head);
|
|
|
|
hb_swap (a.tail, b.tail);
|
|
|
|
hb_swap (a.next, b.next);
|
|
|
|
hb_swap (a.real_links, b.real_links);
|
|
|
|
hb_swap (a.virtual_links, b.virtual_links);
|
|
|
|
}
|
|
|
|
|
2019-03-31 04:16:20 +02:00
|
|
|
bool operator == (const object_t &o) const
|
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
// Virtual links aren't considered for equality since they don't affect the functionality
|
|
|
|
// of the object.
|
2019-03-31 04:16:20 +02:00
|
|
|
return (tail - head == o.tail - o.head)
|
2021-11-30 22:45:22 +01:00
|
|
|
&& (real_links.length == o.real_links.length)
|
2019-04-03 04:27:02 +02:00
|
|
|
&& 0 == hb_memcmp (head, o.head, tail - head)
|
2021-11-30 22:45:22 +01:00
|
|
|
&& real_links.as_bytes () == o.real_links.as_bytes ();
|
2019-03-31 04:16:20 +02:00
|
|
|
}
|
2019-03-31 04:41:48 +02:00
|
|
|
uint32_t hash () const
|
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
// Virtual links aren't considered for equality since they don't affect the functionality
|
|
|
|
// of the object.
|
2019-03-31 04:41:48 +02:00
|
|
|
return hb_bytes_t (head, tail - head).hash () ^
|
2021-11-30 22:45:22 +01:00
|
|
|
real_links.as_bytes ().hash ();
|
2019-03-31 04:41:48 +02:00
|
|
|
}
|
2019-03-31 04:16:20 +02:00
|
|
|
|
2019-03-31 01:26:35 +01:00
|
|
|
struct link_t
|
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
unsigned width: 3;
|
2022-05-20 02:00:58 +02:00
|
|
|
unsigned is_signed: 1;
|
2020-02-19 22:38:04 +01:00
|
|
|
unsigned whence: 2;
|
2022-05-20 02:00:58 +02:00
|
|
|
unsigned bias : 26;
|
|
|
|
unsigned position;
|
2019-03-31 01:26:35 +01:00
|
|
|
objidx_t objidx;
|
2022-02-02 19:30:34 +01:00
|
|
|
|
|
|
|
link_t () = default;
|
|
|
|
|
|
|
|
#ifdef HB_EXPERIMENTAL_API
|
|
|
|
link_t (const hb_link_t &o)
|
|
|
|
{
|
|
|
|
width = o.width;
|
|
|
|
is_signed = 0;
|
|
|
|
whence = 0;
|
|
|
|
position = o.position;
|
|
|
|
bias = 0;
|
|
|
|
objidx = o.objidx;
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-05 02:32:47 +02:00
|
|
|
|
|
|
|
HB_INTERNAL static int cmp (const void* a, const void* b)
|
|
|
|
{
|
2022-08-16 01:16:51 +02:00
|
|
|
int cmp = ((const link_t*)a)->position - ((const link_t*)b)->position;
|
|
|
|
if (cmp) return cmp;
|
|
|
|
|
|
|
|
return ((const link_t*)a)->objidx - ((const link_t*)b)->objidx;
|
2022-08-05 02:32:47 +02:00
|
|
|
}
|
2019-03-31 01:26:35 +01:00
|
|
|
};
|
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
char *head;
|
|
|
|
char *tail;
|
2021-11-30 22:45:22 +01:00
|
|
|
hb_vector_t<link_t> real_links;
|
|
|
|
hb_vector_t<link_t> virtual_links;
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *next;
|
2021-12-01 00:25:40 +01:00
|
|
|
|
|
|
|
auto all_links () const HB_AUTO_RETURN
|
|
|
|
(( hb_concat (this->real_links, this->virtual_links) ));
|
|
|
|
auto all_links_writer () HB_AUTO_RETURN
|
|
|
|
(( hb_concat (this->real_links.writer (), this->virtual_links.writer ()) ));
|
2019-03-31 01:26:35 +01:00
|
|
|
};
|
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
struct snapshot_t
|
|
|
|
{
|
|
|
|
char *head;
|
|
|
|
char *tail;
|
|
|
|
object_t *current; // Just for sanity check
|
2021-11-30 22:45:22 +01:00
|
|
|
unsigned num_real_links;
|
|
|
|
unsigned num_virtual_links;
|
2021-08-26 23:32:17 +02:00
|
|
|
hb_serialize_error_t errors;
|
2020-04-19 01:29:32 +02:00
|
|
|
};
|
2019-03-31 01:26:35 +01:00
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
snapshot_t snapshot ()
|
2021-11-30 22:45:22 +01:00
|
|
|
{ return snapshot_t {
|
|
|
|
head, tail, current, current->real_links.length, current->virtual_links.length, errors }; }
|
2019-03-31 01:26:35 +01:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
hb_serialize_context_t (void *start_, unsigned int size) :
|
|
|
|
start ((char *) start_),
|
|
|
|
end (start + size),
|
|
|
|
current (nullptr)
|
|
|
|
{ reset (); }
|
|
|
|
~hb_serialize_context_t () { fini (); }
|
|
|
|
|
|
|
|
void fini ()
|
2019-03-31 04:46:35 +02:00
|
|
|
{
|
2019-09-24 10:36:28 +02:00
|
|
|
for (object_t *_ : ++hb_iter (packed)) _->fini ();
|
2019-04-03 07:42:22 +02:00
|
|
|
packed.fini ();
|
|
|
|
this->packed_map.fini ();
|
|
|
|
|
|
|
|
while (current)
|
|
|
|
{
|
|
|
|
auto *_ = current;
|
|
|
|
current = current->next;
|
|
|
|
_->fini ();
|
|
|
|
}
|
2019-03-31 04:46:35 +02:00
|
|
|
}
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2021-03-18 18:51:26 +01:00
|
|
|
bool in_error () const { return bool (errors); }
|
2021-03-17 23:53:10 +01:00
|
|
|
|
2021-03-18 18:51:26 +01:00
|
|
|
bool successful () const { return !bool (errors); }
|
2021-03-17 23:53:10 +01:00
|
|
|
|
2021-03-18 22:35:36 +01:00
|
|
|
HB_NODISCARD bool ran_out_of_room () const { return errors & HB_SERIALIZE_ERROR_OUT_OF_ROOM; }
|
|
|
|
HB_NODISCARD bool offset_overflow () const { return errors & HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; }
|
|
|
|
HB_NODISCARD bool only_offset_overflow () const { return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW; }
|
2021-08-26 23:32:17 +02:00
|
|
|
HB_NODISCARD bool only_overflow () const
|
|
|
|
{
|
|
|
|
return errors == HB_SERIALIZE_ERROR_OFFSET_OVERFLOW
|
|
|
|
|| errors == HB_SERIALIZE_ERROR_INT_OVERFLOW
|
|
|
|
|| errors == HB_SERIALIZE_ERROR_ARRAY_OVERFLOW;
|
|
|
|
}
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2020-11-11 01:15:37 +01:00
|
|
|
void reset (void *start_, unsigned int size)
|
|
|
|
{
|
|
|
|
start = (char*) start_;
|
|
|
|
end = start + size;
|
|
|
|
reset ();
|
|
|
|
current = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-30 22:53:54 +01:00
|
|
|
void reset ()
|
|
|
|
{
|
2021-03-18 18:51:26 +01:00
|
|
|
this->errors = HB_SERIALIZE_ERROR_NONE;
|
2019-03-30 22:53:54 +01:00
|
|
|
this->head = this->start;
|
2019-03-30 23:08:39 +01:00
|
|
|
this->tail = this->end;
|
2022-12-02 01:33:53 +01:00
|
|
|
this->zerocopy = nullptr;
|
2019-03-30 22:53:54 +01:00
|
|
|
this->debug_depth = 0;
|
2019-03-31 01:10:59 +01:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
fini ();
|
|
|
|
this->packed.push (nullptr);
|
2020-11-11 01:15:37 +01:00
|
|
|
this->packed_map.init ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 23:53:10 +01:00
|
|
|
bool check_success (bool success,
|
2021-03-18 18:51:26 +01:00
|
|
|
hb_serialize_error_t err_type = HB_SERIALIZE_ERROR_OTHER)
|
2021-03-17 23:53:10 +01:00
|
|
|
{
|
|
|
|
return successful ()
|
2021-03-29 23:34:24 +02:00
|
|
|
&& (success || err (err_type));
|
2021-03-17 23:53:10 +01:00
|
|
|
}
|
2019-04-22 21:16:35 +02:00
|
|
|
|
2019-04-24 16:07:19 +02:00
|
|
|
template <typename T1, typename T2>
|
2021-03-18 18:51:26 +01:00
|
|
|
bool check_equal (T1 &&v1, T2 &&v2, hb_serialize_error_t err_type)
|
2020-10-30 20:16:26 +01:00
|
|
|
{
|
|
|
|
if ((long long) v1 != (long long) v2)
|
|
|
|
{
|
2021-03-18 19:20:03 +01:00
|
|
|
return err (err_type);
|
2020-10-30 20:16:26 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-24 16:07:19 +02:00
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
2021-03-18 18:51:26 +01:00
|
|
|
bool check_assign (T1 &v1, T2 &&v2, hb_serialize_error_t err_type)
|
2021-03-17 23:53:10 +01:00
|
|
|
{ return check_equal (v1 = v2, v2, err_type); }
|
2019-04-24 16:07:19 +02:00
|
|
|
|
2019-04-22 21:16:35 +02:00
|
|
|
template <typename T> bool propagate_error (T &&obj)
|
2019-05-07 08:17:39 +02:00
|
|
|
{ return check_success (!hb_deref (obj).in_error ()); }
|
2019-04-22 21:16:35 +02:00
|
|
|
|
2019-05-08 05:58:43 +02:00
|
|
|
template <typename T1, typename... Ts> bool propagate_error (T1 &&o1, Ts&&... os)
|
2021-11-02 07:18:22 +01:00
|
|
|
{ return propagate_error (std::forward<T1> (o1)) &&
|
|
|
|
propagate_error (std::forward<Ts> (os)...); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
/* To be called around main operation. */
|
|
|
|
template <typename Type>
|
|
|
|
Type *start_serialize ()
|
|
|
|
{
|
|
|
|
DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
|
|
|
|
"start [%p..%p] (%lu bytes)",
|
|
|
|
this->start, this->end,
|
|
|
|
(unsigned long) (this->end - this->start));
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
assert (!current);
|
2019-03-31 01:26:35 +01:00
|
|
|
return push<Type> ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
2019-03-31 03:48:26 +02:00
|
|
|
void end_serialize ()
|
2019-03-30 22:53:54 +01:00
|
|
|
{
|
|
|
|
DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
|
2019-03-31 01:26:35 +01:00
|
|
|
"end [%p..%p] serialized %u bytes; %s",
|
2019-03-30 22:53:54 +01:00
|
|
|
this->start, this->end,
|
2019-03-31 01:26:35 +01:00
|
|
|
(unsigned) (this->head - this->start),
|
2021-03-17 23:53:10 +01:00
|
|
|
successful () ? "successful" : "UNSUCCESSFUL");
|
2019-03-31 03:14:30 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
propagate_error (packed, packed_map);
|
2019-03-31 03:14:30 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
if (unlikely (!current)) return;
|
2021-03-18 18:51:26 +01:00
|
|
|
if (unlikely (in_error()))
|
|
|
|
{
|
|
|
|
// Offset overflows that occur before link resolution cannot be handled
|
|
|
|
// by repacking, so set a more general error.
|
2021-03-18 19:20:03 +01:00
|
|
|
if (offset_overflow ()) err (HB_SERIALIZE_ERROR_OTHER);
|
2021-03-18 18:51:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 00:18:25 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
assert (!current->next);
|
2019-03-31 04:05:51 +02:00
|
|
|
|
|
|
|
/* Only "pack" if there exist other objects... Otherwise, don't bother.
|
2019-03-31 04:49:56 +02:00
|
|
|
* Saves a move. */
|
2019-05-07 21:45:38 +02:00
|
|
|
if (packed.length <= 1)
|
2019-03-31 04:49:56 +02:00
|
|
|
return;
|
|
|
|
|
2020-02-17 02:16:29 +01:00
|
|
|
pop_pack (false);
|
2019-03-31 04:49:56 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
resolve_links ();
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
2019-04-01 23:17:09 +02:00
|
|
|
template <typename Type = void>
|
2019-03-31 01:26:35 +01:00
|
|
|
Type *push ()
|
|
|
|
{
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error ())) return start_embed<Type> ();
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *obj = object_pool.alloc ();
|
|
|
|
if (unlikely (!obj))
|
2019-04-24 16:07:19 +02:00
|
|
|
check_success (false);
|
2019-04-03 07:42:22 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
obj->head = head;
|
|
|
|
obj->tail = tail;
|
|
|
|
obj->next = current;
|
|
|
|
current = obj;
|
|
|
|
}
|
2019-03-31 01:26:35 +01:00
|
|
|
return start_embed<Type> ();
|
|
|
|
}
|
|
|
|
void pop_discard ()
|
|
|
|
{
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *obj = current;
|
|
|
|
if (unlikely (!obj)) return;
|
2022-08-05 22:33:22 +02:00
|
|
|
if (unlikely (in_error() && !only_overflow ())) return;
|
2020-07-30 00:18:25 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
current = current->next;
|
2022-12-02 22:26:36 +01:00
|
|
|
revert (zerocopy ? zerocopy : obj->head, obj->tail);
|
|
|
|
zerocopy = nullptr;
|
2019-09-24 02:28:16 +02:00
|
|
|
obj->fini ();
|
2021-07-08 18:53:45 +02:00
|
|
|
object_pool.release (obj);
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
2020-02-16 17:38:41 +01:00
|
|
|
|
Fix various typos
Found via `codespell -q 3 -S ./perf/texts -L actualy,ba,beng,fo,gir,inout,nd,ot,pres,ro,te,teh,timne`
2022-01-16 13:00:53 +01:00
|
|
|
/* Set share to false when an object is unlikely shareable with others
|
2020-02-16 17:38:41 +01:00
|
|
|
* so not worth an attempt, or a contiguous table is serialized as
|
|
|
|
* multiple consecutive objects in the reverse order so can't be shared.
|
|
|
|
*/
|
|
|
|
objidx_t pop_pack (bool share=true)
|
2019-03-31 01:26:35 +01:00
|
|
|
{
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *obj = current;
|
|
|
|
if (unlikely (!obj)) return 0;
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error())) return 0;
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
current = current->next;
|
|
|
|
obj->tail = head;
|
|
|
|
obj->next = nullptr;
|
2022-12-02 01:33:53 +01:00
|
|
|
assert (obj->head <= obj->tail);
|
2019-04-03 07:42:22 +02:00
|
|
|
unsigned len = obj->tail - obj->head;
|
2022-12-02 01:33:53 +01:00
|
|
|
head = zerocopy ? zerocopy : obj->head; /* Rewind head. */
|
2022-12-03 21:03:38 +01:00
|
|
|
bool was_zerocopy = zerocopy;
|
|
|
|
zerocopy = nullptr;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-04-03 02:20:04 +02:00
|
|
|
if (!len)
|
2019-04-03 07:42:22 +02:00
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
assert (!obj->real_links.length);
|
|
|
|
assert (!obj->virtual_links.length);
|
2019-04-03 02:20:04 +02:00
|
|
|
return 0;
|
2019-04-03 07:42:22 +02:00
|
|
|
}
|
2019-04-03 02:20:04 +02:00
|
|
|
|
2020-02-16 17:38:41 +01:00
|
|
|
objidx_t objidx;
|
2022-11-25 20:37:24 +01:00
|
|
|
uint32_t hash = 0;
|
2020-02-16 17:38:41 +01:00
|
|
|
if (share)
|
2019-03-31 04:46:35 +02:00
|
|
|
{
|
2022-11-22 03:40:32 +01:00
|
|
|
hash = hb_hash (obj);
|
|
|
|
objidx = packed_map.get_with_hash (obj, hash);
|
2020-02-16 17:38:41 +01:00
|
|
|
if (objidx)
|
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
merge_virtual_links (obj, objidx);
|
2020-02-16 17:38:41 +01:00
|
|
|
obj->fini ();
|
|
|
|
return objidx;
|
|
|
|
}
|
2019-03-31 04:46:35 +02:00
|
|
|
}
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
tail -= len;
|
2022-12-03 21:03:38 +01:00
|
|
|
if (was_zerocopy)
|
2022-12-02 01:33:53 +01:00
|
|
|
assert (tail == obj->head);
|
|
|
|
else
|
|
|
|
memmove (tail, obj->head, len);
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
obj->head = tail;
|
|
|
|
obj->tail = tail + len;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
packed.push (obj);
|
2019-03-31 03:14:30 +02:00
|
|
|
|
2021-02-11 19:08:52 +01:00
|
|
|
if (unlikely (!propagate_error (packed)))
|
|
|
|
{
|
|
|
|
/* Obj wasn't successfully added to packed, so clean it up otherwise its
|
|
|
|
* links will be leaked. When we use constructor/destructors properly, we
|
|
|
|
* can remove these. */
|
2020-07-30 01:37:39 +02:00
|
|
|
obj->fini ();
|
2019-03-31 03:14:30 +02:00
|
|
|
return 0;
|
2020-07-30 01:37:39 +02:00
|
|
|
}
|
2019-03-31 03:14:30 +02:00
|
|
|
|
2019-03-31 04:46:35 +02:00
|
|
|
objidx = packed.length - 1;
|
2019-03-31 04:16:20 +02:00
|
|
|
|
2022-11-22 03:40:32 +01:00
|
|
|
if (share) packed_map.set_with_hash (obj, hash, objidx);
|
2020-07-31 00:14:02 +02:00
|
|
|
propagate_error (packed_map);
|
2019-03-31 04:16:20 +02:00
|
|
|
|
|
|
|
return objidx;
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
void revert (snapshot_t snap)
|
|
|
|
{
|
2021-08-26 23:32:17 +02:00
|
|
|
// Overflows that happened after the snapshot will be erased by the revert.
|
|
|
|
if (unlikely (in_error () && !only_overflow ())) return;
|
2020-04-19 01:29:32 +02:00
|
|
|
assert (snap.current == current);
|
2021-11-30 22:45:22 +01:00
|
|
|
current->real_links.shrink (snap.num_real_links);
|
|
|
|
current->virtual_links.shrink (snap.num_virtual_links);
|
2021-08-26 23:32:17 +02:00
|
|
|
errors = snap.errors;
|
2020-04-19 01:29:32 +02:00
|
|
|
revert (snap.head, snap.tail);
|
|
|
|
}
|
2020-07-31 00:14:02 +02:00
|
|
|
|
2020-04-19 01:29:32 +02:00
|
|
|
void revert (char *snap_head,
|
|
|
|
char *snap_tail)
|
2019-03-31 01:26:35 +01:00
|
|
|
{
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error ())) return;
|
2020-04-19 01:29:32 +02:00
|
|
|
assert (snap_head <= head);
|
|
|
|
assert (tail <= snap_tail);
|
|
|
|
head = snap_head;
|
|
|
|
tail = snap_tail;
|
2019-03-31 01:26:35 +01:00
|
|
|
discard_stale_objects ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 01:26:35 +01:00
|
|
|
void discard_stale_objects ()
|
|
|
|
{
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error ())) return;
|
2019-03-31 01:26:35 +01:00
|
|
|
while (packed.length > 1 &&
|
2019-04-03 07:42:22 +02:00
|
|
|
packed.tail ()->head < tail)
|
2019-04-03 02:49:52 +02:00
|
|
|
{
|
2019-04-03 07:42:22 +02:00
|
|
|
packed_map.del (packed.tail ());
|
|
|
|
assert (!packed.tail ()->next);
|
|
|
|
packed.tail ()->fini ();
|
2019-03-31 01:26:35 +01:00
|
|
|
packed.pop ();
|
2019-04-03 02:49:52 +02:00
|
|
|
}
|
2019-04-03 07:42:22 +02:00
|
|
|
if (packed.length > 1)
|
|
|
|
assert (packed.tail ()->head == tail);
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 20:39:59 +02:00
|
|
|
// Adds a virtual link from the current object to objidx. A virtual link is not associated with
|
|
|
|
// an actual offset field. They are solely used to enforce ordering constraints between objects.
|
|
|
|
// Adding a virtual link from object a to object b will ensure that object b is always packed after
|
|
|
|
// object a in the final serialized order.
|
|
|
|
//
|
Fix various typos
Found via `codespell -q 3 -S ./perf/texts -L actualy,ba,beng,fo,gir,inout,nd,ot,pres,ro,te,teh,timne`
2022-01-16 13:00:53 +01:00
|
|
|
// This is useful in certain situations where there needs to be a specific ordering in the
|
2021-09-24 20:39:59 +02:00
|
|
|
// final serialization. Such as when platform bugs require certain orderings, or to provide
|
|
|
|
// guidance to the repacker for better offset overflow resolution.
|
2021-09-23 23:45:20 +02:00
|
|
|
void add_virtual_link (objidx_t objidx)
|
2021-09-23 23:41:42 +02:00
|
|
|
{
|
|
|
|
if (unlikely (in_error ())) return;
|
|
|
|
|
|
|
|
if (!objidx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert (current);
|
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
auto& link = *current->virtual_links.push ();
|
|
|
|
if (current->virtual_links.in_error ())
|
2021-09-23 23:41:42 +02:00
|
|
|
err (HB_SERIALIZE_ERROR_OTHER);
|
|
|
|
|
|
|
|
link.width = 0;
|
|
|
|
link.objidx = objidx;
|
|
|
|
link.is_signed = 0;
|
|
|
|
link.whence = 0;
|
|
|
|
link.position = 0;
|
|
|
|
link.bias = 0;
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:17:09 +02:00
|
|
|
template <typename T>
|
2020-02-18 01:29:40 +01:00
|
|
|
void add_link (T &ofs, objidx_t objidx,
|
2020-03-07 20:02:36 +01:00
|
|
|
whence_t whence = Head,
|
2020-03-06 00:40:44 +01:00
|
|
|
unsigned bias = 0)
|
2019-04-01 23:17:09 +02:00
|
|
|
{
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error ())) return;
|
2019-04-02 01:17:30 +02:00
|
|
|
|
2019-04-02 06:32:29 +02:00
|
|
|
if (!objidx)
|
2019-04-02 01:17:30 +02:00
|
|
|
return;
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
assert (current);
|
|
|
|
assert (current->head <= (const char *) &ofs);
|
2019-04-03 01:53:05 +02:00
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
auto& link = *current->real_links.push ();
|
|
|
|
if (current->real_links.in_error ())
|
2021-08-26 23:32:17 +02:00
|
|
|
err (HB_SERIALIZE_ERROR_OTHER);
|
2020-02-07 01:11:58 +01:00
|
|
|
|
2021-04-17 18:59:45 +02:00
|
|
|
link.width = sizeof (T);
|
2021-09-23 23:14:06 +02:00
|
|
|
link.objidx = objidx;
|
|
|
|
if (unlikely (!sizeof (T)))
|
|
|
|
{
|
|
|
|
// This link is not associated with an actual offset and exists merely to enforce
|
|
|
|
// an ordering constraint.
|
|
|
|
link.is_signed = 0;
|
|
|
|
link.whence = 0;
|
|
|
|
link.position = 0;
|
|
|
|
link.bias = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-02 07:26:46 +01:00
|
|
|
link.is_signed = std::is_signed<hb_unwrap_type (T)>::value;
|
2020-03-06 00:40:44 +01:00
|
|
|
link.whence = (unsigned) whence;
|
2020-02-07 01:11:58 +01:00
|
|
|
link.position = (const char *) &ofs - current->head;
|
2020-03-06 00:40:44 +01:00
|
|
|
link.bias = bias;
|
2020-02-07 01:11:58 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:02:36 +01:00
|
|
|
unsigned to_bias (const void *base) const
|
|
|
|
{
|
2020-08-12 00:40:47 +02:00
|
|
|
if (unlikely (in_error ())) return 0;
|
2020-03-07 20:02:36 +01:00
|
|
|
if (!base) return 0;
|
|
|
|
assert (current);
|
|
|
|
assert (current->head <= (const char *) base);
|
|
|
|
return (const char *) base - current->head;
|
|
|
|
}
|
2020-03-06 00:40:44 +01:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
void resolve_links ()
|
2019-03-31 04:49:56 +02:00
|
|
|
{
|
2019-05-07 21:45:38 +02:00
|
|
|
if (unlikely (in_error ())) return;
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
assert (!current);
|
2019-05-07 21:45:38 +02:00
|
|
|
assert (packed.length > 1);
|
2019-04-01 06:37:14 +02:00
|
|
|
|
2019-05-07 22:45:48 +02:00
|
|
|
for (const object_t* parent : ++hb_iter (packed))
|
2021-11-30 22:45:22 +01:00
|
|
|
for (const object_t::link_t &link : parent->real_links)
|
2019-04-01 06:37:14 +02:00
|
|
|
{
|
2019-05-07 22:45:48 +02:00
|
|
|
const object_t* child = packed[link.objidx];
|
2021-03-18 19:20:03 +01:00
|
|
|
if (unlikely (!child)) { err (HB_SERIALIZE_ERROR_OTHER); return; }
|
2020-04-02 04:49:18 +02:00
|
|
|
unsigned offset = 0;
|
|
|
|
switch ((whence_t) link.whence) {
|
2020-03-06 00:40:44 +01:00
|
|
|
case Head: offset = child->head - parent->head; break;
|
2020-02-19 21:52:18 +01:00
|
|
|
case Tail: offset = child->head - parent->tail; break;
|
|
|
|
case Absolute: offset = (head - start) + (child->head - tail); break;
|
2020-02-07 01:11:58 +01:00
|
|
|
}
|
2019-04-01 06:37:14 +02:00
|
|
|
|
2020-03-06 00:40:44 +01:00
|
|
|
assert (offset >= link.bias);
|
|
|
|
offset -= link.bias;
|
2020-02-07 01:11:58 +01:00
|
|
|
if (link.is_signed)
|
2019-04-01 06:37:14 +02:00
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
assert (link.width == 2 || link.width == 4);
|
|
|
|
if (link.width == 4)
|
2020-02-18 01:29:40 +01:00
|
|
|
assign_offset<int32_t> (parent, link, offset);
|
2020-02-07 01:11:58 +01:00
|
|
|
else
|
2020-02-18 01:29:40 +01:00
|
|
|
assign_offset<int16_t> (parent, link, offset);
|
2019-04-01 06:37:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
assert (link.width == 2 || link.width == 3 || link.width == 4);
|
|
|
|
if (link.width == 4)
|
2020-02-18 01:29:40 +01:00
|
|
|
assign_offset<uint32_t> (parent, link, offset);
|
2021-04-17 18:59:45 +02:00
|
|
|
else if (link.width == 3)
|
|
|
|
assign_offset<uint32_t, 3> (parent, link, offset);
|
2020-02-07 01:11:58 +01:00
|
|
|
else
|
2020-02-18 01:29:40 +01:00
|
|
|
assign_offset<uint16_t> (parent, link, offset);
|
2019-04-01 06:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-31 04:49:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:20:31 +02:00
|
|
|
unsigned int length () const
|
|
|
|
{
|
|
|
|
if (unlikely (!current)) return 0;
|
|
|
|
return this->head - current->head;
|
|
|
|
}
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
void align (unsigned int alignment)
|
|
|
|
{
|
|
|
|
unsigned int l = length () % alignment;
|
|
|
|
if (l)
|
|
|
|
allocate_size<void> (alignment - l);
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:37:38 +02:00
|
|
|
template <typename Type = void>
|
2019-05-08 02:23:46 +02:00
|
|
|
Type *start_embed (const Type *obj HB_UNUSED = nullptr) const
|
|
|
|
{ return reinterpret_cast<Type *> (this->head); }
|
2019-03-30 22:53:54 +01:00
|
|
|
template <typename Type>
|
2019-05-08 02:23:46 +02:00
|
|
|
Type *start_embed (const Type &obj) const
|
2022-01-14 00:17:34 +01:00
|
|
|
{ return start_embed (std::addressof (obj)); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2021-03-18 19:20:03 +01:00
|
|
|
bool err (hb_serialize_error_t err_type)
|
2021-03-17 23:53:10 +01:00
|
|
|
{
|
2021-03-29 23:34:24 +02:00
|
|
|
return !bool ((errors = (errors | err_type)));
|
2021-03-17 23:53:10 +01:00
|
|
|
}
|
2019-04-04 00:48:27 +02:00
|
|
|
|
2022-12-02 00:48:22 +01:00
|
|
|
bool start_zerocopy (size_t size)
|
|
|
|
{
|
|
|
|
if (unlikely (in_error ())) return false;
|
|
|
|
|
|
|
|
if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size)))
|
|
|
|
{
|
|
|
|
err (HB_SERIALIZE_ERROR_OUT_OF_ROOM);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-02 01:33:53 +01:00
|
|
|
assert (!this->zerocopy);
|
|
|
|
this->zerocopy = this->head;
|
|
|
|
|
2022-12-02 00:48:22 +01:00
|
|
|
assert (this->current->head == this->head);
|
2022-12-02 01:33:53 +01:00
|
|
|
this->current->head = this->current->tail = this->head = this->tail - size;
|
2022-12-02 00:48:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-30 22:53:54 +01:00
|
|
|
template <typename Type>
|
2022-12-01 22:19:27 +01:00
|
|
|
Type *allocate_size (size_t size, bool clear = true)
|
2019-03-30 22:53:54 +01:00
|
|
|
{
|
2021-03-17 23:53:10 +01:00
|
|
|
if (unlikely (in_error ())) return nullptr;
|
2019-03-30 23:06:25 +01:00
|
|
|
|
2021-07-27 21:15:07 +02:00
|
|
|
if (unlikely (size > INT_MAX || this->tail - this->head < ptrdiff_t (size)))
|
2019-03-30 23:06:25 +01:00
|
|
|
{
|
2021-03-18 19:20:03 +01:00
|
|
|
err (HB_SERIALIZE_ERROR_OUT_OF_ROOM);
|
2019-03-30 22:53:54 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-12-01 22:19:27 +01:00
|
|
|
if (clear)
|
|
|
|
hb_memset (this->head, 0, size);
|
2019-03-30 22:53:54 +01:00
|
|
|
char *ret = this->head;
|
|
|
|
this->head += size;
|
|
|
|
return reinterpret_cast<Type *> (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *allocate_min ()
|
2019-09-24 10:36:28 +02:00
|
|
|
{ return this->allocate_size<Type> (Type::min_size); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
template <typename Type>
|
2019-05-03 01:20:18 +02:00
|
|
|
Type *embed (const Type *obj)
|
2019-03-30 22:53:54 +01:00
|
|
|
{
|
2019-05-03 01:20:18 +02:00
|
|
|
unsigned int size = obj->get_size ();
|
2022-12-01 22:23:25 +01:00
|
|
|
Type *ret = this->allocate_size<Type> (size, false);
|
2019-03-30 22:53:54 +01:00
|
|
|
if (unlikely (!ret)) return nullptr;
|
2022-11-22 20:54:50 +01:00
|
|
|
hb_memcpy (ret, obj, size);
|
2019-03-30 22:53:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2019-05-03 01:20:18 +02:00
|
|
|
template <typename Type>
|
|
|
|
Type *embed (const Type &obj)
|
2022-01-14 00:17:34 +01:00
|
|
|
{ return embed (std::addressof (obj)); }
|
2019-04-17 17:00:08 +02:00
|
|
|
|
2019-05-02 23:14:33 +02:00
|
|
|
template <typename Type, typename ...Ts> auto
|
2019-05-08 05:58:43 +02:00
|
|
|
_copy (const Type &src, hb_priority<1>, Ts&&... ds) HB_RETURN
|
2021-11-02 07:18:22 +01:00
|
|
|
(Type *, src.copy (this, std::forward<Ts> (ds)...))
|
2019-04-17 17:00:08 +02:00
|
|
|
|
|
|
|
template <typename Type> auto
|
2019-06-17 23:23:04 +02:00
|
|
|
_copy (const Type &src, hb_priority<0>) -> decltype (&(hb_declval<Type> () = src))
|
2019-04-17 17:00:08 +02:00
|
|
|
{
|
|
|
|
Type *ret = this->allocate_size<Type> (sizeof (Type));
|
|
|
|
if (unlikely (!ret)) return nullptr;
|
2019-05-03 01:20:18 +02:00
|
|
|
*ret = src;
|
2019-04-17 17:00:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data
|
2022-11-22 20:54:50 +01:00
|
|
|
* instead of hb_memcpy(). */
|
2019-05-02 23:14:33 +02:00
|
|
|
template <typename Type, typename ...Ts>
|
2019-05-08 05:58:43 +02:00
|
|
|
Type *copy (const Type &src, Ts&&... ds)
|
2021-11-02 07:18:22 +01:00
|
|
|
{ return _copy (src, hb_prioritize, std::forward<Ts> (ds)...); }
|
2019-05-08 02:23:46 +02:00
|
|
|
template <typename Type, typename ...Ts>
|
2019-05-08 05:58:43 +02:00
|
|
|
Type *copy (const Type *src, Ts&&... ds)
|
2021-11-02 07:18:22 +01:00
|
|
|
{ return copy (*src, std::forward<Ts> (ds)...); }
|
2019-04-17 17:00:08 +02:00
|
|
|
|
2019-09-23 22:18:08 +02:00
|
|
|
template<typename Iterator,
|
|
|
|
hb_requires (hb_is_iterator (Iterator)),
|
|
|
|
typename ...Ts>
|
|
|
|
void copy_all (Iterator it, Ts&&... ds)
|
2021-11-02 07:18:22 +01:00
|
|
|
{ for (decltype (*it) _ : it) copy (_, std::forward<Ts> (ds)...); }
|
2019-09-23 22:18:08 +02:00
|
|
|
|
2019-03-30 22:53:54 +01:00
|
|
|
template <typename Type>
|
2019-05-07 23:26:03 +02:00
|
|
|
hb_serialize_context_t& operator << (const Type &obj) & { embed (obj); return *this; }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
template <typename Type>
|
2022-12-01 22:19:27 +01:00
|
|
|
Type *extend_size (Type *obj, size_t size, bool clear = true)
|
2019-03-30 22:53:54 +01:00
|
|
|
{
|
2020-07-30 00:18:25 +02:00
|
|
|
if (unlikely (in_error ())) return nullptr;
|
|
|
|
|
2019-05-08 02:23:46 +02:00
|
|
|
assert (this->start <= (char *) obj);
|
|
|
|
assert ((char *) obj <= this->head);
|
2021-07-28 19:42:31 +02:00
|
|
|
assert ((size_t) (this->head - (char *) obj) <= size);
|
2021-07-28 19:28:38 +02:00
|
|
|
if (unlikely (((char *) obj + size < (char *) obj) ||
|
2022-12-01 22:19:27 +01:00
|
|
|
!this->allocate_size<Type> (((char *) obj) + size - this->head, clear))) return nullptr;
|
2019-05-08 02:23:46 +02:00
|
|
|
return reinterpret_cast<Type *> (obj);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
2019-05-08 02:23:46 +02:00
|
|
|
template <typename Type>
|
2022-12-01 22:19:27 +01:00
|
|
|
Type *extend_size (Type &obj, size_t size, bool clear = true)
|
|
|
|
{ return extend_size (std::addressof (obj), size, clear); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
template <typename Type>
|
2019-05-08 02:23:46 +02:00
|
|
|
Type *extend_min (Type *obj) { return extend_size (obj, obj->min_size); }
|
|
|
|
template <typename Type>
|
2022-01-14 00:17:34 +01:00
|
|
|
Type *extend_min (Type &obj) { return extend_min (std::addressof (obj)); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2019-05-02 23:39:52 +02:00
|
|
|
template <typename Type, typename ...Ts>
|
2019-05-08 05:58:43 +02:00
|
|
|
Type *extend (Type *obj, Ts&&... ds)
|
2021-11-02 07:18:22 +01:00
|
|
|
{ return extend_size (obj, obj->get_size (std::forward<Ts> (ds)...)); }
|
2019-05-08 02:23:46 +02:00
|
|
|
template <typename Type, typename ...Ts>
|
2019-05-08 05:58:43 +02:00
|
|
|
Type *extend (Type &obj, Ts&&... ds)
|
2022-01-14 00:17:34 +01:00
|
|
|
{ return extend (std::addressof (obj), std::forward<Ts> (ds)...); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
/* Output routines. */
|
|
|
|
hb_bytes_t copy_bytes () const
|
|
|
|
{
|
2021-03-17 23:53:10 +01:00
|
|
|
assert (successful ());
|
2019-03-31 04:03:55 +02:00
|
|
|
/* Copy both items from head side and tail side... */
|
|
|
|
unsigned int len = (this->head - this->start)
|
|
|
|
+ (this->end - this->tail);
|
2019-09-24 10:36:28 +02:00
|
|
|
|
2021-07-08 18:58:50 +02:00
|
|
|
// If len is zero don't hb_malloc as the memory won't get properly
|
2021-06-17 01:34:46 +02:00
|
|
|
// cleaned up later.
|
2021-06-16 19:40:46 +02:00
|
|
|
if (!len) return hb_bytes_t ();
|
|
|
|
|
2021-07-08 18:58:50 +02:00
|
|
|
char *p = (char *) hb_malloc (len);
|
2019-09-24 10:36:28 +02:00
|
|
|
if (unlikely (!p)) return hb_bytes_t ();
|
|
|
|
|
2022-11-22 20:54:50 +01:00
|
|
|
hb_memcpy (p, this->start, this->head - this->start);
|
|
|
|
hb_memcpy (p + (this->head - this->start), this->tail, this->end - this->tail);
|
2019-03-31 04:03:55 +02:00
|
|
|
return hb_bytes_t (p, len);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
2019-03-31 04:01:23 +02:00
|
|
|
template <typename Type>
|
|
|
|
Type *copy () const
|
|
|
|
{ return reinterpret_cast<Type *> ((char *) copy_bytes ().arrayZ); }
|
2019-03-30 22:53:54 +01:00
|
|
|
hb_blob_t *copy_blob () const
|
|
|
|
{
|
2019-03-31 04:01:23 +02:00
|
|
|
hb_bytes_t b = copy_bytes ();
|
|
|
|
return hb_blob_create (b.arrayZ, b.length,
|
|
|
|
HB_MEMORY_MODE_WRITABLE,
|
2021-07-08 18:58:50 +02:00
|
|
|
(char *) b.arrayZ, hb_free);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2020-10-30 20:16:26 +01:00
|
|
|
const hb_vector_t<object_t *>& object_graph() const
|
2020-10-29 01:49:09 +01:00
|
|
|
{ return packed; }
|
|
|
|
|
2020-02-18 01:29:40 +01:00
|
|
|
private:
|
2021-04-17 18:59:45 +02:00
|
|
|
template <typename T, unsigned Size = sizeof (T)>
|
2020-02-18 01:29:40 +01:00
|
|
|
void assign_offset (const object_t* parent, const object_t::link_t &link, unsigned offset)
|
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
auto &off = * ((BEInt<T, Size> *) (parent->head + link.position));
|
2020-02-18 01:29:40 +01:00
|
|
|
assert (0 == off);
|
2021-03-18 18:51:26 +01:00
|
|
|
check_assign (off, offset, HB_SERIALIZE_ERROR_OFFSET_OVERFLOW);
|
2020-02-18 01:29:40 +01:00
|
|
|
}
|
|
|
|
|
2022-06-10 15:31:47 +02:00
|
|
|
public:
|
2022-12-02 01:33:53 +01:00
|
|
|
char *start, *head, *tail, *end, *zerocopy;
|
2019-03-30 22:53:54 +01:00
|
|
|
unsigned int debug_depth;
|
2021-03-18 18:51:26 +01:00
|
|
|
hb_serialize_error_t errors;
|
2019-03-31 01:10:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
void merge_virtual_links (const object_t* from, objidx_t to_idx) {
|
|
|
|
object_t* to = packed[to_idx];
|
|
|
|
for (const auto& l : from->virtual_links) {
|
|
|
|
to->virtual_links.push (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
/* Object memory pool. */
|
|
|
|
hb_pool_t<object_t> object_pool;
|
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
/* Stack of currently under construction objects. */
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *current;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 01:10:59 +01:00
|
|
|
/* Stack of packed objects. Object 0 is always nil object. */
|
2019-04-03 07:42:22 +02:00
|
|
|
hb_vector_t<object_t *> packed;
|
2019-03-31 01:10:59 +01:00
|
|
|
|
2019-03-31 01:51:26 +01:00
|
|
|
/* Map view of packed objects. */
|
2022-06-02 19:11:35 +02:00
|
|
|
hb_hashmap_t<const object_t *, objidx_t> packed_map;
|
2019-03-30 22:53:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HB_SERIALIZE_HH */
|