hb-private.hh revision 2409d5f8d7dd8b535ce5ea29e933f7db27d33793
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
32df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if HAVE_CONFIG_H
33df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h"
34df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
35c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
36bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#include "hb-common.h"
37b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
38c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h>
39f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h>
40f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h>
411d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod
421d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod/* We only use these two for debug output.  However, the debug code is
431d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * always seen by the compiler (and optimized out in non-debug builds.
441d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * If including these becomes a problem, we can start thinking about
451d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * someway around that. */
467acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h>
477acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h>
48c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
49acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
50acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
51bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
52bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Essentials */
53bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
54bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef NULL
55bc200457430c083914a64bf4b056153506749610Behdad Esfahbod# define NULL ((void *) 0)
56bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
57bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
58bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef FALSE
59bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define FALSE 0
60bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
61bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef TRUE
62bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRUE 1
63df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
64a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod
65c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */
66c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
67c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN
68c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#define MIN(a,b) ((a) < (b) ? (a) : (b))
697586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod
708a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX
718a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#define MAX(a,b) ((a) > (b) ? (a) : (b))
728a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod
734591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef  ARRAY_LENGTH
744591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
7512360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod
7635a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do
7735a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END   while (0)
785b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
79303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
80303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
81303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
82303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod
83dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
84dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod
85bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
866fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types.  Saves trouble down the road. */
876fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
886fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1);
896fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1);
906fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2);
916fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2);
926fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4);
936fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4);
946fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8);
956fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8);
966fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
97b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
98b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4);
99b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4);
100ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4);
1016fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
102bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */
103bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
104c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
105df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
106494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
10764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
10864d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
109df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
11064d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr)
11164d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr)
112df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
113df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
114df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__
115df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__
116df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x)
117df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
119df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3
12033d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC	__attribute__((pure))
12133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC	__attribute__((const))
122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
12333d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC
12433d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC
125df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
126bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4
12733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED	__attribute__((unused))
128bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else
12933d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED
130bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif
131df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
132eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL
133357d0f29c431b842179ee4b56c21bcc402178ce7Behdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden")))
134eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif
135eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod
136df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
137df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
138df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf
139df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
140df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
141df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER
142df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
143df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline
144df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
145df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
146df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__
147df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
148df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__
149df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
150df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
151df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
1527d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3
1537d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__
1547d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER)
1557d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__
1567d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else
1577d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__
1587d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif
1597d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1607d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1619b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */
16297e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
163c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask)
164c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{
165c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
1669b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return __builtin_popcount (mask);
167c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else
1689b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  /* "HACKMEM 169" */
1699b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register uint32_t y;
1709b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = (mask >> 1) &033333333333;
1719b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = mask - y - ((y >>1) & 033333333333);
1729b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return (((y + (y >> 3)) & 030707070707) % 077);
173c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif
174c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod}
175c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
1769b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */
1779b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
1789b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number)
1799b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{
1809b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
181f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
1829b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else
1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register unsigned int n_bits = 0;
1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  while (number) {
1859b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    n_bits++;
1869b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    number >>= 1;
1879b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  }
1889b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return n_bits;
1899b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif
1909b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod}
191df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
192f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */
193f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
194f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number)
195f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{
196f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
197f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? __builtin_ctz (number) : 0;
198f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else
199f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  register unsigned int n_bits = 0;
200f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  if (unlikely (!number)) return 0;
201f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  while (!(number & 1)) {
202f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    n_bits++;
203f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    number >>= 1;
204f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  }
205f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return n_bits;
206f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif
207f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod}
208f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod
2098f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */
2108f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *);
2118f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
2128f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
213f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* We need external help for these */
214f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
215f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef HAVE_GLIB
216f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
217f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#include <glib.h>
218f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2193e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_atomic_int_t;
220f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	g_atomic_int_exchange_and_add (&(AI), V)
221f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			g_atomic_int_get (&(AI))
222f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_set(AI, V)		g_atomic_int_set (&(AI), V)
223f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
224f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef GStaticMutex hb_mutex_t;
225f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define HB_MUTEX_INIT			G_STATIC_MUTEX_INIT
226f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_init(M)		g_static_mutex_init (&M)
227f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_lock(M)		g_static_mutex_lock (&M)
228f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_trylock(M)		g_static_mutex_trylock (&M)
229f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_unlock(M)		g_static_mutex_unlock (&M)
230f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
231f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
232f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
233f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef _MSC_VER
234ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR2__(x) #x
235ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR1__(x) _HB__STR2__(x)
236ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
23782438c6ad5f60b6afc75c48cef72e18bef532468Bradley Grainger#pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
238f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
239f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#warning "Could not find any system to define platform macros, library will NOT be thread-safe"
240f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
241f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2423e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_atomic_int_t;
243f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	((AI) += (V), (AI) - (V))
244f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			(AI)
245ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_atomic_int_set(AI, V)		HB_STMT_START { (AI) = (V); } HB_STMT_END
246f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2473e8bdbf9414291da5cf61213d5f4275c1ae23ae5Behdad Esfahbodtypedef volatile int hb_mutex_t;
248ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define HB_MUTEX_INIT				0
249ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_init(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
250ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_lock(M)			HB_STMT_START { (M) = 1; } HB_STMT_END
251ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_trylock(M)			((M) = 1, 1)
252ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_unlock(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
253f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
254f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
255f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
256f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
257f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */
258f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2597a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16(v)		((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
2607a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
261e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
2627a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v)	(uint16_t) ((v[0] << 8) + v[1])
2637a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_cmp(a,b)	(a[0] == b[0] && a[1] == b[1])
2647a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
265e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad 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
2667a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v)	(uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
2677a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_cmp(a,b)	(a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
268f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
269f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2704188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */
271db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
272db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define ISALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
273db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))
274db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
275db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
2764188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
2774188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[1], \
2784188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[2], \
2794188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[3]))
2804188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod
281db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
282bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */
283bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
284bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG
285bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0
286bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
287bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
288eaf29edb8fa49390e5f48b78105dfd173aff445bJeff Muizelaarstatic inline hb_bool_t /* always returns TRUE */
289bc200457430c083914a64bf4b056153506749610Behdad Esfahbod_hb_trace (const char *what,
290bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const char *function,
291bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const void *obj,
292bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int depth,
293bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int max_depth)
294bc200457430c083914a64bf4b056153506749610Behdad Esfahbod{
29511e3ec444a85fc72541823c2e98cc92c4ceb19afBehdad Esfahbod  (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
296bc200457430c083914a64bf4b056153506749610Behdad Esfahbod  return TRUE;
297bc200457430c083914a64bf4b056153506749610Behdad Esfahbod}
298bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
299f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
300c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#include "hb-object-private.hh"
301ba8d94ce3360bfaf2c530d01f2af69237018a531Behdad Esfahbod
302acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
303acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
304acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
305c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */
306