hb-serialize.hh 12.1 KB
Newer Older
1 2 3
/*
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
 * Copyright © 2012,2018  Google, Inc.
4
 * Copyright © 2019  Facebook, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  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
28
 * Facebook Author(s): Behdad Esfahbod
29 30 31 32 33 34 35
 */

#ifndef HB_SERIALIZE_HH
#define HB_SERIALIZE_HH

#include "hb.hh"
#include "hb-blob.hh"
B
Behdad Esfahbod 已提交
36
#include "hb-map.hh"
37
#include "hb-pool.hh"
38 39 40 41 42 43 44 45


/*
 * Serialize
 */

struct hb_serialize_context_t
{
46 47
  typedef unsigned objidx_t;

B
Behdad Esfahbod 已提交
48 49 50 51 52 53
  struct range_t
  {
    char *head, *tail;
  };

  struct object_t : range_t
54 55 56
  {
    void fini () { links.fini (); }

57 58 59
    bool operator == (const object_t &o) const
    {
      return (tail - head == o.tail - o.head)
60 61 62
	  && (links.length == o.links.length)
	  && 0 == hb_memcmp (head, o.head, tail - head)
	  && links.as_bytes () == o.links.as_bytes ();
63
    }
B
Behdad Esfahbod 已提交
64 65 66 67 68
    uint32_t hash () const
    {
      return hb_bytes_t (head, tail - head).hash () ^
	     links.as_bytes ().hash ();
    }
69

70 71
    struct link_t
    {
72
      bool is_wide: 1;
B
Behdad Esfahbod 已提交
73
      unsigned position : 31;
74
      unsigned bias;
75 76 77 78
      objidx_t objidx;
    };

    hb_vector_t<link_t> links;
79
    object_t *next;
80 81
  };

B
Behdad Esfahbod 已提交
82
  range_t snapshot () { range_t s = {head, tail} ; return s; }
83 84


85 86 87 88 89 90 91 92
  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 ()
B
Behdad Esfahbod 已提交
93
  {
94 95 96 97 98 99 100 101 102 103 104 105 106
    ++ 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 ();
B
Behdad Esfahbod 已提交
107
  }
108 109 110 111 112 113

  bool in_error () const { return !this->successful; }

  void reset ()
  {
    this->successful = true;
114
    this->ran_out_of_room = false;
115
    this->head = this->start;
B
Behdad Esfahbod 已提交
116
    this->tail = this->end;
117
    this->debug_depth = 0;
118

119 120
    fini ();
    this->packed.push (nullptr);
121 122
  }

123
  bool check_success (bool success)
B
Behdad Esfahbod 已提交
124
  { return this->successful && (success || (err_other_error (), false)); }
B
Behdad Esfahbod 已提交
125

126 127 128 129 130 131 132 133
  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); }

B
Behdad Esfahbod 已提交
134
  template <typename T> bool propagate_error (T &&obj)
135
  { return check_success (!hb_deref (obj).in_error ()); }
B
Behdad Esfahbod 已提交
136

B
Behdad Esfahbod 已提交
137
  template <typename T1, typename... Ts> bool propagate_error (T1 &&o1, Ts&&... os)
138 139
  { return propagate_error (hb_forward<T1> (o1)) &&
	   propagate_error (hb_forward<Ts> (os)...); }
140 141 142 143 144 145 146 147 148 149

  /* 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));

150
    assert (!current);
151
    return push<Type> ();
152
  }
B
Behdad Esfahbod 已提交
153
  void end_serialize ()
154 155
  {
    DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
156
		     "end [%p..%p] serialized %u bytes; %s",
157
		     this->start, this->end,
158
		     (unsigned) (this->head - this->start),
159
		     this->successful ? "successful" : "UNSUCCESSFUL");
B
Behdad Esfahbod 已提交
160

161
    propagate_error (packed, packed_map);
B
Behdad Esfahbod 已提交
162

163 164
    if (unlikely (!current)) return;
    assert (!current->next);
165 166

    /* Only "pack" if there exist other objects... Otherwise, don't bother.
167
     * Saves a move. */
B
Behdad Esfahbod 已提交
168
    if (packed.length <= 1)
169 170 171 172
      return;

    pop_pack ();

173
    resolve_links ();
174 175
  }

176
  template <typename Type = void>
177 178
  Type *push ()
  {
179 180
    object_t *obj = object_pool.alloc ();
    if (unlikely (!obj))
181
      check_success (false);
182 183 184 185 186 187 188
    else
    {
      obj->head = head;
      obj->tail = tail;
      obj->next = current;
      current = obj;
    }
189 190 191 192
    return start_embed<Type> ();
  }
  void pop_discard ()
  {
193 194 195 196 197
    object_t *obj = current;
    if (unlikely (!obj)) return;
    current = current->next;
    revert (*obj);
    object_pool.free (obj);
198 199 200
  }
  objidx_t pop_pack ()
  {
201 202 203 204 205 206
    object_t *obj = current;
    if (unlikely (!obj)) return 0;
    current = current->next;
    obj->tail = head;
    obj->next = nullptr;
    unsigned len = obj->tail - obj->head;
207
    head = obj->head; /* Rewind head. */
B
Behdad Esfahbod 已提交
208

209
    if (!len)
210 211
    {
      assert (!obj->links.length);
212
      return 0;
213
    }
214

215
    objidx_t objidx = packed_map.get (obj);
B
Behdad Esfahbod 已提交
216 217
    if (objidx)
    {
218
      obj->fini ();
B
Behdad Esfahbod 已提交
219 220
      return objidx;
    }
B
Behdad Esfahbod 已提交
221

B
Behdad Esfahbod 已提交
222
    tail -= len;
223
    memmove (tail, obj->head, len);
B
Behdad Esfahbod 已提交
224

225 226
    obj->head = tail;
    obj->tail = tail + len;
B
Behdad Esfahbod 已提交
227

228
    packed.push (obj);
B
Behdad Esfahbod 已提交
229 230 231 232

    if (unlikely (packed.in_error ()))
      return 0;

B
Behdad Esfahbod 已提交
233
    objidx = packed.length - 1;
234

235
    packed_map.set (obj, objidx);
236 237

    return objidx;
238 239
  }

B
Behdad Esfahbod 已提交
240
  void revert (range_t snap)
241 242 243 244 245 246
  {
    assert (snap.head <= head);
    assert (tail <= snap.tail);
    head = snap.head;
    tail = snap.tail;
    discard_stale_objects ();
247 248
  }

249 250 251
  void discard_stale_objects ()
  {
    while (packed.length > 1 &&
252
	   packed.tail ()->head < tail)
253
    {
254 255 256
      packed_map.del (packed.tail ());
      assert (!packed.tail ()->next);
      packed.tail ()->fini ();
257
      packed.pop ();
258
    }
259 260
    if (packed.length > 1)
      assert (packed.tail ()->head == tail);
261 262
  }

263
  template <typename T>
264
  void add_link (T &ofs, objidx_t objidx, const void *base = nullptr)
265
  {
266 267
    static_assert (sizeof (T) == 2 || sizeof (T) == 4, "");

B
Behdad Esfahbod 已提交
268
    if (!objidx)
269 270
      return;

271 272
    assert (current);
    assert (current->head <= (const char *) &ofs);
273

274
    if (!base)
275
      base = current->head;
276
    else
277
      assert (current->head <= (const char *) base);
278

279
    auto& link = *current->links.push ();
280
    link.is_wide = sizeof (T) == 4;
281
    link.position = (const char *) &ofs - current->head;
282
    link.bias = (const char *) base - current->head;
283
    link.objidx = objidx;
284 285
  }

286
  void resolve_links ()
287
  {
B
Behdad Esfahbod 已提交
288 289
    if (unlikely (in_error ())) return;

290
    assert (!current);
B
Behdad Esfahbod 已提交
291
    assert (packed.length > 1);
B
Behdad Esfahbod 已提交
292

B
Minor  
Behdad Esfahbod 已提交
293
    for (const object_t* parent : ++hb_iter (packed))
B
Behdad Esfahbod 已提交
294
    {
295
      for (const object_t::link_t &link : parent->links)
B
Behdad Esfahbod 已提交
296
      {
B
Minor  
Behdad Esfahbod 已提交
297
	const object_t* child = packed[link.objidx];
298
	assert (link.bias <= (size_t) (parent->tail - parent->head));
B
Minor  
Behdad Esfahbod 已提交
299
	unsigned offset = (child->head - parent->head) - link.bias;
B
Behdad Esfahbod 已提交
300

301
	if (link.is_wide)
B
Behdad Esfahbod 已提交
302
	{
303
	  auto &off = * ((BEInt<uint32_t, 4> *) (parent->head + link.position));
304
	  assert (0 == off);
305
	  check_assign (off, offset);
B
Behdad Esfahbod 已提交
306 307 308
	}
	else
	{
309
	  auto &off = * ((BEInt<uint16_t, 2> *) (parent->head + link.position));
310
	  assert (0 == off);
311
	  check_assign (off, offset);
B
Behdad Esfahbod 已提交
312 313 314
	}
      }
    }
315 316
  }

317
  unsigned int length () const { return this->head - current->head; }
318 319 320 321 322 323 324 325

  void align (unsigned int alignment)
  {
    unsigned int l = length () % alignment;
    if (l)
      allocate_size<void> (alignment - l);
  }

B
Behdad Esfahbod 已提交
326
  template <typename Type = void>
327 328
  Type *start_embed (const Type *obj HB_UNUSED = nullptr) const
  { return reinterpret_cast<Type *> (this->head); }
329
  template <typename Type>
330 331
  Type *start_embed (const Type &obj) const
  { return start_embed (hb_addressof (obj)); }
332

333
  /* Following two functions exist to allow setting breakpoint on. */
B
Behdad Esfahbod 已提交
334 335
  void err_ran_out_of_room () { this->ran_out_of_room = true; }
  void err_other_error () { this->successful = false; }
336

337 338 339
  template <typename Type>
  Type *allocate_size (unsigned int size)
  {
340 341
    if (unlikely (!this->successful)) return nullptr;

B
Behdad Esfahbod 已提交
342
    if (this->tail - this->head < ptrdiff_t (size))
343
    {
344
      err_ran_out_of_room ();
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
      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>
361
  Type *embed (const Type *obj)
362
  {
363
    unsigned int size = obj->get_size ();
364 365
    Type *ret = this->allocate_size<Type> (size);
    if (unlikely (!ret)) return nullptr;
366
    memcpy (ret, obj, size);
367 368
    return ret;
  }
369 370
  template <typename Type>
  Type *embed (const Type &obj)
B
Behdad Esfahbod 已提交
371
  { return embed (hb_addressof (obj)); }
B
Behdad Esfahbod 已提交
372

373
  template <typename Type, typename ...Ts> auto
B
Behdad Esfahbod 已提交
374
  _copy (const Type &src, hb_priority<1>, Ts&&... ds) HB_RETURN
375
  (Type *, src.copy (this, hb_forward<Ts> (ds)...))
B
Behdad Esfahbod 已提交
376 377

  template <typename Type> auto
378
  _copy (const Type &src, hb_priority<0>) -> decltype (&(hb_declval<Type> () = src))
B
Behdad Esfahbod 已提交
379 380 381
  {
    Type *ret = this->allocate_size<Type> (sizeof (Type));
    if (unlikely (!ret)) return nullptr;
382
    *ret = src;
B
Behdad Esfahbod 已提交
383 384 385 386 387
    return ret;
  }

  /* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data
   * instead of memcpy(). */
388
  template <typename Type, typename ...Ts>
B
Behdad Esfahbod 已提交
389
  Type *copy (const Type &src, Ts&&... ds)
390
  { return _copy (src, hb_prioritize, hb_forward<Ts> (ds)...); }
391
  template <typename Type, typename ...Ts>
B
Behdad Esfahbod 已提交
392
  Type *copy (const Type *src, Ts&&... ds)
393
  { return copy (*src, hb_forward<Ts> (ds)...); }
B
Behdad Esfahbod 已提交
394

395
  template <typename Type>
B
Minor  
Behdad Esfahbod 已提交
396
  hb_serialize_context_t& operator << (const Type &obj) & { embed (obj); return *this; }
397 398

  template <typename Type>
399
  Type *extend_size (Type *obj, unsigned int size)
400
  {
401 402 403 404 405
    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);
406
  }
407 408 409
  template <typename Type>
  Type *extend_size (Type &obj, unsigned int size)
  { return extend_size (hb_addressof (obj), size); }
410 411

  template <typename Type>
412 413 414
  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)); }
415

416
  template <typename Type, typename ...Ts>
B
Behdad Esfahbod 已提交
417
  Type *extend (Type *obj, Ts&&... ds)
418 419
  { return extend_size (obj, obj->get_size (hb_forward<Ts> (ds)...)); }
  template <typename Type, typename ...Ts>
B
Behdad Esfahbod 已提交
420
  Type *extend (Type &obj, Ts&&... ds)
421
  { return extend (hb_addressof (obj), hb_forward<Ts> (ds)...); }
422 423 424 425 426

  /* Output routines. */
  hb_bytes_t copy_bytes () const
  {
    assert (this->successful);
427 428 429 430
    /* 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);
431
    if (p)
432 433 434 435
    {
      memcpy (p, this->start, this->head - this->start);
      memcpy (p + (this->head - this->start), this->tail, this->end - this->tail);
    }
436 437
    else
      return hb_bytes_t ();
438
    return hb_bytes_t (p, len);
439
  }
B
Behdad Esfahbod 已提交
440 441 442
  template <typename Type>
  Type *copy () const
  { return reinterpret_cast<Type *> ((char *) copy_bytes ().arrayZ); }
443 444
  hb_blob_t *copy_blob () const
  {
B
Behdad Esfahbod 已提交
445 446 447 448
    hb_bytes_t b = copy_bytes ();
    return hb_blob_create (b.arrayZ, b.length,
			   HB_MEMORY_MODE_WRITABLE,
			   (char *) b.arrayZ, free);
449 450
  }

451
  public: /* TODO Make private. */
B
Behdad Esfahbod 已提交
452
  char *start, *head, *tail, *end;
453 454
  unsigned int debug_depth;
  bool successful;
455
  bool ran_out_of_room;
456 457 458

  private:

459 460 461
  /* Object memory pool. */
  hb_pool_t<object_t> object_pool;

B
Behdad Esfahbod 已提交
462
  /* Stack of currently under construction objects. */
463
  object_t *current;
B
Behdad Esfahbod 已提交
464

465
  /* Stack of packed objects.  Object 0 is always nil object. */
466
  hb_vector_t<object_t *> packed;
467

B
Behdad Esfahbod 已提交
468
  /* Map view of packed objects. */
469
  hb_hashmap_t<const object_t *, objidx_t, nullptr, 0> packed_map;
470 471 472 473
};


#endif /* HB_SERIALIZE_HH */