blob: 179e4e9f7418f487475809ef6449b0ef775d54e3 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05006 *
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 Esfahbod2409d5f2011-04-21 17:14:28 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -050027 */
28
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040029#ifndef HB_PRIVATE_HH
30#define HB_PRIVATE_HH
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -050031
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -040032#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -040033#include "config.h"
34#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -050035
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040036#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040037#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040038#ifdef HAVE_OT
39#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040040#define HB_OT_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040041#endif
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040042
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040043#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040044#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040045#include <string.h>
46#include <assert.h>
Behdad Esfahbod1d521512010-04-28 13:18:41 -040047
48/* We only use these two for debug output. However, the debug code is
49 * always seen by the compiler (and optimized out in non-debug builds.
50 * If including these becomes a problem, we can start thinking about
51 * someway around that. */
Behdad Esfahbod7acb3892009-08-05 15:20:34 -040052#include <stdio.h>
53#include <errno.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -040054#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040055
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040056
Behdad Esfahbod52b41852015-10-03 13:20:55 +010057/* Compile-time custom allocator support. */
58
59#if defined(hb_malloc_impl) \
60 && defined(hb_calloc_impl) \
61 && defined(hb_realloc_impl) \
62 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -040063extern "C" void* hb_malloc_impl(size_t size);
64extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
65extern "C" void* hb_realloc_impl(void *ptr, size_t size);
66extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +010067#define malloc hb_malloc_impl
68#define calloc hb_calloc_impl
69#define realloc hb_realloc_impl
70#define free hb_free_impl
71#endif
72
73
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040074/* Compiler attributes */
Behdad Esfahbodbc200452010-04-29 01:40:26 -040075
Behdad Esfahbodbc200452010-04-29 01:40:26 -040076
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040077#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
78#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
79#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
80#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
81#else
82#define likely(expr) (expr)
83#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -040084#endif
85
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040086#ifndef __GNUC__
87#undef __attribute__
88#define __attribute__(x)
89#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -040090
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040091#if __GNUC__ >= 3
92#define HB_PURE_FUNC __attribute__((pure))
93#define HB_CONST_FUNC __attribute__((const))
94#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
95#else
96#define HB_PURE_FUNC
97#define HB_CONST_FUNC
98#define HB_PRINTF_FUNC(format_idx, arg_idx)
99#endif
100#if __GNUC__ >= 4
101#define HB_UNUSED __attribute__((unused))
102#else
103#define HB_UNUSED
104#endif
105
106#ifndef HB_INTERNAL
Behdad Esfahbodf1a8d502014-07-19 16:52:32 -0400107# if !defined(__MINGW32__) && !defined(__CYGWIN__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400108# define HB_INTERNAL __attribute__((__visibility__("hidden")))
109# else
110# define HB_INTERNAL
111# endif
112#endif
113
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400114#if __GNUC__ >= 3
115#define HB_FUNC __PRETTY_FUNCTION__
116#elif defined(_MSC_VER)
117#define HB_FUNC __FUNCSIG__
118#else
119#define HB_FUNC __func__
120#endif
121
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200122/*
123 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
124 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
125 * cases that fall through without a break or return statement. HB_FALLTHROUGH
126 * is only needed on cases that have code:
127 *
128 * switch (foo) {
129 * case 1: // These cases have no code. No fallthrough annotations are needed.
130 * case 2:
131 * case 3:
132 * foo = 4; // This case has code, so a fallthrough annotation is needed:
133 * HB_FALLTHROUGH;
134 * default:
135 * return foo;
136 * }
137 */
138#if defined(__clang__) && __cplusplus >= 201103L
139 /* clang's fallthrough annotations are only available starting in C++11. */
140# define HB_FALLTHROUGH [[clang::fallthrough]]
141#elif defined(_MSC_VER)
142 /*
143 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
144 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
145 */
146# include <sal.h>
147# define HB_FALLTHROUGH __fallthrough
148#else
149# define HB_FALLTHROUGH /* FALLTHROUGH */
150#endif
151
Behdad Esfahbod0fc0a102014-07-21 11:12:54 -0400152#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahboddb308282014-07-19 16:32:04 -0400153 /* We need Windows Vista for both Uniscribe backend and for
154 * MemoryBarrier. We don't support compiling on Windows XP,
155 * though we run on it fine. */
156# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
157# undef _WIN32_WINNT
158# endif
159# ifndef _WIN32_WINNT
160# define _WIN32_WINNT 0x0600
161# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400162# ifndef WIN32_LEAN_AND_MEAN
163# define WIN32_LEAN_AND_MEAN 1
164# endif
165# ifndef STRICT
166# define STRICT 1
167# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500168
Konstantin Rittf3537b62015-01-25 09:50:51 +0400169# if defined(_WIN32_WCE)
170 /* Some things not defined on Windows CE. */
Behdad Esfahbod56071882015-04-01 11:04:33 -0700171# define strdup _strdup
Konstantin Rittf3537b62015-01-25 09:50:51 +0400172# define getenv(Name) NULL
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400173# if _WIN32_WCE < 0x800
174# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400175static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400176# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400177# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
178# define getenv(Name) NULL
179# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700180# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400181# define snprintf _snprintf
ThePhDe0a828e2015-06-23 09:07:17 -0400182# elif defined(_MSC_VER) && _MSC_VER >= 1900
ThePhD8ad89f02015-06-23 09:09:24 -0400183# /* Covers VC++ Error for strdup being a deprecated POSIX name and to instead use _strdup instead */
ThePhDe0a828e2015-06-23 09:07:17 -0400184# define strdup _strdup
Konstantin Rittf3537b62015-01-25 09:50:51 +0400185# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400186#endif
187
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400188#if HAVE_ATEXIT
189/* atexit() is only safe to be called from shared libraries on certain
190 * platforms. Whitelist.
191 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
192# if defined(__linux) && defined(__GLIBC_PREREQ)
193# if __GLIBC_PREREQ(2,3)
194/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
195# define HB_USE_ATEXIT 1
196# endif
197# elif defined(_MSC_VER) || defined(__MINGW32__)
198/* For MSVC:
199 * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx
200 * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx
201 * mingw32 headers say atexit is safe to use in shared libraries.
202 */
203# define HB_USE_ATEXIT 1
204# elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
205/* This was fixed in Android NKD r8 or r8b:
206 * https://code.google.com/p/android/issues/detail?id=6455
207 * which introduced GCC 4.6:
208 * https://developer.android.com/tools/sdk/ndk/index.html
209 */
210# define HB_USE_ATEXIT 1
211# endif
212#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500213
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400214/* Basics */
215
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400216
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400217#ifndef NULL
218# define NULL ((void *) 0)
219#endif
220
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400221#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700222template <typename Type>
223static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400224
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500225#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700226template <typename Type>
227static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400228
Behdad Esfahbod16f175c2013-11-12 17:22:49 -0500229static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
230{ return (a + (b - 1)) / b; }
231
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500232
Behdad Esfahbod45917532009-11-04 18:15:59 -0500233#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700234template <typename Type, unsigned int n>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500235static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400236/* A const version, but does not detect erratically being called on pointers. */
237#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400238
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400239#define HB_STMT_START do
240#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500241
Behdad Esfahbodc3064102014-06-03 16:59:41 -0400242#define _ASSERT_STATIC1(_line, _cond) HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400243#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
244#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500245
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700246template <unsigned int cond> class hb_assert_constant_t {};
247
248#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400249
Behdad Esfahbod4be46ba2012-05-11 14:39:01 +0200250#define _PASTE1(a,b) a##b
251#define PASTE(a,b) _PASTE1(a,b)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400252
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400253/* Lets assert int types. Saves trouble down the road. */
254
255ASSERT_STATIC (sizeof (int8_t) == 1);
256ASSERT_STATIC (sizeof (uint8_t) == 1);
257ASSERT_STATIC (sizeof (int16_t) == 2);
258ASSERT_STATIC (sizeof (uint16_t) == 2);
259ASSERT_STATIC (sizeof (int32_t) == 4);
260ASSERT_STATIC (sizeof (uint32_t) == 4);
261ASSERT_STATIC (sizeof (int64_t) == 8);
262ASSERT_STATIC (sizeof (uint64_t) == 8);
263
Behdad Esfahbodb13640d2011-04-11 12:29:31 -0400264ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
265ASSERT_STATIC (sizeof (hb_position_t) == 4);
266ASSERT_STATIC (sizeof (hb_mask_t) == 4);
Behdad Esfahbodae9eeaf2011-04-11 11:49:08 -0400267ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400268
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400269
270/* We like our types POD */
271
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400272#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
273#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
274#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400275
276#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400277# define _ASSERT_INSTANCE_POD1(_line, _instance) \
278 HB_STMT_START { \
279 typedef __typeof__(_instance) _type_##_line; \
280 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
281 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400282#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400283# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400284#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400285# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
286# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400287
288/* Check _assertion in a method environment */
289#define _ASSERT_POD1(_line) \
Behdad Esfahboddac86022014-06-03 17:57:00 -0400290 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400291 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
292# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
293# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400294
295
296
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400297/* Misc */
298
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400299/* Void! */
300struct _hb_void_t {};
Behdad Esfahboda5e4f6d2015-06-10 10:57:46 -0700301typedef const _hb_void_t *hb_void_t;
302#define HB_VOID ((const _hb_void_t *) NULL)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400303
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100304/* Return the number of 1 bits in mask. */
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400305static inline HB_CONST_FUNC unsigned int
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400306_hb_popcount32 (uint32_t mask)
307{
308#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100309 return __builtin_popcount (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400310#else
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100311 /* "HACKMEM 169" */
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700312 uint32_t y;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100313 y = (mask >> 1) &033333333333;
314 y = mask - y - ((y >>1) & 033333333333);
315 return (((y + (y >> 3)) & 030707070707) % 077);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400316#endif
317}
318
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100319/* Returns the number of bits needed to store number */
320static inline HB_CONST_FUNC unsigned int
321_hb_bit_storage (unsigned int number)
322{
323#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100324 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100325#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700326 unsigned int n_bits = 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100327 while (number) {
328 n_bits++;
329 number >>= 1;
330 }
331 return n_bits;
332#endif
333}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400334
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100335/* Returns the number of zero bits in the least significant side of number */
336static inline HB_CONST_FUNC unsigned int
337_hb_ctz (unsigned int number)
338{
339#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
340 return likely (number) ? __builtin_ctz (number) : 0;
341#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700342 unsigned int n_bits = 0;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100343 if (unlikely (!number)) return 0;
344 while (!(number & 1)) {
345 n_bits++;
346 number >>= 1;
347 }
348 return n_bits;
349#endif
350}
351
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400352static inline bool
353_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
354{
355 return (size > 0) && (count >= ((unsigned int) -1) / size);
356}
357
358
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400359/* Type of bsearch() / qsort() compare function */
360typedef int (*hb_compare_func_t) (const void *, const void *);
361
362
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400363
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400364
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400365/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400366
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400367
Behdad Esfahbod7e8c3892014-07-25 11:18:11 -0400368#define HB_PREALLOCED_ARRAY_INIT {0, 0, NULL}
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400369template <typename Type, unsigned int StaticSize=16>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400370struct hb_prealloced_array_t
371{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400372 unsigned int len;
373 unsigned int allocated;
374 Type *array;
375 Type static_array[StaticSize];
376
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400377 void init (void) { memset (this, 0, sizeof (*this)); }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200378
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400379 inline Type& operator [] (unsigned int i) { return array[i]; }
380 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400381
382 inline Type *push (void)
383 {
384 if (!array) {
385 array = static_array;
386 allocated = ARRAY_LENGTH (static_array);
387 }
388 if (likely (len < allocated))
389 return &array[len++];
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400390
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400391 /* Need to reallocate */
392 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400393 Type *new_array = NULL;
394
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400395 if (array == static_array) {
396 new_array = (Type *) calloc (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400397 if (new_array)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400398 memcpy (new_array, array, len * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400399 } else {
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400400 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400401 if (likely (!overflows)) {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400402 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400403 }
404 }
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400405
406 if (unlikely (!new_array))
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400407 return NULL;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400408
409 array = new_array;
410 allocated = new_allocated;
411 return &array[len++];
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400412 }
413
414 inline void pop (void)
415 {
416 len--;
Behdad Esfahbod639afdc2013-08-06 14:28:12 -0400417 }
418
419 inline void remove (unsigned int i)
420 {
421 if (unlikely (i >= len))
422 return;
423 memmove (static_cast<void *> (&array[i]),
424 static_cast<void *> (&array[i + 1]),
425 (len - i - 1) * sizeof (Type));
426 len--;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400427 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400428
429 inline void shrink (unsigned int l)
430 {
431 if (l < len)
432 len = l;
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400433 }
434
Behdad Esfahbod68435692011-05-05 14:12:37 -0400435 template <typename T>
436 inline Type *find (T v) {
437 for (unsigned int i = 0; i < len; i++)
438 if (array[i] == v)
439 return &array[i];
440 return NULL;
441 }
442 template <typename T>
443 inline const Type *find (T v) const {
444 for (unsigned int i = 0; i < len; i++)
445 if (array[i] == v)
446 return &array[i];
447 return NULL;
448 }
449
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400450 inline void qsort (void)
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400451 {
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400452 ::qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400453 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400454
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400455 inline void qsort (unsigned int start, unsigned int end)
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400456 {
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400457 ::qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400458 }
459
Behdad Esfahbod68435692011-05-05 14:12:37 -0400460 template <typename T>
461 inline Type *bsearch (T *key)
462 {
463 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
464 }
465 template <typename T>
466 inline const Type *bsearch (T *key) const
467 {
468 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
469 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400470
471 inline void finish (void)
472 {
473 if (array != static_array)
474 free (array);
475 array = NULL;
476 allocated = len = 0;
477 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400478};
479
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500480template <typename Type>
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400481struct hb_auto_array_t : hb_prealloced_array_t <Type>
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500482{
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400483 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
484 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500485};
486
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400487
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400488#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400489template <typename item_t, typename lock_t>
490struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400491{
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400492 hb_prealloced_array_t <item_t, 2> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400493
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400494 inline void init (void) { items.init (); }
495
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400496 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200497 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400498 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400499 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400500 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400501 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200502 if (replace) {
503 item_t old = *item;
504 *item = v;
505 l.unlock ();
506 old.finish ();
507 }
508 else {
509 item = NULL;
510 l.unlock ();
511 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400512 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400513 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400514 if (likely (item))
515 *item = v;
516 l.unlock ();
517 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400518 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400519 }
520
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400521 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400522 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400523 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400524 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400525 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400526 if (item) {
527 item_t old = *item;
528 *item = items[items.len - 1];
529 items.pop ();
530 l.unlock ();
531 old.finish ();
532 } else {
533 l.unlock ();
534 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400535 }
536
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400537 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400538 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400539 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400540 l.lock ();
541 item_t *item = items.find (v);
542 if (item)
543 *i = *item;
544 l.unlock ();
545 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400546 }
547
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400548 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400549 inline item_t *find_or_insert (T v, lock_t &l)
550 {
551 l.lock ();
552 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400553 if (!item) {
554 item = items.push ();
555 if (likely (item))
556 *item = v;
557 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400558 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400559 return item;
560 }
561
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400562 inline void finish (lock_t &l)
563 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400564 if (!items.len) {
565 /* No need for locking. */
566 items.finish ();
567 return;
568 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400569 l.lock ();
570 while (items.len) {
571 item_t old = items[items.len - 1];
572 items.pop ();
573 l.unlock ();
574 old.finish ();
575 l.lock ();
576 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400577 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400578 l.unlock ();
579 }
580
581};
582
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400583
Behdad Esfahbod41880962011-04-11 14:58:28 -0400584/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400585
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500586static inline bool ISALPHA (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400587{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500588static inline bool ISALNUM (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400589{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500590static inline bool ISSPACE (unsigned char c)
591{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400592static inline unsigned char TOUPPER (unsigned char c)
593{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
594static inline unsigned char TOLOWER (unsigned char c)
595{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400596
Behdad Esfahbod41880962011-04-11 14:58:28 -0400597#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
598 ((const char *) s)[1], \
599 ((const char *) s)[2], \
600 ((const char *) s)[3]))
601
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400602
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400603/* C++ helpers */
604
605/* Makes class uncopyable. Use in private: section. */
606#define NO_COPY(T) \
607 T (const T &o); \
Behdad Esfahbod970e0922011-06-14 14:35:44 -0400608 T &operator = (const T &o)
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400609
610
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400611/* Debug */
612
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400613
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900614/* HB_NDEBUG disables some sanity checks that are very safe to disable and
615 * should be disabled in production systems. If NDEBUG is defined, enable
616 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
617 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
618 * the costlier checks. */
619#ifdef NDEBUG
620#define HB_NDEBUG
621#endif
622
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400623#ifndef HB_DEBUG
624#define HB_DEBUG 0
625#endif
626
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400627static inline bool
628_hb_debug (unsigned int level,
629 unsigned int max_level)
630{
631 return level < max_level;
632}
633
Behdad Esfahbod6c15ddf2013-04-30 11:34:00 -0400634#define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
635#define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400636
Behdad Esfahbod5f541f82015-02-21 16:51:17 +0300637static inline void
638_hb_print_func (const char *func)
639{
640 if (func)
641 {
642 unsigned int func_len = strlen (func);
643 /* Skip "static" */
644 if (0 == strncmp (func, "static ", 7))
645 func += 7;
646 /* Skip "typename" */
647 if (0 == strncmp (func, "typename ", 9))
648 func += 9;
649 /* Skip return type */
650 const char *space = strchr (func, ' ');
651 if (space)
652 func = space + 1;
653 /* Skip parameter list */
654 const char *paren = strchr (func, '(');
655 if (paren)
656 func_len = paren - func;
657 fprintf (stderr, "%.*s", func_len, func);
658 }
659}
660
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400661template <int max_level> static inline void
Behdad Esfahbod20810972012-05-10 23:06:58 +0200662_hb_debug_msg_va (const char *what,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200663 const void *obj,
664 const char *func,
665 bool indented,
666 unsigned int level,
667 int level_dir,
668 const char *message,
Behdad Esfahbod29e25552014-08-12 17:02:59 -0400669 va_list ap) HB_PRINTF_FUNC(7, 0);
670template <int max_level> static inline void
671_hb_debug_msg_va (const char *what,
672 const void *obj,
673 const char *func,
674 bool indented,
675 unsigned int level,
676 int level_dir,
677 const char *message,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200678 va_list ap)
Behdad Esfahbod20810972012-05-10 23:06:58 +0200679{
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200680 if (!_hb_debug (level, max_level))
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200681 return;
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200682
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200683 fprintf (stderr, "%-10s", what ? what : "");
684
685 if (obj)
Behdad Esfahbod5ccfe8e2012-05-11 02:19:41 +0200686 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200687 else
688 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
689
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200690 if (indented) {
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400691/* One may want to add ASCII version of these. See:
692 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
693#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
694#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
695#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
696#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
697#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
698 static const char bars[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
Behdad Esfahbod7235f332013-06-10 14:39:51 -0400699 fprintf (stderr, "%2u %s" VRBAR "%s",
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200700 level,
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400701 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
702 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200703 } else
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400704 fprintf (stderr, " " VRBAR LBAR);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200705
Behdad Esfahbod5f541f82015-02-21 16:51:17 +0300706 _hb_print_func (func);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200707
708 if (message)
Behdad Esfahbod5f541f82015-02-21 16:51:17 +0300709 {
710 fprintf (stderr, ": ");
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200711 vfprintf (stderr, message, ap);
Behdad Esfahbod5f541f82015-02-21 16:51:17 +0300712 }
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200713
714 fprintf (stderr, "\n");
Behdad Esfahbod20810972012-05-10 23:06:58 +0200715}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200716template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200717_hb_debug_msg_va<0> (const char *what HB_UNUSED,
718 const void *obj HB_UNUSED,
719 const char *func HB_UNUSED,
720 bool indented HB_UNUSED,
721 unsigned int level HB_UNUSED,
722 int level_dir HB_UNUSED,
723 const char *message HB_UNUSED,
724 va_list ap HB_UNUSED) {}
Behdad Esfahbod20810972012-05-10 23:06:58 +0200725
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400726template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400727_hb_debug_msg (const char *what,
728 const void *obj,
729 const char *func,
730 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200731 unsigned int level,
732 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400733 const char *message,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200734 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400735template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400736_hb_debug_msg (const char *what,
737 const void *obj,
738 const char *func,
739 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200740 unsigned int level,
741 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400742 const char *message,
743 ...)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400744{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400745 va_list ap;
746 va_start (ap, message);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200747 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400748 va_end (ap);
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400749}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200750template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200751_hb_debug_msg<0> (const char *what HB_UNUSED,
752 const void *obj HB_UNUSED,
753 const char *func HB_UNUSED,
754 bool indented HB_UNUSED,
755 unsigned int level HB_UNUSED,
756 int level_dir HB_UNUSED,
757 const char *message HB_UNUSED,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200758 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200759template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200760_hb_debug_msg<0> (const char *what HB_UNUSED,
761 const void *obj HB_UNUSED,
762 const char *func HB_UNUSED,
763 bool indented HB_UNUSED,
764 unsigned int level HB_UNUSED,
765 int level_dir HB_UNUSED,
766 const char *message HB_UNUSED,
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200767 ...) {}
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400768
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400769#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
770#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
771#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400772
773
774/*
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500775 * Printer
776 */
777
778template <typename T>
Behdad Esfahbod5c7d6f02014-12-12 20:28:49 -0800779struct hb_printer_t {
780 const char *print (const T&) { return "something"; }
781};
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500782
783template <>
784struct hb_printer_t<bool> {
785 const char *print (bool v) { return v ? "true" : "false"; }
786};
787
788template <>
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500789struct hb_printer_t<hb_void_t> {
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500790 const char *print (hb_void_t) { return ""; }
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500791};
792
793
794/*
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400795 * Trace
796 */
797
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500798template <typename T>
799static inline void _hb_warn_no_return (bool returned)
800{
801 if (unlikely (!returned)) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100802 fprintf (stderr, "OUCH, returned with no call to return_trace(). This is a bug, please report.\n");
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500803 }
804}
805template <>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -0700806/*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500807{}
808
809template <int max_level, typename ret_t>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400810struct hb_auto_trace_t {
811 explicit inline hb_auto_trace_t (unsigned int *plevel_,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200812 const char *what_,
813 const void *obj_,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400814 const char *func,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200815 const char *message,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200816 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400817 {
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200818 if (plevel) ++*plevel;
Behdad Esfahbod20810972012-05-10 23:06:58 +0200819
820 va_list ap;
821 va_start (ap, message);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400822 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
Behdad Esfahbod20810972012-05-10 23:06:58 +0200823 va_end (ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400824 }
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200825 inline ~hb_auto_trace_t (void)
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200826 {
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500827 _hb_warn_no_return<ret_t> (returned);
828 if (!returned) {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400829 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200830 }
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200831 if (plevel) --*plevel;
832 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400833
Behdad Esfahbod2c9d6482012-11-23 16:49:19 -0500834 inline ret_t ret (ret_t v, unsigned int line = 0)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200835 {
836 if (unlikely (returned)) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100837 fprintf (stderr, "OUCH, double calls to return_trace(). This is a bug, please report.\n");
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200838 return v;
839 }
840
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500841 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
842 "return %s (line %d)",
843 hb_printer_t<ret_t>().print (v), line);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200844 if (plevel) --*plevel;
845 plevel = NULL;
846 returned = true;
847 return v;
848 }
849
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400850 private:
851 unsigned int *plevel;
Behdad Esfahbod96595232012-05-11 03:33:36 +0200852 const char *what;
853 const void *obj;
Behdad Esfahbodc779d822012-11-23 14:07:24 -0500854 bool returned;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400855};
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500856template <typename ret_t> /* Optimize when tracing is disabled */
857struct hb_auto_trace_t<0, ret_t> {
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200858 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
859 const char *what HB_UNUSED,
860 const void *obj HB_UNUSED,
861 const char *func HB_UNUSED,
862 const char *message HB_UNUSED,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200863 ...) {}
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200864
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500865 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400866};
867
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100868#define return_trace(RET) return trace.ret (RET, __LINE__)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400869
870/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400871
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400872template <typename T> class hb_assert_unsigned_t;
873template <> class hb_assert_unsigned_t<unsigned char> {};
Dominik Röttsches9e7c7202014-07-17 14:40:34 +0300874template <> class hb_assert_unsigned_t<unsigned short> {};
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400875template <> class hb_assert_unsigned_t<unsigned int> {};
876template <> class hb_assert_unsigned_t<unsigned long> {};
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400877
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400878template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400879hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400880{
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400881 /* The sizeof() is here to force template instantiation.
882 * I'm sure there are better ways to do this but can't think of
883 * one right now. Declaring a variable won't work as HB_UNUSED
Behdad Esfahbod68e04af2015-02-21 16:30:28 +0300884 * is unusable on some platforms and unused types are less likely
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400885 * to generate a warning than unused variables. */
886 ASSERT_STATIC (sizeof (hb_assert_unsigned_t<T>) >= 0);
887
Behdad Esfahbodc2b151d2014-08-10 18:52:07 -0400888 /* The casts below are important as if T is smaller than int,
889 * the subtract results will become a signed int! */
890 return (T)(u - lo) <= (T)(hi - lo);
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400891}
892
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400893template <typename T> static inline bool
Behdad Esfahbodc98b7182013-12-31 15:55:40 +0800894hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
895{
896 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
897}
898
899template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400900hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400901{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400902 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400903}
904
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400905
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800906/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800907/* To my surprise, looks like the function resolver is happy to silently cast
908 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800909 * originally had in mind... :(.
910 *
Behdad Esfahbodf94c0ec2015-11-20 13:21:29 -0800911 * For MSVC warnings, see: https://github.com/behdad/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800912 */
913#ifdef _MSC_VER
914# pragma warning(disable:4200)
915# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800916#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800917#define HB_MARK_AS_FLAG_T(T) \
918 extern "C++" { \
919 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
920 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
921 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
922 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
923 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
924 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
925 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
926 }
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800927
928
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400929/* Useful for set-operations on small enums.
930 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbodf8160a42015-07-21 15:50:02 +0100931 * (FLAG_SAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400932 */
Behdad Esfahbodf8160a42015-07-21 15:50:02 +0100933#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((x) < 32) + (1U << (x)))
934#define FLAG_SAFE(x) (1U << (x))
Behdad Esfahbod5b5617e2015-07-21 15:52:15 +0100935#define FLAG_UNSAFE(x) ((x) < 32 ? FLAG_SAFE(x) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400936#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400937
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400938
Behdad Esfahbodc1e87442014-10-01 11:07:08 -0400939template <typename T, typename T2> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100940hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400941{
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100942 for (unsigned int i = 1; i < len; i++)
943 {
944 unsigned int j = i;
945 while (j && compar (&array[j - 1], &array[i]) > 0)
946 j--;
947 if (i == j)
948 continue;
949 /* Move item i to occupy place for item j, shift what's in between. */
950 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100951 T t = array[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100952 memmove (&array[j + 1], &array[j], (i - j) * sizeof (T));
953 array[j] = t;
954 }
955 if (array2)
956 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100957 T2 t = array2[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100958 memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2));
959 array2[j] = t;
960 }
961 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400962}
963
Behdad Esfahbod250398b2014-10-01 11:28:01 -0400964template <typename T> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100965hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400966{
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100967 hb_stable_sort (array, len, compar, (int *) NULL);
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400968}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400969
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400970static inline hb_bool_t
971hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
972{
973 /* Pain because we don't know whether s is nul-terminated. */
974 char buf[64];
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500975 len = MIN (ARRAY_LENGTH (buf) - 1, len);
976 strncpy (buf, s, len);
977 buf[len] = '\0';
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400978
979 char *end;
980 errno = 0;
981 unsigned long v = strtoul (buf, &end, base);
982 if (errno) return false;
983 if (*end) return false;
984 *out = v;
985 return true;
986}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400987
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400988
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500989/* Global runtime options. */
990
991struct hb_options_t
992{
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700993 unsigned int initialized : 1;
994 unsigned int uniscribe_bug_compatible : 1;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500995};
996
997union hb_options_union_t {
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700998 unsigned int i;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500999 hb_options_t opts;
1000};
1001ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t));
1002
1003HB_INTERNAL void
1004_hb_options_init (void);
1005
1006extern HB_INTERNAL hb_options_union_t _hb_options;
1007
1008static inline hb_options_t
1009hb_options (void)
1010{
1011 if (unlikely (!_hb_options.i))
1012 _hb_options_init ();
1013
1014 return _hb_options.opts;
1015}
1016
Steven R. Loomisa13b0232015-12-11 10:21:27 -08001017/* Size signifying variable-sized array */
1018#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -05001019
Behdad Esfahbodc57d4542011-04-20 18:50:27 -04001020#endif /* HB_PRIVATE_HH */