Towards normalization
[framework/uifw/harfbuzz.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 HB_END_DECLS
68
69 #undef MIN
70 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
71
72 #undef MAX
73 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
74
75 HB_BEGIN_DECLS
76
77 #undef  ARRAY_LENGTH
78 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
79
80 #define HB_STMT_START do
81 #define HB_STMT_END   while (0)
82
83 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
84 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
85 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
86
87 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
88 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
89
90
91 /* Lets assert int types.  Saves trouble down the road. */
92
93 ASSERT_STATIC (sizeof (int8_t) == 1);
94 ASSERT_STATIC (sizeof (uint8_t) == 1);
95 ASSERT_STATIC (sizeof (int16_t) == 2);
96 ASSERT_STATIC (sizeof (uint16_t) == 2);
97 ASSERT_STATIC (sizeof (int32_t) == 4);
98 ASSERT_STATIC (sizeof (uint32_t) == 4);
99 ASSERT_STATIC (sizeof (int64_t) == 8);
100 ASSERT_STATIC (sizeof (uint64_t) == 8);
101
102 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
103 ASSERT_STATIC (sizeof (hb_position_t) == 4);
104 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
105 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
106
107 /* Misc */
108
109
110 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
111 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
112 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
113 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
114 #else
115 #define likely(expr) (expr)
116 #define unlikely(expr) (expr)
117 #endif
118
119 #ifndef __GNUC__
120 #undef __attribute__
121 #define __attribute__(x)
122 #endif
123
124 #if __GNUC__ >= 3
125 #define HB_PURE_FUNC    __attribute__((pure))
126 #define HB_CONST_FUNC   __attribute__((const))
127 #else
128 #define HB_PURE_FUNC
129 #define HB_CONST_FUNC
130 #endif
131 #if __GNUC__ >= 4
132 #define HB_UNUSED       __attribute__((unused))
133 #else
134 #define HB_UNUSED
135 #endif
136
137 #ifndef HB_INTERNAL
138 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
139 #endif
140
141
142 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
143 #define snprintf _snprintf
144 #endif
145
146 #ifdef _MSC_VER
147 #undef inline
148 #define inline __inline
149 #endif
150
151 #ifdef __STRICT_ANSI__
152 #undef inline
153 #define inline __inline__
154 #endif
155
156
157 #if __GNUC__ >= 3
158 #define HB_FUNC __PRETTY_FUNCTION__
159 #elif defined(_MSC_VER)
160 #define HB_FUNC __FUNCSIG__
161 #else
162 #define HB_FUNC __func__
163 #endif
164
165
166 /* Return the number of 1 bits in mask. */
167 static inline HB_CONST_FUNC 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   /* "HACKMEM 169" */
174   register uint32_t y;
175   y = (mask >> 1) &033333333333;
176   y = mask - y - ((y >>1) & 033333333333);
177   return (((y + (y >> 3)) & 030707070707) % 077);
178 #endif
179 }
180
181 /* Returns the number of bits needed to store number */
182 static inline HB_CONST_FUNC unsigned int
183 _hb_bit_storage (unsigned int number)
184 {
185 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
186   return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
187 #else
188   register unsigned int n_bits = 0;
189   while (number) {
190     n_bits++;
191     number >>= 1;
192   }
193   return n_bits;
194 #endif
195 }
196
197 /* Returns the number of zero bits in the least significant side of number */
198 static inline HB_CONST_FUNC unsigned int
199 _hb_ctz (unsigned int number)
200 {
201 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
202   return likely (number) ? __builtin_ctz (number) : 0;
203 #else
204   register unsigned int n_bits = 0;
205   if (unlikely (!number)) return 0;
206   while (!(number & 1)) {
207     n_bits++;
208     number >>= 1;
209   }
210   return n_bits;
211 #endif
212 }
213
214 static inline bool
215 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
216 {
217   return (size > 0) && (count >= ((unsigned int) -1) / size);
218 }
219
220
221 /* Type of bsearch() / qsort() compare function */
222 typedef int (*hb_compare_func_t) (const void *, const void *);
223
224
225 HB_END_DECLS
226
227
228 /* arrays and maps */
229
230
231 template <typename Type, unsigned int StaticSize>
232 struct hb_prealloced_array_t {
233
234   unsigned int len;
235   unsigned int allocated;
236   Type *array;
237   Type static_array[StaticSize];
238
239   inline Type& operator [] (unsigned int i) { return array[i]; }
240   inline const Type& operator [] (unsigned int i) const { return array[i]; }
241
242   inline Type *push (void)
243   {
244     if (!array) {
245       array = static_array;
246       allocated = ARRAY_LENGTH (static_array);
247     }
248     if (likely (len < allocated))
249       return &array[len++];
250
251     /* Need to reallocate */
252     unsigned int new_allocated = allocated + (allocated >> 1) + 8;
253     Type *new_array = NULL;
254
255     if (array == static_array) {
256       new_array = (Type *) calloc (new_allocated, sizeof (Type));
257       if (new_array)
258         memcpy (new_array, array, len * sizeof (Type));
259     } else {
260       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
261       if (likely (!overflows)) {
262         new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
263       }
264     }
265
266     if (unlikely (!new_array))
267       return NULL;
268
269     array = new_array;
270     allocated = new_allocated;
271     return &array[len++];
272   }
273
274   inline void pop (void)
275   {
276     len--;
277     /* TODO: shrink array if needed */
278   }
279
280   inline void shrink (unsigned int l)
281   {
282      if (l < len)
283        len = l;
284     /* TODO: shrink array if needed */
285   }
286
287   template <typename T>
288   inline Type *find (T v) {
289     for (unsigned int i = 0; i < len; i++)
290       if (array[i] == v)
291         return &array[i];
292     return NULL;
293   }
294   template <typename T>
295   inline const Type *find (T v) const {
296     for (unsigned int i = 0; i < len; i++)
297       if (array[i] == v)
298         return &array[i];
299     return NULL;
300   }
301
302   inline void sort (void)
303   {
304     qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
305   }
306
307   inline void sort (unsigned int start, unsigned int end)
308   {
309     qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
310   }
311
312   template <typename T>
313   inline Type *bsearch (T *key)
314   {
315     return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
316   }
317   template <typename T>
318   inline const Type *bsearch (T *key) const
319   {
320     return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
321   }
322
323   inline void finish (void)
324   {
325     if (array != static_array)
326       free (array);
327     array = NULL;
328     allocated = len = 0;
329   }
330 };
331
332 template <typename Type>
333 struct hb_array_t : hb_prealloced_array_t<Type, 2> {};
334
335
336 template <typename item_t, typename lock_t>
337 struct hb_lockable_set_t
338 {
339   hb_array_t <item_t> items;
340
341   template <typename T>
342   inline item_t *replace_or_insert (T v, lock_t &l)
343   {
344     l.lock ();
345     item_t *item = items.find (v);
346     if (item) {
347       item_t old = *item;
348       *item = v;
349       l.unlock ();
350       old.finish ();
351     } else {
352       item = items.push ();
353       if (likely (item))
354         *item = v;
355       l.unlock ();
356     }
357     return item;
358   }
359
360   template <typename T>
361   inline void remove (T v, lock_t &l)
362   {
363     l.lock ();
364     item_t *item = items.find (v);
365     if (item) {
366       item_t old = *item;
367       *item = items[items.len - 1];
368       items.pop ();
369       l.unlock ();
370       old.finish ();
371     } else {
372       l.unlock ();
373     }
374   }
375
376   template <typename T>
377   inline bool find (T v, item_t *i, lock_t &l)
378   {
379     l.lock ();
380     item_t *item = items.find (v);
381     if (item)
382       *i = *item;
383     l.unlock ();
384     return !!item;
385   }
386
387   template <typename T>
388   inline item_t *find_or_insert (T v, lock_t &l)
389   {
390     l.lock ();
391     item_t *item = items.find (v);
392     if (!item) {
393       item = items.push ();
394       if (likely (item))
395         *item = v;
396     }
397     l.unlock ();
398     return item;
399   }
400
401   inline void finish (lock_t &l)
402   {
403     l.lock ();
404     while (items.len) {
405       item_t old = items[items.len - 1];
406         items.pop ();
407         l.unlock ();
408         old.finish ();
409         l.lock ();
410     }
411     items.finish ();
412     l.unlock ();
413   }
414
415 };
416
417
418 HB_BEGIN_DECLS
419
420
421 /* Big-endian handling */
422
423 static inline uint16_t hb_be_uint16 (const uint16_t v)
424 {
425   const uint8_t *V = (const uint8_t *) &v;
426   return (uint16_t) (V[0] << 8) + V[1];
427 }
428
429 #define hb_be_uint16_put(v,V)   HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
430 #define hb_be_uint16_get(v)     (uint16_t) ((v[0] << 8) + v[1])
431 #define hb_be_uint16_eq(a,b)    (a[0] == b[0] && a[1] == b[1])
432
433 #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
434 #define hb_be_uint32_get(v)     (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
435 #define hb_be_uint32_eq(a,b)    (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
436
437
438 /* ASCII tag/character handling */
439
440 static inline unsigned char ISALPHA (unsigned char c)
441 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
442 static inline unsigned char ISALNUM (unsigned char c)
443 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
444 static inline unsigned char TOUPPER (unsigned char c)
445 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
446 static inline unsigned char TOLOWER (unsigned char c)
447 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
448
449 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
450                                   ((const char *) s)[1], \
451                                   ((const char *) s)[2], \
452                                   ((const char *) s)[3]))
453
454
455 /* C++ helpers */
456
457 /* Makes class uncopyable.  Use in private: section. */
458 #define NO_COPY(T) \
459   T (const T &o); \
460   T &operator = (const T &o)
461
462
463 /* Debug */
464
465 #ifndef HB_DEBUG
466 #define HB_DEBUG 0
467 #endif
468
469 static inline bool /* always returns TRUE */
470 _hb_trace (const char *what,
471            const char *function,
472            const void *obj,
473            unsigned int depth,
474            unsigned int max_depth)
475 {
476   (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
477   return TRUE;
478 }
479
480
481 /* Pre-mature optimization:
482  * Checks for lo <= u <= hi but with an optimization if lo and hi
483  * are only different in a contiguous set of lower-most bits.
484  */
485 static inline bool
486 hb_codepoint_in_range (hb_codepoint_t u, hb_codepoint_t lo, hb_codepoint_t hi)
487 {
488   if ( ((lo^hi) & lo) == 0 &&
489        ((lo^hi) & hi) == (lo^hi) &&
490        ((lo^hi) & ((lo^hi) + 1)) == 0 )
491     return (u & ~(lo^hi)) == lo;
492   else
493     return lo <= u && u <= hi;
494 }
495
496
497 /* Useful for set-operations on small enums */
498 #define FLAG(x) (1<<(x))
499
500 HB_END_DECLS
501
502 #endif /* HB_PRIVATE_HH */