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