hb-private.hh revision 829e814ff358c5e700ba4df54932696801aa9f65
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/* 22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2007,2008,2009 Red Hat, Inc. 32409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2011 Google, Inc. 464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 5c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod * This is part of HarfBuzz, a text shaping library. 664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without 864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this 964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the 1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in 1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software. 1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE. 1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * 2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod 262409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Google Author(s): Behdad Esfahbod 2764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */ 2864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod 29c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#ifndef HB_PRIVATE_HH 30c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#define HB_PRIVATE_HH 315b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 325ddd9cc499f2470eca239ae357a5c8a3626c0809Behdad Esfahbod#ifdef HAVE_CONFIG_H 33df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#include "config.h" 34df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 35c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 36d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#include "hb.h" 37d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#define HB_H_IN 383e32cd9570fd8b09901fb790b80365ae425f681aBehdad Esfahbod#ifdef HAVE_OT 393e32cd9570fd8b09901fb790b80365ae425f681aBehdad Esfahbod#include "hb-ot.h" 40d1c9eb458c843215da8df84b596bfae51fee135bBehdad Esfahbod#define HB_OT_H_IN 413e32cd9570fd8b09901fb790b80365ae425f681aBehdad Esfahbod#endif 42b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod 43c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#include <stdlib.h> 44b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod#include <stddef.h> 45f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <string.h> 46f0954d1e08ae288eda9904e17108fc73f48c0b98Behdad Esfahbod#include <assert.h> 471d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod 481d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod/* We only use these two for debug output. However, the debug code is 491d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * always seen by the compiler (and optimized out in non-debug builds. 501d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * If including these becomes a problem, we can start thinking about 511d52151a19ed8ec776276aac5cc6ce3769e0d947Behdad Esfahbod * someway around that. */ 527acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <stdio.h> 537acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod#include <errno.h> 54cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#include <stdarg.h> 55c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 56acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod 57bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 58bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Essentials */ 59bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 60bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef NULL 61bc200457430c083914a64bf4b056153506749610Behdad Esfahbod# define NULL ((void *) 0) 62bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif 63bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 64bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef FALSE 65bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define FALSE 0 66bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 67bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#undef TRUE 68bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRUE 1 69df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 70a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod 71c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */ 72c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 73153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 74c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN 75153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } 767586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod 778a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX 78153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodtemplate <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } 79153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 808a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod 814591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef ARRAY_LENGTH 824591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) 8312360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod 8435a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do 8535a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END while (0) 865b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 87303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 88303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 89303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 90303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod 91dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1])) 924ec30aec3014be6effc09cbbc88dcd075f3826dfBehdad Esfahbod#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) 93dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod 94bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 956fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types. Saves trouble down the road. */ 966fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 976fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1); 986fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1); 996fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2); 1006fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2); 1016fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4); 1026fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4); 1036fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8); 1046fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8); 1056fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 106b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4); 107b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4); 108b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4); 109ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4); 1106fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 111bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */ 112bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 113c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 114df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 115494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) 11664d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) 11764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) 118df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 11964d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr) 12064d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr) 121df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 122df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 123df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__ 124df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__ 125df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x) 126df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 127df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 128df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3 12933d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC __attribute__((pure)) 13033d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC __attribute__((const)) 131cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) 132df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 13333d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC 13433d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC 13544b4f50d05fd51d8056a78e359fbb00fbcac43a0Behdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx) 136df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 137bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4 13833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED __attribute__((unused)) 139bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else 14033d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED 141bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif 142df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 143eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL 144f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# ifndef __MINGW32__ 145f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden"))) 146f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# else 147f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# define HB_INTERNAL 148f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# endif 149eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif 150eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod 151df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 152df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) 153df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf 154df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 155df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 156df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER 157df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 158df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline 159df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 160df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 161df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__ 162df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 163df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__ 164df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 165df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 166df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 1677d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3 1687d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__ 1697d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER) 1707d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__ 1717d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else 1727d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__ 1737d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif 1747d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 1757d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 1769b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */ 17797e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 178c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask) 179c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{ 180c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 1819b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return __builtin_popcount (mask); 182c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else 1839b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod /* "HACKMEM 169" */ 1849b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod register uint32_t y; 1859b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = (mask >> 1) &033333333333; 1869b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = mask - y - ((y >>1) & 033333333333); 1879b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return (((y + (y >> 3)) & 030707070707) % 077); 188c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 189c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod} 190c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 1919b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */ 1929b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 1939b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number) 1949b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{ 1959b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 196f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; 1979b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else 1989b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod register unsigned int n_bits = 0; 1999b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod while (number) { 2009b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod n_bits++; 2019b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod number >>= 1; 2029b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod } 2039b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return n_bits; 2049b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif 2059b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod} 206df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 207f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */ 208f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 209f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number) 210f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{ 211f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 212f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? __builtin_ctz (number) : 0; 213f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else 214f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod register unsigned int n_bits = 0; 215f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod if (unlikely (!number)) return 0; 216f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod while (!(number & 1)) { 217f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod n_bits++; 218f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod number >>= 1; 219f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod } 220f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return n_bits; 221f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif 222f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod} 223f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod 224080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbodstatic inline bool 225080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) 226080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod{ 227080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod return (size > 0) && (count >= ((unsigned int) -1) / size); 228080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod} 229080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 230080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 2318f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */ 2328f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *); 2338f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 2348f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 235852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 236852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 237852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod/* arrays and maps */ 238852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 239852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 240852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type, unsigned int StaticSize> 241b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_prealloced_array_t { 242852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 243852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int len; 244852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int allocated; 245852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type *array; 246852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type static_array[StaticSize]; 247852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 248efde8113258b117ec0a7fbffe6d681442d045c41Behdad Esfahbod hb_prealloced_array_t (void) { memset (this, 0, sizeof (*this)); } 249efde8113258b117ec0a7fbffe6d681442d045c41Behdad Esfahbod 250265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline Type& operator [] (unsigned int i) { return array[i]; } 251265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline const Type& operator [] (unsigned int i) const { return array[i]; } 252852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 253852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline Type *push (void) 254852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 255852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (!array) { 256852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod array = static_array; 257852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod allocated = ARRAY_LENGTH (static_array); 258852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 259852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (likely (len < allocated)) 260852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return &array[len++]; 2615a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 262852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod /* Need to reallocate */ 263852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int new_allocated = allocated + (allocated >> 1) + 8; 2645a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod Type *new_array = NULL; 2655a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 266852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (array == static_array) { 267852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) calloc (new_allocated, sizeof (Type)); 2685a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (new_array) 269852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod memcpy (new_array, array, len * sizeof (Type)); 270852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } else { 271080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); 2725a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (likely (!overflows)) { 273852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); 274852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 275852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 2765a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 2775a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (unlikely (!new_array)) 278852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return NULL; 2795a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 2805a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod array = new_array; 2815a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod allocated = new_allocated; 2825a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod return &array[len++]; 283852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 284852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 285852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline void pop (void) 286852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 287852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod len--; 288852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod /* TODO: shrink array if needed */ 289852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 29044b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 29144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void shrink (unsigned int l) 29244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 29344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod if (l < len) 29444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod len = l; 29544b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod /* TODO: shrink array if needed */ 29644b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 29744b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 2986843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 2996843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *find (T v) { 3006843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 3016843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 3026843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 3036843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 3046843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3056843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3066843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *find (T v) const { 3076843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 3086843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 3096843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 3106843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 3116843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3126843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 31344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void sort (void) 31444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 31544b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 31644b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 3176843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 318b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod inline void sort (unsigned int start, unsigned int end) 319b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod { 320b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); 321b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod } 322b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod 3236843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3246843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *bsearch (T *key) 3256843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3266843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3276843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3286843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3296843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *bsearch (T *key) const 3306843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3316843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3326843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3336a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod 3346a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod inline void finish (void) 3356a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod { 3366a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod if (array != static_array) 3376a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod free (array); 3386a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod array = NULL; 3396a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod allocated = len = 0; 3406a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod } 341852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod}; 342852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 343852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type> 344b214ec3ac0ce6568e9226fd09661d52de11dca96Behdad Esfahbodstruct hb_array_t : hb_prealloced_array_t<Type, 2> {}; 345a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 346a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 34745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodtemplate <typename item_t, typename lock_t> 34845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodstruct hb_lockable_set_t 349852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod{ 350852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod hb_array_t <item_t> items; 351852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 352478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 35333ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod inline item_t *replace_or_insert (T v, lock_t &l, bool replace) 354852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 35545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 3566843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 35745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 35833ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod if (replace) { 35933ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod item_t old = *item; 36033ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod *item = v; 36133ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod l.unlock (); 36233ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod old.finish (); 36333ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod } 36433ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod else { 36533ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod item = NULL; 36633ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod l.unlock (); 36733ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod } 36845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 369852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod item = items.push (); 37045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (likely (item)) 37145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = v; 37245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 37345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 374b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 375852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 376852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 377811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod template <typename T> 37845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void remove (T v, lock_t &l) 379852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 38045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 3816843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 38245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 38345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = *item; 38445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = items[items.len - 1]; 38545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 38645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 38745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 38845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 38945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 39045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 391852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 392a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 393478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 39445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline bool find (T v, item_t *i, lock_t &l) 395852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 39645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 39745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 39845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) 39945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *i = *item; 40045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 40145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return !!item; 402811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod } 403811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod 404b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod template <typename T> 40545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *find_or_insert (T v, lock_t &l) 40645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 40745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 40845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 409b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (!item) { 410b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod item = items.push (); 411b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (likely (item)) 412b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod *item = v; 413b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 41445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 415b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 416b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 417b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod 41845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void finish (lock_t &l) 41945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 42045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 42145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod while (items.len) { 42245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = items[items.len - 1]; 42345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 42445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 42545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 42645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 42745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 4286a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod items.finish (); 42945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 43045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 43145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 43245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod}; 43345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 434852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 435a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 436a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 437f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */ 438f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 439153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline uint16_t hb_be_uint16 (const uint16_t v) 440153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ 441153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod const uint8_t *V = (const uint8_t *) &v; 442153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod return (uint16_t) (V[0] << 8) + V[1]; 443153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod} 4447a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 445e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod/* Note, of the following macros, uint16_get is the one called many many times. 446e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * If there is any optimizations to be done, it's in that macro. However, I 447e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which 448e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * results in a single ror instruction, does NOT speed this up. In fact, it 449e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * resulted in a minor slowdown. At any rate, note that v may not be correctly 450e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * aligned, so I think the current implementation is optimal. 451e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod */ 452e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod 453e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END 4547a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) 455153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) 4567a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 457e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad 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 4587a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) 459153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) 460f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 461f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 4624188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */ 463db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 464153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALPHA (unsigned char c) 465153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 466153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char ISALNUM (unsigned char c) 467153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } 468153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOUPPER (unsigned char c) 469153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } 470153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOLOWER (unsigned char c) 471153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } 472db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 4734188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ 4744188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[1], \ 4754188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[2], \ 4764188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[3])) 4774188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod 478db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 479831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* C++ helpers */ 480831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 481831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* Makes class uncopyable. Use in private: section. */ 482831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod#define NO_COPY(T) \ 483831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod T (const T &o); \ 484970e092dc23cbeb7897d4c7bb58c042209f518fbBehdad Esfahbod T &operator = (const T &o) 485831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 486831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 487bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */ 488bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 489cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 490bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG 491bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0 492bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif 493bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 494cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstatic inline bool 495cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug (unsigned int level, 496cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod unsigned int max_level) 497cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{ 498cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod return level < max_level; 499cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod} 500cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 501cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) 50243ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2Behdad Esfahbod#define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0)) 50343ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2Behdad Esfahbod 504829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <int max_level> inline void 505208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod_hb_debug_msg_va (const char *what, 5061e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const void *obj, 5071e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *func, 5081e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod bool indented, 5091e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5101e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 5111e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *message, 5121e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod va_list ap) 513208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod{ 5146eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (!_hb_debug (level, max_level)) 515829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod return; 5166eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5171e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod static const char bars[] = "││││││││││││││││││││││││││││││││││││││││"; 5181e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod 5196eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "%-10s", what ? what : ""); 5206eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5216eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (obj) 5226eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "(%p) ", obj); 5236eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod else 5246eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); 5256eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5266eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (indented) 5276eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "%2d %s├%s", 5286eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod level, 5296eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), 3 * level), 5306eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod level_dir ? (level_dir > 0 ? "╮" : "╯") : "╴"); 5316eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod else 5326eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, " ├╴"); 5336eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5346eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (func) 5356eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "%s: ", func); 5366eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5376eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (message) 5386eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod vfprintf (stderr, message, ap); 5396eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5406eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "\n"); 541208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod} 542829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 543208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod_hb_debug_msg_va<0> (const char *what, 5441e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const void *obj, 5451e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *func, 5461e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod bool indented, 5471e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5481e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 5491e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *message, 550829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod va_list ap) {} 551208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod 552829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <int max_level> inline void 553cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what, 554cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 555cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 556cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 5571e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5581e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 559cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 5601e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod ...) HB_PRINTF_FUNC(7, 8); 561829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <int max_level> inline void 562cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what, 563cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 564cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 565cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 5661e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5671e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 568cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 569cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod ...) 570cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{ 571cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_list ap; 572cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_start (ap, message); 573829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap); 574cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_end (ap); 575cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod} 576829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 577cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg<0> (const char *what, 578cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 579cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 580cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 5811e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5821e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 583cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 5841e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod ...) HB_PRINTF_FUNC(7, 8); 585829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 586cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg<0> (const char *what, 587cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 588cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 589cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 5901e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5911e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 592cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 593829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod ...) {} 594bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 5951e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, TRUE, (LEVEL), (LEVEL_DIR), __VA_ARGS__) 5961e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, FALSE, 0, 0, __VA_ARGS__) 5971e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, FALSE, 0, 0, __VA_ARGS__) 598cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 599cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 600cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/* 601cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod * Trace 602cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod */ 603cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 604208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbodtemplate <int max_level, typename T> 605cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstruct hb_auto_trace_t { 606cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod explicit inline hb_auto_trace_t (unsigned int *plevel_, 607cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *what, 608cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 609cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 610208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod const char *message, 611208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod ...) : plevel(plevel_) 612cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod { 6131e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod if (plevel) ++*plevel; 614208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod 615208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_list ap; 616208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_start (ap, message); 6171e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod _hb_debug_msg_va<max_level> (what, obj, func, TRUE, plevel ? *plevel : 0, +1, message, ap); 618208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_end (ap); 619cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod } 6201e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod ~hb_auto_trace_t (void) 6211e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod { 6221e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod _hb_debug_msg<max_level> (NULL, NULL, NULL, TRUE, plevel ? *plevel : 1, -1, " "); 6231e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod 6241e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod if (plevel) --*plevel; 6251e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod } 626cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 627cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod private: 628cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod unsigned int *plevel; 629cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}; 630208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbodtemplate <typename T> /* Optimize when tracing is disabled */ 631208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbodstruct hb_auto_trace_t<0, T> { 632cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod explicit inline hb_auto_trace_t (unsigned int *plevel_, 633cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *what, 634cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 635cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 636208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod const char *message, 637208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod ...) {} 638cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}; 639cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 640cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 641cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/* Misc */ 642cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 643f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 6447b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod/* Pre-mature optimization: 6457b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * Checks for lo <= u <= hi but with an optimization if lo and hi 6467b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * are only different in a contiguous set of lower-most bits. 6477b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod */ 6488f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbodtemplate <typename T> inline bool 6498f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbodhb_in_range (T u, T lo, T hi) 6507b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod{ 6517b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod if ( ((lo^hi) & lo) == 0 && 6527b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod ((lo^hi) & hi) == (lo^hi) && 6537b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod ((lo^hi) & ((lo^hi) + 1)) == 0 ) 6547b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod return (u & ~(lo^hi)) == lo; 6557b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod else 6567b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod return lo <= u && u <= hi; 6577b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod} 6587b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 6597b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 66045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod/* Useful for set-operations on small enums. 66145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * For example, for testing "x ∈ {x1, x2, x3}" use: 66245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) 66345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod */ 6647b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod#define FLAG(x) (1<<(x)) 6657b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 6668f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbod 66745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodtemplate <typename T> inline void 66845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodhb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) 66945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{ 67045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod if (unlikely (!len)) 67145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod return; 67245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 67345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod unsigned int k = len - 1; 67445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod do { 67545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod unsigned int new_k = 0; 67645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 67745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod for (unsigned int j = 0; j < k; j++) 67845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod if (compar (&array[j], &array[j+1]) > 0) { 67945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod T t; 68045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod t = array[j]; 68145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod array[j] = array[j + 1]; 68245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod array[j + 1] = t; 68345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 68445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod new_k = j; 68545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod } 68645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod k = new_k; 68745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod } while (k); 68845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod} 68945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 69045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 69145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 692acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod 693c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */ 694