blob: a451d164f91a0de224be1793e26c8c544eb9df29 [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 Esfahbodae2b8542014-06-03 16:59:09 -040057/* Compiler attributes */
Behdad Esfahbodbc200452010-04-29 01:40:26 -040058
Behdad Esfahbodbc200452010-04-29 01:40:26 -040059
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040060#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
61#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
62#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
63#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
64#else
65#define likely(expr) (expr)
66#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -040067#endif
68
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040069#ifndef __GNUC__
70#undef __attribute__
71#define __attribute__(x)
72#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -040073
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040074#if __GNUC__ >= 3
75#define HB_PURE_FUNC __attribute__((pure))
76#define HB_CONST_FUNC __attribute__((const))
77#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
78#else
79#define HB_PURE_FUNC
80#define HB_CONST_FUNC
81#define HB_PRINTF_FUNC(format_idx, arg_idx)
82#endif
83#if __GNUC__ >= 4
84#define HB_UNUSED __attribute__((unused))
85#else
86#define HB_UNUSED
87#endif
88
89#ifndef HB_INTERNAL
90# ifndef __MINGW32__
91# define HB_INTERNAL __attribute__((__visibility__("hidden")))
92# else
93# define HB_INTERNAL
94# endif
95#endif
96
97#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
98#define snprintf _snprintf
99#endif
100
101#ifdef _MSC_VER
102#undef inline
103#define inline __inline
104#endif
105
106#ifdef __STRICT_ANSI__
107#undef inline
108#define inline __inline__
109#endif
110
111#if __GNUC__ >= 3
112#define HB_FUNC __PRETTY_FUNCTION__
113#elif defined(_MSC_VER)
114#define HB_FUNC __FUNCSIG__
115#else
116#define HB_FUNC __func__
117#endif
118
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500119
120
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400121/* Basics */
122
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400123
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400124#ifndef NULL
125# define NULL ((void *) 0)
126#endif
127
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400128#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700129template <typename Type>
130static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400131
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500132#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700133template <typename Type>
134static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400135
Behdad Esfahbod16f175c2013-11-12 17:22:49 -0500136static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
137{ return (a + (b - 1)) / b; }
138
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500139
Behdad Esfahbod45917532009-11-04 18:15:59 -0500140#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700141template <typename Type, unsigned int n>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500142static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400143/* A const version, but does not detect erratically being called on pointers. */
144#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400145
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400146#define HB_STMT_START do
147#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500148
Behdad Esfahbodc3064102014-06-03 16:59:41 -0400149#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 -0400150#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
151#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500152
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400153#define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahbod4ec30ae2011-06-28 14:13:38 -0400154#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400155
Behdad Esfahbod4be46ba2012-05-11 14:39:01 +0200156#define _PASTE1(a,b) a##b
157#define PASTE(a,b) _PASTE1(a,b)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400158
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400159/* Lets assert int types. Saves trouble down the road. */
160
161ASSERT_STATIC (sizeof (int8_t) == 1);
162ASSERT_STATIC (sizeof (uint8_t) == 1);
163ASSERT_STATIC (sizeof (int16_t) == 2);
164ASSERT_STATIC (sizeof (uint16_t) == 2);
165ASSERT_STATIC (sizeof (int32_t) == 4);
166ASSERT_STATIC (sizeof (uint32_t) == 4);
167ASSERT_STATIC (sizeof (int64_t) == 8);
168ASSERT_STATIC (sizeof (uint64_t) == 8);
169
Behdad Esfahbodb13640d2011-04-11 12:29:31 -0400170ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
171ASSERT_STATIC (sizeof (hb_position_t) == 4);
172ASSERT_STATIC (sizeof (hb_mask_t) == 4);
Behdad Esfahbodae9eeaf2011-04-11 11:49:08 -0400173ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400174
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400175
176/* We like our types POD */
177
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400178#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
179#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
180#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400181
182#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400183# define _ASSERT_INSTANCE_POD1(_line, _instance) \
184 HB_STMT_START { \
185 typedef __typeof__(_instance) _type_##_line; \
186 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
187 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400188#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400189# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400190#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400191# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
192# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400193
194/* Check _assertion in a method environment */
195#define _ASSERT_POD1(_line) \
Behdad Esfahboddac86022014-06-03 17:57:00 -0400196 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400197 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
198# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
199# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400200
201
202
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400203/* Misc */
204
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400205/* Void! */
206struct _hb_void_t {};
207typedef const _hb_void_t &hb_void_t;
208#define HB_VOID (* (const _hb_void_t *) NULL)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400209
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100210/* Return the number of 1 bits in mask. */
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400211static inline HB_CONST_FUNC unsigned int
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400212_hb_popcount32 (uint32_t mask)
213{
214#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100215 return __builtin_popcount (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400216#else
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100217 /* "HACKMEM 169" */
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700218 uint32_t y;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100219 y = (mask >> 1) &033333333333;
220 y = mask - y - ((y >>1) & 033333333333);
221 return (((y + (y >> 3)) & 030707070707) % 077);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400222#endif
223}
224
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100225/* Returns the number of bits needed to store number */
226static inline HB_CONST_FUNC unsigned int
227_hb_bit_storage (unsigned int number)
228{
229#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100230 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100231#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700232 unsigned int n_bits = 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100233 while (number) {
234 n_bits++;
235 number >>= 1;
236 }
237 return n_bits;
238#endif
239}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400240
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100241/* Returns the number of zero bits in the least significant side of number */
242static inline HB_CONST_FUNC unsigned int
243_hb_ctz (unsigned int number)
244{
245#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
246 return likely (number) ? __builtin_ctz (number) : 0;
247#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700248 unsigned int n_bits = 0;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100249 if (unlikely (!number)) return 0;
250 while (!(number & 1)) {
251 n_bits++;
252 number >>= 1;
253 }
254 return n_bits;
255#endif
256}
257
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400258static inline bool
259_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
260{
261 return (size > 0) && (count >= ((unsigned int) -1) / size);
262}
263
264
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400265/* Type of bsearch() / qsort() compare function */
266typedef int (*hb_compare_func_t) (const void *, const void *);
267
268
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400269
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400270
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400271/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400272
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400273
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400274#define HB_PREALLOCED_ARRAY_INIT {0}
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400275template <typename Type, unsigned int StaticSize=16>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400276struct hb_prealloced_array_t
277{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400278 unsigned int len;
279 unsigned int allocated;
280 Type *array;
281 Type static_array[StaticSize];
282
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400283 void init (void) { memset (this, 0, sizeof (*this)); }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200284
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400285 inline Type& operator [] (unsigned int i) { return array[i]; }
286 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400287
288 inline Type *push (void)
289 {
290 if (!array) {
291 array = static_array;
292 allocated = ARRAY_LENGTH (static_array);
293 }
294 if (likely (len < allocated))
295 return &array[len++];
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400296
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400297 /* Need to reallocate */
298 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400299 Type *new_array = NULL;
300
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400301 if (array == static_array) {
302 new_array = (Type *) calloc (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400303 if (new_array)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400304 memcpy (new_array, array, len * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400305 } else {
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400306 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400307 if (likely (!overflows)) {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400308 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400309 }
310 }
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400311
312 if (unlikely (!new_array))
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400313 return NULL;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400314
315 array = new_array;
316 allocated = new_allocated;
317 return &array[len++];
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400318 }
319
320 inline void pop (void)
321 {
322 len--;
Behdad Esfahbod639afdc2013-08-06 14:28:12 -0400323 }
324
325 inline void remove (unsigned int i)
326 {
327 if (unlikely (i >= len))
328 return;
329 memmove (static_cast<void *> (&array[i]),
330 static_cast<void *> (&array[i + 1]),
331 (len - i - 1) * sizeof (Type));
332 len--;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400333 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400334
335 inline void shrink (unsigned int l)
336 {
337 if (l < len)
338 len = l;
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400339 }
340
Behdad Esfahbod68435692011-05-05 14:12:37 -0400341 template <typename T>
342 inline Type *find (T v) {
343 for (unsigned int i = 0; i < len; i++)
344 if (array[i] == v)
345 return &array[i];
346 return NULL;
347 }
348 template <typename T>
349 inline const Type *find (T v) const {
350 for (unsigned int i = 0; i < len; i++)
351 if (array[i] == v)
352 return &array[i];
353 return NULL;
354 }
355
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400356 inline void qsort (void)
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400357 {
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400358 ::qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400359 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400360
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400361 inline void qsort (unsigned int start, unsigned int end)
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400362 {
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400363 ::qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400364 }
365
Behdad Esfahbod68435692011-05-05 14:12:37 -0400366 template <typename T>
367 inline Type *bsearch (T *key)
368 {
369 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
370 }
371 template <typename T>
372 inline const Type *bsearch (T *key) const
373 {
374 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
375 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400376
377 inline void finish (void)
378 {
379 if (array != static_array)
380 free (array);
381 array = NULL;
382 allocated = len = 0;
383 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400384};
385
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500386template <typename Type>
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400387struct hb_auto_array_t : hb_prealloced_array_t <Type>
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500388{
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400389 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
390 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500391};
392
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400393
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400394#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400395template <typename item_t, typename lock_t>
396struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400397{
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400398 hb_prealloced_array_t <item_t, 2> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400399
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400400 inline void init (void) { items.init (); }
401
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400402 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200403 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400404 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400405 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400406 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400407 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200408 if (replace) {
409 item_t old = *item;
410 *item = v;
411 l.unlock ();
412 old.finish ();
413 }
414 else {
415 item = NULL;
416 l.unlock ();
417 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400418 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400419 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400420 if (likely (item))
421 *item = v;
422 l.unlock ();
423 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400424 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400425 }
426
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400427 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400428 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400429 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400430 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400431 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400432 if (item) {
433 item_t old = *item;
434 *item = items[items.len - 1];
435 items.pop ();
436 l.unlock ();
437 old.finish ();
438 } else {
439 l.unlock ();
440 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400441 }
442
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400443 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400444 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400445 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400446 l.lock ();
447 item_t *item = items.find (v);
448 if (item)
449 *i = *item;
450 l.unlock ();
451 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400452 }
453
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400454 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400455 inline item_t *find_or_insert (T v, lock_t &l)
456 {
457 l.lock ();
458 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400459 if (!item) {
460 item = items.push ();
461 if (likely (item))
462 *item = v;
463 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400464 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400465 return item;
466 }
467
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400468 inline void finish (lock_t &l)
469 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400470 if (!items.len) {
471 /* No need for locking. */
472 items.finish ();
473 return;
474 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400475 l.lock ();
476 while (items.len) {
477 item_t old = items[items.len - 1];
478 items.pop ();
479 l.unlock ();
480 old.finish ();
481 l.lock ();
482 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400483 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400484 l.unlock ();
485 }
486
487};
488
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400489
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400490
491
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400492/* Big-endian handling */
493
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400494static inline uint16_t hb_be_uint16 (const uint16_t v)
495{
496 const uint8_t *V = (const uint8_t *) &v;
Behdad Esfahbodfcd6f532012-06-08 09:59:43 -0400497 return (V[0] << 8) | V[1];
498}
499
500static inline uint16_t hb_uint16_swap (const uint16_t v)
501{
502 return (v >> 8) | (v << 8);
503}
504
505static inline uint32_t hb_uint32_swap (const uint32_t v)
506{
507 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400508}
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400509
Behdad Esfahbode74616b2012-04-15 14:12:13 -0400510/* Note, of the following macros, uint16_get is the one called many many times.
511 * If there is any optimizations to be done, it's in that macro. However, I
512 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
513 * results in a single ror instruction, does NOT speed this up. In fact, it
514 * resulted in a minor slowdown. At any rate, note that v may not be correctly
515 * aligned, so I think the current implementation is optimal.
516 */
517
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400518#define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
Behdad Esfahbod7a52f282010-04-21 02:14:44 -0400519#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400520#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400521
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400522#define hb_be_uint32_put(v,V) HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
Behdad Esfahbod7a52f282010-04-21 02:14:44 -0400523#define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400524#define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400525
Behdad Esfahbod2b712bb2013-04-16 16:11:09 -0400526#define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500527#define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2])
528#define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
529
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400530
Behdad Esfahbod41880962011-04-11 14:58:28 -0400531/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400532
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500533static inline bool ISALPHA (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400534{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500535static inline bool ISALNUM (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400536{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500537static inline bool ISSPACE (unsigned char c)
538{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400539static inline unsigned char TOUPPER (unsigned char c)
540{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
541static inline unsigned char TOLOWER (unsigned char c)
542{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400543
Behdad Esfahbod41880962011-04-11 14:58:28 -0400544#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
545 ((const char *) s)[1], \
546 ((const char *) s)[2], \
547 ((const char *) s)[3]))
548
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400549
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400550/* C++ helpers */
551
552/* Makes class uncopyable. Use in private: section. */
553#define NO_COPY(T) \
554 T (const T &o); \
Behdad Esfahbod970e0922011-06-14 14:35:44 -0400555 T &operator = (const T &o)
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400556
557
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400558/* Debug */
559
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400560
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400561#ifndef HB_DEBUG
562#define HB_DEBUG 0
563#endif
564
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400565static inline bool
566_hb_debug (unsigned int level,
567 unsigned int max_level)
568{
569 return level < max_level;
570}
571
Behdad Esfahbod6c15ddf2013-04-30 11:34:00 -0400572#define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
573#define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400574
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400575template <int max_level> static inline void
Behdad Esfahbod20810972012-05-10 23:06:58 +0200576_hb_debug_msg_va (const char *what,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200577 const void *obj,
578 const char *func,
579 bool indented,
580 unsigned int level,
581 int level_dir,
582 const char *message,
583 va_list ap)
Behdad Esfahbod20810972012-05-10 23:06:58 +0200584{
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200585 if (!_hb_debug (level, max_level))
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200586 return;
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200587
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200588 fprintf (stderr, "%-10s", what ? what : "");
589
590 if (obj)
Behdad Esfahbod5ccfe8e2012-05-11 02:19:41 +0200591 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200592 else
593 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
594
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200595 if (indented) {
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400596/* One may want to add ASCII version of these. See:
597 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
598#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
599#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
600#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
601#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
602#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
603 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 -0400604 fprintf (stderr, "%2u %s" VRBAR "%s",
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200605 level,
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400606 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
607 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200608 } else
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400609 fprintf (stderr, " " VRBAR LBAR);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200610
Behdad Esfahbodadf77582012-11-23 17:32:00 -0500611 if (func)
612 {
Behdad Esfahbod29ff2542013-02-25 19:04:56 -0500613 unsigned int func_len = strlen (func);
614#ifndef HB_DEBUG_VERBOSE
Behdad Esfahbodadf77582012-11-23 17:32:00 -0500615 /* Skip "typename" */
616 if (0 == strncmp (func, "typename ", 9))
617 func += 9;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400618 /* Skip return type */
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200619 const char *space = strchr (func, ' ');
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400620 if (space)
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200621 func = space + 1;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400622 /* Skip parameter list */
623 const char *paren = strchr (func, '(');
Behdad Esfahbod29ff2542013-02-25 19:04:56 -0500624 if (paren)
625 func_len = paren - func;
626#endif
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200627 fprintf (stderr, "%.*s: ", func_len, func);
Behdad Esfahbod85f73fa2012-05-11 02:40:42 +0200628 }
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200629
630 if (message)
631 vfprintf (stderr, message, ap);
632
633 fprintf (stderr, "\n");
Behdad Esfahbod20810972012-05-10 23:06:58 +0200634}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200635template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200636_hb_debug_msg_va<0> (const char *what HB_UNUSED,
637 const void *obj HB_UNUSED,
638 const char *func HB_UNUSED,
639 bool indented HB_UNUSED,
640 unsigned int level HB_UNUSED,
641 int level_dir HB_UNUSED,
642 const char *message HB_UNUSED,
643 va_list ap HB_UNUSED) {}
Behdad Esfahbod20810972012-05-10 23:06:58 +0200644
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400645template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400646_hb_debug_msg (const char *what,
647 const void *obj,
648 const char *func,
649 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200650 unsigned int level,
651 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400652 const char *message,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200653 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400654template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400655_hb_debug_msg (const char *what,
656 const void *obj,
657 const char *func,
658 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200659 unsigned int level,
660 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400661 const char *message,
662 ...)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400663{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400664 va_list ap;
665 va_start (ap, message);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200666 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400667 va_end (ap);
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400668}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200669template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200670_hb_debug_msg<0> (const char *what HB_UNUSED,
671 const void *obj HB_UNUSED,
672 const char *func HB_UNUSED,
673 bool indented HB_UNUSED,
674 unsigned int level HB_UNUSED,
675 int level_dir HB_UNUSED,
676 const char *message HB_UNUSED,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200677 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200678template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200679_hb_debug_msg<0> (const char *what HB_UNUSED,
680 const void *obj HB_UNUSED,
681 const char *func HB_UNUSED,
682 bool indented HB_UNUSED,
683 unsigned int level HB_UNUSED,
684 int level_dir HB_UNUSED,
685 const char *message HB_UNUSED,
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200686 ...) {}
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400687
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400688#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
689#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
690#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 -0400691
692
693/*
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500694 * Printer
695 */
696
697template <typename T>
698struct hb_printer_t {};
699
700template <>
701struct hb_printer_t<bool> {
702 const char *print (bool v) { return v ? "true" : "false"; }
703};
704
705template <>
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500706struct hb_printer_t<hb_void_t> {
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500707 const char *print (hb_void_t) { return ""; }
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500708};
709
710
711/*
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400712 * Trace
713 */
714
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500715template <typename T>
716static inline void _hb_warn_no_return (bool returned)
717{
718 if (unlikely (!returned)) {
719 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
720 }
721}
722template <>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -0700723/*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500724{}
725
726template <int max_level, typename ret_t>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400727struct hb_auto_trace_t {
728 explicit inline hb_auto_trace_t (unsigned int *plevel_,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200729 const char *what_,
730 const void *obj_,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400731 const char *func,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200732 const char *message,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200733 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400734 {
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200735 if (plevel) ++*plevel;
Behdad Esfahbod20810972012-05-10 23:06:58 +0200736
737 va_list ap;
738 va_start (ap, message);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400739 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
Behdad Esfahbod20810972012-05-10 23:06:58 +0200740 va_end (ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400741 }
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200742 inline ~hb_auto_trace_t (void)
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200743 {
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500744 _hb_warn_no_return<ret_t> (returned);
745 if (!returned) {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400746 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200747 }
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200748 if (plevel) --*plevel;
749 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400750
Behdad Esfahbod2c9d6482012-11-23 16:49:19 -0500751 inline ret_t ret (ret_t v, unsigned int line = 0)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200752 {
753 if (unlikely (returned)) {
754 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
755 return v;
756 }
757
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500758 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
759 "return %s (line %d)",
760 hb_printer_t<ret_t>().print (v), line);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200761 if (plevel) --*plevel;
762 plevel = NULL;
763 returned = true;
764 return v;
765 }
766
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400767 private:
768 unsigned int *plevel;
Behdad Esfahbod96595232012-05-11 03:33:36 +0200769 const char *what;
770 const void *obj;
Behdad Esfahbodc779d822012-11-23 14:07:24 -0500771 bool returned;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400772};
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500773template <typename ret_t> /* Optimize when tracing is disabled */
774struct hb_auto_trace_t<0, ret_t> {
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200775 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
776 const char *what HB_UNUSED,
777 const void *obj HB_UNUSED,
778 const char *func HB_UNUSED,
779 const char *message HB_UNUSED,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200780 ...) {}
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200781
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500782 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400783};
784
Behdad Esfahbodae63cf22012-07-19 20:45:41 -0400785#define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400786
787/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400788
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400789template <typename T> class hb_assert_unsigned_t;
790template <> class hb_assert_unsigned_t<unsigned char> {};
Dominik Röttsches9e7c7202014-07-17 14:40:34 +0300791template <> class hb_assert_unsigned_t<unsigned short> {};
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400792template <> class hb_assert_unsigned_t<unsigned int> {};
793template <> class hb_assert_unsigned_t<unsigned long> {};
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400794
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400795template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400796hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400797{
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400798 /* The sizeof() is here to force template instantiation.
799 * I'm sure there are better ways to do this but can't think of
800 * one right now. Declaring a variable won't work as HB_UNUSED
801 * is unsable on some platforms and unused types are less likely
802 * to generate a warning than unused variables. */
803 ASSERT_STATIC (sizeof (hb_assert_unsigned_t<T>) >= 0);
804
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400805 return (u - lo) <= (hi - lo);
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400806}
807
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400808template <typename T> static inline bool
Behdad Esfahbodc98b7182013-12-31 15:55:40 +0800809hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
810{
811 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
812}
813
814template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400815hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400816{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400817 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 -0400818}
819
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400820
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400821/* Useful for set-operations on small enums.
822 * For example, for testing "x ∈ {x1, x2, x3}" use:
823 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
824 */
Behdad Esfahbodefb4ad72012-07-20 14:27:38 -0400825#define FLAG(x) (1<<(x))
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400826#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400827
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400828
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400829template <typename T, typename T2> inline void
830hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400831{
832 if (unlikely (!len))
833 return;
834
835 unsigned int k = len - 1;
836 do {
837 unsigned int new_k = 0;
838
839 for (unsigned int j = 0; j < k; j++)
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400840 if (compar (&array[j], &array[j+1]) > 0)
841 {
842 {
843 T t;
844 t = array[j];
845 array[j] = array[j + 1];
846 array[j + 1] = t;
847 }
848 if (array2)
849 {
850 T2 t;
851 t = array2[j];
852 array2[j] = array2[j + 1];
853 array2[j + 1] = t;
854 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400855
856 new_k = j;
857 }
858 k = new_k;
859 } while (k);
860}
861
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400862template <typename T> inline void
863hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
864{
865 hb_bubble_sort (array, len, compar, (int *) NULL);
866}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400867
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400868static inline hb_bool_t
869hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
870{
871 /* Pain because we don't know whether s is nul-terminated. */
872 char buf[64];
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500873 len = MIN (ARRAY_LENGTH (buf) - 1, len);
874 strncpy (buf, s, len);
875 buf[len] = '\0';
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400876
877 char *end;
878 errno = 0;
879 unsigned long v = strtoul (buf, &end, base);
880 if (errno) return false;
881 if (*end) return false;
882 *out = v;
883 return true;
884}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400885
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400886
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500887/* Global runtime options. */
888
889struct hb_options_t
890{
891 int initialized : 1;
892 int uniscribe_bug_compatible : 1;
893};
894
895union hb_options_union_t {
896 int i;
897 hb_options_t opts;
898};
899ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t));
900
901HB_INTERNAL void
902_hb_options_init (void);
903
904extern HB_INTERNAL hb_options_union_t _hb_options;
905
906static inline hb_options_t
907hb_options (void)
908{
909 if (unlikely (!_hb_options.i))
910 _hb_options_init ();
911
912 return _hb_options.opts;
913}
914
915
Behdad Esfahbodc57d4542011-04-20 18:50:27 -0400916#endif /* HB_PRIVATE_HH */