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