+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
*
-* Copyright (C) 1997-2012, International Business Machines
+* Copyright (C) 1997-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
typedef size_t uintptr_t;
#endif
-/**
- * \def U_HAVE_MSVC_2003_OR_EARLIER
- * Flag for workaround of MSVC 2003 optimization bugs
- * @internal
- */
-#if !defined(U_HAVE_MSVC_2003_OR_EARLIER) && defined(_MSC_VER) && (_MSC_VER < 1400)
-#define U_HAVE_MSVC_2003_OR_EARLIER
-#endif
-
/*===========================================================================*/
/** @{ Information about POSIX support */
/*===========================================================================*/
#ifdef U_HAVE_NL_LANGINFO_CODESET
/* Use the predefined value. */
-#elif U_PLATFORM_HAS_WIN32_API
+#elif U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
# define U_HAVE_NL_LANGINFO_CODESET 0
#else
# define U_HAVE_NL_LANGINFO_CODESET 1
# define U_NL_LANGINFO_CODESET CODESET
#endif
-#ifdef U_TZSET
+#if defined(U_TZSET) || defined(U_HAVE_TZSET)
/* Use the predefined value. */
#elif U_PLATFORM_USES_ONLY_WIN32_API
+ // UWP doesn't support tzset or environment variables for tz
+#if U_PLATFORM_HAS_WINUWP_API == 0
# define U_TZSET _tzset
+#endif
#elif U_PLATFORM == U_PF_OS400
/* not defined */
#else
# define U_TZSET tzset
#endif
-#ifdef U_TIMEZONE
+#if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
/* Use the predefined value. */
#elif U_PLATFORM == U_PF_ANDROID
# define U_TIMEZONE timezone
+#elif defined(__UCLIBC__)
+ // uClibc does not have __timezone or _timezone.
+#elif defined(_NEWLIB_VERSION)
+# define U_TIMEZONE _timezone
+#elif defined(__GLIBC__)
+ // glibc
+# define U_TIMEZONE __timezone
#elif U_PLATFORM_IS_LINUX_BASED
-# if !defined(__UCLIBC__)
- /* __timezone is only available in glibc */
-# define U_TIMEZONE __timezone
-# endif
+ // not defined
#elif U_PLATFORM_USES_ONLY_WIN32_API
# define U_TIMEZONE _timezone
#elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
/* not defined */
#elif U_PLATFORM == U_PF_OS400
/* not defined */
+#elif U_PLATFORM == U_PF_IPHONE
+ /* not defined */
#else
# define U_TIMEZONE timezone
#endif
-#ifdef U_TZNAME
+#if defined(U_TZNAME) || defined(U_HAVE_TZNAME)
/* Use the predefined value. */
#elif U_PLATFORM_USES_ONLY_WIN32_API
+ /* not usable on all windows platforms */
+#if U_PLATFORM_HAS_WINUWP_API == 0
# define U_TZNAME _tzname
+#endif
#elif U_PLATFORM == U_PF_OS400
/* not defined */
#else
#ifdef U_HAVE_MMAP
/* Use the predefined value. */
-#elif U_PLATFORM_HAS_WIN32_API
+#elif U_PLATFORM_USES_ONLY_WIN32_API
# define U_HAVE_MMAP 0
#else
# define U_HAVE_MMAP 1
*/
#ifdef U_HAVE_DIRENT_H
/* Use the predefined value. */
-#elif U_PLATFORM_HAS_WIN32_API
+#elif U_PLATFORM_USES_ONLY_WIN32_API
# define U_HAVE_DIRENT_H 0
#else
# define U_HAVE_DIRENT_H 1
*/
#ifdef U_HAVE_GCC_ATOMICS
/* Use the predefined value. */
-#elif U_GCC_MAJOR_MINOR >= 404
+#elif U_PLATFORM == U_PF_MINGW
+ #define U_HAVE_GCC_ATOMICS 0
+#elif U_GCC_MAJOR_MINOR >= 404 || defined(__clang__)
+ /* TODO: Intel icc and IBM xlc on AIX also support gcc atomics. (Intel originated them.)
+ * Add them for these compilers.
+ * Note: Clang sets __GNUC__ defines for version 4.2, so misses the 4.4 test here.
+ */
# define U_HAVE_GCC_ATOMICS 1
#else
# define U_HAVE_GCC_ATOMICS 0
/** @} */
-/*===========================================================================*/
-/** @{ Code alignment */
-/*===========================================================================*/
-
/**
- * \def U_ALIGN_CODE
- * This is used to align code fragments to a specific byte boundary.
- * This is useful for getting consistent performance test results.
+ * \def U_HAVE_STD_ATOMICS
+ * Defines whether to use the C++11 std::atomic functions.
+ * If false, ICU will fall back to compiler or platform specific alternatives.
+ * Note: support for these fall back options for atomics will be removed in a future version
+ * of ICU, and the use of C++ 11 atomics will be required.
* @internal
*/
-#ifdef U_ALIGN_CODE
+#ifdef U_HAVE_STD_ATOMICS
/* Use the predefined value. */
-#elif defined(_MSC_VER) && defined(_M_IX86) && !defined(_MANAGED)
-# define U_ALIGN_CODE(boundarySize) __asm align boundarySize
#else
-# define U_ALIGN_CODE(boundarySize)
+# define U_HAVE_STD_ATOMICS 1
#endif
-/** @} */
+/**
+ * \def U_HAVE_CLANG_ATOMICS
+ * Defines whether Clang c11 style built-in atomics are available.
+ * These are used in preference to gcc atomics when both are available.
+ */
+#ifdef U_HAVE_CLANG_ATOMICS
+ /* Use the predefined value. */
+#elif __has_builtin(__c11_atomic_load) && \
+ __has_builtin(__c11_atomic_store) && \
+ __has_builtin(__c11_atomic_fetch_add) && \
+ __has_builtin(__c11_atomic_fetch_sub)
+# define U_HAVE_CLANG_ATOMICS 1
+#else
+# define U_HAVE_CLANG_ATOMICS 0
+#endif
+
+
+/**
+ * \def U_HAVE_STD_MUTEX
+ * Defines whether to use the C++11 std::mutex functions.
+ * If false, ICU will fall back to compiler or platform specific alternatives.
+ * std::mutex is preferred, and used by default unless this setting is overridden.
+ * Note: support for other options for mutexes will be removed in a future version
+ * of ICU, and the use of std::mutex will be required.
+ * @internal
+ */
+#ifdef U_HAVE_STD_MUTEX
+ /* Use the predefined value. */
+#else
+# define U_HAVE_STD_MUTEX 1
+#endif
/*===========================================================================*/
/** @{ Programs used by ICU code */
/**
* Platform utilities isolates the platform dependencies of the
- * libarary. For each platform which this code is ported to, these
+ * library. For each platform which this code is ported to, these
* functions may have to be re-implemented.
*/
*/
U_INTERNAL double U_EXPORT2 uprv_round(double x);
+/**
+ * Adds the signed integers a and b, storing the result in res.
+ * Checks for signed integer overflow.
+ * Similar to the GCC/Clang extension __builtin_add_overflow
+ *
+ * @param a The first operand.
+ * @param b The second operand.
+ * @param res a + b
+ * @return true if overflow occurred; false if no overflow occurred.
+ * @internal
+ */
+U_INTERNAL UBool U_EXPORT2 uprv_add32_overflow(int32_t a, int32_t b, int32_t* res);
+
+/**
+ * Multiplies the signed integers a and b, storing the result in res.
+ * Checks for signed integer overflow.
+ * Similar to the GCC/Clang extension __builtin_mul_overflow
+ *
+ * @param a The first multiplicand.
+ * @param b The second multiplicand.
+ * @param res a * b
+ * @return true if overflow occurred; false if no overflow occurred.
+ * @internal
+ */
+U_INTERNAL UBool U_EXPORT2 uprv_mul32_overflow(int32_t a, int32_t b, int32_t* res);
+
#if 0
/**
* Returns the number of digits after the decimal point in a double number x.
/**
* Please use uloc_getDefault() instead.
- * Return the default locale ID string by querying ths system, or
+ * Return the default locale ID string by querying the system, or
* zero if one cannot be found.
* This function can call setlocale() on Unix platforms. Please read the
* platform documentation on setlocale() before calling this function.
*/
U_INTERNAL const char* U_EXPORT2 uprv_tzname(int n);
+/**
+ * Reset the global tzname cache.
+ * @internal
+ */
+U_INTERNAL void uprv_tzname_clear_cache();
+
/**
* Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
* This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
# endif
#endif
+
+#ifdef __cplusplus
+/**
+ * Pin a buffer capacity such that doing pointer arithmetic
+ * on the destination pointer and capacity cannot overflow.
+ *
+ * The pinned capacity must fulfill the following conditions (for positive capacities):
+ * - dest + capacity is a valid pointer according to the machine arcitecture (AS/400, 64-bit, etc.)
+ * - (dest + capacity) >= dest
+ * - The size (in bytes) of T[capacity] does not exceed 0x7fffffff
+ *
+ * @param dest the destination buffer pointer.
+ * @param capacity the requested buffer capacity, in units of type T.
+ * @return the pinned capacity.
+ * @internal
+ */
+template <typename T>
+inline int32_t pinCapacity(T *dest, int32_t capacity) {
+ if (capacity <= 0) { return capacity; }
+
+ uintptr_t destInt = (uintptr_t)dest;
+ uintptr_t maxInt;
+
+# if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
+ // We have 31-bit pointers.
+ maxInt = 0x7fffffff;
+# elif U_PLATFORM == U_PF_OS400
+ maxInt = (uintptr_t)uprv_maximumPtr((void *)dest);
+# else
+ maxInt = destInt + 0x7fffffffu;
+ if (maxInt < destInt) {
+ // Less than 2GB to the end of the address space.
+ // Pin to that to prevent address overflow.
+ maxInt = (uintptr_t)-1;
+ }
+# endif
+
+ uintptr_t maxBytes = maxInt - destInt; // max. 2GB
+ int32_t maxCapacity = (int32_t)(maxBytes / sizeof(T));
+ return capacity <= maxCapacity ? capacity : maxCapacity;
+}
+#endif // __cplusplus
+
/* Dynamic Library Functions */
typedef void (UVoidFunction)(void);