Minor
[profile/ivi/org.tizen.video-player.git] / src / hb-private.hh
1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2011  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 #if HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "hb-common.h"
37
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <string.h>
41 #include <assert.h>
42
43 /* We only use these two for debug output.  However, the debug code is
44  * always seen by the compiler (and optimized out in non-debug builds.
45  * If including these becomes a problem, we can start thinking about
46  * someway around that. */
47 #include <stdio.h>
48 #include <errno.h>
49 #include <stdarg.h>
50
51 HB_BEGIN_DECLS
52
53
54 /* Essentials */
55
56 #ifndef NULL
57 # define NULL ((void *) 0)
58 #endif
59
60 #undef FALSE
61 #define FALSE 0
62
63 #undef TRUE
64 #define TRUE 1
65
66
67 /* Basics */
68
69 HB_END_DECLS
70
71 #undef MIN
72 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
73
74 #undef MAX
75 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
76
77 HB_BEGIN_DECLS
78
79 #undef  ARRAY_LENGTH
80 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
81
82 #define HB_STMT_START do
83 #define HB_STMT_END   while (0)
84
85 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
86 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
87 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
88
89 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
90 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
91
92
93 /* Lets assert int types.  Saves trouble down the road. */
94
95 ASSERT_STATIC (sizeof (int8_t) == 1);
96 ASSERT_STATIC (sizeof (uint8_t) == 1);
97 ASSERT_STATIC (sizeof (int16_t) == 2);
98 ASSERT_STATIC (sizeof (uint16_t) == 2);
99 ASSERT_STATIC (sizeof (int32_t) == 4);
100 ASSERT_STATIC (sizeof (uint32_t) == 4);
101 ASSERT_STATIC (sizeof (int64_t) == 8);
102 ASSERT_STATIC (sizeof (uint64_t) == 8);
103
104 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
105 ASSERT_STATIC (sizeof (hb_position_t) == 4);
106 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
107 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
108
109 /* Misc */
110
111
112 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
113 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
114 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
115 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
116 #else
117 #define likely(expr) (expr)
118 #define unlikely(expr) (expr)
119 #endif
120
121 #ifndef __GNUC__
122 #undef __attribute__
123 #define __attribute__(x)
124 #endif
125
126 #if __GNUC__ >= 3
127 #define HB_PURE_FUNC    __attribute__((pure))
128 #define HB_CONST_FUNC   __attribute__((const))
129 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
130 #else
131 #define HB_PURE_FUNC
132 #define HB_CONST_FUNC
133 #define HB_PRINTF_FUCN(format_idx, arg_idx)
134 #endif
135 #if __GNUC__ >= 4
136 #define HB_UNUSED       __attribute__((unused))
137 #else
138 #define HB_UNUSED
139 #endif
140
141 #ifndef HB_INTERNAL
142 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
143 #endif
144
145
146 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
147 #define snprintf _snprintf
148 #endif
149
150 #ifdef _MSC_VER
151 #undef inline
152 #define inline __inline
153 #endif
154
155 #ifdef __STRICT_ANSI__
156 #undef inline
157 #define inline __inline__
158 #endif
159
160
161 #if __GNUC__ >= 3
162 #define HB_FUNC __PRETTY_FUNCTION__
163 #elif defined(_MSC_VER)
164 #define HB_FUNC __FUNCSIG__
165 #else
166 #define HB_FUNC __func__
167 #endif
168
169
170 /* Return the number of 1 bits in mask. */
171 static inline HB_CONST_FUNC unsigned int
172 _hb_popcount32 (uint32_t mask)
173 {
174 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
175   return __builtin_popcount (mask);
176 #else
177   /* "HACKMEM 169" */
178   register uint32_t y;
179   y = (mask >> 1) &033333333333;
180   y = mask - y - ((y >>1) & 033333333333);
181   return (((y + (y >> 3)) & 030707070707) % 077);
182 #endif
183 }
184
185 /* Returns the number of bits needed to store number */
186 static inline HB_CONST_FUNC unsigned int
187 _hb_bit_storage (unsigned int number)
188 {
189 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
190   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
191 #else
192   register unsigned int n_bits = 0;
193   while (number) {
194     n_bits++;
195     number >>= 1;
196   }
197   return n_bits;
198 #endif
199 }
200
201 /* Returns the number of zero bits in the least significant side of number */
202 static inline HB_CONST_FUNC unsigned int
203 _hb_ctz (unsigned int number)
204 {
205 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
206   return likely (number) ? __builtin_ctz (number) : 0;
207 #else
208   register unsigned int n_bits = 0;
209   if (unlikely (!number)) return 0;
210   while (!(number & 1)) {
211     n_bits++;
212     number >>= 1;
213   }
214   return n_bits;
215 #endif
216 }
217
218 static inline bool
219 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
220 {
221   return (size > 0) && (count >= ((unsigned int) -1) / size);
222 }
223
224
225 /* Type of bsearch() / qsort() compare function */
226 typedef int (*hb_compare_func_t) (const void *, const void *);
227
228
229 HB_END_DECLS
230
231
232 /* arrays and maps */
233
234
235 template <typename Type, unsigned int StaticSize>
236 struct hb_prealloced_array_t {
237
238   unsigned int len;
239   unsigned int allocated;
240   Type *array;
241   Type static_array[StaticSize];
242
243   inline Type& operator [] (unsigned int i) { return array[i]; }
244   inline const Type& operator [] (unsigned int i) const { return array[i]; }
245
246   inline Type *push (void)
247   {
248     if (!array) {
249       array = static_array;
250       allocated = ARRAY_LENGTH (static_array);
251     }
252     if (likely (len < allocated))
253       return &array[len++];
254
255     /* Need to reallocate */
256     unsigned int new_allocated = allocated + (allocated >> 1) + 8;
257     Type *new_array = NULL;
258
259     if (array == static_array) {
260       new_array = (Type *) calloc (new_allocated, sizeof (Type));
261       if (new_array)
262         memcpy (new_array, array, len * sizeof (Type));
263     } else {
264       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
265       if (likely (!overflows)) {
266         new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
267       }
268     }
269
270     if (unlikely (!new_array))
271       return NULL;
272
273     array = new_array;
274     allocated = new_allocated;
275     return &array[len++];
276   }
277
278   inline void pop (void)
279   {
280     len--;
281     /* TODO: shrink array if needed */
282   }
283
284   inline void shrink (unsigned int l)
285   {
286      if (l < len)
287        len = l;
288     /* TODO: shrink array if needed */
289   }
290
291   template <typename T>
292   inline Type *find (T v) {
293     for (unsigned int i = 0; i < len; i++)
294       if (array[i] == v)
295         return &array[i];
296     return NULL;
297   }
298   template <typename T>
299   inline const Type *find (T v) const {
300     for (unsigned int i = 0; i < len; i++)
301       if (array[i] == v)
302         return &array[i];
303     return NULL;
304   }
305
306   inline void sort (void)
307   {
308     qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
309   }
310
311   inline void sort (unsigned int start, unsigned int end)
312   {
313     qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
314   }
315
316   template <typename T>
317   inline Type *bsearch (T *key)
318   {
319     return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
320   }
321   template <typename T>
322   inline const Type *bsearch (T *key) const
323   {
324     return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
325   }
326
327   inline void finish (void)
328   {
329     if (array != static_array)
330       free (array);
331     array = NULL;
332     allocated = len = 0;
333   }
334 };
335
336 template <typename Type>
337 struct hb_array_t : hb_prealloced_array_t<Type, 2> {};
338
339
340 template <typename item_t, typename lock_t>
341 struct hb_lockable_set_t
342 {
343   hb_array_t <item_t> items;
344
345   template <typename T>
346   inline item_t *replace_or_insert (T v, lock_t &l)
347   {
348     l.lock ();
349     item_t *item = items.find (v);
350     if (item) {
351       item_t old = *item;
352       *item = v;
353       l.unlock ();
354       old.finish ();
355     } else {
356       item = items.push ();
357       if (likely (item))
358         *item = v;
359       l.unlock ();
360     }
361     return item;
362   }
363
364   template <typename T>
365   inline void remove (T v, lock_t &l)
366   {
367     l.lock ();
368     item_t *item = items.find (v);
369     if (item) {
370       item_t old = *item;
371       *item = items[items.len - 1];
372       items.pop ();
373       l.unlock ();
374       old.finish ();
375     } else {
376       l.unlock ();
377     }
378   }
379
380   template <typename T>
381   inline bool find (T v, item_t *i, lock_t &l)
382   {
383     l.lock ();
384     item_t *item = items.find (v);
385     if (item)
386       *i = *item;
387     l.unlock ();
388     return !!item;
389   }
390
391   template <typename T>
392   inline item_t *find_or_insert (T v, lock_t &l)
393   {
394     l.lock ();
395     item_t *item = items.find (v);
396     if (!item) {
397       item = items.push ();
398       if (likely (item))
399         *item = v;
400     }
401     l.unlock ();
402     return item;
403   }
404
405   inline void finish (lock_t &l)
406   {
407     l.lock ();
408     while (items.len) {
409       item_t old = items[items.len - 1];
410         items.pop ();
411         l.unlock ();
412         old.finish ();
413         l.lock ();
414     }
415     items.finish ();
416     l.unlock ();
417   }
418
419 };
420
421
422 HB_BEGIN_DECLS
423
424
425 /* Big-endian handling */
426
427 static inline uint16_t hb_be_uint16 (const uint16_t v)
428 {
429   const uint8_t *V = (const uint8_t *) &v;
430   return (uint16_t) (V[0] << 8) + V[1];
431 }
432
433 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
434 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
435 #define hb_be_uint16_eq(a,b)    (a[0] == b[0] && a[1] == b[1])
436
437 #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
438 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
439 #define hb_be_uint32_eq(a,b)    (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
440
441
442 /* ASCII tag/character handling */
443
444 static inline unsigned char ISALPHA (unsigned char c)
445 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
446 static inline unsigned char ISALNUM (unsigned char c)
447 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
448 static inline unsigned char TOUPPER (unsigned char c)
449 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
450 static inline unsigned char TOLOWER (unsigned char c)
451 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
452
453 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
454                                   ((const char *) s)[1], \
455                                   ((const char *) s)[2], \
456                                   ((const char *) s)[3]))
457
458
459 /* C++ helpers */
460
461 /* Makes class uncopyable.  Use in private: section. */
462 #define NO_COPY(T) \
463   T (const T &o); \
464   T &operator = (const T &o)
465
466
467 /* Debug */
468
469 HB_END_DECLS
470
471 #ifndef HB_DEBUG
472 #define HB_DEBUG 0
473 #endif
474
475 static inline bool
476 _hb_debug (unsigned int level,
477            unsigned int max_level)
478 {
479   return level < max_level;
480 }
481
482 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
483 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
484
485 template <int max_level> inline bool /* always returns TRUE */
486 _hb_debug_msg (const char *what,
487                const void *obj,
488                const char *func,
489                bool indented,
490                int level,
491                const char *message,
492                ...) HB_PRINTF_FUNC(6, 7);
493 template <int max_level> inline bool /* always returns TRUE */
494 _hb_debug_msg (const char *what,
495                const void *obj,
496                const char *func,
497                bool indented,
498                int level,
499                const char *message,
500                ...)
501 {
502   va_list ap;
503   va_start (ap, message);
504
505   (void) (_hb_debug (level, max_level) &&
506           fprintf (stderr, "%s(%p): ", what, obj) &&
507           (func && fprintf (stderr, "%s: ", func), TRUE) &&
508           (indented && fprintf (stderr, "%-*d-> ", level + 1, level), TRUE) &&
509           vfprintf (stderr, message, ap) &&
510           fprintf (stderr, "\n"));
511
512   va_end (ap);
513
514   return TRUE;
515 }
516 template <> inline bool /* always returns TRUE */
517 _hb_debug_msg<0> (const char *what,
518                   const void *obj,
519                   const char *func,
520                   bool indented,
521                   int level,
522                   const char *message,
523                   ...) HB_PRINTF_FUNC(6, 7);
524 template <> inline bool /* always returns TRUE */
525 _hb_debug_msg<0> (const char *what,
526                   const void *obj,
527                   const char *func,
528                   bool indented,
529                   int level,
530                   const char *message,
531                   ...)
532 {
533   return TRUE;
534 }
535
536 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, FALSE, (LEVEL), __VA_ARGS__)
537 #define DEBUG_MSG(WHAT, OBJ, ...) DEBUG_MSG_LEVEL (WHAT, OBJ, 0, __VA_ARGS__)
538 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, FALSE, 0, __VA_ARGS__)
539
540
541 /*
542  * Trace
543  */
544
545 template <int max_level>
546 struct hb_auto_trace_t {
547   explicit inline hb_auto_trace_t (unsigned int *plevel_,
548                                    const char *what,
549                                    const void *obj,
550                                    const char *func,
551                                    const char *message) : plevel(plevel_)
552   {
553     if (max_level) ++*plevel;
554     /* TODO support variadic args here */
555     _hb_debug_msg<max_level> (what, obj, func, TRUE, *plevel, "%s", message);
556   }
557   ~hb_auto_trace_t (void) { if (max_level) --*plevel; }
558
559   private:
560   unsigned int *plevel;
561 };
562 template <> /* Optimize when tracing is disabled */
563 struct hb_auto_trace_t<0> {
564   explicit inline hb_auto_trace_t (unsigned int *plevel_,
565                                    const char *what,
566                                    const void *obj,
567                                    const char *func,
568                                    const char *message) {}
569 };
570
571 HB_BEGIN_DECLS
572
573
574 /* Misc */
575
576 HB_END_DECLS
577
578 /* Pre-mature optimization:
579  * Checks for lo <= u <= hi but with an optimization if lo and hi
580  * are only different in a contiguous set of lower-most bits.
581  */
582 template <typename T> inline bool
583 hb_in_range (T u, T lo, T hi)
584 {
585   if ( ((lo^hi) & lo) == 0 &&
586        ((lo^hi) & hi) == (lo^hi) &&
587        ((lo^hi) & ((lo^hi) + 1)) == 0 )
588     return (u & ~(lo^hi)) == lo;
589   else
590     return lo <= u && u <= hi;
591 }
592
593 HB_BEGIN_DECLS
594
595
596 /* Useful for set-operations on small enums */
597 #define FLAG(x) (1<<(x))
598
599
600 HB_END_DECLS
601
602 #endif /* HB_PRIVATE_HH */