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 | |
Ebrahim Byagowi | 5dbbd0f | 2018-01-11 12:33:22 +0330 | [diff] [blame] | 32 | #define _GNU_SOURCE 1 |
| 33 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 34 | #ifdef HAVE_CONFIG_H |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 35 | #include "config.h" |
| 36 | #endif |
Behdad Esfahbod | 12360f7 | 2008-01-23 15:50:38 -0500 | [diff] [blame] | 37 | |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 38 | #include "hb.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 39 | #define HB_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 40 | #ifdef HAVE_OT |
| 41 | #include "hb-ot.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 42 | #define HB_OT_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 43 | #endif |
Behdad Esfahbod | b28815c | 2009-08-04 22:35:36 -0400 | [diff] [blame] | 44 | |
Behdad Esfahbod | 7cdd6ab | 2018-01-10 04:33:08 +0100 | [diff] [blame] | 45 | #include <math.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 46 | #include <stdlib.h> |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 47 | #include <stddef.h> |
Behdad Esfahbod | f0954d1 | 2009-07-30 15:33:57 -0400 | [diff] [blame] | 48 | #include <string.h> |
| 49 | #include <assert.h> |
Behdad Esfahbod | 7acb389 | 2009-08-05 15:20:34 -0400 | [diff] [blame] | 50 | #include <errno.h> |
Behdad Esfahbod | 40ec3bb | 2017-11-03 16:57:30 -0400 | [diff] [blame] | 51 | #include <stdio.h> |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 52 | #include <stdarg.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 53 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 54 | |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 55 | #define HB_PASTE1(a,b) a##b |
| 56 | #define HB_PASTE(a,b) HB_PASTE1(a,b) |
| 57 | |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 58 | /* Compile-time custom allocator support. */ |
| 59 | |
| 60 | #if defined(hb_malloc_impl) \ |
| 61 | && defined(hb_calloc_impl) \ |
| 62 | && defined(hb_realloc_impl) \ |
| 63 | && defined(hb_free_impl) |
Behdad Esfahbod | cc6ea30 | 2015-10-12 17:21:52 -0400 | [diff] [blame] | 64 | extern "C" void* hb_malloc_impl(size_t size); |
| 65 | extern "C" void* hb_calloc_impl(size_t nmemb, size_t size); |
| 66 | extern "C" void* hb_realloc_impl(void *ptr, size_t size); |
| 67 | extern "C" void hb_free_impl(void *ptr); |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 68 | #define malloc hb_malloc_impl |
| 69 | #define calloc hb_calloc_impl |
| 70 | #define realloc hb_realloc_impl |
| 71 | #define free hb_free_impl |
| 72 | #endif |
| 73 | |
| 74 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 75 | /* Compiler attributes */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 77 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 78 | #if __cplusplus < 201103L |
| 79 | |
Behdad Esfahbod | 62e312e | 2017-10-27 09:29:20 -0600 | [diff] [blame] | 80 | #ifndef nullptr |
| 81 | #define nullptr NULL |
| 82 | #endif |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 83 | |
| 84 | // Static assertions |
| 85 | #ifndef static_assert |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 86 | #define static_assert(e, msg) \ |
Behdad Esfahbod | 6f08b12 | 2017-10-26 18:23:03 -0600 | [diff] [blame] | 87 | HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1] |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 88 | #endif // static_assert |
| 89 | |
| 90 | #endif // __cplusplus < 201103L |
| 91 | |
Ebrahim Byagowi | 76c4873 | 2017-06-02 21:53:10 +0430 | [diff] [blame] | 92 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__) |
| 93 | #define likely(expr) (__builtin_expect (!!(expr), 1)) |
| 94 | #define unlikely(expr) (__builtin_expect (!!(expr), 0)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 95 | #else |
| 96 | #define likely(expr) (expr) |
| 97 | #define unlikely(expr) (expr) |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 98 | #endif |
| 99 | |
Steve Lhomme | 0b8f3ab | 2016-07-11 21:57:26 +0200 | [diff] [blame] | 100 | #if !defined(__GNUC__) && !defined(__clang__) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 101 | #undef __attribute__ |
| 102 | #define __attribute__(x) |
| 103 | #endif |
Behdad Esfahbod | a794ebf | 2009-08-06 12:32:35 -0400 | [diff] [blame] | 104 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 105 | #if __GNUC__ >= 3 |
| 106 | #define HB_PURE_FUNC __attribute__((pure)) |
| 107 | #define HB_CONST_FUNC __attribute__((const)) |
| 108 | #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
| 109 | #else |
| 110 | #define HB_PURE_FUNC |
| 111 | #define HB_CONST_FUNC |
| 112 | #define HB_PRINTF_FUNC(format_idx, arg_idx) |
| 113 | #endif |
| 114 | #if __GNUC__ >= 4 |
| 115 | #define HB_UNUSED __attribute__((unused)) |
Behdad Esfahbod | be59f3c | 2017-12-05 09:01:28 -0800 | [diff] [blame] | 116 | #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */ |
| 117 | #define HB_UNUSED __pragma(warning(suppress: 4100 4101)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 118 | #else |
| 119 | #define HB_UNUSED |
| 120 | #endif |
| 121 | |
| 122 | #ifndef HB_INTERNAL |
Behdad Esfahbod | f1a8d50 | 2014-07-19 16:52:32 -0400 | [diff] [blame] | 123 | # if !defined(__MINGW32__) && !defined(__CYGWIN__) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 124 | # define HB_INTERNAL __attribute__((__visibility__("hidden"))) |
| 125 | # else |
| 126 | # define HB_INTERNAL |
| 127 | # endif |
| 128 | #endif |
| 129 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 130 | #if __GNUC__ >= 3 |
| 131 | #define HB_FUNC __PRETTY_FUNCTION__ |
| 132 | #elif defined(_MSC_VER) |
| 133 | #define HB_FUNC __FUNCSIG__ |
| 134 | #else |
| 135 | #define HB_FUNC __func__ |
| 136 | #endif |
| 137 | |
Behdad Esfahbod | 305d2fb | 2015-10-21 11:04:28 -0200 | [diff] [blame] | 138 | /* |
| 139 | * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411 |
| 140 | * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch |
| 141 | * cases that fall through without a break or return statement. HB_FALLTHROUGH |
| 142 | * is only needed on cases that have code: |
| 143 | * |
| 144 | * switch (foo) { |
| 145 | * case 1: // These cases have no code. No fallthrough annotations are needed. |
| 146 | * case 2: |
| 147 | * case 3: |
| 148 | * foo = 4; // This case has code, so a fallthrough annotation is needed: |
| 149 | * HB_FALLTHROUGH; |
| 150 | * default: |
| 151 | * return foo; |
| 152 | * } |
| 153 | */ |
| 154 | #if defined(__clang__) && __cplusplus >= 201103L |
| 155 | /* clang's fallthrough annotations are only available starting in C++11. */ |
| 156 | # define HB_FALLTHROUGH [[clang::fallthrough]] |
| 157 | #elif defined(_MSC_VER) |
| 158 | /* |
| 159 | * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis): |
| 160 | * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx |
| 161 | */ |
| 162 | # include <sal.h> |
| 163 | # define HB_FALLTHROUGH __fallthrough |
| 164 | #else |
| 165 | # define HB_FALLTHROUGH /* FALLTHROUGH */ |
| 166 | #endif |
| 167 | |
Behdad Esfahbod | 0fc0a10 | 2014-07-21 11:12:54 -0400 | [diff] [blame] | 168 | #if defined(_WIN32) || defined(__CYGWIN__) |
Behdad Esfahbod | db30828 | 2014-07-19 16:32:04 -0400 | [diff] [blame] | 169 | /* We need Windows Vista for both Uniscribe backend and for |
| 170 | * MemoryBarrier. We don't support compiling on Windows XP, |
| 171 | * though we run on it fine. */ |
| 172 | # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600 |
| 173 | # undef _WIN32_WINNT |
| 174 | # endif |
| 175 | # ifndef _WIN32_WINNT |
| 176 | # define _WIN32_WINNT 0x0600 |
| 177 | # endif |
Behdad Esfahbod | 270971a | 2014-08-15 14:28:04 -0400 | [diff] [blame] | 178 | # ifndef WIN32_LEAN_AND_MEAN |
| 179 | # define WIN32_LEAN_AND_MEAN 1 |
| 180 | # endif |
| 181 | # ifndef STRICT |
| 182 | # define STRICT 1 |
| 183 | # endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 184 | |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 185 | # if defined(_WIN32_WCE) |
| 186 | /* Some things not defined on Windows CE. */ |
Konstantin Ritt | 5069062 | 2016-04-26 12:02:26 +0400 | [diff] [blame] | 187 | # define vsnprintf _vsnprintf |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 188 | # define getenv(Name) nullptr |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 189 | # if _WIN32_WCE < 0x800 |
| 190 | # define setlocale(Category, Locale) "C" |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 191 | static int errno = 0; /* Use something better? */ |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 192 | # endif |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 193 | # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 194 | # define getenv(Name) nullptr |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 195 | # endif |
Behdad Esfahbod | ce01ad7 | 2015-04-01 11:05:59 -0700 | [diff] [blame] | 196 | # if defined(_MSC_VER) && _MSC_VER < 1900 |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 197 | # define snprintf _snprintf |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 198 | # endif |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 199 | #endif |
| 200 | |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 201 | #if HAVE_ATEXIT |
| 202 | /* atexit() is only safe to be called from shared libraries on certain |
| 203 | * platforms. Whitelist. |
| 204 | * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */ |
| 205 | # if defined(__linux) && defined(__GLIBC_PREREQ) |
| 206 | # if __GLIBC_PREREQ(2,3) |
| 207 | /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */ |
| 208 | # define HB_USE_ATEXIT 1 |
| 209 | # endif |
| 210 | # elif defined(_MSC_VER) || defined(__MINGW32__) |
| 211 | /* For MSVC: |
| 212 | * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx |
| 213 | * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx |
| 214 | * mingw32 headers say atexit is safe to use in shared libraries. |
| 215 | */ |
| 216 | # define HB_USE_ATEXIT 1 |
| 217 | # elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) |
| 218 | /* This was fixed in Android NKD r8 or r8b: |
| 219 | * https://code.google.com/p/android/issues/detail?id=6455 |
| 220 | * which introduced GCC 4.6: |
| 221 | * https://developer.android.com/tools/sdk/ndk/index.html |
| 222 | */ |
| 223 | # define HB_USE_ATEXIT 1 |
Bruce Mitchener | e8859fc | 2018-02-04 01:26:57 +0700 | [diff] [blame^] | 224 | # elif defined(__APPLE__) |
| 225 | /* For macOS and related platforms, the atexit man page indicates |
| 226 | * that it will be invoked when the library is unloaded, not only |
| 227 | * at application exit. |
| 228 | */ |
| 229 | # define HB_USE_ATEXIT 1 |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 230 | # endif |
| 231 | #endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 232 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 233 | /* Basics */ |
| 234 | |
| 235 | #undef MIN |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 236 | template <typename Type> |
| 237 | 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] | 238 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 239 | #undef MAX |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 240 | template <typename Type> |
| 241 | 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] | 242 | |
Behdad Esfahbod | 16f175c | 2013-11-12 17:22:49 -0500 | [diff] [blame] | 243 | static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) |
| 244 | { return (a + (b - 1)) / b; } |
| 245 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 246 | |
Behdad Esfahbod | 4591753 | 2009-11-04 18:15:59 -0500 | [diff] [blame] | 247 | #undef ARRAY_LENGTH |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 248 | template <typename Type, unsigned int n> |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 249 | static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 250 | /* A const version, but does not detect erratically being called on pointers. */ |
| 251 | #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 252 | |
Behdad Esfahbod | 35a7383 | 2009-08-01 19:30:31 -0400 | [diff] [blame] | 253 | #define HB_STMT_START do |
| 254 | #define HB_STMT_END while (0) |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 255 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 256 | template <unsigned int cond> class hb_assert_constant_t; |
| 257 | template <> class hb_assert_constant_t<1> {}; |
Behdad Esfahbod | 672ca3b | 2015-10-26 14:05:05 -0700 | [diff] [blame] | 258 | |
| 259 | #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] | 260 | |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 261 | /* Lets assert int types. Saves trouble down the road. */ |
| 262 | |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 263 | static_assert ((sizeof (int8_t) == 1), ""); |
| 264 | static_assert ((sizeof (uint8_t) == 1), ""); |
| 265 | static_assert ((sizeof (int16_t) == 2), ""); |
| 266 | static_assert ((sizeof (uint16_t) == 2), ""); |
| 267 | static_assert ((sizeof (int32_t) == 4), ""); |
| 268 | static_assert ((sizeof (uint32_t) == 4), ""); |
| 269 | static_assert ((sizeof (int64_t) == 8), ""); |
| 270 | static_assert ((sizeof (uint64_t) == 8), ""); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 271 | |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 272 | static_assert ((sizeof (hb_codepoint_t) == 4), ""); |
| 273 | static_assert ((sizeof (hb_position_t) == 4), ""); |
| 274 | static_assert ((sizeof (hb_mask_t) == 4), ""); |
| 275 | static_assert ((sizeof (hb_var_int_t) == 4), ""); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 276 | |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 277 | |
| 278 | /* We like our types POD */ |
| 279 | |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 280 | #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } |
| 281 | #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) |
| 282 | #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 283 | |
| 284 | #ifdef __GNUC__ |
Behdad Esfahbod | 79e2b47 | 2012-06-06 11:27:17 -0400 | [diff] [blame] | 285 | # define _ASSERT_INSTANCE_POD1(_line, _instance) \ |
| 286 | HB_STMT_START { \ |
| 287 | typedef __typeof__(_instance) _type_##_line; \ |
| 288 | _ASSERT_TYPE_POD1 (_line, _type_##_line); \ |
| 289 | } HB_STMT_END |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 290 | #else |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 291 | # 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] | 292 | #endif |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 293 | # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) |
| 294 | # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 295 | |
| 296 | /* Check _assertion in a method environment */ |
| 297 | #define _ASSERT_POD1(_line) \ |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 298 | HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \ |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 299 | { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } |
| 300 | # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) |
| 301 | # define ASSERT_POD() _ASSERT_POD0 (__LINE__) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 302 | |
| 303 | |
| 304 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 305 | /* Misc */ |
| 306 | |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 307 | /* |
| 308 | * Void! |
| 309 | */ |
| 310 | typedef const struct _hb_void_t *hb_void_t; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 311 | #define HB_VOID ((const _hb_void_t *) nullptr) |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 312 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 313 | /* Return the number of 1 bits in mask. */ |
Behdad Esfahbod | 97e7f8f | 2010-05-11 00:11:36 -0400 | [diff] [blame] | 314 | static inline HB_CONST_FUNC unsigned int |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 315 | _hb_popcount32 (uint32_t mask) |
| 316 | { |
| 317 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 318 | return __builtin_popcount (mask); |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 319 | #else |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 320 | /* "HACKMEM 169" */ |
Behdad Esfahbod | a949cd3 | 2014-03-16 20:22:42 -0700 | [diff] [blame] | 321 | uint32_t y; |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 322 | y = (mask >> 1) &033333333333; |
| 323 | y = mask - y - ((y >>1) & 033333333333); |
| 324 | return (((y + (y >> 3)) & 030707070707) % 077); |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 325 | #endif |
| 326 | } |
Behdad Esfahbod | 6fb4ac7 | 2017-10-15 16:00:09 +0200 | [diff] [blame] | 327 | static inline HB_CONST_FUNC unsigned int |
| 328 | _hb_popcount64 (uint64_t mask) |
| 329 | { |
| 330 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
Behdad Esfahbod | ced86da | 2017-10-22 17:03:36 -0400 | [diff] [blame] | 331 | if (sizeof (long) >= sizeof (mask)) |
| 332 | return __builtin_popcountl (mask); |
Behdad Esfahbod | 6fb4ac7 | 2017-10-15 16:00:09 +0200 | [diff] [blame] | 333 | #endif |
Behdad Esfahbod | 2ee710e | 2017-10-23 08:37:48 -0400 | [diff] [blame] | 334 | return _hb_popcount32 (mask & 0xFFFFFFFF) + _hb_popcount32 (mask >> 32); |
Behdad Esfahbod | 6fb4ac7 | 2017-10-15 16:00:09 +0200 | [diff] [blame] | 335 | } |
| 336 | template <typename T> static inline unsigned int _hb_popcount (T mask); |
| 337 | template <> inline unsigned int _hb_popcount<uint32_t> (uint32_t mask) { return _hb_popcount32 (mask); } |
| 338 | template <> inline unsigned int _hb_popcount<uint64_t> (uint64_t mask) { return _hb_popcount64 (mask); } |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 339 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 340 | /* Returns the number of bits needed to store number */ |
| 341 | static inline HB_CONST_FUNC unsigned int |
| 342 | _hb_bit_storage (unsigned int number) |
| 343 | { |
| 344 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 345 | return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 346 | #else |
Behdad Esfahbod | a949cd3 | 2014-03-16 20:22:42 -0700 | [diff] [blame] | 347 | unsigned int n_bits = 0; |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 348 | while (number) { |
| 349 | n_bits++; |
| 350 | number >>= 1; |
| 351 | } |
| 352 | return n_bits; |
| 353 | #endif |
| 354 | } |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 355 | |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 356 | /* Returns the number of zero bits in the least significant side of number */ |
| 357 | static inline HB_CONST_FUNC unsigned int |
| 358 | _hb_ctz (unsigned int number) |
| 359 | { |
| 360 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
| 361 | return likely (number) ? __builtin_ctz (number) : 0; |
| 362 | #else |
Behdad Esfahbod | a949cd3 | 2014-03-16 20:22:42 -0700 | [diff] [blame] | 363 | unsigned int n_bits = 0; |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 364 | if (unlikely (!number)) return 0; |
| 365 | while (!(number & 1)) { |
| 366 | n_bits++; |
| 367 | number >>= 1; |
| 368 | } |
| 369 | return n_bits; |
| 370 | #endif |
| 371 | } |
| 372 | |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 373 | static inline bool |
| 374 | _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) |
| 375 | { |
| 376 | return (size > 0) && (count >= ((unsigned int) -1) / size); |
| 377 | } |
| 378 | |
| 379 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 380 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 381 | /* arrays and maps */ |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 382 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 383 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 384 | #define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr} |
Behdad Esfahbod | 546b1ad | 2014-06-26 19:10:21 -0400 | [diff] [blame] | 385 | template <typename Type, unsigned int StaticSize=16> |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 386 | struct hb_prealloced_array_t |
| 387 | { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 388 | unsigned int len; |
| 389 | unsigned int allocated; |
| 390 | Type *array; |
| 391 | Type static_array[StaticSize]; |
| 392 | |
Behdad Esfahbod | 56ef4e0 | 2017-10-15 16:38:29 -0400 | [diff] [blame] | 393 | void init (void) |
| 394 | { |
| 395 | len = 0; |
| 396 | allocated = ARRAY_LENGTH (static_array); |
| 397 | array = static_array; |
| 398 | } |
Behdad Esfahbod | efde811 | 2011-08-23 00:04:57 +0200 | [diff] [blame] | 399 | |
Behdad Esfahbod | 265ac61 | 2011-05-05 14:38:16 -0400 | [diff] [blame] | 400 | inline Type& operator [] (unsigned int i) { return array[i]; } |
| 401 | inline const Type& operator [] (unsigned int i) const { return array[i]; } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 402 | |
| 403 | inline Type *push (void) |
| 404 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 405 | if (unlikely (!resize (len + 1))) |
| 406 | return nullptr; |
| 407 | |
| 408 | return &array[len - 1]; |
| 409 | } |
| 410 | |
| 411 | inline bool resize (unsigned int size) |
| 412 | { |
Behdad Esfahbod | 56ef4e0 | 2017-10-15 16:38:29 -0400 | [diff] [blame] | 413 | if (unlikely (size > allocated)) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 414 | { |
| 415 | /* Need to reallocate */ |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 416 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 417 | unsigned int new_allocated = allocated; |
| 418 | while (size >= new_allocated) |
| 419 | new_allocated += (new_allocated >> 1) + 8; |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 420 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 421 | Type *new_array = nullptr; |
| 422 | |
| 423 | if (array == static_array) { |
| 424 | new_array = (Type *) calloc (new_allocated, sizeof (Type)); |
| 425 | if (new_array) |
| 426 | memcpy (new_array, array, len * sizeof (Type)); |
| 427 | } else { |
| 428 | bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); |
| 429 | if (likely (!overflows)) { |
| 430 | new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); |
| 431 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 432 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 433 | |
| 434 | if (unlikely (!new_array)) |
| 435 | return false; |
| 436 | |
| 437 | array = new_array; |
| 438 | allocated = new_allocated; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 439 | } |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 440 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 441 | len = size; |
| 442 | return true; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | inline void pop (void) |
| 446 | { |
| 447 | len--; |
Behdad Esfahbod | 639afdc | 2013-08-06 14:28:12 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | inline void remove (unsigned int i) |
| 451 | { |
| 452 | if (unlikely (i >= len)) |
| 453 | return; |
| 454 | memmove (static_cast<void *> (&array[i]), |
| 455 | static_cast<void *> (&array[i + 1]), |
| 456 | (len - i - 1) * sizeof (Type)); |
| 457 | len--; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 458 | } |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 459 | |
| 460 | inline void shrink (unsigned int l) |
| 461 | { |
| 462 | if (l < len) |
| 463 | len = l; |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 466 | template <typename T> |
| 467 | inline Type *find (T v) { |
| 468 | for (unsigned int i = 0; i < len; i++) |
| 469 | if (array[i] == v) |
| 470 | return &array[i]; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 471 | return nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 472 | } |
| 473 | template <typename T> |
| 474 | inline const Type *find (T v) const { |
| 475 | for (unsigned int i = 0; i < len; i++) |
| 476 | if (array[i] == v) |
| 477 | return &array[i]; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 478 | return nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 481 | inline void qsort (void) |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 482 | { |
Behdad Esfahbod | 0712e91 | 2017-10-29 17:01:47 -0600 | [diff] [blame] | 483 | ::qsort (array, len, sizeof (Type), Type::cmp); |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 484 | } |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 485 | |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 486 | inline void qsort (unsigned int start, unsigned int end) |
Behdad Esfahbod | b70c96d | 2011-07-07 21:07:41 -0400 | [diff] [blame] | 487 | { |
Behdad Esfahbod | 0712e91 | 2017-10-29 17:01:47 -0600 | [diff] [blame] | 488 | ::qsort (array + start, end - start, sizeof (Type), Type::cmp); |
Behdad Esfahbod | b70c96d | 2011-07-07 21:07:41 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 491 | template <typename T> |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 492 | inline Type *bsearch (T *x) |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 493 | { |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 494 | unsigned int i; |
| 495 | return bfind (x, &i) ? &array[i] : nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 496 | } |
| 497 | template <typename T> |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 498 | inline const Type *bsearch (T *x) const |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 499 | { |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 500 | unsigned int i; |
| 501 | return bfind (x, &i) ? &array[i] : nullptr; |
| 502 | } |
| 503 | template <typename T> |
| 504 | inline bool bfind (T *x, unsigned int *i) const |
| 505 | { |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 506 | int min = 0, max = (int) this->len - 1; |
| 507 | while (min <= max) |
| 508 | { |
| 509 | int mid = (min + max) / 2; |
| 510 | int c = this->array[mid].cmp (x); |
| 511 | if (c < 0) |
| 512 | max = mid - 1; |
| 513 | else if (c > 0) |
| 514 | min = mid + 1; |
| 515 | else |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 516 | { |
| 517 | *i = mid; |
| 518 | return true; |
| 519 | } |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 520 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 521 | if (max < 0 || (max < (int) this->len && this->array[max].cmp (x) > 0)) |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 522 | max++; |
| 523 | *i = max; |
| 524 | return false; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 525 | } |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 526 | |
| 527 | inline void finish (void) |
| 528 | { |
| 529 | if (array != static_array) |
| 530 | free (array); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 531 | array = nullptr; |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 532 | allocated = len = 0; |
| 533 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 534 | }; |
| 535 | |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 536 | template <typename Type> |
Behdad Esfahbod | 546b1ad | 2014-06-26 19:10:21 -0400 | [diff] [blame] | 537 | struct hb_auto_array_t : hb_prealloced_array_t <Type> |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 538 | { |
Behdad Esfahbod | 546b1ad | 2014-06-26 19:10:21 -0400 | [diff] [blame] | 539 | hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); } |
| 540 | ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); } |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 541 | }; |
| 542 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 543 | |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 544 | #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 545 | template <typename item_t, typename lock_t> |
| 546 | struct hb_lockable_set_t |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 547 | { |
Behdad Esfahbod | 51f4d4d | 2017-10-27 15:09:22 -0600 | [diff] [blame] | 548 | hb_prealloced_array_t <item_t, 1> items; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 549 | |
Behdad Esfahbod | bf93b63 | 2012-06-05 14:17:32 -0400 | [diff] [blame] | 550 | inline void init (void) { items.init (); } |
| 551 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 552 | template <typename T> |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 553 | 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] | 554 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 555 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 556 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 557 | if (item) { |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 558 | if (replace) { |
| 559 | item_t old = *item; |
| 560 | *item = v; |
| 561 | l.unlock (); |
| 562 | old.finish (); |
| 563 | } |
| 564 | else { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 565 | item = nullptr; |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 566 | l.unlock (); |
| 567 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 568 | } else { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 569 | item = items.push (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 570 | if (likely (item)) |
| 571 | *item = v; |
| 572 | l.unlock (); |
| 573 | } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 574 | return item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Behdad Esfahbod | 811482b | 2011-05-05 13:21:04 -0400 | [diff] [blame] | 577 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 578 | inline void remove (T v, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 579 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 580 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 581 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 582 | if (item) { |
| 583 | item_t old = *item; |
| 584 | *item = items[items.len - 1]; |
| 585 | items.pop (); |
| 586 | l.unlock (); |
| 587 | old.finish (); |
| 588 | } else { |
| 589 | l.unlock (); |
| 590 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 591 | } |
| 592 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 593 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 594 | inline bool find (T v, item_t *i, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 595 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 596 | l.lock (); |
| 597 | item_t *item = items.find (v); |
| 598 | if (item) |
| 599 | *i = *item; |
| 600 | l.unlock (); |
| 601 | return !!item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 602 | } |
| 603 | |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 604 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 605 | inline item_t *find_or_insert (T v, lock_t &l) |
| 606 | { |
| 607 | l.lock (); |
| 608 | item_t *item = items.find (v); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 609 | if (!item) { |
| 610 | item = items.push (); |
| 611 | if (likely (item)) |
| 612 | *item = v; |
| 613 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 614 | l.unlock (); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 615 | return item; |
| 616 | } |
| 617 | |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 618 | inline void finish (lock_t &l) |
| 619 | { |
Behdad Esfahbod | 3f4764b | 2012-07-30 10:06:42 -0400 | [diff] [blame] | 620 | if (!items.len) { |
| 621 | /* No need for locking. */ |
| 622 | items.finish (); |
| 623 | return; |
| 624 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 625 | l.lock (); |
| 626 | while (items.len) { |
| 627 | item_t old = items[items.len - 1]; |
| 628 | items.pop (); |
| 629 | l.unlock (); |
| 630 | old.finish (); |
| 631 | l.lock (); |
| 632 | } |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 633 | items.finish (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 634 | l.unlock (); |
| 635 | } |
| 636 | |
| 637 | }; |
| 638 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 639 | |
Behdad Esfahbod | 4188096 | 2011-04-11 14:58:28 -0400 | [diff] [blame] | 640 | /* ASCII tag/character handling */ |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 641 | |
Behdad Esfahbod | 20b817a | 2013-02-27 18:39:37 -0500 | [diff] [blame] | 642 | static inline bool ISALPHA (unsigned char c) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 643 | { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } |
Behdad Esfahbod | 20b817a | 2013-02-27 18:39:37 -0500 | [diff] [blame] | 644 | static inline bool ISALNUM (unsigned char c) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 645 | { 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] | 646 | static inline bool ISSPACE (unsigned char c) |
| 647 | { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 648 | static inline unsigned char TOUPPER (unsigned char c) |
| 649 | { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } |
| 650 | static inline unsigned char TOLOWER (unsigned char c) |
| 651 | { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 652 | |
| 653 | |
Behdad Esfahbod | 91dd115 | 2016-02-25 13:56:47 +0900 | [diff] [blame] | 654 | /* HB_NDEBUG disables some sanity checks that are very safe to disable and |
| 655 | * should be disabled in production systems. If NDEBUG is defined, enable |
| 656 | * HB_NDEBUG; but if it's desirable that normal assert()s (which are very |
| 657 | * light-weight) to be enabled, then HB_DEBUG can be defined to disable |
| 658 | * the costlier checks. */ |
| 659 | #ifdef NDEBUG |
| 660 | #define HB_NDEBUG |
| 661 | #endif |
| 662 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 663 | |
| 664 | /* Misc */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 665 | |
Behdad Esfahbod | a8b89a0 | 2014-07-11 14:18:01 -0400 | [diff] [blame] | 666 | template <typename T> class hb_assert_unsigned_t; |
| 667 | template <> class hb_assert_unsigned_t<unsigned char> {}; |
Dominik Röttsches | 9e7c720 | 2014-07-17 14:40:34 +0300 | [diff] [blame] | 668 | template <> class hb_assert_unsigned_t<unsigned short> {}; |
Behdad Esfahbod | a8b89a0 | 2014-07-11 14:18:01 -0400 | [diff] [blame] | 669 | template <> class hb_assert_unsigned_t<unsigned int> {}; |
| 670 | template <> class hb_assert_unsigned_t<unsigned long> {}; |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 671 | |
Behdad Esfahbod | b5aeb95 | 2012-07-13 09:45:54 -0400 | [diff] [blame] | 672 | template <typename T> static inline bool |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 673 | hb_in_range (T u, T lo, T hi) |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 674 | { |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 675 | /* The sizeof() is here to force template instantiation. |
| 676 | * I'm sure there are better ways to do this but can't think of |
| 677 | * one right now. Declaring a variable won't work as HB_UNUSED |
Behdad Esfahbod | 68e04af | 2015-02-21 16:30:28 +0300 | [diff] [blame] | 678 | * is unusable on some platforms and unused types are less likely |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 679 | * to generate a warning than unused variables. */ |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 680 | static_assert ((sizeof (hb_assert_unsigned_t<T>) >= 0), ""); |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 681 | |
Behdad Esfahbod | c2b151d | 2014-08-10 18:52:07 -0400 | [diff] [blame] | 682 | /* The casts below are important as if T is smaller than int, |
| 683 | * the subtract results will become a signed int! */ |
| 684 | return (T)(u - lo) <= (T)(hi - lo); |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 685 | } |
| 686 | |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 687 | template <typename T> static inline bool |
Behdad Esfahbod | c98b718 | 2013-12-31 15:55:40 +0800 | [diff] [blame] | 688 | hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) |
| 689 | { |
| 690 | return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); |
| 691 | } |
| 692 | |
| 693 | template <typename T> static inline bool |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 694 | 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] | 695 | { |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 696 | 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] | 697 | } |
| 698 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 699 | |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 700 | /* Enable bitwise ops on enums marked as flags_t */ |
Behdad Esfahbod | 6986208 | 2015-11-04 18:46:22 -0800 | [diff] [blame] | 701 | /* To my surprise, looks like the function resolver is happy to silently cast |
| 702 | * 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] | 703 | * originally had in mind... :(. |
| 704 | * |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 705 | * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163 |
Behdad Esfahbod | e0082ae | 2015-11-17 18:42:13 -0800 | [diff] [blame] | 706 | */ |
| 707 | #ifdef _MSC_VER |
| 708 | # pragma warning(disable:4200) |
| 709 | # pragma warning(disable:4800) |
Chun-wei Fan | 167c327 | 2015-11-09 17:17:56 +0800 | [diff] [blame] | 710 | #endif |
Behdad Esfahbod | 1dc32ea | 2015-11-20 13:24:19 -0800 | [diff] [blame] | 711 | #define HB_MARK_AS_FLAG_T(T) \ |
| 712 | extern "C++" { \ |
| 713 | static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \ |
| 714 | static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \ |
| 715 | static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \ |
| 716 | static inline T operator ~ (T r) { return T (~(unsigned int) r); } \ |
| 717 | static inline T& operator |= (T &l, T r) { l = l | r; return l; } \ |
| 718 | static inline T& operator &= (T& l, T r) { l = l & r; return l; } \ |
| 719 | static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \ |
| 720 | } |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 721 | |
| 722 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 723 | /* Useful for set-operations on small enums. |
| 724 | * For example, for testing "x ∈ {x1, x2, x3}" use: |
Behdad Esfahbod | 6058f98 | 2017-10-19 11:39:52 -0700 | [diff] [blame] | 725 | * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 726 | */ |
Behdad Esfahbod | d8adaa9 | 2017-10-20 13:57:43 -0400 | [diff] [blame] | 727 | #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned int)(x) < 32) + (1U << (unsigned int)(x))) |
| 728 | #define FLAG_UNSAFE(x) ((unsigned int)(x) < 32 ? (1U << (unsigned int)(x)) : 0) |
Behdad Esfahbod | 2c372b8 | 2012-07-20 13:37:48 -0400 | [diff] [blame] | 729 | #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] | 730 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 731 | |
Behdad Esfahbod | c1e8744 | 2014-10-01 11:07:08 -0400 | [diff] [blame] | 732 | template <typename T, typename T2> static inline void |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 733 | 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] | 734 | { |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 735 | for (unsigned int i = 1; i < len; i++) |
| 736 | { |
| 737 | unsigned int j = i; |
| 738 | while (j && compar (&array[j - 1], &array[i]) > 0) |
| 739 | j--; |
| 740 | if (i == j) |
| 741 | continue; |
| 742 | /* Move item i to occupy place for item j, shift what's in between. */ |
| 743 | { |
Behdad Esfahbod | 9309974 | 2015-09-01 16:11:27 +0100 | [diff] [blame] | 744 | T t = array[i]; |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 745 | memmove (&array[j + 1], &array[j], (i - j) * sizeof (T)); |
| 746 | array[j] = t; |
| 747 | } |
| 748 | if (array2) |
| 749 | { |
Behdad Esfahbod | 9309974 | 2015-09-01 16:11:27 +0100 | [diff] [blame] | 750 | T2 t = array2[i]; |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 751 | memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2)); |
| 752 | array2[j] = t; |
| 753 | } |
| 754 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 755 | } |
| 756 | |
Behdad Esfahbod | 250398b | 2014-10-01 11:28:01 -0400 | [diff] [blame] | 757 | template <typename T> static inline void |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 758 | 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] | 759 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 760 | hb_stable_sort (array, len, compar, (int *) nullptr); |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 761 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 762 | |
Behdad Esfahbod | 6f3a300 | 2012-08-07 22:13:25 -0400 | [diff] [blame] | 763 | static inline hb_bool_t |
| 764 | hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) |
| 765 | { |
| 766 | /* Pain because we don't know whether s is nul-terminated. */ |
| 767 | char buf[64]; |
Behdad Esfahbod | 847794e | 2013-02-27 17:59:28 -0500 | [diff] [blame] | 768 | len = MIN (ARRAY_LENGTH (buf) - 1, len); |
| 769 | strncpy (buf, s, len); |
| 770 | buf[len] = '\0'; |
Behdad Esfahbod | 6f3a300 | 2012-08-07 22:13:25 -0400 | [diff] [blame] | 771 | |
| 772 | char *end; |
| 773 | errno = 0; |
| 774 | unsigned long v = strtoul (buf, &end, base); |
| 775 | if (errno) return false; |
| 776 | if (*end) return false; |
| 777 | *out = v; |
| 778 | return true; |
| 779 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 780 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 781 | |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 782 | /* Vectorization */ |
| 783 | |
| 784 | struct HbOpOr |
| 785 | { |
| 786 | static const bool passthru_left = true; |
| 787 | static const bool passthru_right = true; |
| 788 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a | b; } |
| 789 | }; |
| 790 | struct HbOpAnd |
| 791 | { |
| 792 | static const bool passthru_left = false; |
| 793 | static const bool passthru_right = false; |
| 794 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a & b; } |
| 795 | }; |
| 796 | struct HbOpMinus |
| 797 | { |
| 798 | static const bool passthru_left = true; |
| 799 | static const bool passthru_right = false; |
| 800 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a & ~b; } |
| 801 | }; |
| 802 | struct HbOpXor |
| 803 | { |
| 804 | static const bool passthru_left = true; |
| 805 | static const bool passthru_right = true; |
| 806 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; } |
| 807 | }; |
| 808 | |
| 809 | /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */ |
| 810 | template <typename elt_t, unsigned int byte_size> |
| 811 | struct hb_vector_size_t |
| 812 | { |
| 813 | elt_t& operator [] (unsigned int i) { return v[i]; } |
| 814 | const elt_t& operator [] (unsigned int i) const { return v[i]; } |
| 815 | |
| 816 | template <class Op> |
| 817 | inline hb_vector_size_t process (const hb_vector_size_t &o) const |
| 818 | { |
| 819 | hb_vector_size_t r; |
| 820 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 821 | Op::process (r.v[i], v[i], o.v[i]); |
| 822 | return r; |
| 823 | } |
| 824 | inline hb_vector_size_t operator | (const hb_vector_size_t &o) const |
| 825 | { return process<HbOpOr> (o); } |
| 826 | inline hb_vector_size_t operator & (const hb_vector_size_t &o) const |
| 827 | { return process<HbOpAnd> (o); } |
| 828 | inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const |
| 829 | { return process<HbOpXor> (o); } |
| 830 | inline hb_vector_size_t operator ~ () const |
| 831 | { |
| 832 | hb_vector_size_t r; |
| 833 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 834 | r.v[i] = ~v[i]; |
| 835 | return r; |
| 836 | } |
| 837 | |
| 838 | private: |
Behdad Esfahbod | 221ce6c | 2017-10-15 17:58:58 -0400 | [diff] [blame] | 839 | static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, ""); |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 840 | elt_t v[byte_size / sizeof (elt_t)]; |
| 841 | }; |
| 842 | |
| 843 | /* The `vector_size' attribute was introduced in gcc 3.1. */ |
| 844 | #if defined( __GNUC__ ) && ( __GNUC__ >= 4 ) |
| 845 | #define HAVE_VECTOR_SIZE 1 |
| 846 | #endif |
| 847 | |
| 848 | |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 849 | /* Global runtime options. */ |
| 850 | |
| 851 | struct hb_options_t |
| 852 | { |
Behdad Esfahbod | 5c87120 | 2014-10-14 20:07:31 -0700 | [diff] [blame] | 853 | unsigned int initialized : 1; |
| 854 | unsigned int uniscribe_bug_compatible : 1; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 855 | }; |
| 856 | |
| 857 | union hb_options_union_t { |
Behdad Esfahbod | 5c87120 | 2014-10-14 20:07:31 -0700 | [diff] [blame] | 858 | unsigned int i; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 859 | hb_options_t opts; |
| 860 | }; |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 861 | static_assert ((sizeof (int) == sizeof (hb_options_union_t)), ""); |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 862 | |
| 863 | HB_INTERNAL void |
| 864 | _hb_options_init (void); |
| 865 | |
| 866 | extern HB_INTERNAL hb_options_union_t _hb_options; |
| 867 | |
| 868 | static inline hb_options_t |
| 869 | hb_options (void) |
| 870 | { |
| 871 | if (unlikely (!_hb_options.i)) |
| 872 | _hb_options_init (); |
| 873 | |
| 874 | return _hb_options.opts; |
| 875 | } |
| 876 | |
Steven R. Loomis | a13b023 | 2015-12-11 10:21:27 -0800 | [diff] [blame] | 877 | /* Size signifying variable-sized array */ |
| 878 | #define VAR 1 |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 879 | |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 880 | |
| 881 | /* String type. */ |
| 882 | |
| 883 | struct hb_string_t |
| 884 | { |
| 885 | inline hb_string_t (void) : bytes (nullptr), len (0) {} |
| 886 | inline hb_string_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {} |
| 887 | |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 888 | inline int cmp (const hb_string_t &a) const |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 889 | { |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 890 | if (len != a.len) |
| 891 | return (int) a.len - (int) len; |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 892 | |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 893 | return memcmp (a.bytes, bytes, len); |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 894 | } |
| 895 | static inline int cmp (const void *pa, const void *pb) |
| 896 | { |
| 897 | hb_string_t *a = (hb_string_t *) pa; |
| 898 | hb_string_t *b = (hb_string_t *) pb; |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 899 | return b->cmp (*a); |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | const char *bytes; |
| 903 | unsigned int len; |
| 904 | }; |
| 905 | |
| 906 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 907 | #endif /* HB_PRIVATE_HH */ |