Update Copyright headers
[apps/home/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 #undef MIN
68 #define MIN(a,b) ((a) < (b) ? (a) : (b))
69
70 #undef MAX
71 #define MAX(a,b) ((a) > (b) ? (a) : (b))
72
73 #undef  ARRAY_LENGTH
74 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
75
76 #define HB_STMT_START do
77 #define HB_STMT_END   while (0)
78
79 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
80 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
81 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
82
83 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
84
85
86 /* Lets assert int types.  Saves trouble down the road. */
87
88 ASSERT_STATIC (sizeof (int8_t) == 1);
89 ASSERT_STATIC (sizeof (uint8_t) == 1);
90 ASSERT_STATIC (sizeof (int16_t) == 2);
91 ASSERT_STATIC (sizeof (uint16_t) == 2);
92 ASSERT_STATIC (sizeof (int32_t) == 4);
93 ASSERT_STATIC (sizeof (uint32_t) == 4);
94 ASSERT_STATIC (sizeof (int64_t) == 8);
95 ASSERT_STATIC (sizeof (uint64_t) == 8);
96
97 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
98 ASSERT_STATIC (sizeof (hb_position_t) == 4);
99 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
100 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
101
102 /* Misc */
103
104
105 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
106 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
107 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
108 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
109 #else
110 #define likely(expr) (expr)
111 #define unlikely(expr) (expr)
112 #endif
113
114 #ifndef __GNUC__
115 #undef __attribute__
116 #define __attribute__(x)
117 #endif
118
119 #if __GNUC__ >= 3
120 #define HB_PURE_FUNC    __attribute__((pure))
121 #define HB_CONST_FUNC   __attribute__((const))
122 #else
123 #define HB_PURE_FUNC
124 #define HB_CONST_FUNC
125 #endif
126 #if __GNUC__ >= 4
127 #define HB_UNUSED       __attribute__((unused))
128 #else
129 #define HB_UNUSED
130 #endif
131
132 #ifndef HB_INTERNAL
133 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
134 #endif
135
136
137 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
138 #define snprintf _snprintf
139 #endif
140
141 #ifdef _MSC_VER
142 #undef inline
143 #define inline __inline
144 #endif
145
146 #ifdef __STRICT_ANSI__
147 #undef inline
148 #define inline __inline__
149 #endif
150
151
152 #if __GNUC__ >= 3
153 #define HB_FUNC __PRETTY_FUNCTION__
154 #elif defined(_MSC_VER)
155 #define HB_FUNC __FUNCSIG__
156 #else
157 #define HB_FUNC __func__
158 #endif
159
160
161 /* Return the number of 1 bits in mask. */
162 static inline HB_CONST_FUNC unsigned int
163 _hb_popcount32 (uint32_t mask)
164 {
165 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
166   return __builtin_popcount (mask);
167 #else
168   /* "HACKMEM 169" */
169   register uint32_t y;
170   y = (mask >> 1) &033333333333;
171   y = mask - y - ((y >>1) & 033333333333);
172   return (((y + (y >> 3)) & 030707070707) % 077);
173 #endif
174 }
175
176 /* Returns the number of bits needed to store number */
177 static inline HB_CONST_FUNC unsigned int
178 _hb_bit_storage (unsigned int number)
179 {
180 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
181   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
182 #else
183   register unsigned int n_bits = 0;
184   while (number) {
185     n_bits++;
186     number >>= 1;
187   }
188   return n_bits;
189 #endif
190 }
191
192 /* Returns the number of zero bits in the least significant side of number */
193 static inline HB_CONST_FUNC unsigned int
194 _hb_ctz (unsigned int number)
195 {
196 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
197   return likely (number) ? __builtin_ctz (number) : 0;
198 #else
199   register unsigned int n_bits = 0;
200   if (unlikely (!number)) return 0;
201   while (!(number & 1)) {
202     n_bits++;
203     number >>= 1;
204   }
205   return n_bits;
206 #endif
207 }
208
209 /* Type of bsearch() / qsort() compare function */
210 typedef int (*hb_compare_func_t) (const void *, const void *);
211
212
213 /* We need external help for these */
214
215 #ifdef HAVE_GLIB
216
217 #include <glib.h>
218
219 typedef volatile int hb_atomic_int_t;
220 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
221 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
222 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
223
224 typedef GStaticMutex hb_mutex_t;
225 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
226 #define hb_mutex_init(M)                g_static_mutex_init (&M)
227 #define hb_mutex_lock(M)                g_static_mutex_lock (&M)
228 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&M)
229 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&M)
230
231 #else
232
233 #ifdef _MSC_VER
234 #define _HB__STR2__(x) #x
235 #define _HB__STR1__(x) _HB__STR2__(x)
236 #define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
237 #pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
238 #else
239 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
240 #endif
241
242 typedef volatile int hb_atomic_int_t;
243 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
244 #define hb_atomic_int_get(AI)                   (AI)
245 #define hb_atomic_int_set(AI, V)                HB_STMT_START { (AI) = (V); } HB_STMT_END
246
247 typedef volatile int hb_mutex_t;
248 #define HB_MUTEX_INIT                           0
249 #define hb_mutex_init(M)                        HB_STMT_START { (M) = 0; } HB_STMT_END
250 #define hb_mutex_lock(M)                        HB_STMT_START { (M) = 1; } HB_STMT_END
251 #define hb_mutex_trylock(M)                     ((M) = 1, 1)
252 #define hb_mutex_unlock(M)                      HB_STMT_START { (M) = 0; } HB_STMT_END
253
254 #endif
255
256
257 /* Big-endian handling */
258
259 #define hb_be_uint16(v)         ((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
260
261 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
262 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
263 #define hb_be_uint16_cmp(a,b)   (a[0] == b[0] && a[1] == b[1])
264
265 #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
266 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
267 #define hb_be_uint32_cmp(a,b)   (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
268
269
270 /* ASCII tag/character handling */
271
272 #define ISALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
273 #define TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))
274 #define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
275
276 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
277                                   ((const char *) s)[1], \
278                                   ((const char *) s)[2], \
279                                   ((const char *) s)[3]))
280
281
282 /* Debug */
283
284 #ifndef HB_DEBUG
285 #define HB_DEBUG 0
286 #endif
287
288 static inline hb_bool_t /* always returns TRUE */
289 _hb_trace (const char *what,
290            const char *function,
291            const void *obj,
292            unsigned int depth,
293            unsigned int max_depth)
294 {
295   (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
296   return TRUE;
297 }
298
299
300 #include "hb-object-private.hh"
301
302
303 HB_END_DECLS
304
305 #endif /* HB_PRIVATE_HH */