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 /* Compile-time custom allocator support. */ 58 59 #if defined(hb_malloc_impl) \ 60 && defined(hb_calloc_impl) \ 61 && defined(hb_realloc_impl) \ 62 && defined(hb_free_impl) 63 extern "C" void* hb_malloc_impl(size_t size); 64 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size); 65 extern "C" void* hb_realloc_impl(void *ptr, size_t size); 66 extern "C" void hb_free_impl(void *ptr); 67 #define malloc hb_malloc_impl 68 #define calloc hb_calloc_impl 69 #define realloc hb_realloc_impl 70 #define free hb_free_impl 71 #endif 72 73 74 /* Compiler attributes */ 75 76 77 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 78 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) 79 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) 80 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) 81 #else 82 #define likely(expr) (expr) 83 #define unlikely(expr) (expr) 84 #endif 85 86 #ifndef __GNUC__ 87 #undef __attribute__ 88 #define __attribute__(x) 89 #endif 90 91 #if __GNUC__ >= 3 92 #define HB_PURE_FUNC __attribute__((pure)) 93 #define HB_CONST_FUNC __attribute__((const)) 94 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) 95 #else 96 #define HB_PURE_FUNC 97 #define HB_CONST_FUNC 98 #define HB_PRINTF_FUNC(format_idx, arg_idx) 99 #endif 100 #if __GNUC__ >= 4 101 #define HB_UNUSED __attribute__((unused)) 102 #else 103 #define HB_UNUSED 104 #endif 105 106 #ifndef HB_INTERNAL 107 # if !defined(__MINGW32__) && !defined(__CYGWIN__) 108 # define HB_INTERNAL __attribute__((__visibility__("hidden"))) 109 # else 110 # define HB_INTERNAL 111 # endif 112 #endif 113 114 #if __GNUC__ >= 3 115 #define HB_FUNC __PRETTY_FUNCTION__ 116 #elif defined(_MSC_VER) 117 #define HB_FUNC __FUNCSIG__ 118 #else 119 #define HB_FUNC __func__ 120 #endif 121 122 #if defined(_WIN32) || defined(__CYGWIN__) 123 /* We need Windows Vista for both Uniscribe backend and for 124 * MemoryBarrier. We don't support compiling on Windows XP, 125 * though we run on it fine. */ 126 # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600 127 # undef _WIN32_WINNT 128 # endif 129 # ifndef _WIN32_WINNT 130 # define _WIN32_WINNT 0x0600 131 # endif 132 # ifndef WIN32_LEAN_AND_MEAN 133 # define WIN32_LEAN_AND_MEAN 1 134 # endif 135 # ifndef STRICT 136 # define STRICT 1 137 # endif 138 139 # if defined(_WIN32_WCE) 140 /* Some things not defined on Windows CE. */ 141 # define strdup _strdup 142 # define getenv(Name) NULL 143 # if _WIN32_WCE < 0x800 144 # define setlocale(Category, Locale) "C" 145 static int errno = 0; /* Use something better? */ 146 # endif 147 # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) 148 # define getenv(Name) NULL 149 # endif 150 # if defined(_MSC_VER) && _MSC_VER < 1900 151 # define snprintf _snprintf 152 # endif 153 #endif 154 155 #if HAVE_ATEXIT 156 /* atexit() is only safe to be called from shared libraries on certain 157 * platforms. Whitelist. 158 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */ 159 # if defined(__linux) && defined(__GLIBC_PREREQ) 160 # if __GLIBC_PREREQ(2,3) 161 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */ 162 # define HB_USE_ATEXIT 1 163 # endif 164 # elif defined(_MSC_VER) || defined(__MINGW32__) 165 /* For MSVC: 166 * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx 167 * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx 168 * mingw32 headers say atexit is safe to use in shared libraries. 169 */ 170 # define HB_USE_ATEXIT 1 171 # elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) 172 /* This was fixed in Android NKD r8 or r8b: 173 * https://code.google.com/p/android/issues/detail?id=6455 174 * which introduced GCC 4.6: 175 * https://developer.android.com/tools/sdk/ndk/index.html 176 */ 177 # define HB_USE_ATEXIT 1 178 # endif 179 #endif 180 181 /* Basics */ 182 183 184 #ifndef NULL 185 # define NULL ((void *) 0) 186 #endif 187 188 #undef MIN 189 template <typename Type> 190 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } 191 192 #undef MAX 193 template <typename Type> 194 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } 195 196 static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) 197 { return (a + (b - 1)) / b; } 198 199 200 #undef ARRAY_LENGTH 201 template <typename Type, unsigned int n> 202 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } 203 /* A const version, but does not detect erratically being called on pointers. */ 204 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) 205 206 #define HB_STMT_START do 207 #define HB_STMT_END while (0) 208 209 #define _ASSERT_STATIC1(_line, _cond) HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 210 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 211 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 212 213 /* Note: C++ allows sizeof() of variable-lengh arrays. So, if _cond is not 214 * constant, it still compiles (ouch!), but at least we'll get a -Wvla warning. */ 215 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) 216 217 #define _PASTE1(a,b) a##b 218 #define PASTE(a,b) _PASTE1(a,b) 219 220 /* Lets assert int types. Saves trouble down the road. */ 221 222 ASSERT_STATIC (sizeof (int8_t) == 1); 223 ASSERT_STATIC (sizeof (uint8_t) == 1); 224 ASSERT_STATIC (sizeof (int16_t) == 2); 225 ASSERT_STATIC (sizeof (uint16_t) == 2); 226 ASSERT_STATIC (sizeof (int32_t) == 4); 227 ASSERT_STATIC (sizeof (uint32_t) == 4); 228 ASSERT_STATIC (sizeof (int64_t) == 8); 229 ASSERT_STATIC (sizeof (uint64_t) == 8); 230 231 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4); 232 ASSERT_STATIC (sizeof (hb_position_t) == 4); 233 ASSERT_STATIC (sizeof (hb_mask_t) == 4); 234 ASSERT_STATIC (sizeof (hb_var_int_t) == 4); 235 236 237 /* We like our types POD */ 238 239 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } 240 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) 241 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) 242 243 #ifdef __GNUC__ 244 # define _ASSERT_INSTANCE_POD1(_line, _instance) \ 245 HB_STMT_START { \ 246 typedef __typeof__(_instance) _type_##_line; \ 247 _ASSERT_TYPE_POD1 (_line, _type_##_line); \ 248 } HB_STMT_END 249 #else 250 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested 251 #endif 252 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) 253 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) 254 255 /* Check _assertion in a method environment */ 256 #define _ASSERT_POD1(_line) \ 257 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \ 258 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } 259 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) 260 # define ASSERT_POD() _ASSERT_POD0 (__LINE__) 261 262 263 264 /* Misc */ 265 266 /* Void! */ 267 struct _hb_void_t {}; 268 typedef const _hb_void_t *hb_void_t; 269 #define HB_VOID ((const _hb_void_t *) NULL) 270 271 /* Return the number of 1 bits in mask. */ 272 static inline HB_CONST_FUNC unsigned int 273 _hb_popcount32 (uint32_t mask) 274 { 275 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 276 return __builtin_popcount (mask); 277 #else 278 /* "HACKMEM 169" */ 279 uint32_t y; 280 y = (mask >> 1) &033333333333; 281 y = mask - y - ((y >>1) & 033333333333); 282 return (((y + (y >> 3)) & 030707070707) % 077); 283 #endif 284 } 285 286 /* Returns the number of bits needed to store number */ 287 static inline HB_CONST_FUNC unsigned int 288 _hb_bit_storage (unsigned int number) 289 { 290 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 291 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; 292 #else 293 unsigned int n_bits = 0; 294 while (number) { 295 n_bits++; 296 number >>= 1; 297 } 298 return n_bits; 299 #endif 300 } 301 302 /* Returns the number of zero bits in the least significant side of number */ 303 static inline HB_CONST_FUNC unsigned int 304 _hb_ctz (unsigned int number) 305 { 306 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 307 return likely (number) ? __builtin_ctz (number) : 0; 308 #else 309 unsigned int n_bits = 0; 310 if (unlikely (!number)) return 0; 311 while (!(number & 1)) { 312 n_bits++; 313 number >>= 1; 314 } 315 return n_bits; 316 #endif 317 } 318 319 static inline bool 320 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) 321 { 322 return (size > 0) && (count >= ((unsigned int) -1) / size); 323 } 324 325 326 /* Type of bsearch() / qsort() compare function */ 327 typedef int (*hb_compare_func_t) (const void *, const void *); 328 329 330 331 332 /* arrays and maps */ 333 334 335 #define HB_PREALLOCED_ARRAY_INIT {0, 0, NULL} 336 template <typename Type, unsigned int StaticSize=16> 337 struct hb_prealloced_array_t 338 { 339 unsigned int len; 340 unsigned int allocated; 341 Type *array; 342 Type static_array[StaticSize]; 343 344 void init (void) { memset (this, 0, sizeof (*this)); } 345 346 inline Type& operator [] (unsigned int i) { return array[i]; } 347 inline const Type& operator [] (unsigned int i) const { return array[i]; } 348 349 inline Type *push (void) 350 { 351 if (!array) { 352 array = static_array; 353 allocated = ARRAY_LENGTH (static_array); 354 } 355 if (likely (len < allocated)) 356 return &array[len++]; 357 358 /* Need to reallocate */ 359 unsigned int new_allocated = allocated + (allocated >> 1) + 8; 360 Type *new_array = NULL; 361 362 if (array == static_array) { 363 new_array = (Type *) calloc (new_allocated, sizeof (Type)); 364 if (new_array) 365 memcpy (new_array, array, len * sizeof (Type)); 366 } else { 367 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); 368 if (likely (!overflows)) { 369 new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); 370 } 371 } 372 373 if (unlikely (!new_array)) 374 return NULL; 375 376 array = new_array; 377 allocated = new_allocated; 378 return &array[len++]; 379 } 380 381 inline void pop (void) 382 { 383 len--; 384 } 385 386 inline void remove (unsigned int i) 387 { 388 if (unlikely (i >= len)) 389 return; 390 memmove (static_cast<void *> (&array[i]), 391 static_cast<void *> (&array[i + 1]), 392 (len - i - 1) * sizeof (Type)); 393 len--; 394 } 395 396 inline void shrink (unsigned int l) 397 { 398 if (l < len) 399 len = l; 400 } 401 402 template <typename T> 403 inline Type *find (T v) { 404 for (unsigned int i = 0; i < len; i++) 405 if (array[i] == v) 406 return &array[i]; 407 return NULL; 408 } 409 template <typename T> 410 inline const Type *find (T v) const { 411 for (unsigned int i = 0; i < len; i++) 412 if (array[i] == v) 413 return &array[i]; 414 return NULL; 415 } 416 417 inline void qsort (void) 418 { 419 ::qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 420 } 421 422 inline void qsort (unsigned int start, unsigned int end) 423 { 424 ::qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); 425 } 426 427 template <typename T> 428 inline Type *bsearch (T *key) 429 { 430 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 431 } 432 template <typename T> 433 inline const Type *bsearch (T *key) const 434 { 435 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 436 } 437 438 inline void finish (void) 439 { 440 if (array != static_array) 441 free (array); 442 array = NULL; 443 allocated = len = 0; 444 } 445 }; 446 447 template <typename Type> 448 struct hb_auto_array_t : hb_prealloced_array_t <Type> 449 { 450 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); } 451 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); } 452 }; 453 454 455 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} 456 template <typename item_t, typename lock_t> 457 struct hb_lockable_set_t 458 { 459 hb_prealloced_array_t <item_t, 2> items; 460 461 inline void init (void) { items.init (); } 462 463 template <typename T> 464 inline item_t *replace_or_insert (T v, lock_t &l, bool replace) 465 { 466 l.lock (); 467 item_t *item = items.find (v); 468 if (item) { 469 if (replace) { 470 item_t old = *item; 471 *item = v; 472 l.unlock (); 473 old.finish (); 474 } 475 else { 476 item = NULL; 477 l.unlock (); 478 } 479 } else { 480 item = items.push (); 481 if (likely (item)) 482 *item = v; 483 l.unlock (); 484 } 485 return item; 486 } 487 488 template <typename T> 489 inline void remove (T v, lock_t &l) 490 { 491 l.lock (); 492 item_t *item = items.find (v); 493 if (item) { 494 item_t old = *item; 495 *item = items[items.len - 1]; 496 items.pop (); 497 l.unlock (); 498 old.finish (); 499 } else { 500 l.unlock (); 501 } 502 } 503 504 template <typename T> 505 inline bool find (T v, item_t *i, lock_t &l) 506 { 507 l.lock (); 508 item_t *item = items.find (v); 509 if (item) 510 *i = *item; 511 l.unlock (); 512 return !!item; 513 } 514 515 template <typename T> 516 inline item_t *find_or_insert (T v, lock_t &l) 517 { 518 l.lock (); 519 item_t *item = items.find (v); 520 if (!item) { 521 item = items.push (); 522 if (likely (item)) 523 *item = v; 524 } 525 l.unlock (); 526 return item; 527 } 528 529 inline void finish (lock_t &l) 530 { 531 if (!items.len) { 532 /* No need for locking. */ 533 items.finish (); 534 return; 535 } 536 l.lock (); 537 while (items.len) { 538 item_t old = items[items.len - 1]; 539 items.pop (); 540 l.unlock (); 541 old.finish (); 542 l.lock (); 543 } 544 items.finish (); 545 l.unlock (); 546 } 547 548 }; 549 550 551 /* ASCII tag/character handling */ 552 553 static inline bool ISALPHA (unsigned char c) 554 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 555 static inline bool ISALNUM (unsigned char c) 556 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } 557 static inline bool ISSPACE (unsigned char c) 558 { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } 559 static inline unsigned char TOUPPER (unsigned char c) 560 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } 561 static inline unsigned char TOLOWER (unsigned char c) 562 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } 563 564 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ 565 ((const char *) s)[1], \ 566 ((const char *) s)[2], \ 567 ((const char *) s)[3])) 568 569 570 /* C++ helpers */ 571 572 /* Makes class uncopyable. Use in private: section. */ 573 #define NO_COPY(T) \ 574 T (const T &o); \ 575 T &operator = (const T &o) 576 577 578 /* Debug */ 579 580 581 #ifndef HB_DEBUG 582 #define HB_DEBUG 0 583 #endif 584 585 static inline bool 586 _hb_debug (unsigned int level, 587 unsigned int max_level) 588 { 589 return level < max_level; 590 } 591 592 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) 593 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0)) 594 595 static inline void 596 _hb_print_func (const char *func) 597 { 598 if (func) 599 { 600 unsigned int func_len = strlen (func); 601 /* Skip "static" */ 602 if (0 == strncmp (func, "static ", 7)) 603 func += 7; 604 /* Skip "typename" */ 605 if (0 == strncmp (func, "typename ", 9)) 606 func += 9; 607 /* Skip return type */ 608 const char *space = strchr (func, ' '); 609 if (space) 610 func = space + 1; 611 /* Skip parameter list */ 612 const char *paren = strchr (func, '('); 613 if (paren) 614 func_len = paren - func; 615 fprintf (stderr, "%.*s", func_len, func); 616 } 617 } 618 619 template <int max_level> static inline void 620 _hb_debug_msg_va (const char *what, 621 const void *obj, 622 const char *func, 623 bool indented, 624 unsigned int level, 625 int level_dir, 626 const char *message, 627 va_list ap) HB_PRINTF_FUNC(7, 0); 628 template <int max_level> static inline void 629 _hb_debug_msg_va (const char *what, 630 const void *obj, 631 const char *func, 632 bool indented, 633 unsigned int level, 634 int level_dir, 635 const char *message, 636 va_list ap) 637 { 638 if (!_hb_debug (level, max_level)) 639 return; 640 641 fprintf (stderr, "%-10s", what ? what : ""); 642 643 if (obj) 644 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj); 645 else 646 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); 647 648 if (indented) { 649 /* One may want to add ASCII version of these. See: 650 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */ 651 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */ 652 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ 653 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */ 654 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */ 655 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */ 656 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; 657 fprintf (stderr, "%2u %s" VRBAR "%s", 658 level, 659 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level), 660 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR); 661 } else 662 fprintf (stderr, " " VRBAR LBAR); 663 664 _hb_print_func (func); 665 666 if (message) 667 { 668 fprintf (stderr, ": "); 669 vfprintf (stderr, message, ap); 670 } 671 672 fprintf (stderr, "\n"); 673 } 674 template <> inline void 675 _hb_debug_msg_va<0> (const char *what HB_UNUSED, 676 const void *obj HB_UNUSED, 677 const char *func HB_UNUSED, 678 bool indented HB_UNUSED, 679 unsigned int level HB_UNUSED, 680 int level_dir HB_UNUSED, 681 const char *message HB_UNUSED, 682 va_list ap HB_UNUSED) {} 683 684 template <int max_level> static inline void 685 _hb_debug_msg (const char *what, 686 const void *obj, 687 const char *func, 688 bool indented, 689 unsigned int level, 690 int level_dir, 691 const char *message, 692 ...) HB_PRINTF_FUNC(7, 8); 693 template <int max_level> static inline void 694 _hb_debug_msg (const char *what, 695 const void *obj, 696 const char *func, 697 bool indented, 698 unsigned int level, 699 int level_dir, 700 const char *message, 701 ...) 702 { 703 va_list ap; 704 va_start (ap, message); 705 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap); 706 va_end (ap); 707 } 708 template <> inline void 709 _hb_debug_msg<0> (const char *what HB_UNUSED, 710 const void *obj HB_UNUSED, 711 const char *func HB_UNUSED, 712 bool indented HB_UNUSED, 713 unsigned int level HB_UNUSED, 714 int level_dir HB_UNUSED, 715 const char *message HB_UNUSED, 716 ...) HB_PRINTF_FUNC(7, 8); 717 template <> inline void 718 _hb_debug_msg<0> (const char *what HB_UNUSED, 719 const void *obj HB_UNUSED, 720 const char *func HB_UNUSED, 721 bool indented HB_UNUSED, 722 unsigned int level HB_UNUSED, 723 int level_dir HB_UNUSED, 724 const char *message HB_UNUSED, 725 ...) {} 726 727 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__) 728 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__) 729 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__) 730 731 732 /* 733 * Printer 734 */ 735 736 template <typename T> 737 struct hb_printer_t { 738 const char *print (const T&) { return "something"; } 739 }; 740 741 template <> 742 struct hb_printer_t<bool> { 743 const char *print (bool v) { return v ? "true" : "false"; } 744 }; 745 746 template <> 747 struct hb_printer_t<hb_void_t> { 748 const char *print (hb_void_t) { return ""; } 749 }; 750 751 752 /* 753 * Trace 754 */ 755 756 template <typename T> 757 static inline void _hb_warn_no_return (bool returned) 758 { 759 if (unlikely (!returned)) { 760 fprintf (stderr, "OUCH, returned with no call to return_trace(). This is a bug, please report.\n"); 761 } 762 } 763 template <> 764 /*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED) 765 {} 766 767 template <int max_level, typename ret_t> 768 struct hb_auto_trace_t { 769 explicit inline hb_auto_trace_t (unsigned int *plevel_, 770 const char *what_, 771 const void *obj_, 772 const char *func, 773 const char *message, 774 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false) 775 { 776 if (plevel) ++*plevel; 777 778 va_list ap; 779 va_start (ap, message); 780 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap); 781 va_end (ap); 782 } 783 inline ~hb_auto_trace_t (void) 784 { 785 _hb_warn_no_return<ret_t> (returned); 786 if (!returned) { 787 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " "); 788 } 789 if (plevel) --*plevel; 790 } 791 792 inline ret_t ret (ret_t v, unsigned int line = 0) 793 { 794 if (unlikely (returned)) { 795 fprintf (stderr, "OUCH, double calls to return_trace(). This is a bug, please report.\n"); 796 return v; 797 } 798 799 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, 800 "return %s (line %d)", 801 hb_printer_t<ret_t>().print (v), line); 802 if (plevel) --*plevel; 803 plevel = NULL; 804 returned = true; 805 return v; 806 } 807 808 private: 809 unsigned int *plevel; 810 const char *what; 811 const void *obj; 812 bool returned; 813 }; 814 template <typename ret_t> /* Optimize when tracing is disabled */ 815 struct hb_auto_trace_t<0, ret_t> { 816 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED, 817 const char *what HB_UNUSED, 818 const void *obj HB_UNUSED, 819 const char *func HB_UNUSED, 820 const char *message HB_UNUSED, 821 ...) {} 822 823 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; } 824 }; 825 826 #define return_trace(RET) return trace.ret (RET, __LINE__) 827 828 /* Misc */ 829 830 template <typename T> class hb_assert_unsigned_t; 831 template <> class hb_assert_unsigned_t<unsigned char> {}; 832 template <> class hb_assert_unsigned_t<unsigned short> {}; 833 template <> class hb_assert_unsigned_t<unsigned int> {}; 834 template <> class hb_assert_unsigned_t<unsigned long> {}; 835 836 template <typename T> static inline bool 837 hb_in_range (T u, T lo, T hi) 838 { 839 /* The sizeof() is here to force template instantiation. 840 * I'm sure there are better ways to do this but can't think of 841 * one right now. Declaring a variable won't work as HB_UNUSED 842 * is unusable on some platforms and unused types are less likely 843 * to generate a warning than unused variables. */ 844 ASSERT_STATIC (sizeof (hb_assert_unsigned_t<T>) >= 0); 845 846 /* The casts below are important as if T is smaller than int, 847 * the subtract results will become a signed int! */ 848 return (T)(u - lo) <= (T)(hi - lo); 849 } 850 851 template <typename T> static inline bool 852 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) 853 { 854 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); 855 } 856 857 template <typename T> static inline bool 858 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) 859 { 860 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); 861 } 862 863 864 /* Useful for set-operations on small enums. 865 * For example, for testing "x ∈ {x1, x2, x3}" use: 866 * (FLAG_SAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) 867 */ 868 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((x) < 32) + (1U << (x))) 869 #define FLAG_SAFE(x) (1U << (x)) 870 #define FLAG_UNSAFE(x) ((x) < 32 ? FLAG_SAFE(x) : 0) 871 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) 872 873 874 template <typename T, typename T2> static inline void 875 hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) 876 { 877 for (unsigned int i = 1; i < len; i++) 878 { 879 unsigned int j = i; 880 while (j && compar (&array[j - 1], &array[i]) > 0) 881 j--; 882 if (i == j) 883 continue; 884 /* Move item i to occupy place for item j, shift what's in between. */ 885 { 886 T t = array[i]; 887 memmove (&array[j + 1], &array[j], (i - j) * sizeof (T)); 888 array[j] = t; 889 } 890 if (array2) 891 { 892 T2 t = array2[i]; 893 memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2)); 894 array2[j] = t; 895 } 896 } 897 } 898 899 template <typename T> static inline void 900 hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) 901 { 902 hb_stable_sort (array, len, compar, (int *) NULL); 903 } 904 905 static inline hb_bool_t 906 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) 907 { 908 /* Pain because we don't know whether s is nul-terminated. */ 909 char buf[64]; 910 len = MIN (ARRAY_LENGTH (buf) - 1, len); 911 strncpy (buf, s, len); 912 buf[len] = '\0'; 913 914 char *end; 915 errno = 0; 916 unsigned long v = strtoul (buf, &end, base); 917 if (errno) return false; 918 if (*end) return false; 919 *out = v; 920 return true; 921 } 922 923 924 /* Global runtime options. */ 925 926 struct hb_options_t 927 { 928 unsigned int initialized : 1; 929 unsigned int uniscribe_bug_compatible : 1; 930 }; 931 932 union hb_options_union_t { 933 unsigned int i; 934 hb_options_t opts; 935 }; 936 ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t)); 937 938 HB_INTERNAL void 939 _hb_options_init (void); 940 941 extern HB_INTERNAL hb_options_union_t _hb_options; 942 943 static inline hb_options_t 944 hb_options (void) 945 { 946 if (unlikely (!_hb_options.i)) 947 _hb_options_init (); 948 949 return _hb_options.opts; 950 } 951 952 953 #endif /* HB_PRIVATE_HH */