gfx/harfbuzz/src/hb-private.hh

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright © 2007,2008,2009 Red Hat, Inc.
michael@0 3 * Copyright © 2011,2012 Google, Inc.
michael@0 4 *
michael@0 5 * This is part of HarfBuzz, a text shaping library.
michael@0 6 *
michael@0 7 * Permission is hereby granted, without written agreement and without
michael@0 8 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 9 * software and its documentation for any purpose, provided that the
michael@0 10 * above copyright notice and the following two paragraphs appear in
michael@0 11 * all copies of this software.
michael@0 12 *
michael@0 13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 17 * DAMAGE.
michael@0 18 *
michael@0 19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 24 *
michael@0 25 * Red Hat Author(s): Behdad Esfahbod
michael@0 26 * Google Author(s): Behdad Esfahbod
michael@0 27 */
michael@0 28
michael@0 29 #ifndef HB_PRIVATE_HH
michael@0 30 #define HB_PRIVATE_HH
michael@0 31
michael@0 32 #ifdef HAVE_CONFIG_H
michael@0 33 #include "config.h"
michael@0 34 #endif
michael@0 35
michael@0 36 #include "hb.h"
michael@0 37 #define HB_H_IN
michael@0 38 #ifdef HAVE_OT
michael@0 39 #include "hb-ot.h"
michael@0 40 #define HB_OT_H_IN
michael@0 41 #endif
michael@0 42
michael@0 43 #include <stdlib.h>
michael@0 44 #include <stddef.h>
michael@0 45 #include <string.h>
michael@0 46 #include <assert.h>
michael@0 47
michael@0 48 /* We only use these two for debug output. However, the debug code is
michael@0 49 * always seen by the compiler (and optimized out in non-debug builds.
michael@0 50 * If including these becomes a problem, we can start thinking about
michael@0 51 * someway around that. */
michael@0 52 #include <stdio.h>
michael@0 53 #include <errno.h>
michael@0 54 #include <stdarg.h>
michael@0 55
michael@0 56
michael@0 57
michael@0 58 /* Essentials */
michael@0 59
michael@0 60 #ifndef NULL
michael@0 61 # define NULL ((void *) 0)
michael@0 62 #endif
michael@0 63
michael@0 64
michael@0 65 /* Void! */
michael@0 66 struct _hb_void_t {};
michael@0 67 typedef const _hb_void_t &hb_void_t;
michael@0 68 #define HB_VOID (* (const _hb_void_t *) NULL)
michael@0 69
michael@0 70
michael@0 71 /* Basics */
michael@0 72
michael@0 73
michael@0 74 #undef MIN
michael@0 75 template <typename Type>
michael@0 76 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
michael@0 77
michael@0 78 #undef MAX
michael@0 79 template <typename Type>
michael@0 80 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
michael@0 81
michael@0 82 static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
michael@0 83 { return (a + (b - 1)) / b; }
michael@0 84
michael@0 85
michael@0 86 #undef ARRAY_LENGTH
michael@0 87 template <typename Type, unsigned int n>
michael@0 88 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
michael@0 89 /* A const version, but does not detect erratically being called on pointers. */
michael@0 90 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
michael@0 91
michael@0 92 #define HB_STMT_START do
michael@0 93 #define HB_STMT_END while (0)
michael@0 94
michael@0 95 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
michael@0 96 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
michael@0 97 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
michael@0 98
michael@0 99 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
michael@0 100 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
michael@0 101
michael@0 102 #define _PASTE1(a,b) a##b
michael@0 103 #define PASTE(a,b) _PASTE1(a,b)
michael@0 104
michael@0 105 /* Lets assert int types. Saves trouble down the road. */
michael@0 106
michael@0 107 ASSERT_STATIC (sizeof (int8_t) == 1);
michael@0 108 ASSERT_STATIC (sizeof (uint8_t) == 1);
michael@0 109 ASSERT_STATIC (sizeof (int16_t) == 2);
michael@0 110 ASSERT_STATIC (sizeof (uint16_t) == 2);
michael@0 111 ASSERT_STATIC (sizeof (int32_t) == 4);
michael@0 112 ASSERT_STATIC (sizeof (uint32_t) == 4);
michael@0 113 ASSERT_STATIC (sizeof (int64_t) == 8);
michael@0 114 ASSERT_STATIC (sizeof (uint64_t) == 8);
michael@0 115
michael@0 116 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
michael@0 117 ASSERT_STATIC (sizeof (hb_position_t) == 4);
michael@0 118 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
michael@0 119 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
michael@0 120
michael@0 121
michael@0 122 /* We like our types POD */
michael@0 123
michael@0 124 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
michael@0 125 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
michael@0 126 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
michael@0 127
michael@0 128 #ifdef __GNUC__
michael@0 129 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
michael@0 130 HB_STMT_START { \
michael@0 131 typedef __typeof__(_instance) _type_##_line; \
michael@0 132 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
michael@0 133 } HB_STMT_END
michael@0 134 #else
michael@0 135 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
michael@0 136 #endif
michael@0 137 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
michael@0 138 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
michael@0 139
michael@0 140 /* Check _assertion in a method environment */
michael@0 141 #define _ASSERT_POD1(_line) \
michael@0 142 inline void _static_assertion_on_line_##_line (void) const \
michael@0 143 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
michael@0 144 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
michael@0 145 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
michael@0 146
michael@0 147
michael@0 148
michael@0 149 /* Misc */
michael@0 150
michael@0 151
michael@0 152 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
michael@0 153 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
michael@0 154 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
michael@0 155 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
michael@0 156 #else
michael@0 157 #define likely(expr) (expr)
michael@0 158 #define unlikely(expr) (expr)
michael@0 159 #endif
michael@0 160
michael@0 161 #ifndef __GNUC__
michael@0 162 #undef __attribute__
michael@0 163 #define __attribute__(x)
michael@0 164 #endif
michael@0 165
michael@0 166 #if __GNUC__ >= 3
michael@0 167 #define HB_PURE_FUNC __attribute__((pure))
michael@0 168 #define HB_CONST_FUNC __attribute__((const))
michael@0 169 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
michael@0 170 #else
michael@0 171 #define HB_PURE_FUNC
michael@0 172 #define HB_CONST_FUNC
michael@0 173 #define HB_PRINTF_FUNC(format_idx, arg_idx)
michael@0 174 #endif
michael@0 175 #if __GNUC__ >= 4
michael@0 176 #define HB_UNUSED __attribute__((unused))
michael@0 177 #else
michael@0 178 #define HB_UNUSED
michael@0 179 #endif
michael@0 180
michael@0 181 #ifndef HB_INTERNAL
michael@0 182 # ifndef __MINGW32__
michael@0 183 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
michael@0 184 # else
michael@0 185 # define HB_INTERNAL
michael@0 186 # endif
michael@0 187 #endif
michael@0 188
michael@0 189
michael@0 190 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
michael@0 191 #define snprintf _snprintf
michael@0 192 #endif
michael@0 193
michael@0 194 #ifdef _MSC_VER
michael@0 195 #undef inline
michael@0 196 #define inline __inline
michael@0 197 #endif
michael@0 198
michael@0 199 #ifdef __STRICT_ANSI__
michael@0 200 #undef inline
michael@0 201 #define inline __inline__
michael@0 202 #endif
michael@0 203
michael@0 204
michael@0 205 #if __GNUC__ >= 3
michael@0 206 #define HB_FUNC __PRETTY_FUNCTION__
michael@0 207 #elif defined(_MSC_VER)
michael@0 208 #define HB_FUNC __FUNCSIG__
michael@0 209 #else
michael@0 210 #define HB_FUNC __func__
michael@0 211 #endif
michael@0 212
michael@0 213
michael@0 214 /* Return the number of 1 bits in mask. */
michael@0 215 static inline HB_CONST_FUNC unsigned int
michael@0 216 _hb_popcount32 (uint32_t mask)
michael@0 217 {
michael@0 218 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
michael@0 219 return __builtin_popcount (mask);
michael@0 220 #else
michael@0 221 /* "HACKMEM 169" */
michael@0 222 register uint32_t y;
michael@0 223 y = (mask >> 1) &033333333333;
michael@0 224 y = mask - y - ((y >>1) & 033333333333);
michael@0 225 return (((y + (y >> 3)) & 030707070707) % 077);
michael@0 226 #endif
michael@0 227 }
michael@0 228
michael@0 229 /* Returns the number of bits needed to store number */
michael@0 230 static inline HB_CONST_FUNC unsigned int
michael@0 231 _hb_bit_storage (unsigned int number)
michael@0 232 {
michael@0 233 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
michael@0 234 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
michael@0 235 #else
michael@0 236 register unsigned int n_bits = 0;
michael@0 237 while (number) {
michael@0 238 n_bits++;
michael@0 239 number >>= 1;
michael@0 240 }
michael@0 241 return n_bits;
michael@0 242 #endif
michael@0 243 }
michael@0 244
michael@0 245 /* Returns the number of zero bits in the least significant side of number */
michael@0 246 static inline HB_CONST_FUNC unsigned int
michael@0 247 _hb_ctz (unsigned int number)
michael@0 248 {
michael@0 249 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
michael@0 250 return likely (number) ? __builtin_ctz (number) : 0;
michael@0 251 #else
michael@0 252 register unsigned int n_bits = 0;
michael@0 253 if (unlikely (!number)) return 0;
michael@0 254 while (!(number & 1)) {
michael@0 255 n_bits++;
michael@0 256 number >>= 1;
michael@0 257 }
michael@0 258 return n_bits;
michael@0 259 #endif
michael@0 260 }
michael@0 261
michael@0 262 static inline bool
michael@0 263 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
michael@0 264 {
michael@0 265 return (size > 0) && (count >= ((unsigned int) -1) / size);
michael@0 266 }
michael@0 267
michael@0 268
michael@0 269 /* Type of bsearch() / qsort() compare function */
michael@0 270 typedef int (*hb_compare_func_t) (const void *, const void *);
michael@0 271
michael@0 272
michael@0 273
michael@0 274
michael@0 275 /* arrays and maps */
michael@0 276
michael@0 277
michael@0 278 #define HB_PREALLOCED_ARRAY_INIT {0}
michael@0 279 template <typename Type, unsigned int StaticSize>
michael@0 280 struct hb_prealloced_array_t
michael@0 281 {
michael@0 282 unsigned int len;
michael@0 283 unsigned int allocated;
michael@0 284 Type *array;
michael@0 285 Type static_array[StaticSize];
michael@0 286
michael@0 287 void init (void) { memset (this, 0, sizeof (*this)); }
michael@0 288
michael@0 289 inline Type& operator [] (unsigned int i) { return array[i]; }
michael@0 290 inline const Type& operator [] (unsigned int i) const { return array[i]; }
michael@0 291
michael@0 292 inline Type *push (void)
michael@0 293 {
michael@0 294 if (!array) {
michael@0 295 array = static_array;
michael@0 296 allocated = ARRAY_LENGTH (static_array);
michael@0 297 }
michael@0 298 if (likely (len < allocated))
michael@0 299 return &array[len++];
michael@0 300
michael@0 301 /* Need to reallocate */
michael@0 302 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
michael@0 303 Type *new_array = NULL;
michael@0 304
michael@0 305 if (array == static_array) {
michael@0 306 new_array = (Type *) calloc (new_allocated, sizeof (Type));
michael@0 307 if (new_array)
michael@0 308 memcpy (new_array, array, len * sizeof (Type));
michael@0 309 } else {
michael@0 310 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
michael@0 311 if (likely (!overflows)) {
michael@0 312 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
michael@0 313 }
michael@0 314 }
michael@0 315
michael@0 316 if (unlikely (!new_array))
michael@0 317 return NULL;
michael@0 318
michael@0 319 array = new_array;
michael@0 320 allocated = new_allocated;
michael@0 321 return &array[len++];
michael@0 322 }
michael@0 323
michael@0 324 inline void pop (void)
michael@0 325 {
michael@0 326 len--;
michael@0 327 }
michael@0 328
michael@0 329 inline void remove (unsigned int i)
michael@0 330 {
michael@0 331 if (unlikely (i >= len))
michael@0 332 return;
michael@0 333 memmove (static_cast<void *> (&array[i]),
michael@0 334 static_cast<void *> (&array[i + 1]),
michael@0 335 (len - i - 1) * sizeof (Type));
michael@0 336 len--;
michael@0 337 }
michael@0 338
michael@0 339 inline void shrink (unsigned int l)
michael@0 340 {
michael@0 341 if (l < len)
michael@0 342 len = l;
michael@0 343 }
michael@0 344
michael@0 345 template <typename T>
michael@0 346 inline Type *find (T v) {
michael@0 347 for (unsigned int i = 0; i < len; i++)
michael@0 348 if (array[i] == v)
michael@0 349 return &array[i];
michael@0 350 return NULL;
michael@0 351 }
michael@0 352 template <typename T>
michael@0 353 inline const Type *find (T v) const {
michael@0 354 for (unsigned int i = 0; i < len; i++)
michael@0 355 if (array[i] == v)
michael@0 356 return &array[i];
michael@0 357 return NULL;
michael@0 358 }
michael@0 359
michael@0 360 inline void sort (void)
michael@0 361 {
michael@0 362 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
michael@0 363 }
michael@0 364
michael@0 365 inline void sort (unsigned int start, unsigned int end)
michael@0 366 {
michael@0 367 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
michael@0 368 }
michael@0 369
michael@0 370 template <typename T>
michael@0 371 inline Type *bsearch (T *key)
michael@0 372 {
michael@0 373 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
michael@0 374 }
michael@0 375 template <typename T>
michael@0 376 inline const Type *bsearch (T *key) const
michael@0 377 {
michael@0 378 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
michael@0 379 }
michael@0 380
michael@0 381 inline void finish (void)
michael@0 382 {
michael@0 383 if (array != static_array)
michael@0 384 free (array);
michael@0 385 array = NULL;
michael@0 386 allocated = len = 0;
michael@0 387 }
michael@0 388 };
michael@0 389
michael@0 390 #define HB_AUTO_ARRAY_PREALLOCED 16
michael@0 391 template <typename Type>
michael@0 392 struct hb_auto_array_t : hb_prealloced_array_t <Type, HB_AUTO_ARRAY_PREALLOCED>
michael@0 393 {
michael@0 394 hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::init (); }
michael@0 395 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::finish (); }
michael@0 396 };
michael@0 397
michael@0 398
michael@0 399 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
michael@0 400 template <typename item_t, typename lock_t>
michael@0 401 struct hb_lockable_set_t
michael@0 402 {
michael@0 403 hb_prealloced_array_t <item_t, 2> items;
michael@0 404
michael@0 405 inline void init (void) { items.init (); }
michael@0 406
michael@0 407 template <typename T>
michael@0 408 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
michael@0 409 {
michael@0 410 l.lock ();
michael@0 411 item_t *item = items.find (v);
michael@0 412 if (item) {
michael@0 413 if (replace) {
michael@0 414 item_t old = *item;
michael@0 415 *item = v;
michael@0 416 l.unlock ();
michael@0 417 old.finish ();
michael@0 418 }
michael@0 419 else {
michael@0 420 item = NULL;
michael@0 421 l.unlock ();
michael@0 422 }
michael@0 423 } else {
michael@0 424 item = items.push ();
michael@0 425 if (likely (item))
michael@0 426 *item = v;
michael@0 427 l.unlock ();
michael@0 428 }
michael@0 429 return item;
michael@0 430 }
michael@0 431
michael@0 432 template <typename T>
michael@0 433 inline void remove (T v, lock_t &l)
michael@0 434 {
michael@0 435 l.lock ();
michael@0 436 item_t *item = items.find (v);
michael@0 437 if (item) {
michael@0 438 item_t old = *item;
michael@0 439 *item = items[items.len - 1];
michael@0 440 items.pop ();
michael@0 441 l.unlock ();
michael@0 442 old.finish ();
michael@0 443 } else {
michael@0 444 l.unlock ();
michael@0 445 }
michael@0 446 }
michael@0 447
michael@0 448 template <typename T>
michael@0 449 inline bool find (T v, item_t *i, lock_t &l)
michael@0 450 {
michael@0 451 l.lock ();
michael@0 452 item_t *item = items.find (v);
michael@0 453 if (item)
michael@0 454 *i = *item;
michael@0 455 l.unlock ();
michael@0 456 return !!item;
michael@0 457 }
michael@0 458
michael@0 459 template <typename T>
michael@0 460 inline item_t *find_or_insert (T v, lock_t &l)
michael@0 461 {
michael@0 462 l.lock ();
michael@0 463 item_t *item = items.find (v);
michael@0 464 if (!item) {
michael@0 465 item = items.push ();
michael@0 466 if (likely (item))
michael@0 467 *item = v;
michael@0 468 }
michael@0 469 l.unlock ();
michael@0 470 return item;
michael@0 471 }
michael@0 472
michael@0 473 inline void finish (lock_t &l)
michael@0 474 {
michael@0 475 if (!items.len) {
michael@0 476 /* No need for locking. */
michael@0 477 items.finish ();
michael@0 478 return;
michael@0 479 }
michael@0 480 l.lock ();
michael@0 481 while (items.len) {
michael@0 482 item_t old = items[items.len - 1];
michael@0 483 items.pop ();
michael@0 484 l.unlock ();
michael@0 485 old.finish ();
michael@0 486 l.lock ();
michael@0 487 }
michael@0 488 items.finish ();
michael@0 489 l.unlock ();
michael@0 490 }
michael@0 491
michael@0 492 };
michael@0 493
michael@0 494
michael@0 495
michael@0 496
michael@0 497 /* Big-endian handling */
michael@0 498
michael@0 499 static inline uint16_t hb_be_uint16 (const uint16_t v)
michael@0 500 {
michael@0 501 const uint8_t *V = (const uint8_t *) &v;
michael@0 502 return (V[0] << 8) | V[1];
michael@0 503 }
michael@0 504
michael@0 505 static inline uint16_t hb_uint16_swap (const uint16_t v)
michael@0 506 {
michael@0 507 return (v >> 8) | (v << 8);
michael@0 508 }
michael@0 509
michael@0 510 static inline uint32_t hb_uint32_swap (const uint32_t v)
michael@0 511 {
michael@0 512 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
michael@0 513 }
michael@0 514
michael@0 515 /* Note, of the following macros, uint16_get is the one called many many times.
michael@0 516 * If there is any optimizations to be done, it's in that macro. However, I
michael@0 517 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
michael@0 518 * results in a single ror instruction, does NOT speed this up. In fact, it
michael@0 519 * resulted in a minor slowdown. At any rate, note that v may not be correctly
michael@0 520 * aligned, so I think the current implementation is optimal.
michael@0 521 */
michael@0 522
michael@0 523 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
michael@0 524 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
michael@0 525 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
michael@0 526
michael@0 527 #define hb_be_uint32_put(v,V) HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
michael@0 528 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
michael@0 529 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
michael@0 530
michael@0 531 #define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END
michael@0 532 #define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2])
michael@0 533 #define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
michael@0 534
michael@0 535
michael@0 536 /* ASCII tag/character handling */
michael@0 537
michael@0 538 static inline bool ISALPHA (unsigned char c)
michael@0 539 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
michael@0 540 static inline bool ISALNUM (unsigned char c)
michael@0 541 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
michael@0 542 static inline bool ISSPACE (unsigned char c)
michael@0 543 { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
michael@0 544 static inline unsigned char TOUPPER (unsigned char c)
michael@0 545 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
michael@0 546 static inline unsigned char TOLOWER (unsigned char c)
michael@0 547 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
michael@0 548
michael@0 549 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
michael@0 550 ((const char *) s)[1], \
michael@0 551 ((const char *) s)[2], \
michael@0 552 ((const char *) s)[3]))
michael@0 553
michael@0 554
michael@0 555 /* C++ helpers */
michael@0 556
michael@0 557 /* Makes class uncopyable. Use in private: section. */
michael@0 558 #define NO_COPY(T) \
michael@0 559 T (const T &o); \
michael@0 560 T &operator = (const T &o)
michael@0 561
michael@0 562
michael@0 563 /* Debug */
michael@0 564
michael@0 565
michael@0 566 #ifndef HB_DEBUG
michael@0 567 #define HB_DEBUG 0
michael@0 568 #endif
michael@0 569
michael@0 570 static inline bool
michael@0 571 _hb_debug (unsigned int level,
michael@0 572 unsigned int max_level)
michael@0 573 {
michael@0 574 return level < max_level;
michael@0 575 }
michael@0 576
michael@0 577 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
michael@0 578 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
michael@0 579
michael@0 580 template <int max_level> static inline void
michael@0 581 _hb_debug_msg_va (const char *what,
michael@0 582 const void *obj,
michael@0 583 const char *func,
michael@0 584 bool indented,
michael@0 585 unsigned int level,
michael@0 586 int level_dir,
michael@0 587 const char *message,
michael@0 588 va_list ap)
michael@0 589 {
michael@0 590 if (!_hb_debug (level, max_level))
michael@0 591 return;
michael@0 592
michael@0 593 fprintf (stderr, "%-10s", what ? what : "");
michael@0 594
michael@0 595 if (obj)
michael@0 596 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
michael@0 597 else
michael@0 598 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
michael@0 599
michael@0 600 if (indented) {
michael@0 601 /* One may want to add ASCII version of these. See:
michael@0 602 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
michael@0 603 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
michael@0 604 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
michael@0 605 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
michael@0 606 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
michael@0 607 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
michael@0 608 static const char bars[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
michael@0 609 fprintf (stderr, "%2u %s" VRBAR "%s",
michael@0 610 level,
michael@0 611 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
michael@0 612 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
michael@0 613 } else
michael@0 614 fprintf (stderr, " " VRBAR LBAR);
michael@0 615
michael@0 616 if (func)
michael@0 617 {
michael@0 618 unsigned int func_len = strlen (func);
michael@0 619 #ifndef HB_DEBUG_VERBOSE
michael@0 620 /* Skip "typename" */
michael@0 621 if (0 == strncmp (func, "typename ", 9))
michael@0 622 func += 9;
michael@0 623 /* Skip return type */
michael@0 624 const char *space = strchr (func, ' ');
michael@0 625 if (space)
michael@0 626 func = space + 1;
michael@0 627 /* Skip parameter list */
michael@0 628 const char *paren = strchr (func, '(');
michael@0 629 if (paren)
michael@0 630 func_len = paren - func;
michael@0 631 #endif
michael@0 632 fprintf (stderr, "%.*s: ", func_len, func);
michael@0 633 }
michael@0 634
michael@0 635 if (message)
michael@0 636 vfprintf (stderr, message, ap);
michael@0 637
michael@0 638 fprintf (stderr, "\n");
michael@0 639 }
michael@0 640 template <> inline void
michael@0 641 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
michael@0 642 const void *obj HB_UNUSED,
michael@0 643 const char *func HB_UNUSED,
michael@0 644 bool indented HB_UNUSED,
michael@0 645 unsigned int level HB_UNUSED,
michael@0 646 int level_dir HB_UNUSED,
michael@0 647 const char *message HB_UNUSED,
michael@0 648 va_list ap HB_UNUSED) {}
michael@0 649
michael@0 650 template <int max_level> static inline void
michael@0 651 _hb_debug_msg (const char *what,
michael@0 652 const void *obj,
michael@0 653 const char *func,
michael@0 654 bool indented,
michael@0 655 unsigned int level,
michael@0 656 int level_dir,
michael@0 657 const char *message,
michael@0 658 ...) HB_PRINTF_FUNC(7, 8);
michael@0 659 template <int max_level> static inline void
michael@0 660 _hb_debug_msg (const char *what,
michael@0 661 const void *obj,
michael@0 662 const char *func,
michael@0 663 bool indented,
michael@0 664 unsigned int level,
michael@0 665 int level_dir,
michael@0 666 const char *message,
michael@0 667 ...)
michael@0 668 {
michael@0 669 va_list ap;
michael@0 670 va_start (ap, message);
michael@0 671 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
michael@0 672 va_end (ap);
michael@0 673 }
michael@0 674 template <> inline void
michael@0 675 _hb_debug_msg<0> (const char *what HB_UNUSED,
michael@0 676 const void *obj HB_UNUSED,
michael@0 677 const char *func HB_UNUSED,
michael@0 678 bool indented HB_UNUSED,
michael@0 679 unsigned int level HB_UNUSED,
michael@0 680 int level_dir HB_UNUSED,
michael@0 681 const char *message HB_UNUSED,
michael@0 682 ...) HB_PRINTF_FUNC(7, 8);
michael@0 683 template <> inline void
michael@0 684 _hb_debug_msg<0> (const char *what HB_UNUSED,
michael@0 685 const void *obj HB_UNUSED,
michael@0 686 const char *func HB_UNUSED,
michael@0 687 bool indented HB_UNUSED,
michael@0 688 unsigned int level HB_UNUSED,
michael@0 689 int level_dir HB_UNUSED,
michael@0 690 const char *message HB_UNUSED,
michael@0 691 ...) {}
michael@0 692
michael@0 693 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
michael@0 694 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
michael@0 695 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
michael@0 696
michael@0 697
michael@0 698 /*
michael@0 699 * Printer
michael@0 700 */
michael@0 701
michael@0 702 template <typename T>
michael@0 703 struct hb_printer_t {};
michael@0 704
michael@0 705 template <>
michael@0 706 struct hb_printer_t<bool> {
michael@0 707 const char *print (bool v) { return v ? "true" : "false"; }
michael@0 708 };
michael@0 709
michael@0 710 template <>
michael@0 711 struct hb_printer_t<hb_void_t> {
michael@0 712 const char *print (hb_void_t) { return ""; }
michael@0 713 };
michael@0 714
michael@0 715
michael@0 716 /*
michael@0 717 * Trace
michael@0 718 */
michael@0 719
michael@0 720 template <typename T>
michael@0 721 static inline void _hb_warn_no_return (bool returned)
michael@0 722 {
michael@0 723 if (unlikely (!returned)) {
michael@0 724 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
michael@0 725 }
michael@0 726 }
michael@0 727 template <>
michael@0 728 inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
michael@0 729 {}
michael@0 730
michael@0 731 template <int max_level, typename ret_t>
michael@0 732 struct hb_auto_trace_t {
michael@0 733 explicit inline hb_auto_trace_t (unsigned int *plevel_,
michael@0 734 const char *what_,
michael@0 735 const void *obj_,
michael@0 736 const char *func,
michael@0 737 const char *message,
michael@0 738 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
michael@0 739 {
michael@0 740 if (plevel) ++*plevel;
michael@0 741
michael@0 742 va_list ap;
michael@0 743 va_start (ap, message);
michael@0 744 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
michael@0 745 va_end (ap);
michael@0 746 }
michael@0 747 inline ~hb_auto_trace_t (void)
michael@0 748 {
michael@0 749 _hb_warn_no_return<ret_t> (returned);
michael@0 750 if (!returned) {
michael@0 751 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
michael@0 752 }
michael@0 753 if (plevel) --*plevel;
michael@0 754 }
michael@0 755
michael@0 756 inline ret_t ret (ret_t v, unsigned int line = 0)
michael@0 757 {
michael@0 758 if (unlikely (returned)) {
michael@0 759 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
michael@0 760 return v;
michael@0 761 }
michael@0 762
michael@0 763 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
michael@0 764 "return %s (line %d)",
michael@0 765 hb_printer_t<ret_t>().print (v), line);
michael@0 766 if (plevel) --*plevel;
michael@0 767 plevel = NULL;
michael@0 768 returned = true;
michael@0 769 return v;
michael@0 770 }
michael@0 771
michael@0 772 private:
michael@0 773 unsigned int *plevel;
michael@0 774 const char *what;
michael@0 775 const void *obj;
michael@0 776 bool returned;
michael@0 777 };
michael@0 778 template <typename ret_t> /* Optimize when tracing is disabled */
michael@0 779 struct hb_auto_trace_t<0, ret_t> {
michael@0 780 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
michael@0 781 const char *what HB_UNUSED,
michael@0 782 const void *obj HB_UNUSED,
michael@0 783 const char *func HB_UNUSED,
michael@0 784 const char *message HB_UNUSED,
michael@0 785 ...) {}
michael@0 786
michael@0 787 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
michael@0 788 };
michael@0 789
michael@0 790 #define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
michael@0 791
michael@0 792 /* Misc */
michael@0 793
michael@0 794
michael@0 795 /* Pre-mature optimization:
michael@0 796 * Checks for lo <= u <= hi but with an optimization if lo and hi
michael@0 797 * are only different in a contiguous set of lower-most bits.
michael@0 798 */
michael@0 799 template <typename T> static inline bool
michael@0 800 hb_in_range (T u, T lo, T hi)
michael@0 801 {
michael@0 802 if ( ((lo^hi) & lo) == 0 &&
michael@0 803 ((lo^hi) & hi) == (lo^hi) &&
michael@0 804 ((lo^hi) & ((lo^hi) + 1)) == 0 )
michael@0 805 return (u & ~(lo^hi)) == lo;
michael@0 806 else
michael@0 807 return lo <= u && u <= hi;
michael@0 808 }
michael@0 809
michael@0 810 template <typename T> static inline bool
michael@0 811 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
michael@0 812 {
michael@0 813 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
michael@0 814 }
michael@0 815
michael@0 816 template <typename T> static inline bool
michael@0 817 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
michael@0 818 {
michael@0 819 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
michael@0 820 }
michael@0 821
michael@0 822
michael@0 823 /* Useful for set-operations on small enums.
michael@0 824 * For example, for testing "x ∈ {x1, x2, x3}" use:
michael@0 825 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
michael@0 826 */
michael@0 827 #define FLAG(x) (1<<(x))
michael@0 828 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
michael@0 829
michael@0 830
michael@0 831 template <typename T, typename T2> inline void
michael@0 832 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
michael@0 833 {
michael@0 834 if (unlikely (!len))
michael@0 835 return;
michael@0 836
michael@0 837 unsigned int k = len - 1;
michael@0 838 do {
michael@0 839 unsigned int new_k = 0;
michael@0 840
michael@0 841 for (unsigned int j = 0; j < k; j++)
michael@0 842 if (compar (&array[j], &array[j+1]) > 0)
michael@0 843 {
michael@0 844 {
michael@0 845 T t;
michael@0 846 t = array[j];
michael@0 847 array[j] = array[j + 1];
michael@0 848 array[j + 1] = t;
michael@0 849 }
michael@0 850 if (array2)
michael@0 851 {
michael@0 852 T2 t;
michael@0 853 t = array2[j];
michael@0 854 array2[j] = array2[j + 1];
michael@0 855 array2[j + 1] = t;
michael@0 856 }
michael@0 857
michael@0 858 new_k = j;
michael@0 859 }
michael@0 860 k = new_k;
michael@0 861 } while (k);
michael@0 862 }
michael@0 863
michael@0 864 template <typename T> inline void
michael@0 865 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
michael@0 866 {
michael@0 867 hb_bubble_sort (array, len, compar, (int *) NULL);
michael@0 868 }
michael@0 869
michael@0 870 static inline hb_bool_t
michael@0 871 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
michael@0 872 {
michael@0 873 /* Pain because we don't know whether s is nul-terminated. */
michael@0 874 char buf[64];
michael@0 875 len = MIN (ARRAY_LENGTH (buf) - 1, len);
michael@0 876 strncpy (buf, s, len);
michael@0 877 buf[len] = '\0';
michael@0 878
michael@0 879 char *end;
michael@0 880 errno = 0;
michael@0 881 unsigned long v = strtoul (buf, &end, base);
michael@0 882 if (errno) return false;
michael@0 883 if (*end) return false;
michael@0 884 *out = v;
michael@0 885 return true;
michael@0 886 }
michael@0 887
michael@0 888
michael@0 889 /* Global runtime options. */
michael@0 890
michael@0 891 struct hb_options_t
michael@0 892 {
michael@0 893 int initialized : 1;
michael@0 894 int uniscribe_bug_compatible : 1;
michael@0 895 };
michael@0 896
michael@0 897 union hb_options_union_t {
michael@0 898 int i;
michael@0 899 hb_options_t opts;
michael@0 900 };
michael@0 901 ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t));
michael@0 902
michael@0 903 HB_INTERNAL void
michael@0 904 _hb_options_init (void);
michael@0 905
michael@0 906 extern HB_INTERNAL hb_options_union_t _hb_options;
michael@0 907
michael@0 908 static inline hb_options_t
michael@0 909 hb_options (void)
michael@0 910 {
michael@0 911 if (unlikely (!_hb_options.i))
michael@0 912 _hb_options_init ();
michael@0 913
michael@0 914 return _hb_options.opts;
michael@0 915 }
michael@0 916
michael@0 917
michael@0 918 #endif /* HB_PRIVATE_HH */

mercurial