85561d21f77b5062a570b3d2c5e60d5299851000
[framework/uifw/harfbuzz.git] / src / hb-private.hh
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_PRIVATE_HH
28 #define HB_PRIVATE_HH
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "hb-common.h"
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39
40 /* We only use these two for debug output.  However, the debug code is
41  * always seen by the compiler (and optimized out in non-debug builds.
42  * If including these becomes a problem, we can start thinking about
43  * someway around that. */
44 #include <stdio.h>
45 #include <errno.h>
46
47 HB_BEGIN_DECLS
48
49
50 /* Essentials */
51
52 #ifndef NULL
53 # define NULL ((void *) 0)
54 #endif
55
56 #undef FALSE
57 #define FALSE 0
58
59 #undef TRUE
60 #define TRUE 1
61
62
63 /* Basics */
64
65 #undef MIN
66 #define MIN(a,b) ((a) < (b) ? (a) : (b))
67
68 #undef MAX
69 #define MAX(a,b) ((a) > (b) ? (a) : (b))
70
71 #undef  ARRAY_LENGTH
72 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
73
74 #define HB_STMT_START do
75 #define HB_STMT_END   while (0)
76
77 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
78 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
79 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
80
81 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
82
83
84 /* Lets assert int types.  Saves trouble down the road. */
85
86 ASSERT_STATIC (sizeof (int8_t) == 1);
87 ASSERT_STATIC (sizeof (uint8_t) == 1);
88 ASSERT_STATIC (sizeof (int16_t) == 2);
89 ASSERT_STATIC (sizeof (uint16_t) == 2);
90 ASSERT_STATIC (sizeof (int32_t) == 4);
91 ASSERT_STATIC (sizeof (uint32_t) == 4);
92 ASSERT_STATIC (sizeof (int64_t) == 8);
93 ASSERT_STATIC (sizeof (uint64_t) == 8);
94
95 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
96 ASSERT_STATIC (sizeof (hb_position_t) == 4);
97 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
98 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
99
100 /* Misc */
101
102
103 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
104 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
105 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
106 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
107 #else
108 #define likely(expr) (expr)
109 #define unlikely(expr) (expr)
110 #endif
111
112 #ifndef __GNUC__
113 #undef __attribute__
114 #define __attribute__(x)
115 #endif
116
117 #if __GNUC__ >= 3
118 #define HB_PURE_FUNC    __attribute__((pure))
119 #define HB_CONST_FUNC   __attribute__((const))
120 #else
121 #define HB_PURE_FUNC
122 #define HB_CONST_FUNC
123 #endif
124 #if __GNUC__ >= 4
125 #define HB_UNUSED       __attribute__((unused))
126 #else
127 #define HB_UNUSED
128 #endif
129
130 #ifndef HB_INTERNAL
131 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
132 #endif
133
134
135 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
136 #define snprintf _snprintf
137 #endif
138
139 #ifdef _MSC_VER
140 #undef inline
141 #define inline __inline
142 #endif
143
144 #ifdef __STRICT_ANSI__
145 #undef inline
146 #define inline __inline__
147 #endif
148
149
150 #if __GNUC__ >= 3
151 #define HB_FUNC __PRETTY_FUNCTION__
152 #elif defined(_MSC_VER)
153 #define HB_FUNC __FUNCSIG__
154 #else
155 #define HB_FUNC __func__
156 #endif
157
158
159 /* Return the number of 1 bits in mask. */
160 static inline HB_CONST_FUNC unsigned int
161 _hb_popcount32 (uint32_t mask)
162 {
163 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
164   return __builtin_popcount (mask);
165 #else
166   /* "HACKMEM 169" */
167   register uint32_t y;
168   y = (mask >> 1) &033333333333;
169   y = mask - y - ((y >>1) & 033333333333);
170   return (((y + (y >> 3)) & 030707070707) % 077);
171 #endif
172 }
173
174 /* Returns the number of bits needed to store number */
175 static inline HB_CONST_FUNC unsigned int
176 _hb_bit_storage (unsigned int number)
177 {
178 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
179   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
180 #else
181   register unsigned int n_bits = 0;
182   while (number) {
183     n_bits++;
184     number >>= 1;
185   }
186   return n_bits;
187 #endif
188 }
189
190 /* Returns the number of zero bits in the least significant side of number */
191 static inline HB_CONST_FUNC unsigned int
192 _hb_ctz (unsigned int number)
193 {
194 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
195   return likely (number) ? __builtin_ctz (number) : 0;
196 #else
197   register unsigned int n_bits = 0;
198   if (unlikely (!number)) return 0;
199   while (!(number & 1)) {
200     n_bits++;
201     number >>= 1;
202   }
203   return n_bits;
204 #endif
205 }
206
207 /* Type of bsearch() / qsort() compare function */
208 typedef int (*hb_compare_func_t) (const void *, const void *);
209
210
211 /* We need external help for these */
212
213 #ifdef HAVE_GLIB
214
215 #include <glib.h>
216
217 typedef volatile int hb_atomic_int_t;
218 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
219 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
220 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
221
222 typedef GStaticMutex hb_mutex_t;
223 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
224 #define hb_mutex_init(M)                g_static_mutex_init (&M)
225 #define hb_mutex_lock(M)                g_static_mutex_lock (&M)
226 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&M)
227 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&M)
228
229 #else
230
231 #ifdef _MSC_VER
232 #define _HB__STR2__(x) #x
233 #define _HB__STR1__(x) _HB__STR2__(x)
234 #define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
235 #pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
236 #else
237 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
238 #endif
239
240 typedef volatile int hb_atomic_int_t;
241 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
242 #define hb_atomic_int_get(AI)                   (AI)
243 #define hb_atomic_int_set(AI, V)                HB_STMT_START { (AI) = (V); } HB_STMT_END
244
245 typedef volatile int hb_mutex_t;
246 #define HB_MUTEX_INIT                           0
247 #define hb_mutex_init(M)                        HB_STMT_START { (M) = 0; } HB_STMT_END
248 #define hb_mutex_lock(M)                        HB_STMT_START { (M) = 1; } HB_STMT_END
249 #define hb_mutex_trylock(M)                     ((M) = 1, 1)
250 #define hb_mutex_unlock(M)                      HB_STMT_START { (M) = 0; } HB_STMT_END
251
252 #endif
253
254
255 /* Big-endian handling */
256
257 #define hb_be_uint16(v)         ((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
258
259 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
260 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
261 #define hb_be_uint16_cmp(a,b)   (a[0] == b[0] && a[1] == b[1])
262
263 #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
264 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
265 #define hb_be_uint32_cmp(a,b)   (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
266
267
268 /* ASCII tag/character handling */
269
270 #define ISALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
271 #define TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))
272 #define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
273
274 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
275                                   ((const char *) s)[1], \
276                                   ((const char *) s)[2], \
277                                   ((const char *) s)[3]))
278
279
280 /* Debug */
281
282 #ifndef HB_DEBUG
283 #define HB_DEBUG 0
284 #endif
285
286 static inline hb_bool_t /* always returns TRUE */
287 _hb_trace (const char *what,
288            const char *function,
289            const void *obj,
290            unsigned int depth,
291            unsigned int max_depth)
292 {
293   (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
294   return TRUE;
295 }
296
297
298 #include "hb-object-private.hh"
299
300
301 HB_END_DECLS
302
303 #endif /* HB_PRIVATE_HH */