blob: 34f2e15211b8edfa7755ba6af84c44e916ea83ef [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! */
66typedef struct {} void_t;
67
68
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040069/* Basics */
70
Behdad Esfahbod153142d2011-04-27 01:49:03 -040071
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040072#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -070073template <typename Type>
74static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040075
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -050076#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -070077template <typename Type>
78static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -040079
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -050080
Behdad Esfahbod45917532009-11-04 18:15:59 -050081#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -070082template <typename Type, unsigned int n>
83static inline unsigned int ARRAY_LENGTH (const Type (&a)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -040084/* A const version, but does not detect erratically being called on pointers. */
85#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040086
Behdad Esfahbod35a73832009-08-01 19:30:31 -040087#define HB_STMT_START do
88#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -050089
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -040090#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
91#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
92#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
Behdad Esfahbod303fe622008-01-23 00:20:48 -050093
Behdad Esfahbod79e2b472012-06-06 11:27:17 -040094#define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahbod4ec30ae2011-06-28 14:13:38 -040095#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
Behdad Esfahboddcb70262011-04-21 16:34:22 -040096
Behdad Esfahbod4be46ba2012-05-11 14:39:01 +020097#define _PASTE1(a,b) a##b
98#define PASTE(a,b) _PASTE1(a,b)
Behdad Esfahbodbc200452010-04-29 01:40:26 -040099
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400100/* Lets assert int types. Saves trouble down the road. */
101
102ASSERT_STATIC (sizeof (int8_t) == 1);
103ASSERT_STATIC (sizeof (uint8_t) == 1);
104ASSERT_STATIC (sizeof (int16_t) == 2);
105ASSERT_STATIC (sizeof (uint16_t) == 2);
106ASSERT_STATIC (sizeof (int32_t) == 4);
107ASSERT_STATIC (sizeof (uint32_t) == 4);
108ASSERT_STATIC (sizeof (int64_t) == 8);
109ASSERT_STATIC (sizeof (uint64_t) == 8);
110
Behdad Esfahbodb13640d2011-04-11 12:29:31 -0400111ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
112ASSERT_STATIC (sizeof (hb_position_t) == 4);
113ASSERT_STATIC (sizeof (hb_mask_t) == 4);
Behdad Esfahbodae9eeaf2011-04-11 11:49:08 -0400114ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400115
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400116
117/* We like our types POD */
118
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400119#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
120#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
121#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400122
123#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400124# define _ASSERT_INSTANCE_POD1(_line, _instance) \
125 HB_STMT_START { \
126 typedef __typeof__(_instance) _type_##_line; \
127 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
128 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400129#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400130# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400131#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400132# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
133# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400134
135/* Check _assertion in a method environment */
136#define _ASSERT_POD1(_line) \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400137 inline void _static_assertion_on_line_##_line (void) const \
138 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
139# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
140# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400141
142
143
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400144/* Misc */
145
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400146
Behdad Esfahboddf660282009-08-01 20:46:02 -0400147#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
Behdad Esfahbod494d28a2010-05-10 23:50:07 -0400148#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400149#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
150#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
Behdad Esfahboddf660282009-08-01 20:46:02 -0400151#else
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400152#define likely(expr) (expr)
153#define unlikely(expr) (expr)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400154#endif
155
156#ifndef __GNUC__
157#undef __attribute__
158#define __attribute__(x)
159#endif
160
161#if __GNUC__ >= 3
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400162#define HB_PURE_FUNC __attribute__((pure))
163#define HB_CONST_FUNC __attribute__((const))
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400164#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
Behdad Esfahboddf660282009-08-01 20:46:02 -0400165#else
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400166#define HB_PURE_FUNC
167#define HB_CONST_FUNC
Behdad Esfahbod44b4f502011-08-04 00:52:20 -0400168#define HB_PRINTF_FUNC(format_idx, arg_idx)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400169#endif
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500170#if __GNUC__ >= 4
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400171#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500172#else
Behdad Esfahbod33d13fd2010-04-29 13:56:44 -0400173#define HB_UNUSED
Behdad Esfahbodbc7830e2010-02-17 15:14:57 -0500174#endif
Behdad Esfahboddf660282009-08-01 20:46:02 -0400175
Behdad Esfahbodeee85982010-05-12 23:22:55 -0400176#ifndef HB_INTERNAL
Behdad Esfahbodf60271c2011-08-02 09:56:30 -0400177# ifndef __MINGW32__
178# define HB_INTERNAL __attribute__((__visibility__("hidden")))
179# else
180# define HB_INTERNAL
181# endif
Behdad Esfahbodeee85982010-05-12 23:22:55 -0400182#endif
183
Behdad Esfahboddf660282009-08-01 20:46:02 -0400184
185#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
186#define snprintf _snprintf
187#endif
188
189#ifdef _MSC_VER
190#undef inline
191#define inline __inline
192#endif
193
194#ifdef __STRICT_ANSI__
195#undef inline
196#define inline __inline__
197#endif
198
199
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400200#if __GNUC__ >= 3
201#define HB_FUNC __PRETTY_FUNCTION__
202#elif defined(_MSC_VER)
203#define HB_FUNC __FUNCSIG__
204#else
205#define HB_FUNC __func__
206#endif
207
208
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100209/* Return the number of 1 bits in mask. */
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400210static inline HB_CONST_FUNC unsigned int
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400211_hb_popcount32 (uint32_t mask)
212{
213#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100214 return __builtin_popcount (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400215#else
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100216 /* "HACKMEM 169" */
217 register uint32_t y;
218 y = (mask >> 1) &033333333333;
219 y = mask - y - ((y >>1) & 033333333333);
220 return (((y + (y >> 3)) & 030707070707) % 077);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400221#endif
222}
223
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100224/* Returns the number of bits needed to store number */
225static inline HB_CONST_FUNC unsigned int
226_hb_bit_storage (unsigned int number)
227{
228#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100229 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100230#else
231 register unsigned int n_bits = 0;
232 while (number) {
233 n_bits++;
234 number >>= 1;
235 }
236 return n_bits;
237#endif
238}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400239
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100240/* Returns the number of zero bits in the least significant side of number */
241static inline HB_CONST_FUNC unsigned int
242_hb_ctz (unsigned int number)
243{
244#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
245 return likely (number) ? __builtin_ctz (number) : 0;
246#else
247 register unsigned int n_bits = 0;
248 if (unlikely (!number)) return 0;
249 while (!(number & 1)) {
250 n_bits++;
251 number >>= 1;
252 }
253 return n_bits;
254#endif
255}
256
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400257static inline bool
258_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
259{
260 return (size > 0) && (count >= ((unsigned int) -1) / size);
261}
262
263
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400264/* Type of bsearch() / qsort() compare function */
265typedef int (*hb_compare_func_t) (const void *, const void *);
266
267
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400268
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400269
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400270/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400271
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400272
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400273#define HB_PREALLOCED_ARRAY_INIT {0}
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400274template <typename Type, unsigned int StaticSize>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400275struct hb_prealloced_array_t
276{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400277 unsigned int len;
278 unsigned int allocated;
279 Type *array;
280 Type static_array[StaticSize];
281
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400282 void init (void) { memset (this, 0, sizeof (*this)); }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200283
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400284 inline Type& operator [] (unsigned int i) { return array[i]; }
285 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400286
287 inline Type *push (void)
288 {
289 if (!array) {
290 array = static_array;
291 allocated = ARRAY_LENGTH (static_array);
292 }
293 if (likely (len < allocated))
294 return &array[len++];
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400295
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400296 /* Need to reallocate */
297 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400298 Type *new_array = NULL;
299
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400300 if (array == static_array) {
301 new_array = (Type *) calloc (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400302 if (new_array)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400303 memcpy (new_array, array, len * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400304 } else {
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400305 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400306 if (likely (!overflows)) {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400307 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400308 }
309 }
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400310
311 if (unlikely (!new_array))
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400312 return NULL;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400313
314 array = new_array;
315 allocated = new_allocated;
316 return &array[len++];
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400317 }
318
319 inline void pop (void)
320 {
321 len--;
322 /* TODO: shrink array if needed */
323 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400324
325 inline void shrink (unsigned int l)
326 {
327 if (l < len)
328 len = l;
329 /* TODO: shrink array if needed */
330 }
331
Behdad Esfahbod68435692011-05-05 14:12:37 -0400332 template <typename T>
333 inline Type *find (T v) {
334 for (unsigned int i = 0; i < len; i++)
335 if (array[i] == v)
336 return &array[i];
337 return NULL;
338 }
339 template <typename T>
340 inline const Type *find (T v) const {
341 for (unsigned int i = 0; i < len; i++)
342 if (array[i] == v)
343 return &array[i];
344 return NULL;
345 }
346
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400347 inline void sort (void)
348 {
349 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
350 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400351
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400352 inline void sort (unsigned int start, unsigned int end)
353 {
354 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
355 }
356
Behdad Esfahbod68435692011-05-05 14:12:37 -0400357 template <typename T>
358 inline Type *bsearch (T *key)
359 {
360 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
361 }
362 template <typename T>
363 inline const Type *bsearch (T *key) const
364 {
365 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
366 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400367
368 inline void finish (void)
369 {
370 if (array != static_array)
371 free (array);
372 array = NULL;
373 allocated = len = 0;
374 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400375};
376
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400377
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400378#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400379template <typename item_t, typename lock_t>
380struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400381{
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400382 hb_prealloced_array_t <item_t, 2> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400383
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400384 inline void init (void) { items.init (); }
385
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400386 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200387 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400388 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400389 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400390 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400391 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200392 if (replace) {
393 item_t old = *item;
394 *item = v;
395 l.unlock ();
396 old.finish ();
397 }
398 else {
399 item = NULL;
400 l.unlock ();
401 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400402 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400403 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400404 if (likely (item))
405 *item = v;
406 l.unlock ();
407 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400408 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400409 }
410
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400411 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400412 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400413 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400414 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400415 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400416 if (item) {
417 item_t old = *item;
418 *item = items[items.len - 1];
419 items.pop ();
420 l.unlock ();
421 old.finish ();
422 } else {
423 l.unlock ();
424 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400425 }
426
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400427 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400428 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400429 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400430 l.lock ();
431 item_t *item = items.find (v);
432 if (item)
433 *i = *item;
434 l.unlock ();
435 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400436 }
437
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400438 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400439 inline item_t *find_or_insert (T v, lock_t &l)
440 {
441 l.lock ();
442 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400443 if (!item) {
444 item = items.push ();
445 if (likely (item))
446 *item = v;
447 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400448 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400449 return item;
450 }
451
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400452 inline void finish (lock_t &l)
453 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400454 if (!items.len) {
455 /* No need for locking. */
456 items.finish ();
457 return;
458 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400459 l.lock ();
460 while (items.len) {
461 item_t old = items[items.len - 1];
462 items.pop ();
463 l.unlock ();
464 old.finish ();
465 l.lock ();
466 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400467 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400468 l.unlock ();
469 }
470
471};
472
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400473
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400474
475
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400476/* Big-endian handling */
477
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400478static inline uint16_t hb_be_uint16 (const uint16_t v)
479{
480 const uint8_t *V = (const uint8_t *) &v;
Behdad Esfahbodfcd6f532012-06-08 09:59:43 -0400481 return (V[0] << 8) | V[1];
482}
483
484static inline uint16_t hb_uint16_swap (const uint16_t v)
485{
486 return (v >> 8) | (v << 8);
487}
488
489static inline uint32_t hb_uint32_swap (const uint32_t v)
490{
491 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400492}
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400493
Behdad Esfahbode74616b2012-04-15 14:12:13 -0400494/* Note, of the following macros, uint16_get is the one called many many times.
495 * If there is any optimizations to be done, it's in that macro. However, I
496 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
497 * results in a single ror instruction, does NOT speed this up. In fact, it
498 * resulted in a minor slowdown. At any rate, note that v may not be correctly
499 * aligned, so I think the current implementation is optimal.
500 */
501
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400502#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 -0400503#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400504#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400505
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400506#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 -0400507#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 -0400508#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 -0400509
510
Behdad Esfahbod41880962011-04-11 14:58:28 -0400511/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400512
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400513static inline unsigned char ISALPHA (unsigned char c)
514{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
515static inline unsigned char ISALNUM (unsigned char c)
516{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
517static inline unsigned char TOUPPER (unsigned char c)
518{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
519static inline unsigned char TOLOWER (unsigned char c)
520{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400521
Behdad Esfahbod41880962011-04-11 14:58:28 -0400522#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
523 ((const char *) s)[1], \
524 ((const char *) s)[2], \
525 ((const char *) s)[3]))
526
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400527
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400528/* C++ helpers */
529
530/* Makes class uncopyable. Use in private: section. */
531#define NO_COPY(T) \
532 T (const T &o); \
Behdad Esfahbod970e0922011-06-14 14:35:44 -0400533 T &operator = (const T &o)
Behdad Esfahbod831886a2011-05-11 21:27:52 -0400534
535
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400536/* Debug */
537
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400538
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400539#ifndef HB_DEBUG
540#define HB_DEBUG 0
541#endif
542
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400543static inline bool
544_hb_debug (unsigned int level,
545 unsigned int max_level)
546{
547 return level < max_level;
548}
549
550#define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400551#define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
552
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400553template <int max_level> static inline void
Behdad Esfahbod20810972012-05-10 23:06:58 +0200554_hb_debug_msg_va (const char *what,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200555 const void *obj,
556 const char *func,
557 bool indented,
558 unsigned int level,
559 int level_dir,
560 const char *message,
561 va_list ap)
Behdad Esfahbod20810972012-05-10 23:06:58 +0200562{
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200563 if (!_hb_debug (level, max_level))
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200564 return;
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200565
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200566 fprintf (stderr, "%-10s", what ? what : "");
567
568 if (obj)
Behdad Esfahbod5ccfe8e2012-05-11 02:19:41 +0200569 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200570 else
571 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
572
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200573 if (indented) {
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400574/* One may want to add ASCII version of these. See:
575 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
576#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
577#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
578#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
579#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
580#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
581 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;
582 fprintf (stderr, "%2d %s" VRBAR "%s",
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200583 level,
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400584 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
585 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200586 } else
Behdad Esfahbod6932a412012-06-26 10:46:31 -0400587 fprintf (stderr, " " VRBAR LBAR);
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200588
Behdad Esfahbod85f73fa2012-05-11 02:40:42 +0200589 if (func) {
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400590 /* Skip return type */
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200591 const char *space = strchr (func, ' ');
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400592 if (space)
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200593 func = space + 1;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400594 /* Skip parameter list */
595 const char *paren = strchr (func, '(');
596 unsigned int func_len = paren ? paren - func : strlen (func);
Behdad Esfahbodd7bba012012-05-11 02:46:26 +0200597 fprintf (stderr, "%.*s: ", func_len, func);
Behdad Esfahbod85f73fa2012-05-11 02:40:42 +0200598 }
Behdad Esfahbod6eec6f42012-05-11 00:50:38 +0200599
600 if (message)
601 vfprintf (stderr, message, ap);
602
603 fprintf (stderr, "\n");
Behdad Esfahbod20810972012-05-10 23:06:58 +0200604}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200605template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200606_hb_debug_msg_va<0> (const char *what HB_UNUSED,
607 const void *obj HB_UNUSED,
608 const char *func HB_UNUSED,
609 bool indented HB_UNUSED,
610 unsigned int level HB_UNUSED,
611 int level_dir HB_UNUSED,
612 const char *message HB_UNUSED,
613 va_list ap HB_UNUSED) {}
Behdad Esfahbod20810972012-05-10 23:06:58 +0200614
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400615template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400616_hb_debug_msg (const char *what,
617 const void *obj,
618 const char *func,
619 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200620 unsigned int level,
621 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400622 const char *message,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200623 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod2d1dcb32012-10-07 17:13:46 -0400624template <int max_level> static inline void
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400625_hb_debug_msg (const char *what,
626 const void *obj,
627 const char *func,
628 bool indented,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200629 unsigned int level,
630 int level_dir,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400631 const char *message,
632 ...)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400633{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400634 va_list ap;
635 va_start (ap, message);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200636 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400637 va_end (ap);
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400638}
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200639template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200640_hb_debug_msg<0> (const char *what HB_UNUSED,
641 const void *obj HB_UNUSED,
642 const char *func HB_UNUSED,
643 bool indented HB_UNUSED,
644 unsigned int level HB_UNUSED,
645 int level_dir HB_UNUSED,
646 const char *message HB_UNUSED,
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200647 ...) HB_PRINTF_FUNC(7, 8);
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200648template <> inline void
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200649_hb_debug_msg<0> (const char *what HB_UNUSED,
650 const void *obj HB_UNUSED,
651 const char *func HB_UNUSED,
652 bool indented HB_UNUSED,
653 unsigned int level HB_UNUSED,
654 int level_dir HB_UNUSED,
655 const char *message HB_UNUSED,
Behdad Esfahbod829e8142012-05-11 00:52:16 +0200656 ...) {}
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400657
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400658#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
659#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
660#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 -0400661
662
663/*
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500664 * Printer
665 */
666
667template <typename T>
668struct hb_printer_t {};
669
670template <>
671struct hb_printer_t<bool> {
672 const char *print (bool v) { return v ? "true" : "false"; }
673};
674
675template <>
676struct hb_printer_t<void_t> {
677 const char *print (void_t v) { return ""; }
678};
679
680
681/*
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400682 * Trace
683 */
684
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500685template <typename T>
686static inline void _hb_warn_no_return (bool returned)
687{
688 if (unlikely (!returned)) {
689 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
690 }
691}
692template <>
693inline void _hb_warn_no_return<void_t> (bool returned)
694{}
695
696template <int max_level, typename ret_t>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400697struct hb_auto_trace_t {
698 explicit inline hb_auto_trace_t (unsigned int *plevel_,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200699 const char *what_,
700 const void *obj_,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400701 const char *func,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200702 const char *message,
Behdad Esfahbod96595232012-05-11 03:33:36 +0200703 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400704 {
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200705 if (plevel) ++*plevel;
Behdad Esfahbod20810972012-05-10 23:06:58 +0200706
707 va_list ap;
708 va_start (ap, message);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400709 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
Behdad Esfahbod20810972012-05-10 23:06:58 +0200710 va_end (ap);
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400711 }
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200712 inline ~hb_auto_trace_t (void)
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200713 {
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500714 _hb_warn_no_return<ret_t> (returned);
715 if (!returned) {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400716 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200717 }
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200718 if (plevel) --*plevel;
719 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400720
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500721 inline ret_t ret (ret_t v, unsigned int line = 0)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200722 {
723 if (unlikely (returned)) {
724 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
725 return v;
726 }
727
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500728 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
729 "return %s (line %d)",
730 hb_printer_t<ret_t>().print (v), line);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200731 if (plevel) --*plevel;
732 plevel = NULL;
733 returned = true;
734 return v;
735 }
736
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400737 private:
738 unsigned int *plevel;
Behdad Esfahbod96595232012-05-11 03:33:36 +0200739 const char *what;
740 const void *obj;
Behdad Esfahbodc779d822012-11-23 14:07:24 -0500741 bool returned;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400742};
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500743template <typename ret_t> /* Optimize when tracing is disabled */
744struct hb_auto_trace_t<0, ret_t> {
Behdad Esfahbod93345ed2012-05-13 16:01:08 +0200745 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
746 const char *what HB_UNUSED,
747 const void *obj HB_UNUSED,
748 const char *func HB_UNUSED,
749 const char *message HB_UNUSED,
Behdad Esfahbod20810972012-05-10 23:06:58 +0200750 ...) {}
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200751
752 template <typename T>
Behdad Esfahbodae63cf22012-07-19 20:45:41 -0400753 inline T ret (T v, unsigned int line = 0) { return v; }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400754};
755
Behdad Esfahbodae63cf22012-07-19 20:45:41 -0400756#define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400757
758/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400759
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400760
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400761/* Pre-mature optimization:
762 * Checks for lo <= u <= hi but with an optimization if lo and hi
763 * are only different in a contiguous set of lower-most bits.
764 */
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400765template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400766hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400767{
768 if ( ((lo^hi) & lo) == 0 &&
769 ((lo^hi) & hi) == (lo^hi) &&
770 ((lo^hi) & ((lo^hi) + 1)) == 0 )
771 return (u & ~(lo^hi)) == lo;
772 else
773 return lo <= u && u <= hi;
774}
775
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400776template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400777hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400778{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400779 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 -0400780}
781
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400782
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400783/* Useful for set-operations on small enums.
784 * For example, for testing "x ∈ {x1, x2, x3}" use:
785 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
786 */
Behdad Esfahbodefb4ad72012-07-20 14:27:38 -0400787#define FLAG(x) (1<<(x))
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400788#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400789
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400790
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400791template <typename T, typename T2> inline void
792hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400793{
794 if (unlikely (!len))
795 return;
796
797 unsigned int k = len - 1;
798 do {
799 unsigned int new_k = 0;
800
801 for (unsigned int j = 0; j < k; j++)
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400802 if (compar (&array[j], &array[j+1]) > 0)
803 {
804 {
805 T t;
806 t = array[j];
807 array[j] = array[j + 1];
808 array[j + 1] = t;
809 }
810 if (array2)
811 {
812 T2 t;
813 t = array2[j];
814 array2[j] = array2[j + 1];
815 array2[j + 1] = t;
816 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400817
818 new_k = j;
819 }
820 k = new_k;
821 } while (k);
822}
823
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400824template <typename T> inline void
825hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
826{
827 hb_bubble_sort (array, len, compar, (int *) NULL);
828}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400829
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400830static inline hb_bool_t
831hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
832{
833 /* Pain because we don't know whether s is nul-terminated. */
834 char buf[64];
835 strncpy (buf, s, MIN (ARRAY_LENGTH (buf) - 1, len));
836 buf[MIN (ARRAY_LENGTH (buf) - 1, len)] = '\0';
837
838 char *end;
839 errno = 0;
840 unsigned long v = strtoul (buf, &end, base);
841 if (errno) return false;
842 if (*end) return false;
843 *out = v;
844 return true;
845}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400846
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400847
Behdad Esfahbodc57d4542011-04-20 18:50:27 -0400848#endif /* HB_PRIVATE_HH */