d749267abb7f5fc08d4c51a788e2f837d46945df
[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 <string.h>
40 #include <assert.h>
41
42 /* We only use these two for debug output.  However, the debug code is
43  * always seen by the compiler (and optimized out in non-debug builds.
44  * If including these becomes a problem, we can start thinking about
45  * someway around that. */
46 #include <stdio.h>
47 #include <errno.h>
48
49 HB_BEGIN_DECLS
50
51
52 /* Essentials */
53
54 #ifndef NULL
55 # define NULL ((void *) 0)
56 #endif
57
58 #undef FALSE
59 #define FALSE 0
60
61 #undef TRUE
62 #define TRUE 1
63
64
65 /* Basics */
66
67 HB_END_DECLS
68
69 #undef MIN
70 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
71
72 #undef MAX
73 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
74
75 HB_BEGIN_DECLS
76
77 #undef  ARRAY_LENGTH
78 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
79
80 #define HB_STMT_START do
81 #define HB_STMT_END   while (0)
82
83 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
84 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
85 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
86
87 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
88 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
89
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 /* Misc */
108
109
110 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
111 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
112 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
113 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
114 #else
115 #define likely(expr) (expr)
116 #define unlikely(expr) (expr)
117 #endif
118
119 #ifndef __GNUC__
120 #undef __attribute__
121 #define __attribute__(x)
122 #endif
123
124 #if __GNUC__ >= 3
125 #define HB_PURE_FUNC    __attribute__((pure))
126 #define HB_CONST_FUNC   __attribute__((const))
127 #else
128 #define HB_PURE_FUNC
129 #define HB_CONST_FUNC
130 #endif
131 #if __GNUC__ >= 4
132 #define HB_UNUSED       __attribute__((unused))
133 #else
134 #define HB_UNUSED
135 #endif
136
137 #ifndef HB_INTERNAL
138 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
139 #endif
140
141
142 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
143 #define snprintf _snprintf
144 #endif
145
146 #ifdef _MSC_VER
147 #undef inline
148 #define inline __inline
149 #endif
150
151 #ifdef __STRICT_ANSI__
152 #undef inline
153 #define inline __inline__
154 #endif
155
156
157 #if __GNUC__ >= 3
158 #define HB_FUNC __PRETTY_FUNCTION__
159 #elif defined(_MSC_VER)
160 #define HB_FUNC __FUNCSIG__
161 #else
162 #define HB_FUNC __func__
163 #endif
164
165
166 /* Return the number of 1 bits in mask. */
167 static inline HB_CONST_FUNC unsigned int
168 _hb_popcount32 (uint32_t mask)
169 {
170 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
171   return __builtin_popcount (mask);
172 #else
173   /* "HACKMEM 169" */
174   register uint32_t y;
175   y = (mask >> 1) &033333333333;
176   y = mask - y - ((y >>1) & 033333333333);
177   return (((y + (y >> 3)) & 030707070707) % 077);
178 #endif
179 }
180
181 /* Returns the number of bits needed to store number */
182 static inline HB_CONST_FUNC unsigned int
183 _hb_bit_storage (unsigned int number)
184 {
185 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
186   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
187 #else
188   register unsigned int n_bits = 0;
189   while (number) {
190     n_bits++;
191     number >>= 1;
192   }
193   return n_bits;
194 #endif
195 }
196
197 /* Returns the number of zero bits in the least significant side of number */
198 static inline HB_CONST_FUNC unsigned int
199 _hb_ctz (unsigned int number)
200 {
201 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
202   return likely (number) ? __builtin_ctz (number) : 0;
203 #else
204   register unsigned int n_bits = 0;
205   if (unlikely (!number)) return 0;
206   while (!(number & 1)) {
207     n_bits++;
208     number >>= 1;
209   }
210   return n_bits;
211 #endif
212 }
213
214 static inline bool
215 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
216 {
217   return (size > 0) && (count >= ((unsigned int) -1) / size);
218 }
219
220
221 /* Type of bsearch() / qsort() compare function */
222 typedef int (*hb_compare_func_t) (const void *, const void *);
223
224
225 HB_END_DECLS
226
227
228 /* arrays and maps */
229
230
231 template <typename Type, unsigned int StaticSize>
232 struct hb_prealloced_array_t {
233
234   unsigned int len;
235   unsigned int allocated;
236   Type *array;
237   Type static_array[StaticSize];
238
239   inline Type& operator [] (unsigned int i) { return array[i]; }
240   inline const Type& operator [] (unsigned int i) const { return array[i]; }
241
242   inline Type *push (void)
243   {
244     if (!array) {
245       array = static_array;
246       allocated = ARRAY_LENGTH (static_array);
247     }
248     if (likely (len < allocated))
249       return &array[len++];
250
251     /* Need to reallocate */
252     unsigned int new_allocated = allocated + (allocated >> 1) + 8;
253     Type *new_array = NULL;
254
255     if (array == static_array) {
256       new_array = (Type *) calloc (new_allocated, sizeof (Type));
257       if (new_array)
258         memcpy (new_array, array, len * sizeof (Type));
259     } else {
260       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
261       if (likely (!overflows)) {
262         new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
263       }
264     }
265
266     if (unlikely (!new_array))
267       return NULL;
268
269     array = new_array;
270     allocated = new_allocated;
271     return &array[len++];
272   }
273
274   inline void pop (void)
275   {
276     len--;
277     /* TODO: shrink array if needed */
278   }
279
280   inline void shrink (unsigned int l)
281   {
282      if (l < len)
283        len = l;
284     /* TODO: shrink array if needed */
285   }
286
287   template <typename T>
288   inline Type *find (T v) {
289     for (unsigned int i = 0; i < len; i++)
290       if (array[i] == v)
291         return &array[i];
292     return NULL;
293   }
294   template <typename T>
295   inline const Type *find (T v) const {
296     for (unsigned int i = 0; i < len; i++)
297       if (array[i] == v)
298         return &array[i];
299     return NULL;
300   }
301
302   inline void sort (void)
303   {
304     qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
305   }
306
307   template <typename T>
308   inline Type *bsearch (T *key)
309   {
310     return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
311   }
312   template <typename T>
313   inline const Type *bsearch (T *key) const
314   {
315     return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
316   }
317
318   inline void finish (void)
319   {
320     if (array != static_array)
321       free (array);
322     array = NULL;
323     allocated = len = 0;
324   }
325 };
326
327 template <typename Type>
328 struct hb_array_t : hb_prealloced_array_t<Type, 2> {};
329
330
331 template <typename item_t, typename lock_t>
332 struct hb_lockable_set_t
333 {
334   hb_array_t <item_t> items;
335
336   template <typename T>
337   inline item_t *replace_or_insert (T v, lock_t &l)
338   {
339     l.lock ();
340     item_t *item = items.find (v);
341     if (item) {
342       item_t old = *item;
343       *item = v;
344       l.unlock ();
345       old.finish ();
346     } else {
347       item = items.push ();
348       if (likely (item))
349         *item = v;
350       l.unlock ();
351     }
352     return item;
353   }
354
355   template <typename T>
356   inline void remove (T v, lock_t &l)
357   {
358     l.lock ();
359     item_t *item = items.find (v);
360     if (item) {
361       item_t old = *item;
362       *item = items[items.len - 1];
363       items.pop ();
364       l.unlock ();
365       old.finish ();
366     } else {
367       l.unlock ();
368     }
369   }
370
371   template <typename T>
372   inline bool find (T v, item_t *i, lock_t &l)
373   {
374     l.lock ();
375     item_t *item = items.find (v);
376     if (item)
377       *i = *item;
378     l.unlock ();
379     return !!item;
380   }
381
382   template <typename T>
383   inline item_t *find_or_insert (T v, lock_t &l)
384   {
385     l.lock ();
386     item_t *item = items.find (v);
387     if (!item) {
388       item = items.push ();
389       if (likely (item))
390         *item = v;
391     }
392     l.unlock ();
393     return item;
394   }
395
396   inline void finish (lock_t &l)
397   {
398     l.lock ();
399     while (items.len) {
400       item_t old = items[items.len - 1];
401         items.pop ();
402         l.unlock ();
403         old.finish ();
404         l.lock ();
405     }
406     items.finish ();
407     l.unlock ();
408   }
409
410 };
411
412
413 HB_BEGIN_DECLS
414
415
416 /* Big-endian handling */
417
418 static inline uint16_t hb_be_uint16 (const uint16_t v)
419 {
420   const uint8_t *V = (const uint8_t *) &v;
421   return (uint16_t) (V[0] << 8) + V[1];
422 }
423
424 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
425 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
426 #define hb_be_uint16_eq(a,b)    (a[0] == b[0] && a[1] == b[1])
427
428 #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
429 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
430 #define hb_be_uint32_eq(a,b)    (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
431
432
433 /* ASCII tag/character handling */
434
435 static inline unsigned char ISALPHA (unsigned char c)
436 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
437 static inline unsigned char ISALNUM (unsigned char c)
438 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
439 static inline unsigned char TOUPPER (unsigned char c)
440 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
441 static inline unsigned char TOLOWER (unsigned char c)
442 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
443
444 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
445                                   ((const char *) s)[1], \
446                                   ((const char *) s)[2], \
447                                   ((const char *) s)[3]))
448
449
450 /* C++ helpers */
451
452 /* Makes class uncopyable.  Use in private: section. */
453 #define NO_COPY(T) \
454   T (const T &o); \
455   T &operator = (const T &o)
456
457
458 /* Debug */
459
460 #ifndef HB_DEBUG
461 #define HB_DEBUG 0
462 #endif
463
464 static inline bool /* always returns TRUE */
465 _hb_trace (const char *what,
466            const char *function,
467            const void *obj,
468            unsigned int depth,
469            unsigned int max_depth)
470 {
471   (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
472   return TRUE;
473 }
474
475
476 HB_END_DECLS
477
478 #endif /* HB_PRIVATE_HH */