src/java.desktop/share/native/libfontmanager/harfbuzz/hb-private.hh
changeset 50826 f5b95be8b6e2
parent 50648 2eb666723f65
child 51000 7c8841474f57
equal deleted inserted replaced
50825:aa0a35b071fb 50826:f5b95be8b6e2
    49 #include <assert.h>
    49 #include <assert.h>
    50 #include <errno.h>
    50 #include <errno.h>
    51 #include <stdio.h>
    51 #include <stdio.h>
    52 #include <stdarg.h>
    52 #include <stdarg.h>
    53 
    53 
    54 #if defined(_MSC_VER) || defined(__MINGW32__)
    54 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
    55 #include <intrin.h>
    55 #include <intrin.h>
    56 #endif
    56 #endif
    57 
    57 
    58 #define HB_PASTE1(a,b) a##b
    58 #define HB_PASTE1(a,b) a##b
    59 #define HB_PASTE(a,b) HB_PASTE1(a,b)
    59 #define HB_PASTE(a,b) HB_PASTE1(a,b)
    87 // Static assertions
    87 // Static assertions
    88 #ifndef static_assert
    88 #ifndef static_assert
    89 #define static_assert(e, msg) \
    89 #define static_assert(e, msg) \
    90         HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
    90         HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
    91 #endif // static_assert
    91 #endif // static_assert
       
    92 
       
    93 #ifdef __GNUC__
       
    94 #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
       
    95 #define thread_local __thread
       
    96 #endif
       
    97 #else
       
    98 #define thread_local
       
    99 #endif
    92 
   100 
    93 #endif // __cplusplus < 201103L
   101 #endif // __cplusplus < 201103L
    94 
   102 
    95 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
   103 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
    96 #define likely(expr) (__builtin_expect (!!(expr), 1))
   104 #define likely(expr) (__builtin_expect (!!(expr), 1))
   219 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
   227 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
   220 #      define HB_USE_ATEXIT 1
   228 #      define HB_USE_ATEXIT 1
   221 #    endif
   229 #    endif
   222 #  elif defined(_MSC_VER) || defined(__MINGW32__)
   230 #  elif defined(_MSC_VER) || defined(__MINGW32__)
   223 /* For MSVC:
   231 /* For MSVC:
   224  * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx
   232  * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
   225  * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx
   233  * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
   226  * mingw32 headers say atexit is safe to use in shared libraries.
   234  * mingw32 headers say atexit is safe to use in shared libraries.
   227  */
   235  */
   228 #    define HB_USE_ATEXIT 1
   236 #    define HB_USE_ATEXIT 1
   229 #  elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
   237 #  elif defined(__ANDROID__)
   230 /* This was fixed in Android NKD r8 or r8b:
   238 /* This is available since Android NKD r8 or r8b:
   231  * https://code.google.com/p/android/issues/detail?id=6455
   239  * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
   232  * which introduced GCC 4.6:
       
   233  * https://developer.android.com/tools/sdk/ndk/index.html
       
   234  */
   240  */
   235 #    define HB_USE_ATEXIT 1
   241 #    define HB_USE_ATEXIT 1
   236 #  elif defined(__APPLE__)
   242 #  elif defined(__APPLE__)
   237 /* For macOS and related platforms, the atexit man page indicates
   243 /* For macOS and related platforms, the atexit man page indicates
   238  * that it will be invoked when the library is unloaded, not only
   244  * that it will be invoked when the library is unloaded, not only
   239  * at application exit.
   245  * at application exit.
   240  */
   246  */
   241 #    define HB_USE_ATEXIT 1
   247 #    define HB_USE_ATEXIT 1
   242 #  endif
   248 #  endif
   243 #endif
   249 #endif
       
   250 #ifdef HB_NO_ATEXIT
       
   251 #  undef HB_USE_ATEXIT
       
   252 #endif
   244 
   253 
   245 /* Basics */
   254 /* Basics */
   246 
   255 
   247 #undef MIN
   256 #undef MIN
   248 template <typename Type>
   257 template <typename Type>
   312 # define _ASSERT_POD0(_line)    _ASSERT_POD1 (_line)
   321 # define _ASSERT_POD0(_line)    _ASSERT_POD1 (_line)
   313 # define ASSERT_POD()           _ASSERT_POD0 (__LINE__)
   322 # define ASSERT_POD()           _ASSERT_POD0 (__LINE__)
   314 
   323 
   315 
   324 
   316 
   325 
   317 /* Misc */
   326 /* Tiny functions */
   318 
   327 
   319 /*
   328 /*
   320  * Void!
   329  * Void!
   321  */
   330  */
   322 typedef const struct _hb_void_t *hb_void_t;
   331 typedef const struct _hb_void_t *hb_void_t;
   358     unsigned int shift = 64;
   367     unsigned int shift = 64;
   359     return _hb_popcount<uint64_t> ((uint64_t) v) + _hb_popcount ((uint64_t) (v >> shift));
   368     return _hb_popcount<uint64_t> ((uint64_t) v) + _hb_popcount ((uint64_t) (v >> shift));
   360   }
   369   }
   361 
   370 
   362   assert (0);
   371   assert (0);
   363   return 0;
   372   return 0; /* Shut up stupid compiler. */
   364 }
   373 }
   365 
   374 
   366 /* Returns the number of bits needed to store number */
   375 /* Returns the number of bits needed to store number */
   367 template <typename T>
   376 template <typename T>
   368 static inline HB_CONST_FUNC unsigned int
   377 static inline HB_CONST_FUNC unsigned int
   379 
   388 
   380   if (sizeof (T) <= sizeof (unsigned long long))
   389   if (sizeof (T) <= sizeof (unsigned long long))
   381     return sizeof (unsigned long long) * 8 - __builtin_clzll (v);
   390     return sizeof (unsigned long long) * 8 - __builtin_clzll (v);
   382 #endif
   391 #endif
   383 
   392 
   384 #if defined(_MSC_VER) || defined(__MINGW32__)
   393 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
   385   if (sizeof (T) <= sizeof (unsigned int))
   394   if (sizeof (T) <= sizeof (unsigned int))
   386   {
   395   {
   387     unsigned long where;
   396     unsigned long where;
   388     _BitScanReverse (&where, v);
   397     _BitScanReverse (&where, v);
   389     return 1 + where;
   398     return 1 + where;
   413     return r + 1;
   422     return r + 1;
   414   }
   423   }
   415   if (sizeof (T) <= 8)
   424   if (sizeof (T) <= 8)
   416   {
   425   {
   417     /* "bithacks" */
   426     /* "bithacks" */
   418     const uint64_t b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000};
   427     const uint64_t b[] = {0x2ULL, 0xCULL, 0xF0ULL, 0xFF00ULL, 0xFFFF0000ULL, 0xFFFFFFFF00000000ULL};
   419     const unsigned int S[] = {1, 2, 4, 8, 16, 32};
   428     const unsigned int S[] = {1, 2, 4, 8, 16, 32};
   420     unsigned int r = 0;
   429     unsigned int r = 0;
   421     for (int i = 5; i >= 0; i--)
   430     for (int i = 5; i >= 0; i--)
   422       if (v & b[i])
   431       if (v & b[i])
   423       {
   432       {
   427     return r + 1;
   436     return r + 1;
   428   }
   437   }
   429   if (sizeof (T) == 16)
   438   if (sizeof (T) == 16)
   430   {
   439   {
   431     unsigned int shift = 64;
   440     unsigned int shift = 64;
   432     return (v >> shift) ? _hb_bit_storage<uint64_t> ((uint64_t) v >> shift) + shift :
   441     return (v >> shift) ? _hb_bit_storage<uint64_t> ((uint64_t) (v >> shift)) + shift :
   433                           _hb_bit_storage<uint64_t> ((uint64_t) v);
   442                           _hb_bit_storage<uint64_t> ((uint64_t) v);
   434   }
   443   }
   435 
   444 
   436   assert (0);
   445   assert (0);
   437   return 0;
   446   return 0; /* Shut up stupid compiler. */
   438 }
   447 }
   439 
   448 
   440 /* Returns the number of zero bits in the least significant side of v */
   449 /* Returns the number of zero bits in the least significant side of v */
   441 template <typename T>
   450 template <typename T>
   442 static inline HB_CONST_FUNC unsigned int
   451 static inline HB_CONST_FUNC unsigned int
   453 
   462 
   454   if (sizeof (T) <= sizeof (unsigned long long))
   463   if (sizeof (T) <= sizeof (unsigned long long))
   455     return __builtin_ctzll (v);
   464     return __builtin_ctzll (v);
   456 #endif
   465 #endif
   457 
   466 
   458 #if defined(_MSC_VER) || defined(__MINGW32__)
   467 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
   459   if (sizeof (T) <= sizeof (unsigned int))
   468   if (sizeof (T) <= sizeof (unsigned int))
   460   {
   469   {
   461     unsigned long where;
   470     unsigned long where;
   462     _BitScanForward (&where, v);
   471     _BitScanForward (&where, v);
   463     return where;
   472     return where;
   489   {
   498   {
   490     /* "bithacks" */
   499     /* "bithacks" */
   491     unsigned int c = 64;
   500     unsigned int c = 64;
   492     v &= - (int64_t) (v);
   501     v &= - (int64_t) (v);
   493     if (v) c--;
   502     if (v) c--;
   494     if (v & 0x00000000FFFFFFFF) c -= 32;
   503     if (v & 0x00000000FFFFFFFFULL) c -= 32;
   495     if (v & 0x0000FFFF0000FFFF) c -= 16;
   504     if (v & 0x0000FFFF0000FFFFULL) c -= 16;
   496     if (v & 0x00FF00FF00FF00FF) c -= 8;
   505     if (v & 0x00FF00FF00FF00FFULL) c -= 8;
   497     if (v & 0x0F0F0F0F0F0F0F0F) c -= 4;
   506     if (v & 0x0F0F0F0F0F0F0F0FULL) c -= 4;
   498     if (v & 0x3333333333333333) c -= 2;
   507     if (v & 0x3333333333333333ULL) c -= 2;
   499     if (v & 0x5555555555555555) c -= 1;
   508     if (v & 0x5555555555555555ULL) c -= 1;
   500     return c;
   509     return c;
   501   }
   510   }
   502   if (sizeof (T) == 16)
   511   if (sizeof (T) == 16)
   503   {
   512   {
   504     unsigned int shift = 64;
   513     unsigned int shift = 64;
   505     return (uint64_t) v ? _hb_bit_storage<uint64_t> ((uint64_t) v) :
   514     return (uint64_t) v ? _hb_bit_storage<uint64_t> ((uint64_t) v) :
   506                           _hb_bit_storage<uint64_t> ((uint64_t) v >> shift) + shift;
   515                           _hb_bit_storage<uint64_t> ((uint64_t) v >> shift) + shift;
   507   }
   516   }
   508 
   517 
   509   assert (0);
   518   assert (0);
   510   return 0;
   519   return 0; /* Shut up stupid compiler. */
   511 }
   520 }
   512 
   521 
   513 static inline bool
   522 static inline bool
   514 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
   523 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
   515 {
   524 {
   522   return ((v - 1) | 3) + 1;
   531   return ((v - 1) | 3) + 1;
   523 }
   532 }
   524 
   533 
   525 
   534 
   526 
   535 
       
   536 /*
       
   537  *
       
   538  * Utility types
       
   539  *
       
   540  */
       
   541 
       
   542 #define HB_DISALLOW_COPY_AND_ASSIGN(TypeName) \
       
   543   TypeName(const TypeName&); \
       
   544   void operator=(const TypeName&)
       
   545 
       
   546 /*
       
   547  * Static pools
       
   548  */
       
   549 
       
   550 /* Global nul-content Null pool.  Enlarge as necessary. */
       
   551 
       
   552 #define HB_NULL_POOL_SIZE 264
       
   553 static_assert (HB_NULL_POOL_SIZE % sizeof (void *) == 0, "Align HB_NULL_POOL_SIZE.");
       
   554 
       
   555 #ifdef HB_NO_VISIBILITY
       
   556 static
       
   557 #else
       
   558 extern HB_INTERNAL
       
   559 #endif
       
   560 void * const _hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)]
       
   561 #ifdef HB_NO_VISIBILITY
       
   562 = {}
       
   563 #endif
       
   564 ;
       
   565 /* Generic nul-content Null objects. */
       
   566 template <typename Type>
       
   567 static inline Type const & Null (void) {
       
   568   static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
       
   569   return *reinterpret_cast<Type const *> (_hb_NullPool);
       
   570 }
       
   571 #define Null(Type) Null<Type>()
       
   572 
       
   573 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
       
   574 #define DEFINE_NULL_DATA(Namespace, Type, data) \
       
   575 } /* Close namespace. */ \
       
   576 static const char _Null##Type[sizeof (Namespace::Type) + 1] = data; /* +1 is for nul-termination in data */ \
       
   577 template <> \
       
   578 /*static*/ inline const Namespace::Type& Null<Namespace::Type> (void) { \
       
   579   return *reinterpret_cast<const Namespace::Type *> (_Null##Type); \
       
   580 } \
       
   581 namespace Namespace { \
       
   582 /* The following line really exists such that we end in a place needing semicolon */ \
       
   583 static_assert (Namespace::Type::min_size + 1 <= sizeof (_Null##Type), "Null pool too small.  Enlarge.")
       
   584 
       
   585 
       
   586 /* Global writable pool.  Enlarge as necessary. */
       
   587 
       
   588 /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
       
   589  * for correct operation. It only exist to catch and divert program logic bugs instead of
       
   590  * causing bad memory access. So, races there are not actually introducing incorrectness
       
   591  * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
       
   592 #ifdef HB_NO_VISIBILITY
       
   593 static
       
   594 #else
       
   595 extern HB_INTERNAL
       
   596 #endif
       
   597 /*thread_local*/ void * _hb_CrapPool[HB_NULL_POOL_SIZE / sizeof (void *)]
       
   598 #ifdef HB_NO_VISIBILITY
       
   599 = {}
       
   600 #endif
       
   601 ;
       
   602 /* CRAP pool: Common Region for Access Protection. */
       
   603 template <typename Type>
       
   604 static inline Type& Crap (void) {
       
   605   static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
       
   606   Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
       
   607   *obj = Null(Type);
       
   608   return *obj;
       
   609 }
       
   610 #define Crap(Type) Crap<Type>()
       
   611 
       
   612 template <typename Type>
       
   613 struct CrapOrNull {
       
   614   static inline Type & get (void) { return Crap(Type); }
       
   615 };
       
   616 template <typename Type>
       
   617 struct CrapOrNull<const Type> {
       
   618   static inline Type const & get (void) { return Null(Type); }
       
   619 };
       
   620 #define CrapOrNull(Type) CrapOrNull<Type>::get ()
       
   621 
       
   622 
       
   623 
   527 /* arrays and maps */
   624 /* arrays and maps */
   528 
   625 
   529 
   626 
   530 #define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr}
   627 #define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr}
   531 template <typename Type, unsigned int StaticSize=16>
   628 template <typename Type, unsigned int StaticSize=8>
   532 struct hb_prealloced_array_t
   629 struct hb_vector_t
   533 {
   630 {
   534   unsigned int len;
   631   unsigned int len;
   535   unsigned int allocated;
   632   unsigned int allocated;
   536   Type *array;
   633   bool successful;
       
   634   Type *arrayZ;
   537   Type static_array[StaticSize];
   635   Type static_array[StaticSize];
   538 
   636 
   539   void init (void)
   637   void init (void)
   540   {
   638   {
   541     len = 0;
   639     len = 0;
   542     allocated = ARRAY_LENGTH (static_array);
   640     allocated = ARRAY_LENGTH (static_array);
   543     array = static_array;
   641     successful = true;
   544   }
   642     arrayZ = static_array;
   545 
   643   }
   546   inline Type& operator [] (unsigned int i) { return array[i]; }
   644 
   547   inline const Type& operator [] (unsigned int i) const { return array[i]; }
   645   inline Type& operator [] (unsigned int i)
       
   646   {
       
   647     if (unlikely (i >= len))
       
   648       return Crap (Type);
       
   649     return arrayZ[i];
       
   650   }
       
   651   inline const Type& operator [] (unsigned int i) const
       
   652   {
       
   653     if (unlikely (i >= len))
       
   654       return Null(Type);
       
   655     return arrayZ[i];
       
   656   }
   548 
   657 
   549   inline Type *push (void)
   658   inline Type *push (void)
   550   {
   659   {
   551     if (unlikely (!resize (len + 1)))
   660     if (unlikely (!resize (len + 1)))
   552       return nullptr;
   661       return &Crap(Type);
   553 
   662     return &arrayZ[len - 1];
   554     return &array[len - 1];
   663   }
       
   664   inline Type *push (const Type& v)
       
   665   {
       
   666     Type *p = push ();
       
   667     *p = v;
       
   668     return p;
   555   }
   669   }
   556 
   670 
   557   /* Allocate for size but don't adjust len. */
   671   /* Allocate for size but don't adjust len. */
   558   inline bool alloc(unsigned int size)
   672   inline bool alloc (unsigned int size)
   559   {
   673   {
       
   674     if (unlikely (!successful))
       
   675       return false;
       
   676 
   560     if (likely (size <= allocated))
   677     if (likely (size <= allocated))
   561       return true;
   678       return true;
   562 
   679 
   563     /* Reallocate */
   680     /* Reallocate */
   564 
   681 
   566     while (size >= new_allocated)
   683     while (size >= new_allocated)
   567       new_allocated += (new_allocated >> 1) + 8;
   684       new_allocated += (new_allocated >> 1) + 8;
   568 
   685 
   569     Type *new_array = nullptr;
   686     Type *new_array = nullptr;
   570 
   687 
   571     if (array == static_array) {
   688     if (arrayZ == static_array)
       
   689     {
   572       new_array = (Type *) calloc (new_allocated, sizeof (Type));
   690       new_array = (Type *) calloc (new_allocated, sizeof (Type));
   573       if (new_array)
   691       if (new_array)
   574         memcpy (new_array, array, len * sizeof (Type));
   692         memcpy (new_array, arrayZ, len * sizeof (Type));
   575           } else {
   693     }
       
   694     else
       
   695     {
   576       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
   696       bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
   577       if (likely (!overflows)) {
   697       if (likely (!overflows))
   578         new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
   698         new_array = (Type *) realloc (arrayZ, new_allocated * sizeof (Type));
   579       }
       
   580     }
   699     }
   581 
   700 
   582     if (unlikely (!new_array))
   701     if (unlikely (!new_array))
       
   702     {
       
   703       successful = false;
   583       return false;
   704       return false;
   584 
   705     }
   585     array = new_array;
   706 
       
   707     arrayZ = new_array;
   586     allocated = new_allocated;
   708     allocated = new_allocated;
   587 
   709 
   588     return true;
   710     return true;
   589   }
   711   }
   590 
   712 
   591   inline bool resize (unsigned int size)
   713   inline bool resize (int size_)
   592   {
   714   {
       
   715     unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
   593     if (!alloc (size))
   716     if (!alloc (size))
   594       return false;
   717       return false;
   595 
   718 
       
   719     if (size > len)
       
   720       memset (arrayZ + len, 0, (size - len) * sizeof (*arrayZ));
       
   721 
   596     len = size;
   722     len = size;
   597     return true;
   723     return true;
   598   }
   724   }
   599 
   725 
   600   inline void pop (void)
   726   inline void pop (void)
   601   {
   727   {
       
   728     if (!len) return;
   602     len--;
   729     len--;
   603   }
   730   }
   604 
   731 
   605   inline void remove (unsigned int i)
   732   inline void remove (unsigned int i)
   606   {
   733   {
   607      if (unlikely (i >= len))
   734      if (unlikely (i >= len))
   608        return;
   735        return;
   609      memmove (static_cast<void *> (&array[i]),
   736      memmove (static_cast<void *> (&arrayZ[i]),
   610               static_cast<void *> (&array[i + 1]),
   737               static_cast<void *> (&arrayZ[i + 1]),
   611               (len - i - 1) * sizeof (Type));
   738               (len - i - 1) * sizeof (Type));
   612      len--;
   739      len--;
   613   }
   740   }
   614 
   741 
   615   inline void shrink (unsigned int l)
   742   inline void shrink (int size_)
   616   {
   743   {
   617      if (l < len)
   744     unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
   618        len = l;
   745      if (size < len)
       
   746        len = size;
   619   }
   747   }
   620 
   748 
   621   template <typename T>
   749   template <typename T>
   622   inline Type *find (T v) {
   750   inline Type *find (T v) {
   623     for (unsigned int i = 0; i < len; i++)
   751     for (unsigned int i = 0; i < len; i++)
   624       if (array[i] == v)
   752       if (arrayZ[i] == v)
   625         return &array[i];
   753         return &arrayZ[i];
   626     return nullptr;
   754     return nullptr;
   627   }
   755   }
   628   template <typename T>
   756   template <typename T>
   629   inline const Type *find (T v) const {
   757   inline const Type *find (T v) const {
   630     for (unsigned int i = 0; i < len; i++)
   758     for (unsigned int i = 0; i < len; i++)
   631       if (array[i] == v)
   759       if (arrayZ[i] == v)
   632         return &array[i];
   760         return &arrayZ[i];
   633     return nullptr;
   761     return nullptr;
   634   }
   762   }
   635 
   763 
   636   inline void qsort (int (*cmp)(const void*, const void*))
   764   inline void qsort (int (*cmp)(const void*, const void*))
   637   {
   765   {
   638     ::qsort (array, len, sizeof (Type), cmp);
   766     ::qsort (arrayZ, len, sizeof (Type), cmp);
   639   }
   767   }
   640 
   768 
   641   inline void qsort (void)
   769   inline void qsort (void)
   642   {
   770   {
   643     ::qsort (array, len, sizeof (Type), Type::cmp);
   771     ::qsort (arrayZ, len, sizeof (Type), Type::cmp);
   644   }
   772   }
   645 
   773 
   646   inline void qsort (unsigned int start, unsigned int end)
   774   inline void qsort (unsigned int start, unsigned int end)
   647   {
   775   {
   648     ::qsort (array + start, end - start, sizeof (Type), Type::cmp);
   776     ::qsort (arrayZ + start, end - start, sizeof (Type), Type::cmp);
   649   }
   777   }
   650 
   778 
   651   template <typename T>
   779   template <typename T>
   652   inline Type *lsearch (const T &x)
   780   inline Type *lsearch (const T &x)
   653   {
   781   {
   654     for (unsigned int i = 0; i < len; i++)
   782     for (unsigned int i = 0; i < len; i++)
   655       if (0 == this->array[i].cmp (&x))
   783       if (0 == this->arrayZ[i].cmp (&x))
   656         return &array[i];
   784         return &arrayZ[i];
   657     return nullptr;
   785     return nullptr;
   658   }
   786   }
   659 
   787 
   660   template <typename T>
   788   template <typename T>
   661   inline Type *bsearch (const T &x)
   789   inline Type *bsearch (const T &x)
   662   {
   790   {
   663     unsigned int i;
   791     unsigned int i;
   664     return bfind (x, &i) ? &array[i] : nullptr;
   792     return bfind (x, &i) ? &arrayZ[i] : nullptr;
   665   }
   793   }
   666   template <typename T>
   794   template <typename T>
   667   inline const Type *bsearch (const T &x) const
   795   inline const Type *bsearch (const T &x) const
   668   {
   796   {
   669     unsigned int i;
   797     unsigned int i;
   670     return bfind (x, &i) ? &array[i] : nullptr;
   798     return bfind (x, &i) ? &arrayZ[i] : nullptr;
   671   }
   799   }
   672   template <typename T>
   800   template <typename T>
   673   inline bool bfind (const T &x, unsigned int *i) const
   801   inline bool bfind (const T &x, unsigned int *i) const
   674   {
   802   {
   675     int min = 0, max = (int) this->len - 1;
   803     int min = 0, max = (int) this->len - 1;
   676     while (min <= max)
   804     while (min <= max)
   677     {
   805     {
   678       int mid = (min + max) / 2;
   806       int mid = (min + max) / 2;
   679       int c = this->array[mid].cmp (&x);
   807       int c = this->arrayZ[mid].cmp (&x);
   680       if (c < 0)
   808       if (c < 0)
   681         max = mid - 1;
   809         max = mid - 1;
   682       else if (c > 0)
   810       else if (c > 0)
   683         min = mid + 1;
   811         min = mid + 1;
   684       else
   812       else
   685       {
   813       {
   686         *i = mid;
   814         *i = mid;
   687         return true;
   815         return true;
   688       }
   816       }
   689     }
   817     }
   690     if (max < 0 || (max < (int) this->len && this->array[max].cmp (&x) > 0))
   818     if (max < 0 || (max < (int) this->len && this->arrayZ[max].cmp (&x) > 0))
   691       max++;
   819       max++;
   692     *i = max;
   820     *i = max;
   693     return false;
   821     return false;
   694   }
   822   }
   695 
   823 
   696   inline void finish (void)
   824   inline void fini (void)
   697   {
   825   {
   698     if (array != static_array)
   826     if (arrayZ != static_array)
   699       free (array);
   827       free (arrayZ);
   700     array = nullptr;
   828     arrayZ = nullptr;
   701     allocated = len = 0;
   829     allocated = len = 0;
   702   }
   830   }
   703 };
   831 };
   704 
   832 
   705 template <typename Type>
   833 template <typename Type>
   706 struct hb_auto_array_t : hb_prealloced_array_t <Type>
   834 struct hb_auto_t : Type
   707 {
   835 {
   708   hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
   836   hb_auto_t (void) { Type::init (); }
   709   ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
   837   ~hb_auto_t (void) { Type::fini (); }
       
   838   private: /* Hide */
       
   839   void init (void) {}
       
   840   void fini (void) {}
   710 };
   841 };
       
   842 template <typename Type>
       
   843 struct hb_auto_array_t : hb_auto_t <hb_vector_t <Type> > {};
   711 
   844 
   712 
   845 
   713 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
   846 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
   714 template <typename item_t, typename lock_t>
   847 template <typename item_t, typename lock_t>
   715 struct hb_lockable_set_t
   848 struct hb_lockable_set_t
   716 {
   849 {
   717   hb_prealloced_array_t <item_t, 1> items;
   850   hb_vector_t <item_t, 1> items;
   718 
   851 
   719   inline void init (void) { items.init (); }
   852   inline void init (void) { items.init (); }
   720 
   853 
   721   template <typename T>
   854   template <typename T>
   722   inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
   855   inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
   726     if (item) {
   859     if (item) {
   727       if (replace) {
   860       if (replace) {
   728         item_t old = *item;
   861         item_t old = *item;
   729         *item = v;
   862         *item = v;
   730         l.unlock ();
   863         l.unlock ();
   731         old.finish ();
   864         old.fini ();
   732       }
   865       }
   733       else {
   866       else {
   734         item = nullptr;
   867         item = nullptr;
   735         l.unlock ();
   868         l.unlock ();
   736       }
   869       }
   737     } else {
   870     } else {
   738       item = items.push ();
   871       item = items.push (v);
   739       if (likely (item))
       
   740         *item = v;
       
   741       l.unlock ();
   872       l.unlock ();
   742     }
   873     }
   743     return item;
   874     return item;
   744   }
   875   }
   745 
   876 
   751     if (item) {
   882     if (item) {
   752       item_t old = *item;
   883       item_t old = *item;
   753       *item = items[items.len - 1];
   884       *item = items[items.len - 1];
   754       items.pop ();
   885       items.pop ();
   755       l.unlock ();
   886       l.unlock ();
   756       old.finish ();
   887       old.fini ();
   757     } else {
   888     } else {
   758       l.unlock ();
   889       l.unlock ();
   759     }
   890     }
   760   }
   891   }
   761 
   892 
   774   inline item_t *find_or_insert (T v, lock_t &l)
   905   inline item_t *find_or_insert (T v, lock_t &l)
   775   {
   906   {
   776     l.lock ();
   907     l.lock ();
   777     item_t *item = items.find (v);
   908     item_t *item = items.find (v);
   778     if (!item) {
   909     if (!item) {
   779       item = items.push ();
   910       item = items.push (v);
   780       if (likely (item))
       
   781         *item = v;
       
   782     }
   911     }
   783     l.unlock ();
   912     l.unlock ();
   784     return item;
   913     return item;
   785   }
   914   }
   786 
   915 
   787   inline void finish (lock_t &l)
   916   inline void fini (lock_t &l)
   788   {
   917   {
   789     if (!items.len) {
   918     if (!items.len) {
   790       /* No need for locking. */
   919       /* No need for locking. */
   791       items.finish ();
   920       items.fini ();
   792       return;
   921       return;
   793     }
   922     }
   794     l.lock ();
   923     l.lock ();
   795     while (items.len) {
   924     while (items.len) {
   796       item_t old = items[items.len - 1];
   925       item_t old = items[items.len - 1];
   797         items.pop ();
   926         items.pop ();
   798         l.unlock ();
   927         l.unlock ();
   799         old.finish ();
   928         old.fini ();
   800         l.lock ();
   929         l.lock ();
   801     }
   930     }
   802     items.finish ();
   931     items.fini ();
   803     l.unlock ();
   932     l.unlock ();
   804   }
   933   }
   805 
   934 
   806 };
   935 };
   807 
   936 
   824  * should be disabled in production systems.  If NDEBUG is defined, enable
   953  * should be disabled in production systems.  If NDEBUG is defined, enable
   825  * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
   954  * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
   826  * light-weight) to be enabled, then HB_DEBUG can be defined to disable
   955  * light-weight) to be enabled, then HB_DEBUG can be defined to disable
   827  * the costlier checks. */
   956  * the costlier checks. */
   828 #ifdef NDEBUG
   957 #ifdef NDEBUG
   829 #define HB_NDEBUG
   958 #define HB_NDEBUG 1
   830 #endif
   959 #endif
   831 
   960 
   832 
   961 
   833 /* Misc */
   962 /* Misc */
   834 
   963 
   973   static const bool passthru_left = true;
  1102   static const bool passthru_left = true;
   974   static const bool passthru_right = true;
  1103   static const bool passthru_right = true;
   975   template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; }
  1104   template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; }
   976 };
  1105 };
   977 
  1106 
       
  1107 
       
  1108 /* Compiler-assisted vectorization. */
       
  1109 
       
  1110 /* The `vector_size' attribute was introduced in gcc 3.1. */
       
  1111 #if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
       
  1112 #define HB_VECTOR_SIZE 128
       
  1113 #elif !defined(HB_VECTOR_SIZE)
       
  1114 #define HB_VECTOR_SIZE 0
       
  1115 #endif
       
  1116 
   978 /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */
  1117 /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */
   979 template <typename elt_t, unsigned int byte_size>
  1118 template <typename elt_t, unsigned int byte_size>
   980 struct hb_vector_size_t
  1119 struct hb_vector_size_t
   981 {
  1120 {
   982   elt_t& operator [] (unsigned int i) { return v[i]; }
  1121   elt_t& operator [] (unsigned int i) { return u.v[i]; }
   983   const elt_t& operator [] (unsigned int i) const { return v[i]; }
  1122   const elt_t& operator [] (unsigned int i) const { return u.v[i]; }
   984 
  1123 
   985   template <class Op>
  1124   template <class Op>
   986   inline hb_vector_size_t process (const hb_vector_size_t &o) const
  1125   inline hb_vector_size_t process (const hb_vector_size_t &o) const
   987   {
  1126   {
   988     hb_vector_size_t r;
  1127     hb_vector_size_t r;
   989     for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
  1128 #if HB_VECTOR_SIZE
   990       Op::process (r.v[i], v[i], o.v[i]);
  1129     if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE)
       
  1130       for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++)
       
  1131         Op::process (r.u.vec[i], u.vec[i], o.u.vec[i]);
       
  1132     else
       
  1133 #endif
       
  1134       for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++)
       
  1135         Op::process (r.u.v[i], u.v[i], o.u.v[i]);
   991     return r;
  1136     return r;
   992   }
  1137   }
   993   inline hb_vector_size_t operator | (const hb_vector_size_t &o) const
  1138   inline hb_vector_size_t operator | (const hb_vector_size_t &o) const
   994   { return process<HbOpOr> (o); }
  1139   { return process<HbOpOr> (o); }
   995   inline hb_vector_size_t operator & (const hb_vector_size_t &o) const
  1140   inline hb_vector_size_t operator & (const hb_vector_size_t &o) const
   997   inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const
  1142   inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const
   998   { return process<HbOpXor> (o); }
  1143   { return process<HbOpXor> (o); }
   999   inline hb_vector_size_t operator ~ () const
  1144   inline hb_vector_size_t operator ~ () const
  1000   {
  1145   {
  1001     hb_vector_size_t r;
  1146     hb_vector_size_t r;
  1002     for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
  1147 #if HB_VECTOR_SIZE && 0
  1003       r.v[i] = ~v[i];
  1148     if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE)
       
  1149       for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++)
       
  1150         r.u.vec[i] = ~u.vec[i];
       
  1151     else
       
  1152 #endif
       
  1153     for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++)
       
  1154       r.u.v[i] = ~u.v[i];
  1004     return r;
  1155     return r;
  1005   }
  1156   }
  1006 
  1157 
  1007   private:
  1158   private:
  1008   static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, "");
  1159   static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, "");
  1009   elt_t v[byte_size / sizeof (elt_t)];
  1160   union {
       
  1161     elt_t v[byte_size / sizeof (elt_t)];
       
  1162 #if HB_VECTOR_SIZE
       
  1163     typedef unsigned long vec_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
       
  1164     vec_t vec[byte_size / sizeof (vec_t)];
       
  1165 #endif
       
  1166   } u;
  1010 };
  1167 };
  1011 
       
  1012 /* The `vector_size' attribute was introduced in gcc 3.1. */
       
  1013 #if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
       
  1014 #define HAVE_VECTOR_SIZE 1
       
  1015 #endif
       
  1016 
  1168 
  1017 
  1169 
  1018 /* Global runtime options. */
  1170 /* Global runtime options. */
  1019 
  1171 
  1020 struct hb_options_t
  1172 struct hb_options_t
  1047 #define VAR 1
  1199 #define VAR 1
  1048 
  1200 
  1049 
  1201 
  1050 /* String type. */
  1202 /* String type. */
  1051 
  1203 
  1052 struct hb_string_t
  1204 struct hb_bytes_t
  1053 {
  1205 {
  1054   inline hb_string_t (void) : bytes (nullptr), len (0) {}
  1206   inline hb_bytes_t (void) : bytes (nullptr), len (0) {}
  1055   inline hb_string_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
  1207   inline hb_bytes_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
  1056 
  1208 
  1057   inline int cmp (const hb_string_t &a) const
  1209   inline int cmp (const hb_bytes_t &a) const
  1058   {
  1210   {
  1059     if (len != a.len)
  1211     if (len != a.len)
  1060       return (int) a.len - (int) len;
  1212       return (int) a.len - (int) len;
  1061 
  1213 
  1062     return memcmp (a.bytes, bytes, len);
  1214     return memcmp (a.bytes, bytes, len);
  1063   }
  1215   }
  1064   static inline int cmp (const void *pa, const void *pb)
  1216   static inline int cmp (const void *pa, const void *pb)
  1065   {
  1217   {
  1066     hb_string_t *a = (hb_string_t *) pa;
  1218     hb_bytes_t *a = (hb_bytes_t *) pa;
  1067     hb_string_t *b = (hb_string_t *) pb;
  1219     hb_bytes_t *b = (hb_bytes_t *) pb;
  1068     return b->cmp (*a);
  1220     return b->cmp (*a);
  1069   }
  1221   }
  1070 
  1222 
  1071   const char *bytes;
  1223   const char *bytes;
  1072   unsigned int len;
  1224   unsigned int len;
  1073 };
  1225 };
  1074 
  1226 
  1075 
  1227 
       
  1228 /* fallback for round() */
       
  1229 #if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
       
  1230 static inline double
       
  1231 round (double x)
       
  1232 {
       
  1233   if (x >= 0)
       
  1234     return floor (x + 0.5);
       
  1235   else
       
  1236     return ceil (x - 0.5);
       
  1237 }
       
  1238 #endif
       
  1239 
       
  1240 
  1076 #endif /* HB_PRIVATE_HH */
  1241 #endif /* HB_PRIVATE_HH */