2017-08-18 23:52:05 +00:00
|
|
|
/*
|
2017-08-21 18:24:32 +00:00
|
|
|
* Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
|
2016-12-21 14:08:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 23:52:05 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-12-21 14:08:44 +00:00
|
|
|
*/
|
2016-12-16 13:13:15 +00:00
|
|
|
|
|
|
|
#ifndef PLATFORM_H_MODULE
|
|
|
|
#define PLATFORM_H_MODULE
|
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-12-21 08:04:59 +00:00
|
|
|
|
2016-12-21 14:08:44 +00:00
|
|
|
|
|
|
|
/* **************************************
|
|
|
|
* Compiler Options
|
|
|
|
****************************************/
|
|
|
|
#if defined(_MSC_VER)
|
2017-11-25 00:44:28 +00:00
|
|
|
# define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
|
|
|
|
# if (_MSC_VER <= 1800) /* 1800 == Visual Studio 2013 */
|
|
|
|
# define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
|
|
|
|
# define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */
|
2016-12-21 14:08:44 +00:00
|
|
|
# endif
|
2018-12-25 23:04:49 +00:00
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
2016-12-21 14:08:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-12-16 13:24:01 +00:00
|
|
|
/* **************************************
|
2016-12-21 08:04:59 +00:00
|
|
|
* Detect 64-bit OS
|
|
|
|
* http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
|
2016-12-16 13:24:01 +00:00
|
|
|
****************************************/
|
2016-12-21 08:04:59 +00:00
|
|
|
#if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \
|
|
|
|
|| defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \
|
|
|
|
|| (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \
|
|
|
|
|| defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \
|
|
|
|
|| defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \
|
|
|
|
|| (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \
|
|
|
|
|| defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \
|
|
|
|
|| (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
|
|
|
|
# if !defined(__64BIT__)
|
|
|
|
# define __64BIT__ 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************************************************
|
|
|
|
* Turn on Large Files support (>4GB) for 32-bit Linux/Unix
|
|
|
|
***********************************************************/
|
2018-10-03 22:34:41 +00:00
|
|
|
#if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */
|
2017-02-13 12:57:29 +00:00
|
|
|
# if !defined(_FILE_OFFSET_BITS)
|
2018-10-03 22:34:41 +00:00
|
|
|
# define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */
|
2016-12-21 08:04:59 +00:00
|
|
|
# endif
|
2018-10-03 22:34:41 +00:00
|
|
|
# if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */
|
|
|
|
# define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */
|
2016-12-21 08:04:59 +00:00
|
|
|
# endif
|
|
|
|
# if defined(_AIX) || defined(__hpux)
|
2018-10-03 22:34:41 +00:00
|
|
|
# define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */
|
2016-12-16 13:13:15 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-12-16 14:00:50 +00:00
|
|
|
/* ************************************************************
|
2016-12-16 13:24:01 +00:00
|
|
|
* Detect POSIX version
|
2018-10-03 21:54:33 +00:00
|
|
|
* PLATFORM_POSIX_VERSION = 0 for non-Unix e.g. Windows
|
|
|
|
* PLATFORM_POSIX_VERSION = 1 for Unix-like but non-POSIX
|
|
|
|
* PLATFORM_POSIX_VERSION > 1 is equal to found _POSIX_VERSION
|
|
|
|
* Value of PLATFORM_POSIX_VERSION can be forced on command line
|
2016-12-16 14:00:50 +00:00
|
|
|
***************************************************************/
|
2018-10-03 21:54:33 +00:00
|
|
|
#ifndef PLATFORM_POSIX_VERSION
|
|
|
|
|
2018-03-13 20:02:21 +00:00
|
|
|
# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \
|
2018-10-03 21:54:33 +00:00
|
|
|
|| defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */
|
|
|
|
/* exception rule : force posix version to 200112L,
|
|
|
|
* note: it's better to use unistd.h's _POSIX_VERSION whenever possible */
|
2016-12-16 13:13:15 +00:00
|
|
|
# define PLATFORM_POSIX_VERSION 200112L
|
2018-10-03 21:54:33 +00:00
|
|
|
|
|
|
|
/* try to determine posix version through official unistd.h's _POSIX_VERSION (http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html).
|
|
|
|
* note : there is no simple way to know in advance if <unistd.h> is present or not on target system,
|
|
|
|
* Posix specification mandates its presence and its content, but target system must respect this spec.
|
|
|
|
* It's necessary to _not_ #include <unistd.h> whenever target OS is not unix-like
|
|
|
|
* otherwise it will block preprocessing stage.
|
|
|
|
* The following list of build macros tries to "guess" if target OS is likely unix-like, and therefore can #include <unistd.h>
|
|
|
|
*/
|
|
|
|
# elif !defined(_WIN32) \
|
2019-04-10 20:26:27 +00:00
|
|
|
&& ( defined(__unix__) || defined(__unix) \
|
|
|
|
|| defined(__midipix__) || defined(__VMS) || defined(__HAIKU__) )
|
2018-10-03 21:54:33 +00:00
|
|
|
|
2016-12-16 13:13:15 +00:00
|
|
|
# if defined(__linux__) || defined(__linux)
|
2017-02-09 22:20:52 +00:00
|
|
|
# ifndef _POSIX_C_SOURCE
|
2018-10-03 21:54:33 +00:00
|
|
|
# define _POSIX_C_SOURCE 200112L /* feature test macro : https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html */
|
2017-02-09 01:25:01 +00:00
|
|
|
# endif
|
2016-12-16 13:13:15 +00:00
|
|
|
# endif
|
|
|
|
# include <unistd.h> /* declares _POSIX_VERSION */
|
|
|
|
# if defined(_POSIX_VERSION) /* POSIX compliant */
|
|
|
|
# define PLATFORM_POSIX_VERSION _POSIX_VERSION
|
2016-12-16 13:24:01 +00:00
|
|
|
# else
|
2018-10-03 21:54:33 +00:00
|
|
|
# define PLATFORM_POSIX_VERSION 1
|
2016-12-16 13:13:15 +00:00
|
|
|
# endif
|
2018-10-03 21:54:33 +00:00
|
|
|
|
|
|
|
# else /* non-unix target platform (like Windows) */
|
|
|
|
# define PLATFORM_POSIX_VERSION 0
|
2016-12-16 13:13:15 +00:00
|
|
|
# endif
|
|
|
|
|
2018-10-03 21:54:33 +00:00
|
|
|
#endif /* PLATFORM_POSIX_VERSION */
|
2016-12-16 13:13:15 +00:00
|
|
|
|
2019-04-10 20:26:27 +00:00
|
|
|
|
2016-12-21 08:04:59 +00:00
|
|
|
/*-*********************************************
|
|
|
|
* Detect if isatty() and fileno() are available
|
|
|
|
************************************************/
|
2018-10-03 21:54:33 +00:00
|
|
|
#if (defined(__linux__) && (PLATFORM_POSIX_VERSION > 1)) \
|
2018-06-26 08:22:45 +00:00
|
|
|
|| (PLATFORM_POSIX_VERSION >= 200112L) \
|
|
|
|
|| defined(__DJGPP__) \
|
|
|
|
|| defined(__MSYS__)
|
2016-12-21 12:23:34 +00:00
|
|
|
# include <unistd.h> /* isatty */
|
2016-12-21 08:04:59 +00:00
|
|
|
# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
|
2017-03-27 19:19:30 +00:00
|
|
|
#elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__)
|
2016-12-21 08:04:59 +00:00
|
|
|
# include <io.h> /* _isatty */
|
|
|
|
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
|
2017-03-27 19:19:30 +00:00
|
|
|
#elif defined(WIN32) || defined(_WIN32)
|
|
|
|
# include <io.h> /* _isatty */
|
|
|
|
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
|
|
|
|
# include <stdio.h> /* FILE */
|
2018-03-20 20:40:29 +00:00
|
|
|
static __inline int IS_CONSOLE(FILE* stdStream) {
|
2017-03-27 19:19:30 +00:00
|
|
|
DWORD dummy;
|
|
|
|
return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);
|
|
|
|
}
|
2016-12-21 08:04:59 +00:00
|
|
|
#else
|
|
|
|
# define IS_CONSOLE(stdStream) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/******************************
|
2017-11-25 00:44:28 +00:00
|
|
|
* OS-specific IO behaviors
|
2016-12-21 08:04:59 +00:00
|
|
|
******************************/
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
|
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _fileno, _get_osfhandle */
|
|
|
|
# if !defined(__DJGPP__)
|
|
|
|
# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
|
|
|
|
# include <winioctl.h> /* FSCTL_SET_SPARSE */
|
2017-11-25 00:44:28 +00:00
|
|
|
# define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
|
2016-12-21 08:04:59 +00:00
|
|
|
# define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
|
|
|
|
# else
|
|
|
|
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
|
|
|
|
# define SET_SPARSE_FILE_MODE(file)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
# define SET_SPARSE_FILE_MODE(file)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-03-31 22:16:43 +00:00
|
|
|
#ifndef ZSTD_SPARSE_DEFAULT
|
|
|
|
# if (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
# define ZSTD_SPARSE_DEFAULT 0
|
|
|
|
# else
|
|
|
|
# define ZSTD_SPARSE_DEFAULT 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2016-12-16 13:24:01 +00:00
|
|
|
|
2018-09-11 18:39:49 +00:00
|
|
|
#ifndef ZSTD_START_SYMBOLLIST_FRAME
|
|
|
|
# ifdef __linux__
|
|
|
|
# define ZSTD_START_SYMBOLLIST_FRAME 2
|
|
|
|
# elif defined __APPLE__
|
|
|
|
# define ZSTD_START_SYMBOLLIST_FRAME 4
|
|
|
|
# else
|
|
|
|
# define ZSTD_START_SYMBOLLIST_FRAME 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-10-03 21:54:33 +00:00
|
|
|
#ifndef ZSTD_SETPRIORITY_SUPPORT
|
|
|
|
/* mandates presence of <sys/resource.h> and support for setpriority() : http://man7.org/linux/man-pages/man2/setpriority.2.html */
|
|
|
|
# define ZSTD_SETPRIORITY_SUPPORT (PLATFORM_POSIX_VERSION >= 200112L)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ZSTD_NANOSLEEP_SUPPORT
|
|
|
|
/* mandates support of nanosleep() within <time.h> : http://man7.org/linux/man-pages/man2/nanosleep.2.html */
|
2018-10-03 22:34:41 +00:00
|
|
|
# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) \
|
|
|
|
|| (PLATFORM_POSIX_VERSION >= 200112L)
|
|
|
|
# define ZSTD_NANOSLEEP_SUPPORT 1
|
|
|
|
# else
|
|
|
|
# define ZSTD_NANOSLEEP_SUPPORT 0
|
|
|
|
# endif
|
2018-10-03 21:54:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-12-16 13:13:15 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* PLATFORM_H_MODULE */
|