1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#ifndef _SYS__WCHAR_LIMITS_H
29#define _SYS__WCHAR_LIMITS_H
30
31#include <android/api-level.h>
32
33/* WCHAR_MIN / WCHAR_MAX can be defined by <stdint.h> or <wchar.h>.
34 * Due to historical reasons, their definition is a bit complex.
35 *
36 * - In NDK r8e and older, all definitions of WCHAR_MIN and WCHAR_MAX
37 *   where 32-bit signed values (with one exception described below),
38 *   despite the fact that wchar_t is 'unsigned' on ARM.
39 *   See http://b.android.com/57749
40 *
41 *   This is no longer the case, unless you define _WCHAR_IS_ALWAYS_SIGNED
42 *   at compile time to restore the old (broken) behaviour. This doesn't
43 *   affect other CPU ABIs.
44 *
45 * - Before API level 9, on ARM, wchar_t was typedef to 'char' when
46 *   compiling C (not C++). Also, the definitions of WCHAR_MIN and
47 *   WCHAR_MAX differed between <stdint.h> and <wchar.h>:
48 *
49 *     <stdint.h> conditionally defined them to INT32_MIN / INT32_MAX.
50 *     <wchar.h> conditionally defined them to 0 and 255 instead.
51 *
52 *   <stdint.h> would only define WCHAR_MIN and WCHAR_MAX when:
53 *    - Compiling C sources.
54 *    - Compiling C++ sources with __STDC_LIMIT_MACROS being defined.
55 *
56 *   <wchar.h> always ends up including <stdint.h> indirectly. This
57 *   means that:
58 *
59 *     - When compiling C sources, WCHAR_MIN / WCHAR_MAX were always
60 *       defined as INT32_MIN / INT32_MAX.
61 *
62 *     - When compiling C++ sources with __STDC_LIMIT_MACROS defined,
63 *       they were always defined to INT32_MIN / INT32_MAX
64 *
65 *     - When compiling C++ sources without __STDC_LIMIT_MACROS defined,
66 *       they were defined by <wchar.h> as 0 and 255, respectively.
67 *
68 *    Keep in mind that this was ARM-specific, only for API level < 9.
69 *
70 *    If _WCHAR_IS_8BIT is defined, the same broken behaviour will
71 *    be restored. See http://b.android.com/57267
72 */
73#if !defined(WCHAR_MIN)
74
75#  if defined(_WCHAR_IS_8BIT) && defined(__arm__) && __ANDROID_API__ < 9
76#    if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS)
77#      define WCHAR_MIN  0
78#      define WCHAR_MAX  255
79#    else
80#      define WCHAR_MIN   (-2147483647 - 1)
81#      define WCHAR_MAX   (2147483647)
82#    endif
83#  elif defined(_WCHAR_IS_ALWAYS_SIGNED)
84#    define WCHAR_MIN   (-2147483647 - 1)
85#    define WCHAR_MAX   (2147483647)
86#  else
87  /* Otherwise, the value is derived from the toolchain configuration.
88   * to avoid putting explicit CPU checks in this header. */
89#    ifndef __WCHAR_MAX__
90#      error "__WCHAR_MAX__ is not defined. Check your toolchain!"
91#    endif
92  /* Clang does define __WCHAR_MAX__, but not __WCHAR_MIN__ */
93#    ifndef __WCHAR_MIN__
94#      if __WCHAR_MAX__ == 4294967295
95#        define __WCHAR_MIN__  (0U)
96#      elif __WCHAR_MAX__ == 2147483647
97#        define __WCHAR_MIN__  (-2147483647 - 1)
98#      else
99#        error "Invalid __WCHAR_MAX__ value. Check your toolchain!"
100#      endif
101#    endif /* !__WCHAR_MIN__ */
102#    define WCHAR_MIN    __WCHAR_MIN__
103#    define WCHAR_MAX    __WCHAR_MAX__
104#  endif /* !_WCHAR_IS_ALWAYS_SIGNED */
105
106#endif /* !WCHAR_MIN */
107
108#endif  /* _SYS__WCHAR_LIMITS_H */
109