Remove lock_instance()
[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, 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_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 /* Basics */
51
52 #undef MIN
53 #define MIN(a,b) ((a) < (b) ? (a) : (b))
54
55 #undef MAX
56 #define MAX(a,b) ((a) > (b) ? (a) : (b))
57
58 #ifndef HB_INTERNAL
59 # define HB_INTERNAL extern
60 #endif
61
62 #ifndef NULL
63 # define NULL ((void *) 0)
64 #endif
65
66 #undef FALSE
67 #define FALSE 0
68
69 #undef TRUE
70 #define TRUE 1
71
72 #undef  ARRAY_LENGTH
73 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
74
75 #define HB_STMT_START do
76 #define HB_STMT_END   while (0)
77
78 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
79 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
80 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
81
82 #define ASSERT_SIZE(_type, _size) ASSERT_STATIC (sizeof (_type) == (_size))
83
84 /* Size signifying variable-sized array */
85 #ifdef FLEXIBLE_ARRAY_MEMBER
86 #define VAR FLEXIBLE_ARRAY_MEMBER
87 #else
88 #define VAR 1
89 #endif
90
91 #define VAR0 (VAR+0)
92 #define ASSERT_SIZE_VAR(_type, _size, _var_type) \
93         ASSERT_STATIC (sizeof (_type) == (_size) + VAR0 * sizeof (_var_type))
94 #define ASSERT_SIZE_VAR2(_type, _size, _var_type1, _var_type2) \
95         ASSERT_STATIC (sizeof (_type) == (_size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2))
96
97 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
98 #define _HB_BOOLEAN_EXPR(expr) \
99   __extension__ ({ \
100      int _hb_boolean_var_; \
101      if (expr) \
102         _hb_boolean_var_ = 1; \
103      else \
104         _hb_boolean_var_ = 0; \
105      _hb_boolean_var_; \
106   })
107 #define HB_LIKELY(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
108 #define HB_UNLIKELY(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
109 #else
110 #define HB_LIKELY(expr) (expr)
111 #define HB_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_GNUC_PURE    __attribute__((pure))
121 #define HB_GNUC_CONST   __attribute__((const))
122 #else
123 #define HB_GNUC_PURE
124 #define HB_GNUC_CONST
125 #endif
126 #if __GNUC__ >= 4
127 #define HB_GNUC_UNUSED  __attribute__((unused))
128 #else
129 #define HB_GNUC_UNUSED
130 #endif
131
132
133 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
134 #define snprintf _snprintf
135 #endif
136
137 #ifdef _MSC_VER
138 #undef inline
139 #define inline __inline
140 #endif
141
142 #ifdef __STRICT_ANSI__
143 #undef inline
144 #define inline __inline__
145 #endif
146
147
148 /* Return the number of 1 bits in mask.
149  *
150  * GCC 3.4 supports a "population count" builtin, which on many targets is
151  * implemented with a single instruction. There is a fallback definition
152  * in libgcc in case a target does not have one, which should be just as
153  * good as the open-coded solution below, (which is "HACKMEM 169").
154  */
155 static HB_GNUC_UNUSED inline unsigned int
156 _hb_popcount32 (uint32_t mask)
157 {
158 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
159     return __builtin_popcount (mask);
160 #else
161     register uint32_t y;
162
163     y = (mask >> 1) &033333333333;
164     y = mask - y - ((y >>1) & 033333333333);
165     return (((y + (y >> 3)) & 030707070707) % 077);
166 #endif
167 }
168
169
170 /* Multiplies a 16dot16 value by another value, then truncates the result */
171 #define _hb_16dot16_mul_round(A,B) (((int64_t) (A) * (B) + 0x8000) / 0x10000)
172
173
174 /* We need external help for these */
175
176 #ifdef HAVE_GLIB
177
178 #include <glib.h>
179
180 typedef int hb_atomic_int_t;
181 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
182 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
183 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
184
185 typedef GStaticMutex hb_mutex_t;
186 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
187 #define hb_mutex_init(M)                g_static_mutex_init (&M)
188 #define hb_mutex_lock(M)                g_static_mutex_lock (&M)
189 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&M)
190 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&M)
191
192 #else
193
194 #ifdef _MSC_VER
195 #pragma message(__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
196 #else
197 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
198 #endif
199
200 typedef int hb_atomic_int_t;
201 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
202 #define hb_atomic_int_get(AI)                   (AI)
203 #define hb_atomic_int_set(AI, V)                HB_STMT_START { (AI) = (V); } HB_STMT_END
204
205 typedef int hb_mutex_t;
206 #define HB_MUTEX_INIT                           0
207 #define hb_mutex_init(M)                        HB_STMT_START { (M) = 0; } HB_STMT_END
208 #define hb_mutex_lock(M)                        HB_STMT_START { (M) = 1; } HB_STMT_END
209 #define hb_mutex_trylock(M)                     ((M) = 1, 1)
210 #define hb_mutex_unlock(M)                      HB_STMT_START { (M) = 0; } HB_STMT_END
211
212 #endif
213
214
215 /* Big-endian handling */
216
217 #define hb_be_uint16(v)         ((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
218
219 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
220 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
221 #define hb_be_uint16_cmp(a,b)   (a[0] == b[0] && a[1] == b[1])
222
223 #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
224 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
225 #define hb_be_uint32_cmp(a,b)   (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
226
227
228
229 #include "hb-object-private.h"
230
231 #endif /* HB_PRIVATE_H */