[HB] Fix blob to use a actual mutex
[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 #define hb_be_uint8
50 #define hb_be_int8
51 #define hb_be_uint16(v)                 ((uint16_t) hb_be_int16 ((uint16_t) v))
52 #define hb_be_uint32(v)                 ((uint32_t) hb_be_int32 ((uint32_t) v))
53
54 /* We need external help for these */
55
56 #if HAVE_GLIB
57
58 #include <glib.h>
59
60 /* Macros to convert to/from BigEndian */
61 #define hb_be_int16(v)          GINT16_FROM_BE (v)
62 #define hb_be_int32(v)          GINT32_FROM_BE (v)
63
64 typedef int hb_atomic_int_t;
65 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
66 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
67 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
68
69 typedef GStaticMutex hb_mutex_t;
70 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
71 #define hb_mutex_init(M)                g_static_mutex_init (&M)
72 #define hb_mutex_lock(M)                g_static_mutex_lock (&M)
73 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&M)
74 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&M)
75
76 #else
77 #error "Could not find any system to define platform macros, see hb-private.h"
78 #endif
79
80
81 /* Basics */
82
83 #undef MIN
84 #define MIN(a,b) ((a) < (b) ? (a) : (b))
85
86 #ifndef HB_INTERNAL
87 # define HB_INTERNAL
88 #endif
89
90 #ifndef NULL
91 # define NULL ((void *)0)
92 #endif
93
94 #ifndef FALSE
95 # define FALSE 0
96 #endif
97
98 #ifndef TRUE
99 # define TRUE 1
100 #endif
101
102 #define HB_STMT_START do
103 #define HB_STMT_END   while (0)
104
105 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
106 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
107 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
108
109 #define ASSERT_SIZE(_type, _size) ASSERT_STATIC (sizeof (_type) == (_size))
110
111
112 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
113 #define _CAIRO_BOOLEAN_EXPR(expr)                   \
114  __extension__ ({                               \
115    int _cairo_boolean_var_;                         \
116    if (expr)                                    \
117       _cairo_boolean_var_ = 1;                      \
118    else                                         \
119       _cairo_boolean_var_ = 0;                      \
120    _cairo_boolean_var_;                             \
121 })
122 #define HB_LIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 1))
123 #define HB_UNLIKELY(expr) (__builtin_expect (_CAIRO_BOOLEAN_EXPR(expr), 0))
124 #else
125 #define HB_LIKELY(expr) (expr)
126 #define HB_UNLIKELY(expr) (expr)
127 #endif
128
129 #ifndef __GNUC__
130 #undef __attribute__
131 #define __attribute__(x)
132 #endif
133
134 #if __GNUC__ >= 3
135 #define HB_GNUC_UNUSED  __attribute__((unused))
136 #define HB_GNUC_PURE    __attribute__((pure))
137 #define HB_GNUC_CONST   __attribute__((const))
138 #else
139 #define HB_GNUC_UNUSED
140 #define HB_GNUC_PURE
141 #define HB_GNUC_CONST
142 #endif
143
144
145 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
146 #define snprintf _snprintf
147 #endif
148
149 #ifdef _MSC_VER
150 #undef inline
151 #define inline __inline
152 #endif
153
154 #ifdef __STRICT_ANSI__
155 #undef inline
156 #define inline __inline__
157 #endif
158
159
160 /* Return the number of 1 bits in mask.
161  *
162  * GCC 3.4 supports a "population count" builtin, which on many targets is
163  * implemented with a single instruction. There is a fallback definition
164  * in libgcc in case a target does not have one, which should be just as
165  * good as the open-coded solution below, (which is "HACKMEM 169").
166  */
167 static HB_GNUC_UNUSED inline 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     register uint32_t y;
174
175     y = (mask >> 1) &033333333333;
176     y = mask - y - ((y >>1) & 033333333333);
177     return (((y + (y >> 3)) & 030707070707) % 077);
178 #endif
179 }
180
181
182 #include "hb-object-private.h"
183
184 #endif /* HB_PRIVATE_H */