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