hb-private.hh revision c57d454accff66e5f2c58006e8fb40bc020b6182
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
26c78683c042250a7b5a6fc6ebae4717b03fadf9eBehdad Esfahbod * Copyright (C) 2007,2008,2009  Red Hat, Inc.
364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
4c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without
764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the
964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software.
1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE.
1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */
2664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod
27c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#ifndef HB_PRIVATE_HH
28c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#define HB_PRIVATE_HH
295b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
30df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if HAVE_CONFIG_H
31df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h"
32df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
33c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
34bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#include "hb-common.h"
35b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
36c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h>
37f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h>
38f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h>
391d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod
401d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod/* We only use these two for debug output.  However, the debug code is
411d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * always seen by the compiler (and optimized out in non-debug builds.
421d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * If including these becomes a problem, we can start thinking about
431d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * someway around that. */
447acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h>
457acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h>
46c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
47acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
48acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
49bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
50bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Essentials */
51bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
52bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef NULL
53bc200457430c083914a64bf4b056153506749610Behdad Esfahbod# define NULL ((void *) 0)
54bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
55bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
56bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef FALSE
57bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define FALSE 0
58bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
59bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef TRUE
60bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRUE 1
61df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
62a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod
63c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */
64c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
65c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN
66c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#define MIN(a,b) ((a) < (b) ? (a) : (b))
677586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod
688a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX
698a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#define MAX(a,b) ((a) > (b) ? (a) : (b))
708a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod
714591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef  ARRAY_LENGTH
724591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
7312360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod
7435a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do
7535a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END   while (0)
765b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod
77303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
78303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
79303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
80303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod
81bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
826fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types.  Saves trouble down the road. */
836fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
846fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1);
856fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1);
866fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2);
876fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2);
886fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4);
896fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4);
906fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8);
916fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8);
926fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
93b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
94b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4);
95b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4);
96ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4);
976fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod
98bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */
99bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
100c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
101df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
102494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
10364d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
10464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
105df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
10664d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr)
10764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr)
108df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
109df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
110df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__
111df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__
112df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x)
113df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
114df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
115df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3
11633d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC	__attribute__((pure))
11733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC	__attribute__((const))
118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else
11933d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC
12033d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC
121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
122bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4
12333d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED	__attribute__((unused))
124bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else
12533d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED
126bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif
127df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
128eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL
129357d0f29c431b842179ee4b56c21bcc402178ce7Behdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden")))
130eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif
131eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod
132df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
133df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
134df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf
135df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
136df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
137df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER
138df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
139df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline
140df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
141df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
142df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__
143df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline
144df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__
145df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif
146df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
147df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
1487d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3
1497d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__
1507d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER)
1517d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__
1527d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else
1537d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__
1547d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif
1557d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1567d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod
1579b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */
15897e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
159c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask)
160c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{
161c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
1629b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return __builtin_popcount (mask);
163c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else
1649b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  /* "HACKMEM 169" */
1659b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register uint32_t y;
1669b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = (mask >> 1) &033333333333;
1679b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  y = mask - y - ((y >>1) & 033333333333);
1689b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return (((y + (y >> 3)) & 030707070707) % 077);
169c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif
170c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod}
171c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod
1729b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */
1739b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
1749b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number)
1759b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{
1769b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
177f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
1789b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else
1799b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  register unsigned int n_bits = 0;
1809b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  while (number) {
1819b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    n_bits++;
1829b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod    number >>= 1;
1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  }
1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod  return n_bits;
1859b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif
1869b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod}
187df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod
188f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */
189f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int
190f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number)
191f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{
192f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
193f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return likely (number) ? __builtin_ctz (number) : 0;
194f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else
195f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  register unsigned int n_bits = 0;
196f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  if (unlikely (!number)) return 0;
197f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  while (!(number & 1)) {
198f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    n_bits++;
199f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod    number >>= 1;
200f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  }
201f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod  return n_bits;
202f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif
203f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod}
204f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod
2058f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */
2068f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *);
2078f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
2088f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod
209f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* We need external help for these */
210f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
211f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef HAVE_GLIB
212f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
213f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#include <glib.h>
214f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
215f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef int hb_atomic_int_t;
216f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	g_atomic_int_exchange_and_add (&(AI), V)
217f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			g_atomic_int_get (&(AI))
218f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_set(AI, V)		g_atomic_int_set (&(AI), V)
219f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
220f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef GStaticMutex hb_mutex_t;
221f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define HB_MUTEX_INIT			G_STATIC_MUTEX_INIT
222f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_init(M)		g_static_mutex_init (&M)
223f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_lock(M)		g_static_mutex_lock (&M)
224f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_trylock(M)		g_static_mutex_trylock (&M)
225f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_mutex_unlock(M)		g_static_mutex_unlock (&M)
226f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
227f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
228f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
229f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#ifdef _MSC_VER
230ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR2__(x) #x
231ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__STR1__(x) _HB__STR2__(x)
232ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad Esfahbod#define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
23382438c6ad5f60b6afc75c48cef72e18bef532468Bradley Grainger#pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
234f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#else
235f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#warning "Could not find any system to define platform macros, library will NOT be thread-safe"
236f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
237f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
238f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef int hb_atomic_int_t;
239f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_fetch_and_add(AI, V)	((AI) += (V), (AI) - (V))
240f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#define hb_atomic_int_get(AI)			(AI)
241ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_atomic_int_set(AI, V)		HB_STMT_START { (AI) = (V); } HB_STMT_END
242f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
243f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbodtypedef int hb_mutex_t;
244ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define HB_MUTEX_INIT				0
245ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_init(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
246ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_lock(M)			HB_STMT_START { (M) = 1; } HB_STMT_END
247ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_trylock(M)			((M) = 1, 1)
248ffff7dc44cb2a35a60f92831165e9d3c3a61ce19Behdad Esfahbod#define hb_mutex_unlock(M)			HB_STMT_START { (M) = 0; } HB_STMT_END
249f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
250f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod#endif
251f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
252f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
253f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */
254f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2557a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16(v)		((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
2567a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
257e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
2587a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v)	(uint16_t) ((v[0] << 8) + v[1])
2597a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_cmp(a,b)	(a[0] == b[0] && a[1] == b[1])
2607a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod
261e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad 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
2627a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v)	(uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
2637a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_cmp(a,b)	(a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
264f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
265f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
2664188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */
267db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
268db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define ISALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
269db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))
270db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
271db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
2724188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
2734188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[1], \
2744188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[2], \
2754188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod				  ((const char *) s)[3]))
2764188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod
277db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod
278bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */
279bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
280bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG
281bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0
282bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif
283bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
284eaf29edb8fa49390e5f48b78105dfd173aff445bJeff Muizelaarstatic inline hb_bool_t /* always returns TRUE */
285bc200457430c083914a64bf4b056153506749610Behdad Esfahbod_hb_trace (const char *what,
286bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const char *function,
287bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   const void *obj,
288bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int depth,
289bc200457430c083914a64bf4b056153506749610Behdad Esfahbod	   unsigned int max_depth)
290bc200457430c083914a64bf4b056153506749610Behdad Esfahbod{
29111e3ec444a85fc72541823c2e98cc92c4ceb19afBehdad Esfahbod  (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
292bc200457430c083914a64bf4b056153506749610Behdad Esfahbod  return TRUE;
293bc200457430c083914a64bf4b056153506749610Behdad Esfahbod}
294bc200457430c083914a64bf4b056153506749610Behdad Esfahbod
295f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod
296c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#include "hb-object-private.hh"
297ba8d94ce3360bfaf2c530d01f2af69237018a531Behdad Esfahbod
298acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
299acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
300acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
301c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */
302