Start ft glue
[framework/uifw/harfbuzz.git] / src / hb-private.h
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, an OpenType Layout engine 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_H
28 #define HB_PRIVATE_H
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 /* #define HB_DEBUG 1 */
35 #ifndef HB_DEBUG
36 #define HB_DEBUG 0
37 #endif
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <assert.h>
42 #if HB_DEBUG
43 #include <stdio.h>
44 #include <errno.h>
45 #endif
46
47 #include "hb-common.h"
48
49
50 /* We need external help for these */
51
52 #ifdef HAVE_GLIB
53
54 #include <glib.h>
55
56 typedef int hb_atomic_int_t;
57 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
58 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
59 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
60
61 typedef GStaticMutex hb_mutex_t;
62 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
63 #define hb_mutex_init(M)                g_static_mutex_init (&M)
64 #define hb_mutex_lock(M)                g_static_mutex_lock (&M)
65 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&M)
66 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&M)
67
68 #else
69
70 #ifdef _MSC_VER
71 #pragma message(__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
72 #else
73 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
74 #endif
75
76 typedef int hb_atomic_int_t;
77 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
78 #define hb_atomic_int_get(AI)                   (AI)
79 #define hb_atomic_int_set(AI, V)                do { (AI) = (V); } while (0)
80
81 typedef int hb_mutex_t;
82 #define HB_MUTEX_INIT                   0
83 #define hb_mutex_init(M)                do { (M) = 0; } while (0)
84 #define hb_mutex_lock(M)                do { (M) = 1; } while (0)
85 #define hb_mutex_trylock(M)             ((M) = 1, 1)
86 #define hb_mutex_unlock(M)              do { (M) = 0; } while (0)
87
88 #endif
89
90
91 /* Big-endian handling */
92
93 #define hb_be_uint16(v)                 ((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
94
95 #define hb_be_uint8_put_unaligned(v,V)  (v[0] = (V), 0)
96 #define hb_be_uint8_get_unaligned(v)    (uint8_t) (v[0])
97 #define hb_be_uint8_cmp_unaligned(a,b)  (a[0] == b[0])
98 #define hb_be_int8_put_unaligned        hb_be_uint8_put_unaligned
99 #define hb_be_int8_get_unaligned        (int8_t) hb_be_uint8_get_unaligned
100 #define hb_be_int8_cmp_unaligned        hb_be_uint8_cmp_unaligned
101
102 #define hb_be_uint16_put_unaligned(v,V) (v[0] = (V>>8), v[1] = (V), 0)
103 #define hb_be_uint16_get_unaligned(v)   (uint16_t) ((v[0] << 8) + v[1])
104 #define hb_be_uint16_cmp_unaligned(a,b) (a[0] == b[0] && a[1] == b[1])
105 #define hb_be_int16_put_unaligned       hb_be_uint16_put_unaligned
106 #define hb_be_int16_get_unaligned       (int16_t) hb_be_uint16_get_unaligned
107 #define hb_be_int16_cmp_unaligned       hb_be_uint16_cmp_unaligned
108
109 #define hb_be_uint32_put_unaligned(v,V) (v[0] = (V>>24), v[1] = (V>>16), v[2] = (V>>8), v[3] = (V), 0)
110 #define hb_be_uint32_get_unaligned(v)   (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
111 #define hb_be_uint32_cmp_unaligned(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
112 #define hb_be_int32_put_unaligned       hb_be_uint32_put_unaligned
113 #define hb_be_int32_get_unaligned       (int32_t) hb_be_uint32_get_unaligned
114 #define hb_be_int32_cmp_unaligned       hb_be_uint32_cmp_unaligned
115
116
117 /* Basics */
118
119 #undef MIN
120 #define MIN(a,b) ((a) < (b) ? (a) : (b))
121
122 #ifndef HB_INTERNAL
123 # define HB_INTERNAL extern
124 #endif
125
126 #ifndef NULL
127 # define NULL ((void *)0)
128 #endif
129
130 #ifndef FALSE
131 # define FALSE 0
132 #endif
133
134 #ifndef TRUE
135 # define TRUE 1
136 #endif
137
138 #define HB_STMT_START do
139 #define HB_STMT_END   while (0)
140
141 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
142 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
143 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
144
145 #define ASSERT_SIZE(_type, _size) ASSERT_STATIC (sizeof (_type) == (_size))
146
147 #define VAR 1 /* Size signifying variable-sized array */
148 #define ASSERT_SIZE_VAR(_type, _size, _var_type) \
149         ASSERT_STATIC (sizeof (_type) == (_size) + VAR * sizeof (_var_type))
150 #define ASSERT_SIZE_VAR2(_type, _size, _var_type1, _var_type2) \
151         ASSERT_STATIC (sizeof (_type) == (_size) + VAR * sizeof (_var_type1) + VAR * sizeof (_var_type2))
152
153 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
154 #define _HB_BOOLEAN_EXPR(expr)                   \
155  __extension__ ({                               \
156    int _cairo_boolean_var_;                         \
157    if (expr)                                    \
158       _cairo_boolean_var_ = 1;                      \
159    else                                         \
160       _cairo_boolean_var_ = 0;                      \
161    _cairo_boolean_var_;                             \
162 })
163 #define HB_LIKELY(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
164 #define HB_UNLIKELY(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
165 #else
166 #define HB_LIKELY(expr) (expr)
167 #define HB_UNLIKELY(expr) (expr)
168 #endif
169
170 #ifndef __GNUC__
171 #undef __attribute__
172 #define __attribute__(x)
173 #endif
174
175 #if __GNUC__ >= 3
176 #define HB_GNUC_UNUSED  __attribute__((unused))
177 #define HB_GNUC_PURE    __attribute__((pure))
178 #define HB_GNUC_CONST   __attribute__((const))
179 #else
180 #define HB_GNUC_UNUSED
181 #define HB_GNUC_PURE
182 #define HB_GNUC_CONST
183 #endif
184
185
186 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
187 #define snprintf _snprintf
188 #endif
189
190 #ifdef _MSC_VER
191 #undef inline
192 #define inline __inline
193 #endif
194
195 #ifdef __STRICT_ANSI__
196 #undef inline
197 #define inline __inline__
198 #endif
199
200
201 /* Return the number of 1 bits in mask.
202  *
203  * GCC 3.4 supports a "population count" builtin, which on many targets is
204  * implemented with a single instruction. There is a fallback definition
205  * in libgcc in case a target does not have one, which should be just as
206  * good as the open-coded solution below, (which is "HACKMEM 169").
207  */
208 static HB_GNUC_UNUSED inline unsigned int
209 _hb_popcount32 (uint32_t mask)
210 {
211 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
212     return __builtin_popcount (mask);
213 #else
214     register uint32_t y;
215
216     y = (mask >> 1) &033333333333;
217     y = mask - y - ((y >>1) & 033333333333);
218     return (((y + (y >> 3)) & 030707070707) % 077);
219 #endif
220 }
221
222
223 /* Multiplies a 16dot16 value by another value, then truncates the result */
224 #define _hb_16dot16_mul_trunc(A,B) ((int64_t) (A) * (B) / 0x10000)
225
226 #include "hb-object-private.h"
227
228 #endif /* HB_PRIVATE_H */