0cb049e8d053d5cd32552e45eb7c01dae26f7ed5
[framework/uifw/harfbuzz.git] / src / hb-private.hh
1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2011,2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_PRIVATE_HH
30 #define HB_PRIVATE_HH
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "hb.h"
37 #define HB_H_IN
38 #ifdef HAVE_OT
39 #include "hb-ot.h"
40 #define HB_OT_H_IN
41 #endif
42
43 #include <stdlib.h>
44 #include <stddef.h>
45 #include <string.h>
46 #include <assert.h>
47
48 /* We only use these two for debug output.  However, the debug code is
49  * always seen by the compiler (and optimized out in non-debug builds.
50  * If including these becomes a problem, we can start thinking about
51  * someway around that. */
52 #include <stdio.h>
53 #include <errno.h>
54 #include <stdarg.h>
55
56
57
58 /* Essentials */
59
60 #ifndef NULL
61 # define NULL ((void *) 0)
62 #endif
63
64
65 /* Basics */
66
67
68 #undef MIN
69 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
70
71 #undef MAX
72 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
73
74
75 #undef  ARRAY_LENGTH
76 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
77
78 #define HB_STMT_START do
79 #define HB_STMT_END   while (0)
80
81 #define _ASSERT_STATIC1(_line, _cond)   typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
82 #define _ASSERT_STATIC0(_line, _cond)   _ASSERT_STATIC1 (_line, (_cond))
83 #define ASSERT_STATIC(_cond)            _ASSERT_STATIC0 (__LINE__, (_cond))
84
85 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
86 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
87
88 #define _PASTE1(a,b) a##b
89 #define PASTE(a,b) _PASTE1(a,b)
90
91 /* Lets assert int types.  Saves trouble down the road. */
92
93 ASSERT_STATIC (sizeof (int8_t) == 1);
94 ASSERT_STATIC (sizeof (uint8_t) == 1);
95 ASSERT_STATIC (sizeof (int16_t) == 2);
96 ASSERT_STATIC (sizeof (uint16_t) == 2);
97 ASSERT_STATIC (sizeof (int32_t) == 4);
98 ASSERT_STATIC (sizeof (uint32_t) == 4);
99 ASSERT_STATIC (sizeof (int64_t) == 8);
100 ASSERT_STATIC (sizeof (uint64_t) == 8);
101
102 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
103 ASSERT_STATIC (sizeof (hb_position_t) == 4);
104 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
105 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
106
107
108 /* We like our types POD */
109
110 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
111 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
112 #define ASSERT_TYPE_POD(_type)          _ASSERT_TYPE_POD0 (__LINE__, _type)
113
114 #ifdef __GNUC__
115 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
116         HB_STMT_START { \
117                 typedef __typeof__(_instance) _type_##_line; \
118                 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
119         } HB_STMT_END
120 #else
121 # define _ASSERT_INSTANCE_POD1(_line, _instance)        typedef int _assertion_on_line_##_line##_not_tested
122 #endif
123 # define _ASSERT_INSTANCE_POD0(_line, _instance)        _ASSERT_INSTANCE_POD1 (_line, _instance)
124 # define ASSERT_INSTANCE_POD(_instance)                 _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
125
126 /* Check _assertion in a method environment */
127 #define _ASSERT_POD1(_line) \
128         inline void _static_assertion_on_line_##_line (void) const \
129         { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
130 # define _ASSERT_POD0(_line)    _ASSERT_POD1 (_line)
131 # define ASSERT_POD()           _ASSERT_POD0 (__LINE__)
132
133
134
135 /* Misc */
136
137
138 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
139 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
140 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
141 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
142 #else
143 #define likely(expr) (expr)
144 #define unlikely(expr) (expr)
145 #endif
146
147 #ifndef __GNUC__
148 #undef __attribute__
149 #define __attribute__(x)
150 #endif
151
152 #if __GNUC__ >= 3
153 #define HB_PURE_FUNC    __attribute__((pure))
154 #define HB_CONST_FUNC   __attribute__((const))
155 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
156 #else
157 #define HB_PURE_FUNC
158 #define HB_CONST_FUNC
159 #define HB_PRINTF_FUNC(format_idx, arg_idx)
160 #endif
161 #if __GNUC__ >= 4
162 #define HB_UNUSED       __attribute__((unused))
163 #else
164 #define HB_UNUSED
165 #endif
166
167 #ifndef HB_INTERNAL
168 # ifndef __MINGW32__
169 #  define HB_INTERNAL __attribute__((__visibility__("hidden")))
170 # else
171 #  define HB_INTERNAL
172 # endif
173 #endif
174
175
176 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
177 #define snprintf _snprintf
178 #endif
179
180 #ifdef _MSC_VER
181 #undef inline
182 #define inline __inline
183 #endif
184
185 #ifdef __STRICT_ANSI__
186 #undef inline
187 #define inline __inline__
188 #endif
189
190
191 #if __GNUC__ >= 3
192 #define HB_FUNC __PRETTY_FUNCTION__
193 #elif defined(_MSC_VER)
194 #define HB_FUNC __FUNCSIG__
195 #else
196 #define HB_FUNC __func__
197 #endif
198
199
200 /* Return the number of 1 bits in mask. */
201 static inline HB_CONST_FUNC unsigned int
202 _hb_popcount32 (uint32_t mask)
203 {
204 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
205   return __builtin_popcount (mask);
206 #else
207   /* "HACKMEM 169" */
208   register uint32_t y;
209   y = (mask >> 1) &033333333333;
210   y = mask - y - ((y >>1) & 033333333333);
211   return (((y + (y >> 3)) & 030707070707) % 077);
212 #endif
213 }
214
215 /* Returns the number of bits needed to store number */
216 static inline HB_CONST_FUNC unsigned int
217 _hb_bit_storage (unsigned int number)
218 {
219 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
220   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
221 #else
222   register unsigned int n_bits = 0;
223   while (number) {
224     n_bits++;
225     number >>= 1;
226   }
227   return n_bits;
228 #endif
229 }
230
231 /* Returns the number of zero bits in the least significant side of number */
232 static inline HB_CONST_FUNC unsigned int
233 _hb_ctz (unsigned int number)
234 {
235 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
236   return likely (number) ? __builtin_ctz (number) : 0;
237 #else
238   register unsigned int n_bits = 0;
239   if (unlikely (!number)) return 0;
240   while (!(number & 1)) {
241     n_bits++;
242     number >>= 1;
243   }
244   return n_bits;
245 #endif
246 }
247
248 static inline bool
249 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
250 {
251   return (size > 0) && (count >= ((unsigned int) -1) / size);
252 }
253
254
255 /* Type of bsearch() / qsort() compare function */
256 typedef int (*hb_compare_func_t) (const void *, const void *);
257
258
259
260
261 /* arrays and maps */
262
263
264 #define HB_PREALLOCED_ARRAY_INIT {0}
265 template <typename Type, unsigned int StaticSize>
266 struct hb_prealloced_array_t
267 {
268   unsigned int len;
269   unsigned int allocated;
270   Type *array;
271   Type static_array[StaticSize];
272
273   void init (void) { memset (this, 0, sizeof (*this)); }
274
275   inline Type& operator [] (unsigned int i) { return array[i]; }
276   inline const Type& operator [] (unsigned int i) const { return array[i]; }
277
278   inline Type *push (void)
279   {
280     if (!array) {
281       array = static_array;
282       allocated = ARRAY_LENGTH (static_array);
283     }
284     if (likely (len < allocated))
285       return &array[len++];
286
287     /* Need to reallocate */
288     unsigned int new_allocated = allocated + (allocated >> 1) + 8;
289     Type *new_array = NULL;
290
291     if (array == static_array) {
292       new_array = (Type *) calloc (new_allocated, sizeof (Type));
293       if (new_array)
294         memcpy (new_array, array, len * sizeof (Type));
295     } else {
296       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
297       if (likely (!overflows)) {
298         new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
299       }
300     }
301
302     if (unlikely (!new_array))
303       return NULL;
304
305     array = new_array;
306     allocated = new_allocated;
307     return &array[len++];
308   }
309
310   inline void pop (void)
311   {
312     len--;
313     /* TODO: shrink array if needed */
314   }
315
316   inline void shrink (unsigned int l)
317   {
318      if (l < len)
319        len = l;
320     /* TODO: shrink array if needed */
321   }
322
323   template <typename T>
324   inline Type *find (T v) {
325     for (unsigned int i = 0; i < len; i++)
326       if (array[i] == v)
327         return &array[i];
328     return NULL;
329   }
330   template <typename T>
331   inline const Type *find (T v) const {
332     for (unsigned int i = 0; i < len; i++)
333       if (array[i] == v)
334         return &array[i];
335     return NULL;
336   }
337
338   inline void sort (void)
339   {
340     qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
341   }
342
343   inline void sort (unsigned int start, unsigned int end)
344   {
345     qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
346   }
347
348   template <typename T>
349   inline Type *bsearch (T *key)
350   {
351     return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
352   }
353   template <typename T>
354   inline const Type *bsearch (T *key) const
355   {
356     return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
357   }
358
359   inline void finish (void)
360   {
361     if (array != static_array)
362       free (array);
363     array = NULL;
364     allocated = len = 0;
365   }
366 };
367
368
369 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
370 template <typename item_t, typename lock_t>
371 struct hb_lockable_set_t
372 {
373   hb_prealloced_array_t <item_t, 2> items;
374
375   inline void init (void) { items.init (); }
376
377   template <typename T>
378   inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
379   {
380     l.lock ();
381     item_t *item = items.find (v);
382     if (item) {
383       if (replace) {
384         item_t old = *item;
385         *item = v;
386         l.unlock ();
387         old.finish ();
388       }
389       else {
390         item = NULL;
391         l.unlock ();
392       }
393     } else {
394       item = items.push ();
395       if (likely (item))
396         *item = v;
397       l.unlock ();
398     }
399     return item;
400   }
401
402   template <typename T>
403   inline void remove (T v, lock_t &l)
404   {
405     l.lock ();
406     item_t *item = items.find (v);
407     if (item) {
408       item_t old = *item;
409       *item = items[items.len - 1];
410       items.pop ();
411       l.unlock ();
412       old.finish ();
413     } else {
414       l.unlock ();
415     }
416   }
417
418   template <typename T>
419   inline bool find (T v, item_t *i, lock_t &l)
420   {
421     l.lock ();
422     item_t *item = items.find (v);
423     if (item)
424       *i = *item;
425     l.unlock ();
426     return !!item;
427   }
428
429   template <typename T>
430   inline item_t *find_or_insert (T v, lock_t &l)
431   {
432     l.lock ();
433     item_t *item = items.find (v);
434     if (!item) {
435       item = items.push ();
436       if (likely (item))
437         *item = v;
438     }
439     l.unlock ();
440     return item;
441   }
442
443   inline void finish (lock_t &l)
444   {
445     l.lock ();
446     while (items.len) {
447       item_t old = items[items.len - 1];
448         items.pop ();
449         l.unlock ();
450         old.finish ();
451         l.lock ();
452     }
453     items.finish ();
454     l.unlock ();
455   }
456
457 };
458
459
460
461
462 /* Big-endian handling */
463
464 static inline uint16_t hb_be_uint16 (const uint16_t v)
465 {
466   const uint8_t *V = (const uint8_t *) &v;
467   return (uint16_t) (V[0] << 8) + V[1];
468 }
469
470 /* Note, of the following macros, uint16_get is the one called many many times.
471  * If there is any optimizations to be done, it's in that macro.  However, I
472  * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
473  * results in a single ror instruction, does NOT speed this up.  In fact, it
474  * resulted in a minor slowdown.  At any rate, note that v may not be correctly
475  * aligned, so I think the current implementation is optimal.
476  */
477
478 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
479 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
480 #define hb_be_uint16_eq(a,b)    (a[0] == b[0] && a[1] == b[1])
481
482 #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
483 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
484 #define hb_be_uint32_eq(a,b)    (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
485
486
487 /* ASCII tag/character handling */
488
489 static inline unsigned char ISALPHA (unsigned char c)
490 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
491 static inline unsigned char ISALNUM (unsigned char c)
492 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
493 static inline unsigned char TOUPPER (unsigned char c)
494 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
495 static inline unsigned char TOLOWER (unsigned char c)
496 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
497
498 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
499                                   ((const char *) s)[1], \
500                                   ((const char *) s)[2], \
501                                   ((const char *) s)[3]))
502
503
504 /* C++ helpers */
505
506 /* Makes class uncopyable.  Use in private: section. */
507 #define NO_COPY(T) \
508   T (const T &o); \
509   T &operator = (const T &o)
510
511
512 /* Debug */
513
514
515 #ifndef HB_DEBUG
516 #define HB_DEBUG 0
517 #endif
518
519 static inline bool
520 _hb_debug (unsigned int level,
521            unsigned int max_level)
522 {
523   return level < max_level;
524 }
525
526 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
527 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
528
529 template <int max_level> inline void
530 _hb_debug_msg_va (const char *what,
531                   const void *obj,
532                   const char *func,
533                   bool indented,
534                   unsigned int level,
535                   int level_dir,
536                   const char *message,
537                   va_list ap)
538 {
539   if (!_hb_debug (level, max_level))
540     return;
541
542   fprintf (stderr, "%-10s", what ? what : "");
543
544   if (obj)
545     fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
546   else
547     fprintf (stderr, " %*s  ", (unsigned int) (2 * sizeof (void *)), "");
548
549   if (indented) {
550     static const char bars[] = "││││││││││││││││││││││││││││││││││││││││";
551     fprintf (stderr, "%2d %s├%s",
552              level,
553              bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), 3 * level),
554              level_dir ? (level_dir > 0 ? "╮" : "╯") : "╴");
555   } else
556     fprintf (stderr, "   ├╴");
557
558   if (func) {
559     /* If there's a class name, just write that. */
560     const char *dotdot = strstr (func, "::");
561     const char *space = strchr (func, ' ');
562     if (space && dotdot && space < dotdot)
563       func = space + 1;
564     unsigned int func_len = dotdot ? dotdot - func : strlen (func);
565     fprintf (stderr, "%.*s: ", func_len, func);
566   }
567
568   if (message)
569     vfprintf (stderr, message, ap);
570
571   fprintf (stderr, "\n");
572 }
573 template <> inline void
574 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
575                      const void *obj HB_UNUSED,
576                      const char *func HB_UNUSED,
577                      bool indented HB_UNUSED,
578                      unsigned int level HB_UNUSED,
579                      int level_dir HB_UNUSED,
580                      const char *message HB_UNUSED,
581                      va_list ap HB_UNUSED) {}
582
583 template <int max_level> inline void
584 _hb_debug_msg (const char *what,
585                const void *obj,
586                const char *func,
587                bool indented,
588                unsigned int level,
589                int level_dir,
590                const char *message,
591                ...) HB_PRINTF_FUNC(7, 8);
592 template <int max_level> inline void
593 _hb_debug_msg (const char *what,
594                const void *obj,
595                const char *func,
596                bool indented,
597                unsigned int level,
598                int level_dir,
599                const char *message,
600                ...)
601 {
602   va_list ap;
603   va_start (ap, message);
604   _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
605   va_end (ap);
606 }
607 template <> inline void
608 _hb_debug_msg<0> (const char *what HB_UNUSED,
609                   const void *obj HB_UNUSED,
610                   const char *func HB_UNUSED,
611                   bool indented HB_UNUSED,
612                   unsigned int level HB_UNUSED,
613                   int level_dir HB_UNUSED,
614                   const char *message HB_UNUSED,
615                   ...) HB_PRINTF_FUNC(7, 8);
616 template <> inline void
617 _hb_debug_msg<0> (const char *what HB_UNUSED,
618                   const void *obj HB_UNUSED,
619                   const char *func HB_UNUSED,
620                   bool indented HB_UNUSED,
621                   unsigned int level HB_UNUSED,
622                   int level_dir HB_UNUSED,
623                   const char *message HB_UNUSED,
624                   ...) {}
625
626 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...)       _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL,    true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
627 #define DEBUG_MSG(WHAT, OBJ, ...)                               _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL,    false, 0, 0, __VA_ARGS__)
628 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...)                          _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
629
630
631 /*
632  * Trace
633  */
634
635 template <int max_level>
636 struct hb_auto_trace_t {
637   explicit inline hb_auto_trace_t (unsigned int *plevel_,
638                                    const char *what_,
639                                    const void *obj_,
640                                    const char *func,
641                                    const char *message,
642                                    ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
643   {
644     if (plevel) ++*plevel;
645
646     va_list ap;
647     va_start (ap, message);
648     _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
649     va_end (ap);
650   }
651   inline ~hb_auto_trace_t (void)
652   {
653     if (unlikely (!returned)) {
654       fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN.  This is a bug, please report.  Level was %d.\n", plevel ? *plevel : -1);
655       _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
656       return;
657     }
658
659     if (plevel) --*plevel;
660   }
661
662   inline bool ret (bool v)
663   {
664     if (unlikely (returned)) {
665       fprintf (stderr, "OUCH, double calls to TRACE_RETURN.  This is a bug, please report.\n");
666       return v;
667     }
668
669     _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, "return %s", v ? "true" : "false");
670     if (plevel) --*plevel;
671     plevel = NULL;
672     returned = true;
673     return v;
674   }
675
676   private:
677   unsigned int *plevel;
678   bool returned;
679   const char *what;
680   const void *obj;
681 };
682 template <> /* Optimize when tracing is disabled */
683 struct hb_auto_trace_t<0> {
684   explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
685                                    const char *what HB_UNUSED,
686                                    const void *obj HB_UNUSED,
687                                    const char *func HB_UNUSED,
688                                    const char *message HB_UNUSED,
689                                    ...) {}
690
691   template <typename T>
692   inline T ret (T v) { return v; }
693 };
694
695 #define TRACE_RETURN(RET) trace.ret (RET)
696
697 /* Misc */
698
699
700 /* Pre-mature optimization:
701  * Checks for lo <= u <= hi but with an optimization if lo and hi
702  * are only different in a contiguous set of lower-most bits.
703  */
704 template <typename T> inline bool
705 hb_in_range (T u, T lo, T hi)
706 {
707   if ( ((lo^hi) & lo) == 0 &&
708        ((lo^hi) & hi) == (lo^hi) &&
709        ((lo^hi) & ((lo^hi) + 1)) == 0 )
710     return (u & ~(lo^hi)) == lo;
711   else
712     return lo <= u && u <= hi;
713 }
714
715
716 /* Useful for set-operations on small enums.
717  * For example, for testing "x ∈ {x1, x2, x3}" use:
718  * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
719  */
720 #define FLAG(x) (1<<(x))
721
722
723 template <typename T> inline void
724 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
725 {
726   if (unlikely (!len))
727     return;
728
729   unsigned int k = len - 1;
730   do {
731     unsigned int new_k = 0;
732
733     for (unsigned int j = 0; j < k; j++)
734       if (compar (&array[j], &array[j+1]) > 0) {
735         T t;
736         t = array[j];
737         array[j] = array[j + 1];
738         array[j + 1] = t;
739
740         new_k = j;
741       }
742     k = new_k;
743   } while (k);
744 }
745
746
747
748
749 #endif /* HB_PRIVATE_HH */