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