hb-private.hh revision d1c9eb458c843215da8df84b596bfae51fee135b
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2007,2008,2009  Red Hat, Inc.
32409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2011  Google, Inc.
464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
5c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without
864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the
1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software.
1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE.
1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
262409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Google Author(s): Behdad Esfahbod
2764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */
2864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod
29c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#ifndef HB_PRIVATE_HH
30c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#define HB_PRIVATE_HH
315b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
325ddd9cc499f2470eca239ae357a5c8a3626c0809Behdad Esfahbod#ifdef HAVE_CONFIG_H
33df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h"
34df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
35c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
36d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#include "hb.h"
37d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#include "hb-ot.h"
38d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#define HB_H_IN
39d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#define HB_OT_H_IN
40b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
41c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h>
42b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod#include <stddef.h>
43f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h>
44f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h>
451d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod
461d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod/* We only use these two for debug output.  However, the debug code is
471d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * always seen by the compiler (and optimized out in non-debug builds.
481d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * If including these becomes a problem, we can start thinking about
491d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * someway around that. */
507acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h>
517acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h>
52cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#include <stdarg.h>
53c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
54acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
55bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
56bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Essentials */
57bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
58bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef NULL
59bc200457430c083914a64bf4b056153506749610Behdad Esfahbod# define NULL ((void *) 0)
60bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
61bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
62bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef FALSE
63bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define FALSE 0
64bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
65bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef TRUE
66bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRUE 1
67df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
68a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod
69c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */
70c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
71153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod
72c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN
73153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
747586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod
758a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX
76153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
77153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod
788a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod
794591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef  ARRAY_LENGTH
804591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
8112360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod
8235a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do
8335a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END   while (0)
845b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
85303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
86303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
87303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
88303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod
89dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
904ec30aec3014be6effc09cbbc88dcd075f3826dfBehdad Esfahbod#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
91dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod
92bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
936fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types.  Saves trouble down the road. */
946fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
956fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1);
966fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1);
976fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2);
986fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2);
996fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4);
1006fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4);
1016fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8);
1026fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8);
1036fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
104b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
105b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4);
106b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4);
107ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4);
1086fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
109bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */
110bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
111c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
112df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
113494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
11464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
11564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
116df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
11764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr)
11864d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr)
119df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
120df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__
122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__
123df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x)
124df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
125df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
126df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3
12733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC	__attribute__((pure))
12833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC	__attribute__((const))
129cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
130df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
13133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC
13233d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC
13344b4f50d05fd51d8056a78e359fbb00fbcac43a0Behdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx)
134df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
135bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4
13633d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED	__attribute__((unused))
137bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else
13833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED
139bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif
140df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
141eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL
142f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# ifndef __MINGW32__
143f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod#  define HB_INTERNAL __attribute__((__visibility__("hidden")))
144f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# else
145f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod#  define HB_INTERNAL
146f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# endif
147eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif
148eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod
149df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
150df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
151df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf
152df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
153df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
154df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER
155df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
156df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline
157df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
158df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
159df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__
160df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
161df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__
162df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
163df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
164df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
1657d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3
1667d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__
1677d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER)
1687d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__
1697d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else
1707d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__
1717d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif
1727d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1737d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1749b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */
17597e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
176c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask)
177c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{
178c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
1799b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return __builtin_popcount (mask);
180c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else
1819b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  /* "HACKMEM 169" */
1829b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register uint32_t y;
1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = (mask >> 1) &033333333333;
1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = mask - y - ((y >>1) & 033333333333);
1859b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return (((y + (y >> 3)) & 030707070707) % 077);
186c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif
187c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod}
188c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
1899b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */
1909b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
1919b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number)
1929b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{
1939b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
194f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
1959b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else
1969b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register unsigned int n_bits = 0;
1979b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  while (number) {
1989b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    n_bits++;
1999b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    number >>= 1;
2009b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  }
2019b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return n_bits;
2029b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif
2039b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod}
204df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
205f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */
206f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
207f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number)
208f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{
209f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
210f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? __builtin_ctz (number) : 0;
211f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else
212f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  register unsigned int n_bits = 0;
213f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  if (unlikely (!number)) return 0;
214f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  while (!(number & 1)) {
215f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    n_bits++;
216f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    number >>= 1;
217f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  }
218f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return n_bits;
219f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif
220f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod}
221f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod
222080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbodstatic inline bool
223080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
224080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod{
225080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod  return (size > 0) && (count >= ((unsigned int) -1) / size);
226080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod}
227080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod
228080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod
2298f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */
2308f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *);
2318f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
2328f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
233852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
234852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
235852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod/* arrays and maps */
236852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
237852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
238852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type, unsigned int StaticSize>
239b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_prealloced_array_t {
240852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
241852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  unsigned int len;
242852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  unsigned int allocated;
243852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  Type *array;
244852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  Type static_array[StaticSize];
245852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
246efde8113258b117ec0a7fbffe6d681442d045c41Behdad Esfahbod  hb_prealloced_array_t (void) { memset (this, 0, sizeof (*this)); }
247efde8113258b117ec0a7fbffe6d681442d045c41Behdad Esfahbod
248265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod  inline Type& operator [] (unsigned int i) { return array[i]; }
249265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const { return array[i]; }
250852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
251852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  inline Type *push (void)
252852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  {
253852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    if (!array) {
254852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      array = static_array;
255852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      allocated = ARRAY_LENGTH (static_array);
256852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    }
257852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    if (likely (len < allocated))
258852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      return &array[len++];
2595a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod
260852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    /* Need to reallocate */
261852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    unsigned int new_allocated = allocated + (allocated >> 1) + 8;
2625a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod    Type *new_array = NULL;
2635a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod
264852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    if (array == static_array) {
265852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      new_array = (Type *) calloc (new_allocated, sizeof (Type));
2665a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod      if (new_array)
267852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod        memcpy (new_array, array, len * sizeof (Type));
268852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    } else {
269080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod      bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
2705a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod      if (likely (!overflows)) {
271852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod	new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
272852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      }
273852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    }
2745a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod
2755a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod    if (unlikely (!new_array))
276852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      return NULL;
2775a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod
2785a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod    array = new_array;
2795a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod    allocated = new_allocated;
2805a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod    return &array[len++];
281852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  }
282852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
283852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  inline void pop (void)
284852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  {
285852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    len--;
286852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod    /* TODO: shrink array if needed */
287852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  }
28844b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod
28944b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  inline void shrink (unsigned int l)
29044b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  {
29144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod     if (l < len)
29244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod       len = l;
29344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod    /* TODO: shrink array if needed */
29444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  }
29544b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod
2966843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  template <typename T>
2976843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  inline Type *find (T v) {
2986843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    for (unsigned int i = 0; i < len; i++)
2996843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod      if (array[i] == v)
3006843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod	return &array[i];
3016843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    return NULL;
3026843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  }
3036843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  template <typename T>
3046843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  inline const Type *find (T v) const {
3056843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    for (unsigned int i = 0; i < len; i++)
3066843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod      if (array[i] == v)
3076843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod	return &array[i];
3086843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    return NULL;
3096843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  }
3106843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod
31144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  inline void sort (void)
31244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  {
31344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod    qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
31444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod  }
3156843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod
316b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod  inline void sort (unsigned int start, unsigned int end)
317b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod  {
318b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod    qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
319b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod  }
320b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod
3216843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  template <typename T>
3226843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  inline Type *bsearch (T *key)
3236843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  {
3246843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
3256843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  }
3266843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  template <typename T>
3276843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  inline const Type *bsearch (T *key) const
3286843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  {
3296843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
3306843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  }
3316a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod
3326a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod  inline void finish (void)
3336a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod  {
3346a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod    if (array != static_array)
3356a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod      free (array);
3366a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod    array = NULL;
3376a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod    allocated = len = 0;
3386a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod  }
339852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod};
340852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
341852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type>
342b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_array_t : hb_prealloced_array_t<Type, 2> {};
343a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
344a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
34545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodtemplate <typename item_t, typename lock_t>
34645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodstruct hb_lockable_set_t
347852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod{
348852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  hb_array_t <item_t> items;
349852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
350478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod  template <typename T>
35133ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod  inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
352852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  {
35345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.lock ();
3546843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    item_t *item = items.find (v);
35545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    if (item) {
35633ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod      if (replace) {
35733ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod	item_t old = *item;
35833ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod	*item = v;
35933ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod	l.unlock ();
36033ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod	old.finish ();
36133ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod      }
36233ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod      else {
36333ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod        item = NULL;
36433ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod	l.unlock ();
36533ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod      }
36645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    } else {
367852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod      item = items.push ();
36845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      if (likely (item))
36945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod	*item = v;
37045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      l.unlock ();
37145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    }
372b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod    return item;
373852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  }
374852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
375811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod  template <typename T>
37645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  inline void remove (T v, lock_t &l)
377852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  {
37845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.lock ();
3796843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod    item_t *item = items.find (v);
38045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    if (item) {
38145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      item_t old = *item;
38245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      *item = items[items.len - 1];
38345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      items.pop ();
38445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      l.unlock ();
38545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      old.finish ();
38645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    } else {
38745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      l.unlock ();
38845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    }
389852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  }
390a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
391478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod  template <typename T>
39245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  inline bool find (T v, item_t *i, lock_t &l)
393852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod  {
39445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.lock ();
39545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    item_t *item = items.find (v);
39645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    if (item)
39745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      *i = *item;
39845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.unlock ();
39945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    return !!item;
400811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod  }
401811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod
402b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod  template <typename T>
40345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  inline item_t *find_or_insert (T v, lock_t &l)
40445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  {
40545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.lock ();
40645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    item_t *item = items.find (v);
407b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod    if (!item) {
408b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod      item = items.push ();
409b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod      if (likely (item))
410b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod        *item = v;
411b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod    }
41245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.unlock ();
413b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod    return item;
414b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod  }
415b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod
41645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  inline void finish (lock_t &l)
41745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  {
41845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.lock ();
41945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    while (items.len) {
42045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod      item_t old = items[items.len - 1];
42145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod	items.pop ();
42245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod	l.unlock ();
42345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod	old.finish ();
42445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod	l.lock ();
42545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    }
4266a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod    items.finish ();
42745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod    l.unlock ();
42845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod  }
42945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod
43045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod};
43145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod
432852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod
433a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
434a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod
435f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */
436f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
437153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline uint16_t hb_be_uint16 (const uint16_t v)
438153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{
439153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod  const uint8_t *V = (const uint8_t *) &v;
440153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod  return (uint16_t) (V[0] << 8) + V[1];
441153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod}
4427a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
443e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
4447a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v)	(uint16_t) ((v[0] << 8) + v[1])
445153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint16_eq(a,b)	(a[0] == b[0] && a[1] == b[1])
4467a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
447e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#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
4487a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v)	(uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
449153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint32_eq(a,b)	(a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
450f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
451f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
4524188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */
453db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
454153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALPHA (unsigned char c)
455153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
456153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALNUM (unsigned char c)
457153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
458153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOUPPER (unsigned char c)
459153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
460153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOLOWER (unsigned char c)
461153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
462db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
4634188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
4644188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[1], \
4654188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[2], \
4664188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[3]))
4674188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod
468db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
469831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* C++ helpers */
470831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod
471831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* Makes class uncopyable.  Use in private: section. */
472831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod#define NO_COPY(T) \
473831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod  T (const T &o); \
474970e092dc23cbeb7897d4c7bb58c042209f518fbBehdad Esfahbod  T &operator = (const T &o)
475831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod
476831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod
477bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */
478bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
479cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
480bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG
481bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0
482bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
483bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
484cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstatic inline bool
485cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug (unsigned int level,
486cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	   unsigned int max_level)
487cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{
488cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  return level < max_level;
489cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}
490cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
491cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
49243ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2Behdad Esfahbod#define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
49343ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2Behdad Esfahbod
494cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <int max_level> inline bool /* always returns TRUE */
495cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what,
496cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const void *obj,
497cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const char *func,
498cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       bool indented,
499cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       int level,
500cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const char *message,
501cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       ...) HB_PRINTF_FUNC(6, 7);
502cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <int max_level> inline bool /* always returns TRUE */
503cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what,
504cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const void *obj,
505cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const char *func,
506cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       bool indented,
507cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       int level,
508cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       const char *message,
509cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	       ...)
510cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{
511cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  va_list ap;
512cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  va_start (ap, message);
513cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
514cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  (void) (_hb_debug (level, max_level) &&
5150fbb2dc83132a89201ad8b56c6909610437d2da0Behdad Esfahbod	  fprintf (stderr, "%s", what) &&
5160fbb2dc83132a89201ad8b56c6909610437d2da0Behdad Esfahbod	  (obj && fprintf (stderr, "(%p)", obj), TRUE) &&
5170fbb2dc83132a89201ad8b56c6909610437d2da0Behdad Esfahbod	  fprintf (stderr, ": ") &&
518cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	  (func && fprintf (stderr, "%s: ", func), TRUE) &&
519cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	  (indented && fprintf (stderr, "%-*d-> ", level + 1, level), TRUE) &&
520cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	  vfprintf (stderr, message, ap) &&
521cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod	  fprintf (stderr, "\n"));
522cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
523cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  va_end (ap);
524decd4e3e16424dc311e9fb5b663170414a11556aBehdad Esfahbod
525cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  return TRUE;
526cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}
527cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <> inline bool /* always returns TRUE */
528cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg<0> (const char *what,
529cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const void *obj,
530cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const char *func,
531cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  bool indented,
532cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  int level,
533cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const char *message,
534cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  ...) HB_PRINTF_FUNC(6, 7);
535cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <> inline bool /* always returns TRUE */
536cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg<0> (const char *what,
537cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const void *obj,
538cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const char *func,
539cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  bool indented,
540cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  int level,
541cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  const char *message,
542cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod		  ...)
543bc200457430c083914a64bf4b056153506749610Behdad Esfahbod{
544bc200457430c083914a64bf4b056153506749610Behdad Esfahbod  return TRUE;
545bc200457430c083914a64bf4b056153506749610Behdad Esfahbod}
546bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
547cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, FALSE, (LEVEL), __VA_ARGS__)
548cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define DEBUG_MSG(WHAT, OBJ, ...) DEBUG_MSG_LEVEL (WHAT, OBJ, 0, __VA_ARGS__)
549cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, FALSE, 0, __VA_ARGS__)
550cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
551cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
552cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/*
553cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod * Trace
554cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod */
555cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
556cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <int max_level>
557cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstruct hb_auto_trace_t {
558cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  explicit inline hb_auto_trace_t (unsigned int *plevel_,
559cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *what,
560cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const void *obj,
561cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *func,
562cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *message) : plevel(plevel_)
563cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  {
564cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod    if (max_level) ++*plevel;
565cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod    /* TODO support variadic args here */
566cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod    _hb_debug_msg<max_level> (what, obj, func, TRUE, *plevel, "%s", message);
567cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  }
568cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  ~hb_auto_trace_t (void) { if (max_level) --*plevel; }
569cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
570cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  private:
571cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  unsigned int *plevel;
572cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod};
573cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodtemplate <> /* Optimize when tracing is disabled */
574cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstruct hb_auto_trace_t<0> {
575cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod  explicit inline hb_auto_trace_t (unsigned int *plevel_,
576cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *what,
577cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const void *obj,
578cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *func,
579cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod				   const char *message) {}
580cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod};
581cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
582cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
583cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/* Misc */
584cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod
585f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
5867b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod/* Pre-mature optimization:
5877b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * Checks for lo <= u <= hi but with an optimization if lo and hi
5887b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * are only different in a contiguous set of lower-most bits.
5897b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod */
5908f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbodtemplate <typename T> inline bool
5918f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbodhb_in_range (T u, T lo, T hi)
5927b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod{
5937b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod  if ( ((lo^hi) & lo) == 0 &&
5947b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod       ((lo^hi) & hi) == (lo^hi) &&
5957b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod       ((lo^hi) & ((lo^hi) + 1)) == 0 )
5967b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod    return (u & ~(lo^hi)) == lo;
5977b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod  else
5987b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod    return lo <= u && u <= hi;
5997b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod}
6007b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod
6017b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod
60245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod/* Useful for set-operations on small enums.
60345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * For example, for testing "x ∈ {x1, x2, x3}" use:
60445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
60545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod */
6067b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod#define FLAG(x) (1<<(x))
6077b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod
6088f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbod
60945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodtemplate <typename T> inline void
61045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodhb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
61145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
61245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  if (unlikely (!len))
61345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    return;
61445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
61545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  unsigned int k = len - 1;
61645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  do {
61745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    unsigned int new_k = 0;
61845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
61945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    for (unsigned int j = 0; j < k; j++)
62045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod      if (compar (&array[j], &array[j+1]) > 0) {
62145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod        T t;
62245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	t = array[j];
62345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	array[j] = array[j + 1];
62445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	array[j + 1] = t;
62545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
62645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	new_k = j;
62745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod      }
62845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    k = new_k;
62945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  } while (k);
63045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
63145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
63245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
63345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
634acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
635c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */
636