2019-03-30 22:53:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2007,2008,2009,2010 Red Hat, Inc.
|
|
|
|
* Copyright © 2012,2018 Google, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-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 (); }
|
|
|
|
|
|
|
|
struct link_t
|
|
|
|
{
|
|
|
|
bool wide: 1;
|
|
|
|
unsigned offset : 31;
|
|
|
|
objidx_t objidx;
|
|
|
|
};
|
|
|
|
|
|
|
|
hb_vector_t<link_t> links;
|
|
|
|
};
|
|
|
|
|
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-03-30 22:53:54 +01:00
|
|
|
hb_serialize_context_t (void *start_, unsigned int size)
|
|
|
|
{
|
|
|
|
this->start = (char *) start_;
|
|
|
|
this->end = this->start + size;
|
|
|
|
reset ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-31 03:14:30 +02:00
|
|
|
this->current.reset ();
|
|
|
|
this->packed.reset ();
|
2019-03-31 03:00:03 +02:00
|
|
|
this->packed.push ()->head = this->end;
|
2019-03-31 01:51:26 +01:00
|
|
|
this->packed_map.reset ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool propagate_error (bool e)
|
|
|
|
{ return this->successful = this->successful && e; }
|
|
|
|
template <typename T> bool propagate_error (const T &obj)
|
|
|
|
{ return this->successful = this->successful && !obj.in_error (); }
|
|
|
|
template <typename T> bool propagate_error (const T *obj)
|
|
|
|
{ return this->successful = this->successful && !obj->in_error (); }
|
2019-03-31 01:12:40 +01:00
|
|
|
template <typename T1, typename T2> bool propagate_error (T1 &&o1, T2 &&o2)
|
2019-03-30 22:53:54 +01:00
|
|
|
{ return propagate_error (o1) && propagate_error (o2); }
|
|
|
|
template <typename T1, typename T2, typename T3>
|
2019-03-31 01:12:40 +01:00
|
|
|
bool propagate_error (T1 &&o1, T2 &&o2, T3 &&o3)
|
2019-03-30 22:53:54 +01:00
|
|
|
{ return propagate_error (o1) && propagate_error (o2, o3); }
|
|
|
|
|
|
|
|
/* 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-03-31 01:26:35 +01:00
|
|
|
assert (!current.length);
|
|
|
|
return push<Type> ();
|
2019-03-30 22:53:54 +01:00
|
|
|
}
|
2019-03-31 01:26:35 +01:00
|
|
|
objidx_t 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
|
|
|
|
|
|
|
/* TODO Propagate errors. */
|
|
|
|
|
2019-03-31 01:26:35 +01:00
|
|
|
assert (current.length == 1);
|
|
|
|
return pop_pack ();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *push ()
|
|
|
|
{
|
2019-03-31 03:14:30 +02:00
|
|
|
object_t obj;
|
|
|
|
obj.head = head;
|
|
|
|
obj.tail = tail;
|
|
|
|
current.push (obj);
|
2019-03-31 01:26:35 +01:00
|
|
|
return start_embed<Type> ();
|
|
|
|
}
|
|
|
|
void pop_discard ()
|
|
|
|
{
|
|
|
|
revert (current.pop ());
|
|
|
|
}
|
|
|
|
objidx_t pop_pack ()
|
|
|
|
{
|
2019-03-31 03:14:30 +02:00
|
|
|
object_t obj = current.pop ();
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
unsigned len = head - obj.head;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
tail -= len;
|
|
|
|
memmove (tail, obj.head, len);
|
|
|
|
head = obj.head;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
obj.head = tail;
|
|
|
|
obj.tail = tail + len;
|
2019-03-31 01:51:26 +01:00
|
|
|
|
2019-03-31 03:14:30 +02:00
|
|
|
packed.push (obj);
|
|
|
|
|
|
|
|
/* TODO Handle error. */
|
|
|
|
if (unlikely (packed.in_error ()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return packed.length - 1;
|
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-03-31 03:00:03 +02:00
|
|
|
packed.tail ().head < tail)
|
2019-03-31 01:26:35 +01:00
|
|
|
packed.pop ();
|
2019-03-31 03:00:03 +02:00
|
|
|
assert (packed.tail ().head == tail);
|
2019-03-31 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int length () const { return this->head - current.tail ().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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *start_embed (const Type *_ HB_UNUSED = nullptr) const
|
|
|
|
{
|
|
|
|
Type *ret = reinterpret_cast<Type *> (this->head);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
this->ran_out_of_room = true;
|
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>
|
|
|
|
Type *embed (const Type &obj)
|
|
|
|
{
|
|
|
|
unsigned int size = obj.get_size ();
|
|
|
|
Type *ret = this->allocate_size<Type> (size);
|
|
|
|
if (unlikely (!ret)) return nullptr;
|
|
|
|
memcpy (ret, &obj, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
template <typename Type>
|
|
|
|
hb_serialize_context_t &operator << (const Type &obj) { embed (obj); return *this; }
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *extend_size (Type &obj, unsigned int size)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *extend_min (Type &obj) { return extend_size (obj, obj.min_size); }
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type *extend (Type &obj) { return extend_size (obj, obj.get_size ()); }
|
|
|
|
|
|
|
|
/* Output routines. */
|
|
|
|
template <typename Type>
|
|
|
|
Type *copy () const
|
|
|
|
{
|
|
|
|
assert (this->successful);
|
|
|
|
unsigned int len = this->head - this->start;
|
|
|
|
void *p = malloc (len);
|
|
|
|
if (p)
|
|
|
|
memcpy (p, this->start, len);
|
|
|
|
return reinterpret_cast<Type *> (p);
|
|
|
|
}
|
|
|
|
hb_bytes_t copy_bytes () const
|
|
|
|
{
|
|
|
|
assert (this->successful);
|
|
|
|
unsigned int len = this->head - this->start;
|
|
|
|
void *p = malloc (len);
|
|
|
|
if (p)
|
|
|
|
memcpy (p, this->start, len);
|
|
|
|
else
|
|
|
|
return hb_bytes_t ();
|
|
|
|
return hb_bytes_t ((char *) p, len);
|
|
|
|
}
|
|
|
|
hb_blob_t *copy_blob () const
|
|
|
|
{
|
|
|
|
assert (this->successful);
|
|
|
|
return hb_blob_create (this->start,
|
|
|
|
this->head - this->start,
|
|
|
|
HB_MEMORY_MODE_DUPLICATE,
|
|
|
|
nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
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-03-31 03:14:30 +02:00
|
|
|
/* Stack of currently under construction objects. */
|
|
|
|
/* Note. We store the "end - tail" distance in the length member of these. */
|
|
|
|
hb_vector_t<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. */
|
|
|
|
hb_vector_t<object_t> packed;
|
|
|
|
|
2019-03-31 01:51:26 +01:00
|
|
|
/* Map view of packed objects. */
|
|
|
|
hb_hashmap_t<const object_t *, objidx_t> packed_map;
|
2019-03-30 22:53:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_SERIALIZE_HH */
|