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