hb-private.hh revision 45bfa99034512e886d75b1d45a5a649647f4711f
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 67153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad EsfahbodHB_END_DECLS 68153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 69c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN 70153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } 717586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod 728a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX 73153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } 74153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 75153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad EsfahbodHB_BEGIN_DECLS 768a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod 774591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef ARRAY_LENGTH 784591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) 7912360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod 8035a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do 8135a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END while (0) 825b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 83303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 84303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 85303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 86303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod 87dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1])) 88dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod 89bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 906fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types. Saves trouble down the road. */ 916fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 926fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1); 936fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1); 946fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2); 956fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2); 966fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4); 976fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4); 986fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8); 996fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8); 1006fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 101b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4); 102b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4); 103b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4); 104ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4); 1056fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 106bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */ 107bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 108c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 109df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 110494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) 11164d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) 11264d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) 113df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 11464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr) 11564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr) 116df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 117df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__ 119df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__ 120df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x) 121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 123df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3 12433d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC __attribute__((pure)) 12533d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC __attribute__((const)) 126df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 12733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC 12833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC 129df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 130bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4 13133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED __attribute__((unused)) 132bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else 13333d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED 134bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif 135df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 136eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL 137357d0f29c431b842179ee4b56c21bcc402178ce7Behdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden"))) 138eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif 139eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod 140df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 141df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) 142df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf 143df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 144df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 145df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER 146df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 147df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline 148df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 149df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 150df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__ 151df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 152df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__ 153df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 154df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 155df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 1567d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3 1577d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__ 1587d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER) 1597d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__ 1607d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else 1617d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__ 1627d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif 1637d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 1647d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 1659b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */ 16697e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 167c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask) 168c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{ 169c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 1709b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return __builtin_popcount (mask); 171c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else 1729b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod /* "HACKMEM 169" */ 1739b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod register uint32_t y; 1749b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = (mask >> 1) &033333333333; 1759b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = mask - y - ((y >>1) & 033333333333); 1769b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return (((y + (y >> 3)) & 030707070707) % 077); 177c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 178c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod} 179c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 1809b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */ 1819b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 1829b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number) 1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{ 1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 185f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; 1869b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else 1879b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod register unsigned int n_bits = 0; 1889b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod while (number) { 1899b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod n_bits++; 1909b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod number >>= 1; 1919b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod } 1929b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return n_bits; 1939b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif 1949b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod} 195df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 196f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */ 197f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 198f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number) 199f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{ 200f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 201f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? __builtin_ctz (number) : 0; 202f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else 203f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod register unsigned int n_bits = 0; 204f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod if (unlikely (!number)) return 0; 205f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod while (!(number & 1)) { 206f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod n_bits++; 207f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod number >>= 1; 208f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod } 209f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return n_bits; 210f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif 211f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod} 212f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod 213080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbodstatic inline bool 214080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) 215080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod{ 216080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod return (size > 0) && (count >= ((unsigned int) -1) / size); 217080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod} 218080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 219080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 2208f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */ 2218f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *); 2228f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 2238f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 224852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad EsfahbodHB_END_DECLS 225852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 226852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 227852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod/* arrays and maps */ 228852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 229852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 230852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type, unsigned int StaticSize> 231b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_prealloced_array_t { 232852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 233852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int len; 234852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int allocated; 235852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type *array; 236852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type static_array[StaticSize]; 237852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 238265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline Type& operator [] (unsigned int i) { return array[i]; } 239265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline const Type& operator [] (unsigned int i) const { return array[i]; } 240852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 241852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline Type *push (void) 242852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 243852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (!array) { 244852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod array = static_array; 245852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod allocated = ARRAY_LENGTH (static_array); 246852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 247852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (likely (len < allocated)) 248852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return &array[len++]; 2495a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 250852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod /* Need to reallocate */ 251852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int new_allocated = allocated + (allocated >> 1) + 8; 2525a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod Type *new_array = NULL; 2535a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 254852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (array == static_array) { 255852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) calloc (new_allocated, sizeof (Type)); 2565a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (new_array) 257852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod memcpy (new_array, array, len * sizeof (Type)); 258852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } else { 259080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); 2605a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (likely (!overflows)) { 261852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); 262852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 263852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 2645a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 2655a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (unlikely (!new_array)) 266852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return NULL; 2675a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 2685a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod array = new_array; 2695a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod allocated = new_allocated; 2705a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod return &array[len++]; 271852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 272852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 273852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline void pop (void) 274852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 275852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod len--; 276852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod /* TODO: shrink array if needed */ 277852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 27844b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 27944b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void shrink (unsigned int l) 28044b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 28144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod if (l < len) 28244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod len = l; 28344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod /* TODO: shrink array if needed */ 28444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 28544b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 2866843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 2876843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *find (T v) { 2886843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 2896843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 2906843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 2916843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 2926843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 2936843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 2946843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *find (T v) const { 2956843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 2966843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 2976843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 2986843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 2996843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3006843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 30144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void sort (void) 30244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 30344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 30444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 3056843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 3066843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3076843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *bsearch (T *key) 3086843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3096843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3106843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3116843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3126843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *bsearch (T *key) const 3136843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3146843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3156843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 316852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod}; 317852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 318852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type> 319b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_array_t : hb_prealloced_array_t<Type, 2> {}; 320a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 321a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 32245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodtemplate <typename item_t, typename lock_t> 32345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodstruct hb_lockable_set_t 324852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod{ 325852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod hb_array_t <item_t> items; 326852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 327478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 32845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *replace_or_insert (T v, lock_t &l) 329852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 33045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 3316843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 33245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 33345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = *item; 33445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = v; 33545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 33645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 33745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 338852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod item = items.push (); 33945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (likely (item)) 34045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = v; 34145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 34245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 343b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 344852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 345852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 346811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod template <typename T> 34745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void remove (T v, lock_t &l) 348852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 34945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 3506843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 35145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 35245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = *item; 35345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = items[items.len - 1]; 35445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 35545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 35645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 35745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 35845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 35945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 360852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 361a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 362478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 36345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline bool find (T v, item_t *i, lock_t &l) 364852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 36545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 36645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 36745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) 36845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *i = *item; 36945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 37045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return !!item; 371811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod } 372811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod 373b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod template <typename T> 37445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *find_or_insert (T v, lock_t &l) 37545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 37645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 37745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 378b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (!item) { 379b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod item = items.push (); 380b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (likely (item)) 381b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod *item = v; 382b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 38345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 384b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 385b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 386b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod 38745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void finish (lock_t &l) 38845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 38945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 39045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod while (items.len) { 39145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = items[items.len - 1]; 39245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 39345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 39445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 39545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 39645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 397d37486d87b65c5abaaa2998fa5c9e48eedde0933Behdad Esfahbod items.shrink (0); 39845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 39945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 40045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 40145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod}; 40245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 40345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodtemplate <typename item_t> 40445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodstruct hb_set_t 40545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod{ 40645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod struct lock_t { 40745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod int unused; 40845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 40945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void lock (void) {} 41045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void unlock (void) {} 41145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod }; 41245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 41345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod hb_lockable_set_t <item_t, lock_t> set; 41445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 41545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod template <typename T> 41645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *replace_or_insert (T v) 41745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 41845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod lock_t lock; 41945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return set.replace_or_insert (v, lock); 42045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 42145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 42245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod template <typename T> 42345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void remove (T v) 42445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 42545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod lock_t lock; 42645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod set.remove (v, lock); 42745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 42845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 42945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod template <typename T> 43045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline bool find (T v, item_t *i) 43145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 43245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod lock_t lock; 43345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return set.find (v, i, lock); 43445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 43545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 43645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod template <typename T> 43745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *find_or_insert (T v) 43845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 43945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod lock_t lock; 44045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return set.find_or_insert (v, lock); 44145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 44245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 44345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod void finish (void) 44445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 44545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod lock_t lock; 44645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod set.finish (lock); 447852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 448a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 449852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod}; 450852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 451852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 452852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad EsfahbodHB_BEGIN_DECLS 453a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 454a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 455f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */ 456f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 457153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline uint16_t hb_be_uint16 (const uint16_t v) 458153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ 459153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod const uint8_t *V = (const uint8_t *) &v; 460153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod return (uint16_t) (V[0] << 8) + V[1]; 461153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod} 4627a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 463e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END 4647a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) 465153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) 4667a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 467e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad 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 4687a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) 469153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) 470f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 471f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 4724188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */ 473db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 474153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALPHA (unsigned char c) 475153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 476153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALNUM (unsigned char c) 477153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } 478153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOUPPER (unsigned char c) 479153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } 480153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOLOWER (unsigned char c) 481153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } 482db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 4834188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ 4844188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[1], \ 4854188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[2], \ 4864188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[3])) 4874188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod 488db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 489bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */ 490bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 491bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG 492bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0 493bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif 494bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 495fca368c4682624346a0aaee690e1ad6ed4c0b337Behdad Esfahbodstatic inline bool /* always returns TRUE */ 496bc200457430c083914a64bf4b056153506749610Behdad Esfahbod_hb_trace (const char *what, 497bc200457430c083914a64bf4b056153506749610Behdad Esfahbod const char *function, 498bc200457430c083914a64bf4b056153506749610Behdad Esfahbod const void *obj, 499bc200457430c083914a64bf4b056153506749610Behdad Esfahbod unsigned int depth, 500bc200457430c083914a64bf4b056153506749610Behdad Esfahbod unsigned int max_depth) 501bc200457430c083914a64bf4b056153506749610Behdad Esfahbod{ 50211e3ec444a85fc72541823c2e98cc92c4ceb19afBehdad Esfahbod (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function)); 503bc200457430c083914a64bf4b056153506749610Behdad Esfahbod return TRUE; 504bc200457430c083914a64bf4b056153506749610Behdad Esfahbod} 505bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 506f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 507acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS 508acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod 509c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */ 510