michael@0: /* michael@0: * Copyright © 2007,2008,2009 Red Hat, Inc. michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_PRIVATE_HH michael@0: #define HB_PRIVATE_HH michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "hb.h" michael@0: #define HB_H_IN michael@0: #ifdef HAVE_OT michael@0: #include "hb-ot.h" michael@0: #define HB_OT_H_IN michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* We only use these two for debug output. However, the debug code is michael@0: * always seen by the compiler (and optimized out in non-debug builds. michael@0: * If including these becomes a problem, we can start thinking about michael@0: * someway around that. */ michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: michael@0: michael@0: /* Essentials */ michael@0: michael@0: #ifndef NULL michael@0: # define NULL ((void *) 0) michael@0: #endif michael@0: michael@0: michael@0: /* Void! */ michael@0: struct _hb_void_t {}; michael@0: typedef const _hb_void_t &hb_void_t; michael@0: #define HB_VOID (* (const _hb_void_t *) NULL) michael@0: michael@0: michael@0: /* Basics */ michael@0: michael@0: michael@0: #undef MIN michael@0: template michael@0: static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } michael@0: michael@0: #undef MAX michael@0: template michael@0: static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } michael@0: michael@0: static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) michael@0: { return (a + (b - 1)) / b; } michael@0: michael@0: michael@0: #undef ARRAY_LENGTH michael@0: template michael@0: static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } michael@0: /* A const version, but does not detect erratically being called on pointers. */ michael@0: #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) michael@0: michael@0: #define HB_STMT_START do michael@0: #define HB_STMT_END while (0) michael@0: michael@0: #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] michael@0: #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) michael@0: #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) michael@0: michael@0: #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1])) michael@0: #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) michael@0: michael@0: #define _PASTE1(a,b) a##b michael@0: #define PASTE(a,b) _PASTE1(a,b) michael@0: michael@0: /* Lets assert int types. Saves trouble down the road. */ michael@0: michael@0: ASSERT_STATIC (sizeof (int8_t) == 1); michael@0: ASSERT_STATIC (sizeof (uint8_t) == 1); michael@0: ASSERT_STATIC (sizeof (int16_t) == 2); michael@0: ASSERT_STATIC (sizeof (uint16_t) == 2); michael@0: ASSERT_STATIC (sizeof (int32_t) == 4); michael@0: ASSERT_STATIC (sizeof (uint32_t) == 4); michael@0: ASSERT_STATIC (sizeof (int64_t) == 8); michael@0: ASSERT_STATIC (sizeof (uint64_t) == 8); michael@0: michael@0: ASSERT_STATIC (sizeof (hb_codepoint_t) == 4); michael@0: ASSERT_STATIC (sizeof (hb_position_t) == 4); michael@0: ASSERT_STATIC (sizeof (hb_mask_t) == 4); michael@0: ASSERT_STATIC (sizeof (hb_var_int_t) == 4); michael@0: michael@0: michael@0: /* We like our types POD */ michael@0: michael@0: #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } michael@0: #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) michael@0: #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) michael@0: michael@0: #ifdef __GNUC__ michael@0: # define _ASSERT_INSTANCE_POD1(_line, _instance) \ michael@0: HB_STMT_START { \ michael@0: typedef __typeof__(_instance) _type_##_line; \ michael@0: _ASSERT_TYPE_POD1 (_line, _type_##_line); \ michael@0: } HB_STMT_END michael@0: #else michael@0: # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested michael@0: #endif michael@0: # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) michael@0: # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) michael@0: michael@0: /* Check _assertion in a method environment */ michael@0: #define _ASSERT_POD1(_line) \ michael@0: inline void _static_assertion_on_line_##_line (void) const \ michael@0: { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } michael@0: # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) michael@0: # define ASSERT_POD() _ASSERT_POD0 (__LINE__) michael@0: michael@0: michael@0: michael@0: /* Misc */ michael@0: michael@0: michael@0: #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) michael@0: #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) michael@0: #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) michael@0: #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) michael@0: #else michael@0: #define likely(expr) (expr) michael@0: #define unlikely(expr) (expr) michael@0: #endif michael@0: michael@0: #ifndef __GNUC__ michael@0: #undef __attribute__ michael@0: #define __attribute__(x) michael@0: #endif michael@0: michael@0: #if __GNUC__ >= 3 michael@0: #define HB_PURE_FUNC __attribute__((pure)) michael@0: #define HB_CONST_FUNC __attribute__((const)) michael@0: #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) michael@0: #else michael@0: #define HB_PURE_FUNC michael@0: #define HB_CONST_FUNC michael@0: #define HB_PRINTF_FUNC(format_idx, arg_idx) michael@0: #endif michael@0: #if __GNUC__ >= 4 michael@0: #define HB_UNUSED __attribute__((unused)) michael@0: #else michael@0: #define HB_UNUSED michael@0: #endif michael@0: michael@0: #ifndef HB_INTERNAL michael@0: # ifndef __MINGW32__ michael@0: # define HB_INTERNAL __attribute__((__visibility__("hidden"))) michael@0: # else michael@0: # define HB_INTERNAL michael@0: # endif michael@0: #endif michael@0: michael@0: michael@0: #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) michael@0: #define snprintf _snprintf michael@0: #endif michael@0: michael@0: #ifdef _MSC_VER michael@0: #undef inline michael@0: #define inline __inline michael@0: #endif michael@0: michael@0: #ifdef __STRICT_ANSI__ michael@0: #undef inline michael@0: #define inline __inline__ michael@0: #endif michael@0: michael@0: michael@0: #if __GNUC__ >= 3 michael@0: #define HB_FUNC __PRETTY_FUNCTION__ michael@0: #elif defined(_MSC_VER) michael@0: #define HB_FUNC __FUNCSIG__ michael@0: #else michael@0: #define HB_FUNC __func__ michael@0: #endif michael@0: michael@0: michael@0: /* Return the number of 1 bits in mask. */ michael@0: static inline HB_CONST_FUNC unsigned int michael@0: _hb_popcount32 (uint32_t mask) michael@0: { michael@0: #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) michael@0: return __builtin_popcount (mask); michael@0: #else michael@0: /* "HACKMEM 169" */ michael@0: register uint32_t y; michael@0: y = (mask >> 1) &033333333333; michael@0: y = mask - y - ((y >>1) & 033333333333); michael@0: return (((y + (y >> 3)) & 030707070707) % 077); michael@0: #endif michael@0: } michael@0: michael@0: /* Returns the number of bits needed to store number */ michael@0: static inline HB_CONST_FUNC unsigned int michael@0: _hb_bit_storage (unsigned int number) michael@0: { michael@0: #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) michael@0: return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; michael@0: #else michael@0: register unsigned int n_bits = 0; michael@0: while (number) { michael@0: n_bits++; michael@0: number >>= 1; michael@0: } michael@0: return n_bits; michael@0: #endif michael@0: } michael@0: michael@0: /* Returns the number of zero bits in the least significant side of number */ michael@0: static inline HB_CONST_FUNC unsigned int michael@0: _hb_ctz (unsigned int number) michael@0: { michael@0: #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) michael@0: return likely (number) ? __builtin_ctz (number) : 0; michael@0: #else michael@0: register unsigned int n_bits = 0; michael@0: if (unlikely (!number)) return 0; michael@0: while (!(number & 1)) { michael@0: n_bits++; michael@0: number >>= 1; michael@0: } michael@0: return n_bits; michael@0: #endif michael@0: } michael@0: michael@0: static inline bool michael@0: _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) michael@0: { michael@0: return (size > 0) && (count >= ((unsigned int) -1) / size); michael@0: } michael@0: michael@0: michael@0: /* Type of bsearch() / qsort() compare function */ michael@0: typedef int (*hb_compare_func_t) (const void *, const void *); michael@0: michael@0: michael@0: michael@0: michael@0: /* arrays and maps */ michael@0: michael@0: michael@0: #define HB_PREALLOCED_ARRAY_INIT {0} michael@0: template michael@0: struct hb_prealloced_array_t michael@0: { michael@0: unsigned int len; michael@0: unsigned int allocated; michael@0: Type *array; michael@0: Type static_array[StaticSize]; michael@0: michael@0: void init (void) { memset (this, 0, sizeof (*this)); } michael@0: michael@0: inline Type& operator [] (unsigned int i) { return array[i]; } michael@0: inline const Type& operator [] (unsigned int i) const { return array[i]; } michael@0: michael@0: inline Type *push (void) michael@0: { michael@0: if (!array) { michael@0: array = static_array; michael@0: allocated = ARRAY_LENGTH (static_array); michael@0: } michael@0: if (likely (len < allocated)) michael@0: return &array[len++]; michael@0: michael@0: /* Need to reallocate */ michael@0: unsigned int new_allocated = allocated + (allocated >> 1) + 8; michael@0: Type *new_array = NULL; michael@0: michael@0: if (array == static_array) { michael@0: new_array = (Type *) calloc (new_allocated, sizeof (Type)); michael@0: if (new_array) michael@0: memcpy (new_array, array, len * sizeof (Type)); michael@0: } else { michael@0: bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); michael@0: if (likely (!overflows)) { michael@0: new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); michael@0: } michael@0: } michael@0: michael@0: if (unlikely (!new_array)) michael@0: return NULL; michael@0: michael@0: array = new_array; michael@0: allocated = new_allocated; michael@0: return &array[len++]; michael@0: } michael@0: michael@0: inline void pop (void) michael@0: { michael@0: len--; michael@0: } michael@0: michael@0: inline void remove (unsigned int i) michael@0: { michael@0: if (unlikely (i >= len)) michael@0: return; michael@0: memmove (static_cast (&array[i]), michael@0: static_cast (&array[i + 1]), michael@0: (len - i - 1) * sizeof (Type)); michael@0: len--; michael@0: } michael@0: michael@0: inline void shrink (unsigned int l) michael@0: { michael@0: if (l < len) michael@0: len = l; michael@0: } michael@0: michael@0: template michael@0: inline Type *find (T v) { michael@0: for (unsigned int i = 0; i < len; i++) michael@0: if (array[i] == v) michael@0: return &array[i]; michael@0: return NULL; michael@0: } michael@0: template michael@0: inline const Type *find (T v) const { michael@0: for (unsigned int i = 0; i < len; i++) michael@0: if (array[i] == v) michael@0: return &array[i]; michael@0: return NULL; michael@0: } michael@0: michael@0: inline void sort (void) michael@0: { michael@0: qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); michael@0: } michael@0: michael@0: inline void sort (unsigned int start, unsigned int end) michael@0: { michael@0: qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); michael@0: } michael@0: michael@0: template michael@0: inline Type *bsearch (T *key) michael@0: { michael@0: return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); michael@0: } michael@0: template michael@0: inline const Type *bsearch (T *key) const michael@0: { michael@0: return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); michael@0: } michael@0: michael@0: inline void finish (void) michael@0: { michael@0: if (array != static_array) michael@0: free (array); michael@0: array = NULL; michael@0: allocated = len = 0; michael@0: } michael@0: }; michael@0: michael@0: #define HB_AUTO_ARRAY_PREALLOCED 16 michael@0: template michael@0: struct hb_auto_array_t : hb_prealloced_array_t michael@0: { michael@0: hb_auto_array_t (void) { hb_prealloced_array_t::init (); } michael@0: ~hb_auto_array_t (void) { hb_prealloced_array_t::finish (); } michael@0: }; michael@0: michael@0: michael@0: #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} michael@0: template michael@0: struct hb_lockable_set_t michael@0: { michael@0: hb_prealloced_array_t items; michael@0: michael@0: inline void init (void) { items.init (); } michael@0: michael@0: template michael@0: inline item_t *replace_or_insert (T v, lock_t &l, bool replace) michael@0: { michael@0: l.lock (); michael@0: item_t *item = items.find (v); michael@0: if (item) { michael@0: if (replace) { michael@0: item_t old = *item; michael@0: *item = v; michael@0: l.unlock (); michael@0: old.finish (); michael@0: } michael@0: else { michael@0: item = NULL; michael@0: l.unlock (); michael@0: } michael@0: } else { michael@0: item = items.push (); michael@0: if (likely (item)) michael@0: *item = v; michael@0: l.unlock (); michael@0: } michael@0: return item; michael@0: } michael@0: michael@0: template michael@0: inline void remove (T v, lock_t &l) michael@0: { michael@0: l.lock (); michael@0: item_t *item = items.find (v); michael@0: if (item) { michael@0: item_t old = *item; michael@0: *item = items[items.len - 1]; michael@0: items.pop (); michael@0: l.unlock (); michael@0: old.finish (); michael@0: } else { michael@0: l.unlock (); michael@0: } michael@0: } michael@0: michael@0: template michael@0: inline bool find (T v, item_t *i, lock_t &l) michael@0: { michael@0: l.lock (); michael@0: item_t *item = items.find (v); michael@0: if (item) michael@0: *i = *item; michael@0: l.unlock (); michael@0: return !!item; michael@0: } michael@0: michael@0: template michael@0: inline item_t *find_or_insert (T v, lock_t &l) michael@0: { michael@0: l.lock (); michael@0: item_t *item = items.find (v); michael@0: if (!item) { michael@0: item = items.push (); michael@0: if (likely (item)) michael@0: *item = v; michael@0: } michael@0: l.unlock (); michael@0: return item; michael@0: } michael@0: michael@0: inline void finish (lock_t &l) michael@0: { michael@0: if (!items.len) { michael@0: /* No need for locking. */ michael@0: items.finish (); michael@0: return; michael@0: } michael@0: l.lock (); michael@0: while (items.len) { michael@0: item_t old = items[items.len - 1]; michael@0: items.pop (); michael@0: l.unlock (); michael@0: old.finish (); michael@0: l.lock (); michael@0: } michael@0: items.finish (); michael@0: l.unlock (); michael@0: } michael@0: michael@0: }; michael@0: michael@0: michael@0: michael@0: michael@0: /* Big-endian handling */ michael@0: michael@0: static inline uint16_t hb_be_uint16 (const uint16_t v) michael@0: { michael@0: const uint8_t *V = (const uint8_t *) &v; michael@0: return (V[0] << 8) | V[1]; michael@0: } michael@0: michael@0: static inline uint16_t hb_uint16_swap (const uint16_t v) michael@0: { michael@0: return (v >> 8) | (v << 8); michael@0: } michael@0: michael@0: static inline uint32_t hb_uint32_swap (const uint32_t v) michael@0: { michael@0: return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); michael@0: } michael@0: michael@0: /* Note, of the following macros, uint16_get is the one called many many times. michael@0: * If there is any optimizations to be done, it's in that macro. However, I michael@0: * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which michael@0: * results in a single ror instruction, does NOT speed this up. In fact, it michael@0: * resulted in a minor slowdown. At any rate, note that v may not be correctly michael@0: * aligned, so I think the current implementation is optimal. michael@0: */ michael@0: michael@0: #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END michael@0: #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) michael@0: #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) michael@0: michael@0: #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: #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) michael@0: #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: michael@0: #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: #define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2]) michael@0: #define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) michael@0: michael@0: michael@0: /* ASCII tag/character handling */ michael@0: michael@0: static inline bool ISALPHA (unsigned char c) michael@0: { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } michael@0: static inline bool ISALNUM (unsigned char c) michael@0: { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } michael@0: static inline bool ISSPACE (unsigned char c) michael@0: { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } michael@0: static inline unsigned char TOUPPER (unsigned char c) michael@0: { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } michael@0: static inline unsigned char TOLOWER (unsigned char c) michael@0: { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } michael@0: michael@0: #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ michael@0: ((const char *) s)[1], \ michael@0: ((const char *) s)[2], \ michael@0: ((const char *) s)[3])) michael@0: michael@0: michael@0: /* C++ helpers */ michael@0: michael@0: /* Makes class uncopyable. Use in private: section. */ michael@0: #define NO_COPY(T) \ michael@0: T (const T &o); \ michael@0: T &operator = (const T &o) michael@0: michael@0: michael@0: /* Debug */ michael@0: michael@0: michael@0: #ifndef HB_DEBUG michael@0: #define HB_DEBUG 0 michael@0: #endif michael@0: michael@0: static inline bool michael@0: _hb_debug (unsigned int level, michael@0: unsigned int max_level) michael@0: { michael@0: return level < max_level; michael@0: } michael@0: michael@0: #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) michael@0: #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0)) michael@0: michael@0: template static inline void michael@0: _hb_debug_msg_va (const char *what, michael@0: const void *obj, michael@0: const char *func, michael@0: bool indented, michael@0: unsigned int level, michael@0: int level_dir, michael@0: const char *message, michael@0: va_list ap) michael@0: { michael@0: if (!_hb_debug (level, max_level)) michael@0: return; michael@0: michael@0: fprintf (stderr, "%-10s", what ? what : ""); michael@0: michael@0: if (obj) michael@0: fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj); michael@0: else michael@0: fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); michael@0: michael@0: if (indented) { michael@0: /* One may want to add ASCII version of these. See: michael@0: * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */ michael@0: #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */ michael@0: #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ michael@0: #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */ michael@0: #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */ michael@0: #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */ michael@0: 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: fprintf (stderr, "%2u %s" VRBAR "%s", michael@0: level, michael@0: bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level), michael@0: level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR); michael@0: } else michael@0: fprintf (stderr, " " VRBAR LBAR); michael@0: michael@0: if (func) michael@0: { michael@0: unsigned int func_len = strlen (func); michael@0: #ifndef HB_DEBUG_VERBOSE michael@0: /* Skip "typename" */ michael@0: if (0 == strncmp (func, "typename ", 9)) michael@0: func += 9; michael@0: /* Skip return type */ michael@0: const char *space = strchr (func, ' '); michael@0: if (space) michael@0: func = space + 1; michael@0: /* Skip parameter list */ michael@0: const char *paren = strchr (func, '('); michael@0: if (paren) michael@0: func_len = paren - func; michael@0: #endif michael@0: fprintf (stderr, "%.*s: ", func_len, func); michael@0: } michael@0: michael@0: if (message) michael@0: vfprintf (stderr, message, ap); michael@0: michael@0: fprintf (stderr, "\n"); michael@0: } michael@0: template <> inline void michael@0: _hb_debug_msg_va<0> (const char *what HB_UNUSED, michael@0: const void *obj HB_UNUSED, michael@0: const char *func HB_UNUSED, michael@0: bool indented HB_UNUSED, michael@0: unsigned int level HB_UNUSED, michael@0: int level_dir HB_UNUSED, michael@0: const char *message HB_UNUSED, michael@0: va_list ap HB_UNUSED) {} michael@0: michael@0: template static inline void michael@0: _hb_debug_msg (const char *what, michael@0: const void *obj, michael@0: const char *func, michael@0: bool indented, michael@0: unsigned int level, michael@0: int level_dir, michael@0: const char *message, michael@0: ...) HB_PRINTF_FUNC(7, 8); michael@0: template static inline void michael@0: _hb_debug_msg (const char *what, michael@0: const void *obj, michael@0: const char *func, michael@0: bool indented, michael@0: unsigned int level, michael@0: int level_dir, michael@0: const char *message, michael@0: ...) michael@0: { michael@0: va_list ap; michael@0: va_start (ap, message); michael@0: _hb_debug_msg_va (what, obj, func, indented, level, level_dir, message, ap); michael@0: va_end (ap); michael@0: } michael@0: template <> inline void michael@0: _hb_debug_msg<0> (const char *what HB_UNUSED, michael@0: const void *obj HB_UNUSED, michael@0: const char *func HB_UNUSED, michael@0: bool indented HB_UNUSED, michael@0: unsigned int level HB_UNUSED, michael@0: int level_dir HB_UNUSED, michael@0: const char *message HB_UNUSED, michael@0: ...) HB_PRINTF_FUNC(7, 8); michael@0: template <> inline void michael@0: _hb_debug_msg<0> (const char *what HB_UNUSED, michael@0: const void *obj HB_UNUSED, michael@0: const char *func HB_UNUSED, michael@0: bool indented HB_UNUSED, michael@0: unsigned int level HB_UNUSED, michael@0: int level_dir HB_UNUSED, michael@0: const char *message HB_UNUSED, michael@0: ...) {} michael@0: michael@0: #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__) michael@0: #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__) michael@0: #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__) michael@0: michael@0: michael@0: /* michael@0: * Printer michael@0: */ michael@0: michael@0: template michael@0: struct hb_printer_t {}; michael@0: michael@0: template <> michael@0: struct hb_printer_t { michael@0: const char *print (bool v) { return v ? "true" : "false"; } michael@0: }; michael@0: michael@0: template <> michael@0: struct hb_printer_t { michael@0: const char *print (hb_void_t) { return ""; } michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * Trace michael@0: */ michael@0: michael@0: template michael@0: static inline void _hb_warn_no_return (bool returned) michael@0: { michael@0: if (unlikely (!returned)) { michael@0: fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n"); michael@0: } michael@0: } michael@0: template <> michael@0: inline void _hb_warn_no_return (bool returned HB_UNUSED) michael@0: {} michael@0: michael@0: template michael@0: struct hb_auto_trace_t { michael@0: explicit inline hb_auto_trace_t (unsigned int *plevel_, michael@0: const char *what_, michael@0: const void *obj_, michael@0: const char *func, michael@0: const char *message, michael@0: ...) : plevel (plevel_), what (what_), obj (obj_), returned (false) michael@0: { michael@0: if (plevel) ++*plevel; michael@0: michael@0: va_list ap; michael@0: va_start (ap, message); michael@0: _hb_debug_msg_va (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap); michael@0: va_end (ap); michael@0: } michael@0: inline ~hb_auto_trace_t (void) michael@0: { michael@0: _hb_warn_no_return (returned); michael@0: if (!returned) { michael@0: _hb_debug_msg (what, obj, NULL, true, plevel ? *plevel : 1, -1, " "); michael@0: } michael@0: if (plevel) --*plevel; michael@0: } michael@0: michael@0: inline ret_t ret (ret_t v, unsigned int line = 0) michael@0: { michael@0: if (unlikely (returned)) { michael@0: fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n"); michael@0: return v; michael@0: } michael@0: michael@0: _hb_debug_msg (what, obj, NULL, true, plevel ? *plevel : 1, -1, michael@0: "return %s (line %d)", michael@0: hb_printer_t().print (v), line); michael@0: if (plevel) --*plevel; michael@0: plevel = NULL; michael@0: returned = true; michael@0: return v; michael@0: } michael@0: michael@0: private: michael@0: unsigned int *plevel; michael@0: const char *what; michael@0: const void *obj; michael@0: bool returned; michael@0: }; michael@0: template /* Optimize when tracing is disabled */ michael@0: struct hb_auto_trace_t<0, ret_t> { michael@0: explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED, michael@0: const char *what HB_UNUSED, michael@0: const void *obj HB_UNUSED, michael@0: const char *func HB_UNUSED, michael@0: const char *message HB_UNUSED, michael@0: ...) {} michael@0: michael@0: inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; } michael@0: }; michael@0: michael@0: #define TRACE_RETURN(RET) trace.ret (RET, __LINE__) michael@0: michael@0: /* Misc */ michael@0: michael@0: michael@0: /* Pre-mature optimization: michael@0: * Checks for lo <= u <= hi but with an optimization if lo and hi michael@0: * are only different in a contiguous set of lower-most bits. michael@0: */ michael@0: template static inline bool michael@0: hb_in_range (T u, T lo, T hi) michael@0: { michael@0: if ( ((lo^hi) & lo) == 0 && michael@0: ((lo^hi) & hi) == (lo^hi) && michael@0: ((lo^hi) & ((lo^hi) + 1)) == 0 ) michael@0: return (u & ~(lo^hi)) == lo; michael@0: else michael@0: return lo <= u && u <= hi; michael@0: } michael@0: michael@0: template static inline bool michael@0: hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) michael@0: { michael@0: return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); michael@0: } michael@0: michael@0: template static inline bool michael@0: hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) michael@0: { michael@0: return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); michael@0: } michael@0: michael@0: michael@0: /* Useful for set-operations on small enums. michael@0: * For example, for testing "x ∈ {x1, x2, x3}" use: michael@0: * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) michael@0: */ michael@0: #define FLAG(x) (1<<(x)) michael@0: #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) michael@0: michael@0: michael@0: template inline void michael@0: hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) michael@0: { michael@0: if (unlikely (!len)) michael@0: return; michael@0: michael@0: unsigned int k = len - 1; michael@0: do { michael@0: unsigned int new_k = 0; michael@0: michael@0: for (unsigned int j = 0; j < k; j++) michael@0: if (compar (&array[j], &array[j+1]) > 0) michael@0: { michael@0: { michael@0: T t; michael@0: t = array[j]; michael@0: array[j] = array[j + 1]; michael@0: array[j + 1] = t; michael@0: } michael@0: if (array2) michael@0: { michael@0: T2 t; michael@0: t = array2[j]; michael@0: array2[j] = array2[j + 1]; michael@0: array2[j + 1] = t; michael@0: } michael@0: michael@0: new_k = j; michael@0: } michael@0: k = new_k; michael@0: } while (k); michael@0: } michael@0: michael@0: template inline void michael@0: hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) michael@0: { michael@0: hb_bubble_sort (array, len, compar, (int *) NULL); michael@0: } michael@0: michael@0: static inline hb_bool_t michael@0: hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) michael@0: { michael@0: /* Pain because we don't know whether s is nul-terminated. */ michael@0: char buf[64]; michael@0: len = MIN (ARRAY_LENGTH (buf) - 1, len); michael@0: strncpy (buf, s, len); michael@0: buf[len] = '\0'; michael@0: michael@0: char *end; michael@0: errno = 0; michael@0: unsigned long v = strtoul (buf, &end, base); michael@0: if (errno) return false; michael@0: if (*end) return false; michael@0: *out = v; michael@0: return true; michael@0: } michael@0: michael@0: michael@0: /* Global runtime options. */ michael@0: michael@0: struct hb_options_t michael@0: { michael@0: int initialized : 1; michael@0: int uniscribe_bug_compatible : 1; michael@0: }; michael@0: michael@0: union hb_options_union_t { michael@0: int i; michael@0: hb_options_t opts; michael@0: }; michael@0: ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t)); michael@0: michael@0: HB_INTERNAL void michael@0: _hb_options_init (void); michael@0: michael@0: extern HB_INTERNAL hb_options_union_t _hb_options; michael@0: michael@0: static inline hb_options_t michael@0: hb_options (void) michael@0: { michael@0: if (unlikely (!_hb_options.i)) michael@0: _hb_options_init (); michael@0: michael@0: return _hb_options.opts; michael@0: } michael@0: michael@0: michael@0: #endif /* HB_PRIVATE_HH */