blob: 2fee6f8a4ac6643c63a78e44bf5090ae8024b28c [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 Esfahbodbc200452010-04-29 01:40:26 -040057
58/* Essentials */
59
60#ifndef NULL
61# define NULL ((void *) 0)
62#endif
63
Behdad Esfahboda794ebf2009-08-06 12:32:35 -040064
Behdad Esfahboddabe6982012-11-23 14:21:35 -050065/* Void! */
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -050066struct _hb_void_t;
67typedef const _hb_void_t &hb_void_t;
68#define HB_VOID (* (const _hb_void_t *) NULL)
Behdad Esfahboddabe6982012-11-23 14:21:35 -050069
70
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040071/* Basics */
72
Behdad Esfahbod153142d2011-04-27 01:49:03 -040073
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040074#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -070075template <typename Type>
76static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040077
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -050078#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -070079template <typename Type>
80static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -040081
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -050082
Behdad Esfahbod45917532009-11-04 18:15:59 -050083#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -070084template <typename Type, unsigned int n>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -050085static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -040086/* A const version, but does not detect erratically being called on pointers. */
87#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040088
Behdad Esfahbod35a73832009-08-01 19:30:31 -040089#define HB_STMT_START do
90#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -050091
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -040092#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
93#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
94#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
Behdad Esfahbod303fe622008-01-23 00:20:48 -050095
Behdad Esfahbod79e2b472012-06-06 11:27:17 -040096#define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahbod4ec30ae2011-06-28 14:13:38 -040097#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahboddcb70262011-04-21 16:34:22 -040098
Behdad Esfahbod4be46ba2012-05-11 14:39:01 +020099#define _PASTE1(a,b) a##b
100#define PASTE(a,b) _PASTE1(a,b)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400101
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400102/* Lets assert int types. Saves trouble down the road. */
103
104ASSERT_STATIC (sizeof (int8_t) == 1);
105ASSERT_STATIC (sizeof (uint8_t) == 1);
106ASSERT_STATIC (sizeof (int16_t) == 2);
107ASSERT_STATIC (sizeof (uint16_t) == 2);
108ASSERT_STATIC (sizeof (int32_t) == 4);
109ASSERT_STATIC (sizeof (uint32_t) == 4);
110ASSERT_STATIC (sizeof (int64_t) == 8);
111ASSERT_STATIC (sizeof (uint64_t) == 8);
112
Behdad Esfahbodb13640d2011-04-11 12:29:31 -0400113ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
114ASSERT_STATIC (sizeof (hb_position_t) == 4);
115ASSERT_STATIC (sizeof (hb_mask_t) == 4);
Behdad Esfahbodae9eeaf2011-04-11 11:49:08 -0400116ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400117
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400118
119/* We like our types POD */
120
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400121#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
122#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
123#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400124
125#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400126# define _ASSERT_INSTANCE_POD1(_line, _instance) \
127 HB_STMT_START { \
128 typedef __typeof__(_instance) _type_##_line; \
129 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
130 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400131#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400132# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400133#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400134# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
135# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400136
137/* Check _assertion in a method environment */
138#define _ASSERT_POD1(_line) \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400139 inline void _static_assertion_on_line_##_line (void) const \
140 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
141# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
142# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400143
144
145
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400146/* Misc */
147
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400148
Behdad Esfahboddf660282009-08-01 20:46:02 -0400149#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
Behdad Esfahbod494d28a2010-05-10 23:50:07 -0400150#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400151#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
152#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
Behdad Esfahboddf660282009-08-01 20:46:02 -0400153#else
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400154#define likely(expr) (expr)
155#define unlikely(expr) (expr)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400156#endif
157
158#ifndef __GNUC__
159#undef __attribute__
160#define __attribute__(x)
161#endif
162
163#if __GNUC__ >= 3
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400164#define HB_PURE_FUNC __attribute__((pure))
165#define HB_CONST_FUNC __attribute__((const))
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400166#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
Behdad Esfahboddf660282009-08-01 20:46:02 -0400167#else
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400168#define HB_PURE_FUNC
169#define HB_CONST_FUNC
Behdad Esfahbod44b4f502011-08-04 00:52:20 -0400170#define HB_PRINTF_FUNC(format_idx, arg_idx)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400171#endif
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500172#if __GNUC__ >= 4
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400173#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500174#else
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400175#define HB_UNUSED
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500176#endif
Behdad Esfahboddf660282009-08-01 20:46:02 -0400177
Behdad Esfahbodeee85982010-05-12 23:22:55 -0400178#ifndef HB_INTERNAL
Behdad Esfahbodf60271c2011-08-02 09:56:30 -0400179# ifndef __MINGW32__
180# define HB_INTERNAL __attribute__((__visibility__("hidden")))
181# else
182# define HB_INTERNAL
183# endif
Behdad Esfahbodeee85982010-05-12 23:22:55 -0400184#endif
185
Behdad Esfahboddf660282009-08-01 20:46:02 -0400186
187#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
188#define snprintf _snprintf
189#endif
190
191#ifdef _MSC_VER
192#undef inline
193#define inline __inline
194#endif
195
196#ifdef __STRICT_ANSI__
197#undef inline
198#define inline __inline__
199#endif
200
201
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400202#if __GNUC__ >= 3
203#define HB_FUNC __PRETTY_FUNCTION__
204#elif defined(_MSC_VER)
205#define HB_FUNC __FUNCSIG__
206#else
207#define HB_FUNC __func__
208#endif
209
210
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100211/* Return the number of 1 bits in mask. */
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400212static inline HB_CONST_FUNC unsigned int
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400213_hb_popcount32 (uint32_t mask)
214{
215#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100216 return __builtin_popcount (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400217#else
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100218 /* "HACKMEM 169" */
219 register uint32_t y;
220 y = (mask >> 1) &033333333333;
221 y = mask - y - ((y >>1) & 033333333333);
222 return (((y + (y >> 3)) & 030707070707) % 077);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400223#endif
224}
225
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100226/* Returns the number of bits needed to store number */
227static inline HB_CONST_FUNC unsigned int
228_hb_bit_storage (unsigned int number)
229{
230#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100231 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100232#else
233 register unsigned int n_bits = 0;
234 while (number) {
235 n_bits++;
236 number >>= 1;
237 }
238 return n_bits;
239#endif
240}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400241
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100242/* Returns the number of zero bits in the least significant side of number */
243static inline HB_CONST_FUNC unsigned int
244_hb_ctz (unsigned int number)
245{
246#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
247 return likely (number) ? __builtin_ctz (number) : 0;
248#else
249 register unsigned int n_bits = 0;
250 if (unlikely (!number)) return 0;
251 while (!(number & 1)) {
252 n_bits++;
253 number >>= 1;
254 }
255 return n_bits;
256#endif
257}
258
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400259static inline bool
260_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
261{
262 return (size > 0) && (count >= ((unsigned int) -1) / size);
263}
264
265
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400266/* Type of bsearch() / qsort() compare function */
267typedef int (*hb_compare_func_t) (const void *, const void *);
268
269
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400270
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400271
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400272/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400273
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400274
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400275#define HB_PREALLOCED_ARRAY_INIT {0}
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400276template <typename Type, unsigned int StaticSize>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400277struct hb_prealloced_array_t
278{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400279 unsigned int len;
280 unsigned int allocated;
281 Type *array;
282 Type static_array[StaticSize];
283
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400284 void init (void) { memset (this, 0, sizeof (*this)); }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200285
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400286 inline Type& operator [] (unsigned int i) { return array[i]; }
287 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400288
289 inline Type *push (void)
290 {
291 if (!array) {
292 array = static_array;
293 allocated = ARRAY_LENGTH (static_array);
294 }
295 if (likely (len < allocated))
296 return &array[len++];
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400297
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400298 /* Need to reallocate */
299 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400300 Type *new_array = NULL;
301
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400302 if (array == static_array) {
303 new_array = (Type *) calloc (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400304 if (new_array)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400305 memcpy (new_array, array, len * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400306 } else {
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400307 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400308 if (likely (!overflows)) {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400309 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400310 }
311 }
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400312
313 if (unlikely (!new_array))
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400314 return NULL;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400315
316 array = new_array;
317 allocated = new_allocated;
318 return &array[len++];
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400319 }
320
321 inline void pop (void)
322 {
323 len--;
324 /* TODO: shrink array if needed */
325 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400326
327 inline void shrink (unsigned int l)
328 {
329 if (l < len)
330 len = l;
331 /* TODO: shrink array if needed */
332 }
333
Behdad Esfahbod68435692011-05-05 14:12:37 -0400334 template <typename T>
335 inline Type *find (T v) {
336 for (unsigned int i = 0; i < len; i++)
337 if (array[i] == v)
338 return &array[i];
339 return NULL;
340 }
341 template <typename T>
342 inline const Type *find (T v) const {
343 for (unsigned int i = 0; i < len; i++)
344 if (array[i] == v)
345 return &array[i];
346 return NULL;
347 }
348
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400349 inline void sort (void)
350 {
351 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
352 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400353
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400354 inline void sort (unsigned int start, unsigned int end)
355 {
356 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
357 }
358
Behdad Esfahbod68435692011-05-05 14:12:37 -0400359 template <typename T>
360 inline Type *bsearch (T *key)
361 {
362 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
363 }
364 template <typename T>
365 inline const Type *bsearch (T *key) const
366 {
367 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
368 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400369
370 inline void finish (void)
371 {
372 if (array != static_array)
373 free (array);
374 array = NULL;
375 allocated = len = 0;
376 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400377};
378
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400379
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400380#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400381template <typename item_t, typename lock_t>
382struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400383{
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400384 hb_prealloced_array_t <item_t, 2> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400385
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400386 inline void init (void) { items.init (); }
387
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400388 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200389 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400390 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400391 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400392 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400393 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200394 if (replace) {
395 item_t old = *item;
396 *item = v;
397 l.unlock ();
398 old.finish ();
399 }
400 else {
401 item = NULL;
402 l.unlock ();
403 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400404 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400405 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400406 if (likely (item))
407 *item = v;
408 l.unlock ();
409 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400410 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400411 }
412
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400413 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400414 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400415 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400416 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400417 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400418 if (item) {
419 item_t old = *item;
420 *item = items[items.len - 1];
421 items.pop ();
422 l.unlock ();
423 old.finish ();
424 } else {
425 l.unlock ();
426 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400427 }
428
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400429 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400430 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400431 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400432 l.lock ();
433 item_t *item = items.find (v);
434 if (item)
435 *i = *item;
436 l.unlock ();
437 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400438 }
439
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400440 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400441 inline item_t *find_or_insert (T v, lock_t &l)
442 {
443 l.lock ();
444 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400445 if (!item) {
446 item = items.push ();
447 if (likely (item))
448 *item = v;
449 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400450 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400451 return item;
452 }
453
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400454 inline void finish (lock_t &l)
455 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400456 if (!items.len) {
457 /* No need for locking. */
458 items.finish ();
459 return;
460 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400461 l.lock ();
462 while (items.len) {
463 item_t old = items[items.len - 1];
464 items.pop ();
465 l.unlock ();
466 old.finish ();
467 l.lock ();
468 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400469 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400470 l.unlock ();
471 }
472
473};
474
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400475
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400476
477
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400478/* Big-endian handling */
479
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400480static inline uint16_t hb_be_uint16 (const uint16_t v)
481{
482 const uint8_t *V = (const uint8_t *) &v;
Behdad Esfahbodfcd6f532012-06-08 09:59:43 -0400483 return (V[0] << 8) | V[1];
484}
485
486static inline uint16_t hb_uint16_swap (const uint16_t v)
487{
488 return (v >> 8) | (v << 8);
489}
490
491static inline uint32_t hb_uint32_swap (const uint32_t v)
492{
493 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400494}
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400495
Behdad Esfahbode74616b2012-04-15 14:12:13 -0400496/* Note, of the following macros, uint16_get is the one called many many times.
497 * If there is any optimizations to be done, it's in that macro. However, I
498 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
499 * results in a single ror instruction, does NOT speed this up. In fact, it
500 * resulted in a minor slowdown. At any rate, note that v may not be correctly
501 * aligned, so I think the current implementation is optimal.
502 */
503
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400504#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 -0400505#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400506#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400507
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400508#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 -0400509#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 -0400510#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 -0400511
512
Behdad Esfahbod41880962011-04-11 14:58:28 -0400513/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400514
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400515static inline unsigned char ISALPHA (unsigned char c)
516{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
517static inline unsigned char ISALNUM (unsigned char c)
518{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
519static inline unsigned char TOUPPER (unsigned char c)
520{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
521static inline unsigned char TOLOWER (unsigned char c)
522{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400523
Behdad Esfahbod41880962011-04-11 14:58:28 -0400524#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
525 ((const char *) s)[1], \
526 ((const char *) s)[2], \
527 ((const char *) s)[3]))
528
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400529
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400530/* C++ helpers */
531
532/* Makes class uncopyable. Use in private: section. */
533#define NO_COPY(T) \
534 T (const T &o); \
Behdad Esfahbod970e0922011-06-14 14:35:44 -0400535 T &operator = (const T &o)
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400536
537
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400538/* Debug */
539
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400540
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400541#ifndef HB_DEBUG
542#define HB_DEBUG 0
543#endif
544
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400545static inline bool
546_hb_debug (unsigned int level,
547 unsigned int max_level)
548{
549 return level < max_level;
550}
551
552#define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400553#define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
554
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400555template <int max_level> static inline void
Behdad Esfahbod20810972012-05-10 23:06:58 +0200556_hb_debug_msg_va (const char *what,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200557 const void *obj,
558 const char *func,
559 bool indented,
560 unsigned int level,
561 int level_dir,
562 const char *message,
563 va_list ap)
Behdad Esfahbod20810972012-05-10 23:06:58 +0200564{
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200565 if (!_hb_debug (level, max_level))
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200566 return;
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200567
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200568 fprintf (stderr, "%-10s", what ? what : "");
569
570 if (obj)
Behdad Esfahbod5ccfe8e2012-05-11 02:19:41 +0200571 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200572 else
573 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
574
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200575 if (indented) {
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400576/* One may want to add ASCII version of these. See:
577 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
578#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
579#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
580#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
581#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
582#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
583 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;
584 fprintf (stderr, "%2d %s" VRBAR "%s",
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200585 level,
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400586 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
587 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200588 } else
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400589 fprintf (stderr, " " VRBAR LBAR);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200590
Behdad Esfahbodadf77582012-11-23 17:32:00 -0500591 if (func)
592 {
593 /* Skip "typename" */
594 if (0 == strncmp (func, "typename ", 9))
595 func += 9;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400596 /* Skip return type */
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200597 const char *space = strchr (func, ' ');
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400598 if (space)
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200599 func = space + 1;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400600 /* Skip parameter list */
601 const char *paren = strchr (func, '(');
602 unsigned int func_len = paren ? paren - func : strlen (func);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200603 fprintf (stderr, "%.*s: ", func_len, func);
Behdad Esfahbod85f73fa2012-05-11 02:40:42 +0200604 }
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200605
606 if (message)
607 vfprintf (stderr, message, ap);
608
609 fprintf (stderr, "\n");
Behdad Esfahbod20810972012-05-10 23:06:58 +0200610}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200611template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200612_hb_debug_msg_va<0> (const char *what HB_UNUSED,
613 const void *obj HB_UNUSED,
614 const char *func HB_UNUSED,
615 bool indented HB_UNUSED,
616 unsigned int level HB_UNUSED,
617 int level_dir HB_UNUSED,
618 const char *message HB_UNUSED,
619 va_list ap HB_UNUSED) {}
Behdad Esfahbod20810972012-05-10 23:06:58 +0200620
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400621template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400622_hb_debug_msg (const char *what,
623 const void *obj,
624 const char *func,
625 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200626 unsigned int level,
627 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400628 const char *message,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200629 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400630template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400631_hb_debug_msg (const char *what,
632 const void *obj,
633 const char *func,
634 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200635 unsigned int level,
636 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400637 const char *message,
638 ...)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400639{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400640 va_list ap;
641 va_start (ap, message);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200642 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400643 va_end (ap);
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400644}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200645template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200646_hb_debug_msg<0> (const char *what HB_UNUSED,
647 const void *obj HB_UNUSED,
648 const char *func HB_UNUSED,
649 bool indented HB_UNUSED,
650 unsigned int level HB_UNUSED,
651 int level_dir HB_UNUSED,
652 const char *message HB_UNUSED,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200653 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200654template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200655_hb_debug_msg<0> (const char *what HB_UNUSED,
656 const void *obj HB_UNUSED,
657 const char *func HB_UNUSED,
658 bool indented HB_UNUSED,
659 unsigned int level HB_UNUSED,
660 int level_dir HB_UNUSED,
661 const char *message HB_UNUSED,
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200662 ...) {}
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400663
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400664#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
665#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
666#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 -0400667
668
669/*
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500670 * Printer
671 */
672
673template <typename T>
674struct hb_printer_t {};
675
676template <>
677struct hb_printer_t<bool> {
678 const char *print (bool v) { return v ? "true" : "false"; }
679};
680
681template <>
Behdad Esfahbod130bb3f2012-12-05 16:49:47 -0500682struct hb_printer_t<hb_void_t> {
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500683 const char *print (hb_void_t) { return ""; }
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500684};
685
686
687/*
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400688 * Trace
689 */
690
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500691template <typename T>
692static inline void _hb_warn_no_return (bool returned)
693{
694 if (unlikely (!returned)) {
695 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
696 }
697}
698template <>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500699inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500700{}
701
702template <int max_level, typename ret_t>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400703struct hb_auto_trace_t {
704 explicit inline hb_auto_trace_t (unsigned int *plevel_,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200705 const char *what_,
706 const void *obj_,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400707 const char *func,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200708 const char *message,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200709 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400710 {
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200711 if (plevel) ++*plevel;
Behdad Esfahbod20810972012-05-10 23:06:58 +0200712
713 va_list ap;
714 va_start (ap, message);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400715 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
Behdad Esfahbod20810972012-05-10 23:06:58 +0200716 va_end (ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400717 }
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200718 inline ~hb_auto_trace_t (void)
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200719 {
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500720 _hb_warn_no_return<ret_t> (returned);
721 if (!returned) {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400722 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200723 }
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200724 if (plevel) --*plevel;
725 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400726
Behdad Esfahbod2c9d6482012-11-23 16:49:19 -0500727 inline ret_t ret (ret_t v, unsigned int line = 0)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200728 {
729 if (unlikely (returned)) {
730 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
731 return v;
732 }
733
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500734 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
735 "return %s (line %d)",
736 hb_printer_t<ret_t>().print (v), line);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200737 if (plevel) --*plevel;
738 plevel = NULL;
739 returned = true;
740 return v;
741 }
742
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400743 private:
744 unsigned int *plevel;
Behdad Esfahbod96595232012-05-11 03:33:36 +0200745 const char *what;
746 const void *obj;
Behdad Esfahbodc779d822012-11-23 14:07:24 -0500747 bool returned;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400748};
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500749template <typename ret_t> /* Optimize when tracing is disabled */
750struct hb_auto_trace_t<0, ret_t> {
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200751 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
752 const char *what HB_UNUSED,
753 const void *obj HB_UNUSED,
754 const char *func HB_UNUSED,
755 const char *message HB_UNUSED,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200756 ...) {}
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200757
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500758 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400759};
760
Behdad Esfahbodae63cf22012-07-19 20:45:41 -0400761#define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400762
763/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400764
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400765
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400766/* Pre-mature optimization:
767 * Checks for lo <= u <= hi but with an optimization if lo and hi
768 * are only different in a contiguous set of lower-most bits.
769 */
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400770template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400771hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400772{
773 if ( ((lo^hi) & lo) == 0 &&
774 ((lo^hi) & hi) == (lo^hi) &&
775 ((lo^hi) & ((lo^hi) + 1)) == 0 )
776 return (u & ~(lo^hi)) == lo;
777 else
778 return lo <= u && u <= hi;
779}
780
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400781template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400782hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400783{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400784 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 -0400785}
786
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400787
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400788/* Useful for set-operations on small enums.
789 * For example, for testing "x ∈ {x1, x2, x3}" use:
790 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
791 */
Behdad Esfahbodefb4ad72012-07-20 14:27:38 -0400792#define FLAG(x) (1<<(x))
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400793#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400794
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400795
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400796template <typename T, typename T2> inline void
797hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400798{
799 if (unlikely (!len))
800 return;
801
802 unsigned int k = len - 1;
803 do {
804 unsigned int new_k = 0;
805
806 for (unsigned int j = 0; j < k; j++)
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400807 if (compar (&array[j], &array[j+1]) > 0)
808 {
809 {
810 T t;
811 t = array[j];
812 array[j] = array[j + 1];
813 array[j + 1] = t;
814 }
815 if (array2)
816 {
817 T2 t;
818 t = array2[j];
819 array2[j] = array2[j + 1];
820 array2[j + 1] = t;
821 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400822
823 new_k = j;
824 }
825 k = new_k;
826 } while (k);
827}
828
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400829template <typename T> inline void
830hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
831{
832 hb_bubble_sort (array, len, compar, (int *) NULL);
833}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400834
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400835static inline hb_bool_t
836hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
837{
838 /* Pain because we don't know whether s is nul-terminated. */
839 char buf[64];
840 strncpy (buf, s, MIN (ARRAY_LENGTH (buf) - 1, len));
841 buf[MIN (ARRAY_LENGTH (buf) - 1, len)] = '\0';
842
843 char *end;
844 errno = 0;
845 unsigned long v = strtoul (buf, &end, base);
846 if (errno) return false;
847 if (*end) return false;
848 *out = v;
849 return true;
850}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400851
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400852
Behdad Esfahbodc57d4542011-04-20 18:50:27 -0400853#endif /* HB_PRIVATE_HH */