hb-private.hh revision 6faff8e4132197ba06f0e685b82efe35b546cf64
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/* 22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2007,2008,2009 Red Hat, Inc. 30ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod * Copyright © 2011,2012 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 64a794ebf4be9896393f4badf02905a4007981a588Behdad Esfahbod 65dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod/* Void! */ 665f9569c1391c65d7addff363ad6e5ec9b087f01aBehdad Esfahbodstruct _hb_void_t {}; 67130bb3f6144afe5c88bb5b4e4c98e7cba03b1e26Behdad Esfahbodtypedef const _hb_void_t &hb_void_t; 68130bb3f6144afe5c88bb5b4e4c98e7cba03b1e26Behdad Esfahbod#define HB_VOID (* (const _hb_void_t *) NULL) 69dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 70dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 71c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod/* Basics */ 72c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 73153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 74c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#undef MIN 7525326c2359b0a3e25222b94acd142bc36eff78a4Behdad Esfahbodtemplate <typename Type> 7625326c2359b0a3e25222b94acd142bc36eff78a4Behdad Esfahbodstatic inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } 777586089c6fa8185cad8387869d3703c637e5cbb1Behdad Esfahbod 788a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod#undef MAX 7925326c2359b0a3e25222b94acd142bc36eff78a4Behdad Esfahbodtemplate <typename Type> 8025326c2359b0a3e25222b94acd142bc36eff78a4Behdad Esfahbodstatic inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } 81153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod 8216f175cb2e081e605fe7f9cd01bbe8c24380278aBehdad Esfahbodstatic inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) 8316f175cb2e081e605fe7f9cd01bbe8c24380278aBehdad Esfahbod{ return (a + (b - 1)) / b; } 8416f175cb2e081e605fe7f9cd01bbe8c24380278aBehdad Esfahbod 858a3511ac6c795226699c2b36e03401ecdf88f5f8Behdad Esfahbod 864591753ad4b5ec0224e3f1befdfe4fc5f6075562Behdad Esfahbod#undef ARRAY_LENGTH 8725326c2359b0a3e25222b94acd142bc36eff78a4Behdad Esfahbodtemplate <typename Type, unsigned int n> 880beb66e3a61ae8bb1fa66e54b1ff1abb2f8711e9Behdad Esfahbodstatic inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } 89fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod/* A const version, but does not detect erratically being called on pointers. */ 90fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) 9112360f7c159826ae72271b34486dee59d96aa8caBehdad Esfahbod 9235a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_START do 9335a7383c6138fd705560f0d4bb30659cbd1ab64cBehdad Esfahbod#define HB_STMT_END while (0) 945b3f7702a64fe0513d08a67bdb72704e46fd7cd4Behdad Esfahbod 9573cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 9673cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 9773cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 98303fe62824d4e99df554b6bfaacba05d068522fbBehdad Esfahbod 9979e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod#define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1])) 1004ec30aec3014be6effc09cbbc88dcd075f3826dfBehdad Esfahbod#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) 101dcb7026f33cbcdf60e9b7fcdd44c64cc08702c74Behdad Esfahbod 1024be46bade26faf13f7b4d447e9cc608e183955dcBehdad Esfahbod#define _PASTE1(a,b) a##b 1034be46bade26faf13f7b4d447e9cc608e183955dcBehdad Esfahbod#define PASTE(a,b) _PASTE1(a,b) 104bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 1056fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod/* Lets assert int types. Saves trouble down the road. */ 1066fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 1076fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int8_t) == 1); 1086fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint8_t) == 1); 1096fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int16_t) == 2); 1106fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint16_t) == 2); 1116fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int32_t) == 4); 1126fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint32_t) == 4); 1136fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (int64_t) == 8); 1146fd5364bdc3a2b459175377e9e16c86cff054232Behdad EsfahbodASSERT_STATIC (sizeof (uint64_t) == 8); 1156fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 116b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_codepoint_t) == 4); 117b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_position_t) == 4); 118b13640de6992de9ee9d07e3581c33b8181b70ff9Behdad EsfahbodASSERT_STATIC (sizeof (hb_mask_t) == 4); 119ae9eeaff9300f3bb7bed588c5478e8e5461b3df0Behdad EsfahbodASSERT_STATIC (sizeof (hb_var_int_t) == 4); 1206fd5364bdc3a2b459175377e9e16c86cff054232Behdad Esfahbod 121a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 122a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod/* We like our types POD */ 123a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 12473cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } 12573cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) 12673cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) 127a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 128a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod#ifdef __GNUC__ 12979e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod# define _ASSERT_INSTANCE_POD1(_line, _instance) \ 13079e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod HB_STMT_START { \ 13179e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod typedef __typeof__(_instance) _type_##_line; \ 13279e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod _ASSERT_TYPE_POD1 (_line, _type_##_line); \ 13379e2b4791fe95ede9a1e6b1c71ccc6e36c4fc0e5Behdad Esfahbod } HB_STMT_END 134a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod#else 13573cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested 136a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod#endif 13773cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) 13873cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) 139a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 140a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod/* Check _assertion in a method environment */ 141a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod#define _ASSERT_POD1(_line) \ 14273cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod inline void _static_assertion_on_line_##_line (void) const \ 14373cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } 14473cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) 14573cb02de2dd28b09d4aa76230132248215cfe83dBehdad Esfahbod# define ASSERT_POD() _ASSERT_POD0 (__LINE__) 146a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 147a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 148a00a63b5ef503fafa87e26b517732b2214e01719Behdad Esfahbod 149bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Misc */ 150bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 151c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 152df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 153494d28ad988e505c1c45cc35a7ec6b880cfec5fcBehdad Esfahbod#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) 15464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) 15564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) 156df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 15764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define likely(expr) (expr) 15864d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod#define unlikely(expr) (expr) 159df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 160df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 161df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifndef __GNUC__ 162df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef __attribute__ 163df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define __attribute__(x) 164df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 165df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 166df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if __GNUC__ >= 3 16733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC __attribute__((pure)) 16833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC __attribute__((const)) 169cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) 170df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#else 17133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_PURE_FUNC 17233d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_CONST_FUNC 17344b4f50d05fd51d8056a78e359fbb00fbcac43a0Behdad Esfahbod#define HB_PRINTF_FUNC(format_idx, arg_idx) 174df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 175bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#if __GNUC__ >= 4 17633d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED __attribute__((unused)) 177bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#else 17833d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod#define HB_UNUSED 179bc7830e4259755ce7549025c6f5bf750e78c2ff1Behdad Esfahbod#endif 180df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 181eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#ifndef HB_INTERNAL 182f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# ifndef __MINGW32__ 183f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# define HB_INTERNAL __attribute__((__visibility__("hidden"))) 184f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# else 185f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# define HB_INTERNAL 186f60271c0c2b0101e7b2725f9a9ad950c277a616cBehdad Esfahbod# endif 187eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod#endif 188eee8598d75d3af692d9ececf7e8ac458e892ba9eBehdad Esfahbod 189df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 190df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) 191df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define snprintf _snprintf 192df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 193df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 194df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef _MSC_VER 195df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 196df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline 197df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 198df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 199df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#ifdef __STRICT_ANSI__ 200df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#undef inline 201df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define inline __inline__ 202df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#endif 203df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 204df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 2057d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#if __GNUC__ >= 3 2067d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __PRETTY_FUNCTION__ 2077d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#elif defined(_MSC_VER) 2087d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __FUNCSIG__ 2097d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#else 2107d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#define HB_FUNC __func__ 2117d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod#endif 2127d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 2137d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod 2149b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Return the number of 1 bits in mask. */ 21597e7f8f305c47caf2968a9da3b8407825547286dBehdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 216c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod_hb_popcount32 (uint32_t mask) 217c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod{ 218c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 2199b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return __builtin_popcount (mask); 220c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#else 2219b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod /* "HACKMEM 169" */ 222a949cd329e49d2c0ad6f1e023f324790d886dafeBehdad Esfahbod uint32_t y; 2239b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = (mask >> 1) &033333333333; 2249b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod y = mask - y - ((y >>1) & 033333333333); 2259b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return (((y + (y >> 3)) & 030707070707) % 077); 226c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod#endif 227c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod} 228c7d457aa3ae7138630f52ca7263f663a3ea284c0Behdad Esfahbod 2299b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod/* Returns the number of bits needed to store number */ 2309b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 2319b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod_hb_bit_storage (unsigned int number) 2329b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod{ 2339b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 234f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; 2359b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#else 236a949cd329e49d2c0ad6f1e023f324790d886dafeBehdad Esfahbod unsigned int n_bits = 0; 2379b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod while (number) { 2389b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod n_bits++; 2399b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod number >>= 1; 2409b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod } 2419b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod return n_bits; 2429b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod#endif 2439b6023338530a2dbb8214eb4391ef3e8372f3892Behdad Esfahbod} 244df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod 245f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod/* Returns the number of zero bits in the least significant side of number */ 246f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbodstatic inline HB_CONST_FUNC unsigned int 247f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod_hb_ctz (unsigned int number) 248f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod{ 249f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 250f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return likely (number) ? __builtin_ctz (number) : 0; 251f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#else 252a949cd329e49d2c0ad6f1e023f324790d886dafeBehdad Esfahbod unsigned int n_bits = 0; 253f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod if (unlikely (!number)) return 0; 254f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod while (!(number & 1)) { 255f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod n_bits++; 256f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod number >>= 1; 257f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod } 258f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod return n_bits; 259f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod#endif 260f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod} 261f7acd8df5146155b51d6f50aeb04f54f3030c1c3Behdad Esfahbod 262080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbodstatic inline bool 263080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) 264080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod{ 265080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod return (size > 0) && (count >= ((unsigned int) -1) / size); 266080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod} 267080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 268080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod 2698f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod/* Type of bsearch() / qsort() compare function */ 2708f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbodtypedef int (*hb_compare_func_t) (const void *, const void *); 2718f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 2728f08c3275040870a645ef034a38d30c05c619f21Behdad Esfahbod 273852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 274852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 275852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod/* arrays and maps */ 276852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 277852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 2780e253e97af71e2a7ead153589f61fd579a247502Behdad Esfahbod#define HB_PREALLOCED_ARRAY_INIT {0} 279852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbodtemplate <typename Type, unsigned int StaticSize> 2800e253e97af71e2a7ead153589f61fd579a247502Behdad Esfahbodstruct hb_prealloced_array_t 2810e253e97af71e2a7ead153589f61fd579a247502Behdad Esfahbod{ 282852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int len; 283852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int allocated; 284852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type *array; 285852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod Type static_array[StaticSize]; 286852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 287bf93b636c4963cbc32d5fba7ace1053db6719192Behdad Esfahbod void init (void) { memset (this, 0, sizeof (*this)); } 288efde8113258b117ec0a7fbffe6d681442d045c41Behdad Esfahbod 289265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline Type& operator [] (unsigned int i) { return array[i]; } 290265ac614ea6d26041c7d64739098b76a82bbc4f4Behdad Esfahbod inline const Type& operator [] (unsigned int i) const { return array[i]; } 291852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 292852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline Type *push (void) 293852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 294852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (!array) { 295852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod array = static_array; 296852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod allocated = ARRAY_LENGTH (static_array); 297852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 298852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (likely (len < allocated)) 299852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return &array[len++]; 3005a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 301852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod /* Need to reallocate */ 302852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod unsigned int new_allocated = allocated + (allocated >> 1) + 8; 3035a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod Type *new_array = NULL; 3045a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 305852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod if (array == static_array) { 306852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) calloc (new_allocated, sizeof (Type)); 3075a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (new_array) 308852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod memcpy (new_array, array, len * sizeof (Type)); 309852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } else { 310080a0eb7d82d7195be72c16ece6e0a3ffed636b6Behdad Esfahbod bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); 3115a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (likely (!overflows)) { 312852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); 313852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 314852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 3155a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 3165a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod if (unlikely (!new_array)) 317852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod return NULL; 3185a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod 3195a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod array = new_array; 3205a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod allocated = new_allocated; 3215a5030366e40baa8d96ca67b47a52ad5af143157Behdad Esfahbod return &array[len++]; 322852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 323852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 324852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod inline void pop (void) 325852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 326852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod len--; 327639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod } 328639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod 329639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod inline void remove (unsigned int i) 330639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod { 331639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod if (unlikely (i >= len)) 332639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod return; 333639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod memmove (static_cast<void *> (&array[i]), 334639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod static_cast<void *> (&array[i + 1]), 335639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod (len - i - 1) * sizeof (Type)); 336639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod len--; 337852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 33844b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 33944b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void shrink (unsigned int l) 34044b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 34144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod if (l < len) 34244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod len = l; 34344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 34444b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod 3456843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3466843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *find (T v) { 3476843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 3486843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 3496843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 3506843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 3516843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3526843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3536843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *find (T v) const { 3546843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod for (unsigned int i = 0; i < len; i++) 3556843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod if (array[i] == v) 3566843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return &array[i]; 3576843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return NULL; 3586843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3596843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 36044b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod inline void sort (void) 36144b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod { 36244b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 36344b0a4d2fc62689fc56ef57f412b4bb1e439a614Behdad Esfahbod } 3646843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod 365b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod inline void sort (unsigned int start, unsigned int end) 366b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod { 367b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); 368b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod } 369b70c96dbe41d6512b80fe3d966a1942e1ef64a4bBehdad Esfahbod 3706843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3716843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline Type *bsearch (T *key) 3726843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3736843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3746843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3756843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod template <typename T> 3766843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod inline const Type *bsearch (T *key) const 3776843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod { 3786843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 3796843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod } 3806a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod 3816a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod inline void finish (void) 3826a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod { 3836a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod if (array != static_array) 3846a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod free (array); 3856a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod array = NULL; 3866a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod allocated = len = 0; 3876a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod } 388852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod}; 389852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 390639afdc690c681a302080239a1967ce735ba5be5Behdad Esfahbod#define HB_AUTO_ARRAY_PREALLOCED 16 391c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbodtemplate <typename Type> 392c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbodstruct hb_auto_array_t : hb_prealloced_array_t <Type, HB_AUTO_ARRAY_PREALLOCED> 393c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbod{ 394c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbod hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::init (); } 395c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbod ~hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::finish (); } 396c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbod}; 397c5d91f39802078fc6f6de71940739ec4d04eca07Behdad Esfahbod 398a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 3990e253e97af71e2a7ead153589f61fd579a247502Behdad Esfahbod#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} 40045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodtemplate <typename item_t, typename lock_t> 40145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbodstruct hb_lockable_set_t 402852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod{ 4030e253e97af71e2a7ead153589f61fd579a247502Behdad Esfahbod hb_prealloced_array_t <item_t, 2> items; 404852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 405bf93b636c4963cbc32d5fba7ace1053db6719192Behdad Esfahbod inline void init (void) { items.init (); } 406bf93b636c4963cbc32d5fba7ace1053db6719192Behdad Esfahbod 407478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 40833ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod inline item_t *replace_or_insert (T v, lock_t &l, bool replace) 409852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 41045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 4116843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 41245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 41333ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod if (replace) { 41433ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod item_t old = *item; 41533ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod *item = v; 41633ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod l.unlock (); 41733ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod old.finish (); 41833ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod } 41933ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod else { 42033ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod item = NULL; 42133ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod l.unlock (); 42233ccc77902660ed4b49184e5ec99f4fd0ef63175Behdad Esfahbod } 42345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 424852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod item = items.push (); 42545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (likely (item)) 42645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = v; 42745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 42845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 429b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 430852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 431852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 432811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod template <typename T> 43345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void remove (T v, lock_t &l) 434852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 43545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 4366843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod item_t *item = items.find (v); 43745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) { 43845bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = *item; 43945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *item = items[items.len - 1]; 44045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 44145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 44245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 44345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } else { 44445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 44545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 446852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod } 447a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 448478a42536ff7ab777a7774fbfdb9c5e51334a14eBehdad Esfahbod template <typename T> 44945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline bool find (T v, item_t *i, lock_t &l) 450852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod { 45145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 45245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 45345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod if (item) 45445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod *i = *item; 45545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 45645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod return !!item; 457811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod } 458811482bd650fb5652a9835471ae8ecf0fb185611Behdad Esfahbod 459b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod template <typename T> 46045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline item_t *find_or_insert (T v, lock_t &l) 46145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 46245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 46345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t *item = items.find (v); 464b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (!item) { 465b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod item = items.push (); 466b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod if (likely (item)) 467b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod *item = v; 468b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 46945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 470b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod return item; 471b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod } 472b45f32ee4e599c515ce93e44315283d236b073bbBehdad Esfahbod 47345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod inline void finish (lock_t &l) 47445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod { 4753f4764bb56bb7e42ba8859f1905810bd2f998838Behdad Esfahbod if (!items.len) { 4763f4764bb56bb7e42ba8859f1905810bd2f998838Behdad Esfahbod /* No need for locking. */ 4773f4764bb56bb7e42ba8859f1905810bd2f998838Behdad Esfahbod items.finish (); 4783f4764bb56bb7e42ba8859f1905810bd2f998838Behdad Esfahbod return; 4793f4764bb56bb7e42ba8859f1905810bd2f998838Behdad Esfahbod } 48045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 48145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod while (items.len) { 48245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod item_t old = items[items.len - 1]; 48345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod items.pop (); 48445bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 48545bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod old.finish (); 48645bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.lock (); 48745bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 4886a7ac79e26e85f6781186cf708a12825c0857324Behdad Esfahbod items.finish (); 48945bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod l.unlock (); 49045bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod } 49145bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 49245bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod}; 49345bfa99034512e886d75b1d45a5a649647f4711fBehdad Esfahbod 494852e08ec8fbfbce1d50e571d0bb0b52ef4d4cc58Behdad Esfahbod 495a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 496a9f24c802956d57180d71b83e96a0fb81197df4aBehdad Esfahbod 497f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod/* Big-endian handling */ 498f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 499153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline uint16_t hb_be_uint16 (const uint16_t v) 500153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ 501153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod const uint8_t *V = (const uint8_t *) &v; 502fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod return (V[0] << 8) | V[1]; 503fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod} 504fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod 505fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbodstatic inline uint16_t hb_uint16_swap (const uint16_t v) 506fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod{ 507fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod return (v >> 8) | (v << 8); 508fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod} 509fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod 510fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbodstatic inline uint32_t hb_uint32_swap (const uint32_t v) 511fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod{ 512fcd6f5326166e993b8f5222efbaffe916da98f0aBehdad Esfahbod return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); 513153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod} 5147a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 515e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod/* Note, of the following macros, uint16_get is the one called many many times. 516e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * If there is any optimizations to be done, it's in that macro. However, I 517e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which 518e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * results in a single ror instruction, does NOT speed this up. In fact, it 519e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * resulted in a minor slowdown. At any rate, note that v may not be correctly 520e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod * aligned, so I think the current implementation is optimal. 521e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod */ 522e74616b8898b5f18d9bf82b9e81aefad056c1e36Behdad Esfahbod 523e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod#define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END 5247a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) 525153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) 5267a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod 527e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad 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 5287a52f281178867379adb6e6c6fb0022102f75d17Behdad Esfahbod#define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) 529153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod#define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) 530f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 5312b712bba3f7c459653737dfa2f7d26f17f3b9f64Behdad Esfahbod#define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END 532bd61bc13ea8ff350ada5449b2cfeb612e66ecafaBehdad Esfahbod#define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2]) 533bd61bc13ea8ff350ada5449b2cfeb612e66ecafaBehdad Esfahbod#define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) 534bd61bc13ea8ff350ada5449b2cfeb612e66ecafaBehdad Esfahbod 535f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 5364188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod/* ASCII tag/character handling */ 537db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 53820b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodstatic inline bool ISALPHA (unsigned char c) 539153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 54020b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodstatic inline bool ISALNUM (unsigned char c) 541153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } 54220b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodstatic inline bool ISSPACE (unsigned char c) 54320b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbod{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } 544153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOUPPER (unsigned char c) 545153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } 546153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbodstatic inline unsigned char TOLOWER (unsigned char c) 547153142dac8dd9abaf164bb88af07c600c17fc3a1Behdad Esfahbod{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } 548db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 5494188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ 5504188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[1], \ 5514188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[2], \ 5524188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod ((const char *) s)[3])) 5534188096a7722f09ffa9319986c0286071da10a27Behdad Esfahbod 554db5227c40e5c35fe2ffb750f32b639cb44424a1dBehdad Esfahbod 555831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* C++ helpers */ 556831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 557831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod/* Makes class uncopyable. Use in private: section. */ 558831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod#define NO_COPY(T) \ 559831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod T (const T &o); \ 560970e092dc23cbeb7897d4c7bb58c042209f518fbBehdad Esfahbod T &operator = (const T &o) 561831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 562831886a9b4073cfe27f7e1db0e957cbd5913fd31Behdad Esfahbod 563bc200457430c083914a64bf4b056153506749610Behdad Esfahbod/* Debug */ 564bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 565cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 566bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#ifndef HB_DEBUG 567bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define HB_DEBUG 0 568bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#endif 569bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 570cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstatic inline bool 571cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug (unsigned int level, 572cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod unsigned int max_level) 573cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{ 574cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod return level < max_level; 575cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod} 576cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 5776c15ddfe2b45383824b64058ae69939d002183a6Behdad Esfahbod#define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) 5786c15ddfe2b45383824b64058ae69939d002183a6Behdad Esfahbod#define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0)) 57943ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2Behdad Esfahbod 5802d1dcb3ce3354c2d5e2b4d1d291beb8d23cdea22Behdad Esfahbodtemplate <int max_level> static inline void 581208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod_hb_debug_msg_va (const char *what, 5821e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const void *obj, 5831e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *func, 5841e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod bool indented, 5851e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 5861e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 5871e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod const char *message, 5881e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod va_list ap) 589208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod{ 5906eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (!_hb_debug (level, max_level)) 591829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod return; 5926eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5936eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "%-10s", what ? what : ""); 5946eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 5956eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (obj) 5965ccfe8e2154ad0b58dabcc236bbe9478c17b02abBehdad Esfahbod fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj); 5976eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod else 5986eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); 5996eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 600d7bba01a353efc7432c474dd8755a02db4abd2aeBehdad Esfahbod if (indented) { 6016932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod/* One may want to add ASCII version of these. See: 6026932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */ 6036932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */ 6046932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ 6056932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */ 6066932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */ 6076932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */ 6086932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod static const char bars[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR; 6097235f33f9e5e031622a00a84f4b2e98f16803579Behdad Esfahbod fprintf (stderr, "%2u %s" VRBAR "%s", 6106eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod level, 6116932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level), 6126932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR); 613d7bba01a353efc7432c474dd8755a02db4abd2aeBehdad Esfahbod } else 6146932a41fb61ffc2901c260587b1e98ed9c2a7ea1Behdad Esfahbod fprintf (stderr, " " VRBAR LBAR); 6156eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 616adf7758a27a11fb1a8a14a2673867589437d22a3Behdad Esfahbod if (func) 617adf7758a27a11fb1a8a14a2673867589437d22a3Behdad Esfahbod { 61829ff25481584e20a2fa39195f295c8eb4bf3bad0Behdad Esfahbod unsigned int func_len = strlen (func); 61929ff25481584e20a2fa39195f295c8eb4bf3bad0Behdad Esfahbod#ifndef HB_DEBUG_VERBOSE 620adf7758a27a11fb1a8a14a2673867589437d22a3Behdad Esfahbod /* Skip "typename" */ 621adf7758a27a11fb1a8a14a2673867589437d22a3Behdad Esfahbod if (0 == strncmp (func, "typename ", 9)) 622adf7758a27a11fb1a8a14a2673867589437d22a3Behdad Esfahbod func += 9; 623fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod /* Skip return type */ 624d7bba01a353efc7432c474dd8755a02db4abd2aeBehdad Esfahbod const char *space = strchr (func, ' '); 625fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod if (space) 626d7bba01a353efc7432c474dd8755a02db4abd2aeBehdad Esfahbod func = space + 1; 627fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod /* Skip parameter list */ 628fabd3113a98c5f4114f48920fa7ea38bd65a8d32Behdad Esfahbod const char *paren = strchr (func, '('); 62929ff25481584e20a2fa39195f295c8eb4bf3bad0Behdad Esfahbod if (paren) 63029ff25481584e20a2fa39195f295c8eb4bf3bad0Behdad Esfahbod func_len = paren - func; 63129ff25481584e20a2fa39195f295c8eb4bf3bad0Behdad Esfahbod#endif 632d7bba01a353efc7432c474dd8755a02db4abd2aeBehdad Esfahbod fprintf (stderr, "%.*s: ", func_len, func); 63385f73fa8da1fbb864aef0f3a592b1d65e24d593dBehdad Esfahbod } 6346eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 6356eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod if (message) 6366eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod vfprintf (stderr, message, ap); 6376eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod 6386eec6f406d2cc13dbca422e88492d3d498af02bfBehdad Esfahbod fprintf (stderr, "\n"); 639208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod} 640829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 64193345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod_hb_debug_msg_va<0> (const char *what HB_UNUSED, 64293345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const void *obj HB_UNUSED, 64393345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *func HB_UNUSED, 64493345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod bool indented HB_UNUSED, 64593345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod unsigned int level HB_UNUSED, 64693345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod int level_dir HB_UNUSED, 64793345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *message HB_UNUSED, 64893345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod va_list ap HB_UNUSED) {} 649208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod 6502d1dcb3ce3354c2d5e2b4d1d291beb8d23cdea22Behdad Esfahbodtemplate <int max_level> static inline void 651cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what, 652cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 653cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 654cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 6551e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 6561e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 657cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 6581e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod ...) HB_PRINTF_FUNC(7, 8); 6592d1dcb3ce3354c2d5e2b4d1d291beb8d23cdea22Behdad Esfahbodtemplate <int max_level> static inline void 660cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod_hb_debug_msg (const char *what, 661cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const void *obj, 662cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 663cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod bool indented, 6641e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod unsigned int level, 6651e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod int level_dir, 666cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *message, 667cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod ...) 668cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod{ 669cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_list ap; 670cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_start (ap, message); 671829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap); 672cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod va_end (ap); 673cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod} 674829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 67593345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod_hb_debug_msg<0> (const char *what HB_UNUSED, 67693345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const void *obj HB_UNUSED, 67793345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *func HB_UNUSED, 67893345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod bool indented HB_UNUSED, 67993345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod unsigned int level HB_UNUSED, 68093345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod int level_dir HB_UNUSED, 68193345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *message HB_UNUSED, 6821e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod ...) HB_PRINTF_FUNC(7, 8); 683829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbodtemplate <> inline void 68493345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod_hb_debug_msg<0> (const char *what HB_UNUSED, 68593345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const void *obj HB_UNUSED, 68693345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *func HB_UNUSED, 68793345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod bool indented HB_UNUSED, 68893345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod unsigned int level HB_UNUSED, 68993345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod int level_dir HB_UNUSED, 69093345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *message HB_UNUSED, 691829e814ff358c5e700ba4df54932696801aa9f65Behdad Esfahbod ...) {} 692bc200457430c083914a64bf4b056153506749610Behdad Esfahbod 6930594a2448440208efa0acac9a5d8d52d43108289Behdad 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__) 6940594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__) 6950594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__) 696cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 697cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 698cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/* 699dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod * Printer 700dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod */ 701dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 702dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbodtemplate <typename T> 703dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbodstruct hb_printer_t {}; 704dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 705dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbodtemplate <> 706dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbodstruct hb_printer_t<bool> { 707dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod const char *print (bool v) { return v ? "true" : "false"; } 708dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod}; 709dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 710dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbodtemplate <> 711130bb3f6144afe5c88bb5b4e4c98e7cba03b1e26Behdad Esfahbodstruct hb_printer_t<hb_void_t> { 7120beb66e3a61ae8bb1fa66e54b1ff1abb2f8711e9Behdad Esfahbod const char *print (hb_void_t) { return ""; } 713dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod}; 714dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 715dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod 716dabe698fcbeb02911128b17aa8e3b2d864795960Behdad Esfahbod/* 717cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod * Trace 718cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod */ 719cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 720902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodtemplate <typename T> 721902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodstatic inline void _hb_warn_no_return (bool returned) 722902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod{ 723902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod if (unlikely (!returned)) { 724902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n"); 725902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod } 726902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod} 727902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodtemplate <> 7286faff8e4132197ba06f0e685b82efe35b546cf64Behdad Esfahbod/*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED) 729902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod{} 730902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod 731902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodtemplate <int max_level, typename ret_t> 732cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbodstruct hb_auto_trace_t { 733cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod explicit inline hb_auto_trace_t (unsigned int *plevel_, 7349659523ca32b0e254d0e5fe387d817208d9cb6bfBehdad Esfahbod const char *what_, 7359659523ca32b0e254d0e5fe387d817208d9cb6bfBehdad Esfahbod const void *obj_, 736cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod const char *func, 737208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod const char *message, 7389659523ca32b0e254d0e5fe387d817208d9cb6bfBehdad Esfahbod ...) : plevel (plevel_), what (what_), obj (obj_), returned (false) 739cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod { 7401e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod if (plevel) ++*plevel; 741208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod 742208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_list ap; 743208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_start (ap, message); 7440594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap); 745208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod va_end (ap); 746cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod } 7470ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod inline ~hb_auto_trace_t (void) 7481e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod { 749902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod _hb_warn_no_return<ret_t> (returned); 750902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod if (!returned) { 7510594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " "); 7520ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod } 7531e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod if (plevel) --*plevel; 7541e08830b4fac3a60ae52349cab6e101d389d30cdBehdad Esfahbod } 755cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 7562c9d6485a1f89c11f84e720d3c7978dc11a5039aBehdad Esfahbod inline ret_t ret (ret_t v, unsigned int line = 0) 7570ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod { 7580ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod if (unlikely (returned)) { 7590ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n"); 7600ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod return v; 7610ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod } 7620ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod 763902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, 764902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod "return %s (line %d)", 765902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbod hb_printer_t<ret_t>().print (v), line); 7660ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod if (plevel) --*plevel; 7670ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod plevel = NULL; 7680ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod returned = true; 7690ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod return v; 7700ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod } 7710ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod 772cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod private: 773cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod unsigned int *plevel; 7749659523ca32b0e254d0e5fe387d817208d9cb6bfBehdad Esfahbod const char *what; 7759659523ca32b0e254d0e5fe387d817208d9cb6bfBehdad Esfahbod const void *obj; 776c779d82b2fc801eec0d349a106c0e860448fcf4eBehdad Esfahbod bool returned; 777cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}; 778902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodtemplate <typename ret_t> /* Optimize when tracing is disabled */ 779902cc8aca0b3ff25eeee50b3a84d729e31731ef3Behdad Esfahbodstruct hb_auto_trace_t<0, ret_t> { 78093345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED, 78193345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *what HB_UNUSED, 78293345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const void *obj HB_UNUSED, 78393345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *func HB_UNUSED, 78493345edcbea49bdf0e22f26b5b74a23e601dfab4Behdad Esfahbod const char *message HB_UNUSED, 785208109703c929428c684ddcf9310b8ba780c4c31Behdad Esfahbod ...) {} 7860ab8c8621712d33e1e91dfdb4ad0b335e3d2a3fbBehdad Esfahbod 7870beb66e3a61ae8bb1fa66e54b1ff1abb2f8711e9Behdad Esfahbod inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; } 788cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod}; 789cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 790ae63cf206291befe3920adfe015e6cd0961580e5Behdad Esfahbod#define TRACE_RETURN(RET) trace.ret (RET, __LINE__) 791cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 792cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod/* Misc */ 793cc06c243d8be3ebb1190281653d2dba504c16c0fBehdad Esfahbod 794f60f2166c48d07f556ff83f04e95181946eb03dfBehdad Esfahbod 7957b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod/* Pre-mature optimization: 7967b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * Checks for lo <= u <= hi but with an optimization if lo and hi 7977b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod * are only different in a contiguous set of lower-most bits. 7987b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod */ 799b5aeb95afeb13a66177caada9f5d5ad4cddbd35fBehdad Esfahbodtemplate <typename T> static inline bool 8008f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbodhb_in_range (T u, T lo, T hi) 8017b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod{ 8027b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod if ( ((lo^hi) & lo) == 0 && 8037b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod ((lo^hi) & hi) == (lo^hi) && 8047b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod ((lo^hi) & ((lo^hi) + 1)) == 0 ) 8057b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod return (u & ~(lo^hi)) == lo; 8067b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod else 8077b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod return lo <= u && u <= hi; 8087b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod} 8097b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 8104a7f4f3e56f8f7640ae7337aa1b3324f31e0d4abBehdad Esfahbodtemplate <typename T> static inline bool 811c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbodhb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) 812c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbod{ 813c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbod return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); 814c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbod} 815c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbod 816c98b7183f7dc453d5bac1f2503017cded317a495Behdad Esfahbodtemplate <typename T> static inline bool 817093cd583263a5d427e3377b31585043fb55d2557Behdad Esfahbodhb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) 8184a7f4f3e56f8f7640ae7337aa1b3324f31e0d4abBehdad Esfahbod{ 819093cd583263a5d427e3377b31585043fb55d2557Behdad Esfahbod return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); 8204a7f4f3e56f8f7640ae7337aa1b3324f31e0d4abBehdad Esfahbod} 8214a7f4f3e56f8f7640ae7337aa1b3324f31e0d4abBehdad Esfahbod 8227b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 82345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod/* Useful for set-operations on small enums. 82445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * For example, for testing "x ∈ {x1, x2, x3}" use: 82545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) 82645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod */ 827efb4ad735691837a52447bedc1a66a87d0d9af51Behdad Esfahbod#define FLAG(x) (1<<(x)) 8282c372b80f6befad69e216e3f218b38640b8cc044Behdad Esfahbod#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) 8297b08b0a7f2057937dfc3ab2ec191656bf2386463Behdad Esfahbod 8308f0b64fb6988f9502d2c5e39768a9af133d9a83fBehdad Esfahbod 83139b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbodtemplate <typename T, typename T2> inline void 83239b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbodhb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) 83345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{ 83445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod if (unlikely (!len)) 83545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod return; 83645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 83745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod unsigned int k = len - 1; 83845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod do { 83945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod unsigned int new_k = 0; 84045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 84145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod for (unsigned int j = 0; j < k; j++) 84239b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod if (compar (&array[j], &array[j+1]) > 0) 84339b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod { 84439b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod { 84539b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod T t; 84639b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod t = array[j]; 84739b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod array[j] = array[j + 1]; 84839b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod array[j + 1] = t; 84939b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod } 85039b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod if (array2) 85139b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod { 85239b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod T2 t; 85339b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod t = array2[j]; 85439b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod array2[j] = array2[j + 1]; 85539b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod array2[j + 1] = t; 85639b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod } 85745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 85845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod new_k = j; 85945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod } 86045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod k = new_k; 86145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod } while (k); 86245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod} 86345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 86439b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbodtemplate <typename T> inline void 86539b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbodhb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) 86639b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod{ 86739b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod hb_bubble_sort (array, len, compar, (int *) NULL); 86839b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod} 86945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 8706f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbodstatic inline hb_bool_t 8716f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbodhb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) 8726f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod{ 8736f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod /* Pain because we don't know whether s is nul-terminated. */ 8746f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod char buf[64]; 875847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod len = MIN (ARRAY_LENGTH (buf) - 1, len); 876847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod strncpy (buf, s, len); 877847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod buf[len] = '\0'; 8786f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod 8796f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod char *end; 8806f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod errno = 0; 8816f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod unsigned long v = strtoul (buf, &end, base); 8826f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod if (errno) return false; 8836f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod if (*end) return false; 8846f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod *out = v; 8856f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod return true; 8866f3a300138f659020c21c3e08b7981c78df5f332Behdad Esfahbod} 88745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod 888acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod 889bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod/* Global runtime options. */ 890bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 891bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbodstruct hb_options_t 892bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod{ 893bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod int initialized : 1; 894bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod int uniscribe_bug_compatible : 1; 895bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod}; 896bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 897bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbodunion hb_options_union_t { 898bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod int i; 899bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod hb_options_t opts; 900bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod}; 901bab02d339f39ed5168daaef9461227f78e596a2fBehdad EsfahbodASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t)); 902bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 903bab02d339f39ed5168daaef9461227f78e596a2fBehdad EsfahbodHB_INTERNAL void 904bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod_hb_options_init (void); 905bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 906bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbodextern HB_INTERNAL hb_options_union_t _hb_options; 907bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 908bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbodstatic inline hb_options_t 909bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbodhb_options (void) 910bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod{ 911bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod if (unlikely (!_hb_options.i)) 912bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod _hb_options_init (); 913bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 914bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod return _hb_options.opts; 915bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod} 916bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 917bab02d339f39ed5168daaef9461227f78e596a2fBehdad Esfahbod 918c57d454accff66e5f2c58006e8fb40bc020b6182Behdad Esfahbod#endif /* HB_PRIVATE_HH */ 919