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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Serialize
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct hb_serialize_context_t
|
|
|
|
{
|
2019-03-31 01:26:35 +01:00
|
|
|
typedef unsigned objidx_t;
|
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
struct range_t
|
|
|
|
{
|
|
|
|
char *head, *tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct object_t : range_t
|
2019-03-31 01:26:35 +01:00
|
|
|
{
|
|
|
|
void fini () { links.fini (); }
|
|
|
|
|
2019-03-31 04:16:20 +02:00
|
|
|
bool operator == (const object_t &o) const
|
|
|
|
{
|
|
|
|
return (tail - head == o.tail - o.head)
|
2019-04-03 04:27:02 +02:00
|
|
|
&& (links.length == o.links.length)
|
|
|
|
&& 0 == hb_memcmp (head, o.head, tail - head)
|
|
|
|
&& links.as_bytes () == o.links.as_bytes ();
|
2019-03-31 04:16:20 +02:00
|
|
|
}
|
2019-03-31 04:41:48 +02:00
|
|
|
uint32_t hash () const
|
|
|
|
{
|
|
|
|
return hb_bytes_t (head, tail - head).hash () ^
|
|
|
|
links.as_bytes ().hash ();
|
|
|
|
}
|
2019-03-31 04:16:20 +02:00
|
|
|
|
2019-03-31 01:26:35 +01:00
|
|
|
struct link_t
|
|
|
|
{
|
2019-04-02 01:17:30 +02:00
|
|
|
bool is_wide: 1;
|
2019-04-01 06:37:14 +02:00
|
|
|
unsigned position : 31;
|
2019-05-03 01:20:18 +02:00
|
|
|
unsigned bias;
|
2019-03-31 01:26:35 +01:00
|
|
|
objidx_t objidx;
|
|
|
|
};
|
|
|
|
|
|
|
|
hb_vector_t<link_t> links;
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *next;
|
2019-03-31 01:26:35 +01:00
|
|
|
};
|
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
range_t snapshot () { range_t s = {head, tail} ; return s; }
|
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-04-03 07:42:22 +02:00
|
|
|
++ hb_iter (packed)
|
|
|
|
| hb_apply ([] (object_t *_) { _->fini (); })
|
|
|
|
;
|
|
|
|
packed.fini ();
|
|
|
|
this->packed_map.fini ();
|
|
|
|
|
|
|
|
while (current)
|
|
|
|
{
|
|
|
|
auto *_ = current;
|
|
|
|
current = current->next;
|
|
|
|
_->fini ();
|
|
|
|
}
|
|
|
|
object_pool.fini ();
|
2019-03-31 04:46:35 +02:00
|
|
|
}
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
bool in_error () const { return !this->successful; }
|
|
|
|
|
|
|
|
void reset ()
|
|
|
|
{
|
|
|
|
this->successful = true;
|
2019-03-30 23:06:25 +01:00
|
|
|
this->ran_out_of_room = false;
|
2019-03-30 22:53:54 +01:00
|
|
|
this->head = this->start;
|
2019-03-30 23:08:39 +01:00
|
|
|
this->tail = this->end;
|
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);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-24 16:07:19 +02:00
|
|
|
bool check_success (bool success)
|
2019-04-24 16:22:06 +02:00
|
|
|
{ return this->successful && (success || (err_other_error (), false)); }
|
2019-04-22 21:16:35 +02:00
|
|
|
|
2019-04-24 16:07:19 +02:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool check_equal (T1 &&v1, T2 &&v2)
|
|
|
|
{ return check_success (v1 == v2); }
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool check_assign (T1 &v1, T2 &&v2)
|
|
|
|
{ return check_equal (v1 = v2, v2); }
|
|
|
|
|
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)
|
2019-04-24 05:49:21 +02:00
|
|
|
{ return propagate_error (hb_forward<T1> (o1)) &&
|
|
|
|
propagate_error (hb_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),
|
2019-03-30 22:53:54 +01:00
|
|
|
this->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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
pop_pack ();
|
|
|
|
|
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 ()
|
|
|
|
{
|
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;
|
|
|
|
current = current->next;
|
|
|
|
revert (*obj);
|
|
|
|
object_pool.free (obj);
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
objidx_t pop_pack ()
|
|
|
|
{
|
2019-04-03 07:42:22 +02:00
|
|
|
object_t *obj = current;
|
|
|
|
if (unlikely (!obj)) return 0;
|
|
|
|
current = current->next;
|
|
|
|
obj->tail = head;
|
|
|
|
obj->next = nullptr;
|
|
|
|
unsigned len = obj->tail - obj->head;
|
2019-04-03 08:10:03 +02:00
|
|
|
head = obj->head; /* Rewind head. */
|
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
|
|
|
{
|
|
|
|
assert (!obj->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
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
objidx_t objidx = packed_map.get (obj);
|
2019-03-31 04:46:35 +02:00
|
|
|
if (objidx)
|
|
|
|
{
|
2019-04-03 07:42:22 +02:00
|
|
|
obj->fini ();
|
2019-03-31 04:46:35 +02:00
|
|
|
return objidx;
|
|
|
|
}
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
tail -= len;
|
2019-04-03 07:42:22 +02:00
|
|
|
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
|
|
|
|
|
|
|
if (unlikely (packed.in_error ()))
|
|
|
|
return 0;
|
|
|
|
|
2019-03-31 04:46:35 +02:00
|
|
|
objidx = packed.length - 1;
|
2019-03-31 04:16:20 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
packed_map.set (obj, objidx);
|
2019-03-31 04:16:20 +02:00
|
|
|
|
|
|
|
return objidx;
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
void revert (range_t snap)
|
2019-03-31 01:26:35 +01:00
|
|
|
{
|
|
|
|
assert (snap.head <= head);
|
|
|
|
assert (tail <= snap.tail);
|
|
|
|
head = snap.head;
|
|
|
|
tail = snap.tail;
|
|
|
|
discard_stale_objects ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 01:26:35 +01:00
|
|
|
void discard_stale_objects ()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-01 23:17:09 +02:00
|
|
|
template <typename T>
|
2019-04-02 01:17:30 +02:00
|
|
|
void add_link (T &ofs, objidx_t objidx, const void *base = nullptr)
|
2019-04-01 23:17:09 +02:00
|
|
|
{
|
2019-04-02 01:17:30 +02:00
|
|
|
static_assert (sizeof (T) == 2 || sizeof (T) == 4, "");
|
|
|
|
|
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
|
|
|
|
2019-04-02 01:17:30 +02:00
|
|
|
if (!base)
|
2019-04-03 07:42:22 +02:00
|
|
|
base = current->head;
|
2019-04-02 01:17:30 +02:00
|
|
|
else
|
2019-04-03 07:42:22 +02:00
|
|
|
assert (current->head <= (const char *) base);
|
2019-04-02 01:17:30 +02:00
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
auto& link = *current->links.push ();
|
2019-04-02 01:17:30 +02:00
|
|
|
link.is_wide = sizeof (T) == 4;
|
2019-04-17 23:59:39 +02:00
|
|
|
link.position = (const char *) &ofs - current->head;
|
2019-04-03 07:42:22 +02:00
|
|
|
link.bias = (const char *) base - current->head;
|
2019-04-02 01:17:30 +02:00
|
|
|
link.objidx = objidx;
|
2019-04-01 23:17:09 +02: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))
|
2019-04-01 06:37:14 +02:00
|
|
|
{
|
2019-05-07 20:05:51 +02:00
|
|
|
for (const object_t::link_t &link : parent->links)
|
2019-04-01 06:37:14 +02:00
|
|
|
{
|
2019-05-07 22:45:48 +02:00
|
|
|
const object_t* child = packed[link.objidx];
|
2019-05-07 20:51:10 +02:00
|
|
|
assert (link.bias <= (size_t) (parent->tail - parent->head));
|
2019-05-07 22:45:48 +02:00
|
|
|
unsigned offset = (child->head - parent->head) - link.bias;
|
2019-04-01 06:37:14 +02:00
|
|
|
|
2019-04-02 01:17:30 +02:00
|
|
|
if (link.is_wide)
|
2019-04-01 06:37:14 +02:00
|
|
|
{
|
2019-05-07 20:05:51 +02:00
|
|
|
auto &off = * ((BEInt<uint32_t, 4> *) (parent->head + link.position));
|
2019-04-17 23:58:13 +02:00
|
|
|
assert (0 == off);
|
2019-04-24 16:07:19 +02:00
|
|
|
check_assign (off, offset);
|
2019-04-01 06:37:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-07 20:05:51 +02:00
|
|
|
auto &off = * ((BEInt<uint16_t, 2> *) (parent->head + link.position));
|
2019-04-17 23:58:13 +02:00
|
|
|
assert (0 == off);
|
2019-04-24 16:07:19 +02:00
|
|
|
check_assign (off, offset);
|
2019-04-01 06:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 04:49:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 07:42:22 +02:00
|
|
|
unsigned int length () const { 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
|
|
|
|
{ return start_embed (hb_addressof (obj)); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
2019-04-22 21:37:10 +02:00
|
|
|
/* Following two functions exist to allow setting breakpoint on. */
|
2019-04-24 16:22:06 +02:00
|
|
|
void err_ran_out_of_room () { this->ran_out_of_room = true; }
|
|
|
|
void err_other_error () { this->successful = false; }
|
2019-04-04 00:48:27 +02:00
|
|
|
|
2019-03-30 22:53:54 +01:00
|
|
|
template <typename Type>
|
|
|
|
Type *allocate_size (unsigned int size)
|
|
|
|
{
|
2019-03-30 23:06:25 +01:00
|
|
|
if (unlikely (!this->successful)) return nullptr;
|
|
|
|
|
2019-03-30 23:08:39 +01:00
|
|
|
if (this->tail - this->head < ptrdiff_t (size))
|
2019-03-30 23:06:25 +01:00
|
|
|
{
|
2019-04-04 00:48:27 +02:00
|
|
|
err_ran_out_of_room ();
|
2019-03-30 22:53:54 +01:00
|
|
|
this->successful = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
memset (this->head, 0, size);
|
|
|
|
char *ret = this->head;
|
|
|
|
this->head += size;
|
|
|
|
return reinterpret_cast<Type *> (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *allocate_min ()
|
|
|
|
{
|
|
|
|
return this->allocate_size<Type> (Type::min_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ();
|
2019-03-30 22:53:54 +01:00
|
|
|
Type *ret = this->allocate_size<Type> (size);
|
|
|
|
if (unlikely (!ret)) return nullptr;
|
2019-05-03 01:20:18 +02:00
|
|
|
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)
|
2019-05-08 02:21:27 +02:00
|
|
|
{ return embed (hb_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
|
2019-05-03 01:20:18 +02:00
|
|
|
(Type *, src.copy (this, hb_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
|
|
|
|
* instead of 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)
|
2019-05-03 01:20:18 +02:00
|
|
|
{ return _copy (src, hb_prioritize, hb_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)
|
2019-05-08 02:23:46 +02:00
|
|
|
{ return copy (*src, hb_forward<Ts> (ds)...); }
|
2019-04-17 17:00: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>
|
2019-05-08 02:23:46 +02:00
|
|
|
Type *extend_size (Type *obj, unsigned int size)
|
2019-03-30 22:53:54 +01:00
|
|
|
{
|
2019-05-08 02:23:46 +02:00
|
|
|
assert (this->start <= (char *) obj);
|
|
|
|
assert ((char *) obj <= this->head);
|
|
|
|
assert ((char *) obj + size >= this->head);
|
|
|
|
if (unlikely (!this->allocate_size<Type> (((char *) obj) + size - this->head))) return nullptr;
|
|
|
|
return reinterpret_cast<Type *> (obj);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
2019-05-08 02:23:46 +02:00
|
|
|
template <typename Type>
|
|
|
|
Type *extend_size (Type &obj, unsigned int size)
|
|
|
|
{ return extend_size (hb_addressof (obj), size); }
|
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>
|
|
|
|
Type *extend_min (Type &obj) { return extend_min (hb_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)
|
2019-05-08 02:23:46 +02:00
|
|
|
{ return extend_size (obj, obj->get_size (hb_forward<Ts> (ds)...)); }
|
|
|
|
template <typename Type, typename ...Ts>
|
2019-05-08 05:58:43 +02:00
|
|
|
Type *extend (Type &obj, Ts&&... ds)
|
2019-05-08 02:23:46 +02:00
|
|
|
{ return extend (hb_addressof (obj), hb_forward<Ts> (ds)...); }
|
2019-03-30 22:53:54 +01:00
|
|
|
|
|
|
|
/* Output routines. */
|
|
|
|
hb_bytes_t copy_bytes () const
|
|
|
|
{
|
|
|
|
assert (this->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);
|
|
|
|
char *p = (char *) malloc (len);
|
2019-03-30 22:53:54 +01:00
|
|
|
if (p)
|
2019-03-31 04:03:55 +02:00
|
|
|
{
|
|
|
|
memcpy (p, this->start, this->head - this->start);
|
|
|
|
memcpy (p + (this->head - this->start), this->tail, this->end - this->tail);
|
|
|
|
}
|
2019-03-30 22:53:54 +01:00
|
|
|
else
|
|
|
|
return hb_bytes_t ();
|
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,
|
|
|
|
(char *) b.arrayZ, free);
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 01:10:59 +01:00
|
|
|
public: /* TODO Make private. */
|
2019-03-30 23:08:39 +01:00
|
|
|
char *start, *head, *tail, *end;
|
2019-03-30 22:53:54 +01:00
|
|
|
unsigned int debug_depth;
|
|
|
|
bool successful;
|
2019-03-30 23:06:25 +01:00
|
|
|
bool ran_out_of_room;
|
2019-03-31 01:10:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
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. */
|
2019-03-31 04:26:37 +02:00
|
|
|
hb_hashmap_t<const object_t *, objidx_t, nullptr, 0> packed_map;
|
2019-03-30 22:53:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_SERIALIZE_HH */
|