Path: blob/main/sys/contrib/zstd/programs/platform.h
48254 views
/*1* Copyright (c) Przemyslaw Skibinski, Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef PLATFORM_H_MODULE11#define PLATFORM_H_MODULE1213#if defined (__cplusplus)14extern "C" {15#endif16171819/* **************************************20* Compiler Options21****************************************/22#if defined(_MSC_VER)23# define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */24# define _CRT_NONSTDC_NO_WARNINGS /* Disable C4996 complaining about posix function names */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 */28# endif29# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */30#endif313233/* **************************************34* Detect 64-bit OS35* http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros36****************************************/37#if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \38|| defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \39|| (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \40|| defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \41|| defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \42|| (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \43|| defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \44|| (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */45# if !defined(__64BIT__)46# define __64BIT__ 147# endif48#endif495051/* *********************************************************52* Turn on Large Files support (>4GB) for 32-bit Linux/Unix53***********************************************************/54#if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */55# if !defined(_FILE_OFFSET_BITS)56# define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */57# endif58# if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */59# define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */60# endif61# if defined(_AIX) || defined(__hpux)62# define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */63# endif64#endif656667/* ************************************************************68* Detect POSIX version69* PLATFORM_POSIX_VERSION = 0 for non-Unix e.g. Windows70* PLATFORM_POSIX_VERSION = 1 for Unix-like but non-POSIX71* PLATFORM_POSIX_VERSION > 1 is equal to found _POSIX_VERSION72* Value of PLATFORM_POSIX_VERSION can be forced on command line73***************************************************************/74#ifndef PLATFORM_POSIX_VERSION7576# if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1-2001 (SUSv3) conformant */ \77|| defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */78/* exception rule : force posix version to 200112L,79* note: it's better to use unistd.h's _POSIX_VERSION whenever possible */80# define PLATFORM_POSIX_VERSION 200112L8182/* try to determine posix version through official unistd.h's _POSIX_VERSION (http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html).83* note : there is no simple way to know in advance if <unistd.h> is present or not on target system,84* Posix specification mandates its presence and its content, but target system must respect this spec.85* It's necessary to _not_ #include <unistd.h> whenever target OS is not unix-like86* otherwise it will block preprocessing stage.87* The following list of build macros tries to "guess" if target OS is likely unix-like, and therefore can #include <unistd.h>88*/89# elif !defined(_WIN32) \90&& ( defined(__unix__) || defined(__unix) \91|| defined(__midipix__) || defined(__VMS) || defined(__HAIKU__) )9293# if defined(__linux__) || defined(__linux) || defined(__CYGWIN__)94# ifndef _POSIX_C_SOURCE95# define _POSIX_C_SOURCE 200809L /* feature test macro : https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html */96# endif97# endif98# include <unistd.h> /* declares _POSIX_VERSION */99# if defined(_POSIX_VERSION) /* POSIX compliant */100# define PLATFORM_POSIX_VERSION _POSIX_VERSION101# else102# define PLATFORM_POSIX_VERSION 1103# endif104105# ifdef __UCLIBC__106# ifndef __USE_MISC107# define __USE_MISC /* enable st_mtim on uclibc */108# endif109# endif110111# else /* non-unix target platform (like Windows) */112# define PLATFORM_POSIX_VERSION 0113# endif114115#endif /* PLATFORM_POSIX_VERSION */116117118#if PLATFORM_POSIX_VERSION > 1119/* glibc < 2.26 may not expose struct timespec def without this.120* See issue #1920. */121# ifndef _ATFILE_SOURCE122# define _ATFILE_SOURCE123# endif124#endif125126127/*-*********************************************128* Detect if isatty() and fileno() are available129************************************************/130#if (defined(__linux__) && (PLATFORM_POSIX_VERSION > 1)) \131|| (PLATFORM_POSIX_VERSION >= 200112L) \132|| defined(__DJGPP__)133# include <unistd.h> /* isatty */134# include <stdio.h> /* fileno */135# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))136#elif defined(MSDOS) || defined(OS2)137# include <io.h> /* _isatty */138# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))139#elif defined(WIN32) || defined(_WIN32)140# include <io.h> /* _isatty */141# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */142# include <stdio.h> /* FILE */143static __inline int IS_CONSOLE(FILE* stdStream) {144DWORD dummy;145return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy);146}147#else148# define IS_CONSOLE(stdStream) 0149#endif150151152/******************************153* OS-specific IO behaviors154******************************/155#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)156# include <fcntl.h> /* _O_BINARY */157# include <io.h> /* _setmode, _fileno, _get_osfhandle */158# if !defined(__DJGPP__)159# include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */160# include <winioctl.h> /* FSCTL_SET_SPARSE */161# define SET_BINARY_MODE(file) { int const unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }162# define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }163# else164# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)165# define SET_SPARSE_FILE_MODE(file)166# endif167#else168# define SET_BINARY_MODE(file)169# define SET_SPARSE_FILE_MODE(file)170#endif171172173#ifndef ZSTD_SPARSE_DEFAULT174# if (defined(__APPLE__) && defined(__MACH__))175# define ZSTD_SPARSE_DEFAULT 0176# else177# define ZSTD_SPARSE_DEFAULT 1178# endif179#endif180181182#ifndef ZSTD_START_SYMBOLLIST_FRAME183# ifdef __linux__184# define ZSTD_START_SYMBOLLIST_FRAME 2185# elif defined __APPLE__186# define ZSTD_START_SYMBOLLIST_FRAME 4187# else188# define ZSTD_START_SYMBOLLIST_FRAME 0189# endif190#endif191192193#ifndef ZSTD_SETPRIORITY_SUPPORT194/* mandates presence of <sys/resource.h> and support for setpriority() : http://man7.org/linux/man-pages/man2/setpriority.2.html */195# define ZSTD_SETPRIORITY_SUPPORT (PLATFORM_POSIX_VERSION >= 200112L)196#endif197198199#ifndef ZSTD_NANOSLEEP_SUPPORT200/* mandates support of nanosleep() within <time.h> : http://man7.org/linux/man-pages/man2/nanosleep.2.html */201# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) \202|| (PLATFORM_POSIX_VERSION >= 200112L)203# define ZSTD_NANOSLEEP_SUPPORT 1204# else205# define ZSTD_NANOSLEEP_SUPPORT 0206# endif207#endif208209210#if defined (__cplusplus)211}212#endif213214#endif /* PLATFORM_H_MODULE */215216217