2 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #ifndef PLATFORM_H_MODULE
12 #define PLATFORM_H_MODULE
14 #if defined (__cplusplus)
20 /* **************************************
22 ****************************************/
24 # define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
25 # if (_MSC_VER <= 1800) /* 1800 == Visual Studio 2013 */
26 # define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
27 # define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */
32 /* **************************************
34 * http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
35 ****************************************/
36 #if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \
37 || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \
38 || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \
39 || defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \
40 || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \
41 || (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \
42 || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \
43 || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
44 # if !defined(__64BIT__)
50 /* *********************************************************
51 * Turn on Large Files support (>4GB) for 32-bit Linux/Unix
52 ***********************************************************/
53 #if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */
54 # if !defined(_FILE_OFFSET_BITS)
55 # define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */
57 # if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */
58 # define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */
60 # if defined(_AIX) || defined(__hpux)
61 # define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */
66 /* ************************************************************
67 * Detect POSIX version
68 * PLATFORM_POSIX_VERSION = 0 for non-Unix e.g. Windows
69 * PLATFORM_POSIX_VERSION = 1 for Unix-like but non-POSIX
70 * PLATFORM_POSIX_VERSION > 1 is equal to found _POSIX_VERSION
71 * Value of PLATFORM_POSIX_VERSION can be forced on command line
72 ***************************************************************/
73 #ifndef PLATFORM_POSIX_VERSION
75 # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \
76 || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
77 /* exception rule : force posix version to 200112L,
78 * note: it's better to use unistd.h's _POSIX_VERSION whenever possible */
79 # define PLATFORM_POSIX_VERSION 200112L
81 /* try to determine posix version through official unistd.h's _POSIX_VERSION (http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html).
82 * note : there is no simple way to know in advance if <unistd.h> is present or not on target system,
83 * Posix specification mandates its presence and its content, but target system must respect this spec.
84 * It's necessary to _not_ #include <unistd.h> whenever target OS is not unix-like
85 * otherwise it will block preprocessing stage.
86 * The following list of build macros tries to "guess" if target OS is likely unix-like, and therefore can #include <unistd.h>
88 # elif !defined(_WIN32) \
89 && (defined(__unix__) || defined(__unix) \
90 || defined(__midipix__) || defined(__VMS) || defined(__HAIKU__))
92 # if defined(__linux__) || defined(__linux)
93 # ifndef _POSIX_C_SOURCE
94 # define _POSIX_C_SOURCE 200112L /* feature test macro : https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html */
97 # include <unistd.h> /* declares _POSIX_VERSION */
98 # if defined(_POSIX_VERSION) /* POSIX compliant */
99 # define PLATFORM_POSIX_VERSION _POSIX_VERSION
101 # define PLATFORM_POSIX_VERSION 1
104 # else /* non-unix target platform (like Windows) */
105 # define PLATFORM_POSIX_VERSION 0
108 #endif /* PLATFORM_POSIX_VERSION */
110 /*-*********************************************
111 * Detect if isatty() and fileno() are available
112 ************************************************/
113 #if (defined(__linux__) && (PLATFORM_POSIX_VERSION > 1)) \
114 || (PLATFORM_POSIX_VERSION >= 200112L) \
115 || defined(__DJGPP__) \
117 # include <unistd.h> /* isatty */
118 # define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
119 #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
120 # include <io.h> /* _isatty */
121 # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
122 #elif defined(WIN32) || defined(_WIN32)
123 # include <io.h> /* _isatty */
124 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
125 # include <stdio.h> /* FILE */
126 static __inline int IS_CONSOLE(FILE* stdStream) {
128 return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
131 # define IS_CONSOLE(stdStream) 0
135 /******************************
136 * OS-specific IO behaviors
137 ******************************/
138 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
139 # include <fcntl.h> /* _O_BINARY */
140 # include <io.h> /* _setmode, _fileno, _get_osfhandle */
141 # if !defined(__DJGPP__)
142 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
143 # include <winioctl.h> /* FSCTL_SET_SPARSE */
144 # define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
145 # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
147 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
148 # define SET_SPARSE_FILE_MODE(file)
151 # define SET_BINARY_MODE(file)
152 # define SET_SPARSE_FILE_MODE(file)
156 #ifndef ZSTD_SPARSE_DEFAULT
157 # if (defined(__APPLE__) && defined(__MACH__))
158 # define ZSTD_SPARSE_DEFAULT 0
160 # define ZSTD_SPARSE_DEFAULT 1
165 #ifndef ZSTD_START_SYMBOLLIST_FRAME
167 # define ZSTD_START_SYMBOLLIST_FRAME 2
168 # elif defined __APPLE__
169 # define ZSTD_START_SYMBOLLIST_FRAME 4
171 # define ZSTD_START_SYMBOLLIST_FRAME 0
176 #ifndef ZSTD_SETPRIORITY_SUPPORT
177 /* mandates presence of <sys/resource.h> and support for setpriority() : http://man7.org/linux/man-pages/man2/setpriority.2.html */
178 # define ZSTD_SETPRIORITY_SUPPORT (PLATFORM_POSIX_VERSION >= 200112L)
182 #ifndef ZSTD_NANOSLEEP_SUPPORT
183 /* mandates support of nanosleep() within <time.h> : http://man7.org/linux/man-pages/man2/nanosleep.2.html */
184 # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) \
185 || (PLATFORM_POSIX_VERSION >= 200112L)
186 # define ZSTD_NANOSLEEP_SUPPORT 1
188 # define ZSTD_NANOSLEEP_SUPPORT 0
193 #if defined (__cplusplus)
197 #endif /* PLATFORM_H_MODULE */