Path: blob/main/sys/contrib/zstd/programs/fileio_common.h
289024 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.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 ZSTD_FILEIO_COMMON_H11#define ZSTD_FILEIO_COMMON_H1213#include "../lib/common/mem.h" /* U32, U64 */14#include "fileio_types.h"15#include "platform.h"16#include "timefn.h" /* UTIL_getTime, UTIL_clockSpanMicro */1718/*-*************************************19* Macros20***************************************/21#define KB *(1 <<10)22#define MB *(1 <<20)23#define GB *(1U<<30)24#undef MAX25#define MAX(a,b) ((a)>(b) ? (a) : (b))26#undef MIN /* in case it would be already defined */27#define MIN(a,b) ((a) < (b) ? (a) : (b))2829extern FIO_display_prefs_t g_display_prefs;3031#define DISPLAY_F(f, ...) fprintf((f), __VA_ARGS__)32#define DISPLAYOUT(...) DISPLAY_F(stdout, __VA_ARGS__)33#define DISPLAY(...) DISPLAY_F(stderr, __VA_ARGS__)34#define DISPLAYLEVEL(l, ...) { if (g_display_prefs.displayLevel>=l) { DISPLAY(__VA_ARGS__); } }3536extern UTIL_time_t g_displayClock;3738#define REFRESH_RATE ((U64)(SEC_TO_MICRO / 6))39#define READY_FOR_UPDATE() (UTIL_clockSpanMicro(g_displayClock) > REFRESH_RATE || g_display_prefs.displayLevel >= 4)40#define DELAY_NEXT_UPDATE() { g_displayClock = UTIL_getTime(); }41#define DISPLAYUPDATE(l, ...) { \42if (g_display_prefs.displayLevel>=l && (g_display_prefs.progressSetting != FIO_ps_never)) { \43if (READY_FOR_UPDATE()) { \44DELAY_NEXT_UPDATE(); \45DISPLAY(__VA_ARGS__); \46if (g_display_prefs.displayLevel>=4) fflush(stderr); \47} } }4849#define SHOULD_DISPLAY_SUMMARY() \50(g_display_prefs.displayLevel >= 2 || g_display_prefs.progressSetting == FIO_ps_always)51#define SHOULD_DISPLAY_PROGRESS() \52(g_display_prefs.progressSetting != FIO_ps_never && SHOULD_DISPLAY_SUMMARY())53#define DISPLAY_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYLEVEL(1, __VA_ARGS__); }}54#define DISPLAYUPDATE_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYUPDATE(1, __VA_ARGS__); }}55#define DISPLAY_SUMMARY(...) { if (SHOULD_DISPLAY_SUMMARY()) { DISPLAYLEVEL(1, __VA_ARGS__); } }5657#define EXM_THROW(error, ...) \58{ \59DISPLAYLEVEL(1, "zstd: "); \60DISPLAYLEVEL(5, "Error defined at %s, line %i : \n", __FILE__, __LINE__); \61DISPLAYLEVEL(1, "error %i : ", error); \62DISPLAYLEVEL(1, __VA_ARGS__); \63DISPLAYLEVEL(1, " \n"); \64exit(error); \65}6667#define CHECK_V(v, f) \68v = f; \69if (ZSTD_isError(v)) { \70DISPLAYLEVEL(5, "%s \n", #f); \71EXM_THROW(11, "%s", ZSTD_getErrorName(v)); \72}73#define CHECK(f) { size_t err; CHECK_V(err, f); }747576/* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW */77#if defined(LIBC_NO_FSEEKO)78/* Some older libc implementations don't include these functions (e.g. Bionic < 24) */79# define LONG_SEEK fseek80# define LONG_TELL ftell81#elif defined(_MSC_VER) && _MSC_VER >= 140082# define LONG_SEEK _fseeki6483# define LONG_TELL _ftelli6484#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */85# define LONG_SEEK fseeko86# define LONG_TELL ftello87#elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)88# define LONG_SEEK fseeko6489# define LONG_TELL ftello6490#elif defined(_WIN32) && !defined(__DJGPP__)91# include <windows.h>92static int LONG_SEEK(FILE* file, __int64 offset, int origin) {93LARGE_INTEGER off;94DWORD method;95off.QuadPart = offset;96if (origin == SEEK_END)97method = FILE_END;98else if (origin == SEEK_CUR)99method = FILE_CURRENT;100else101method = FILE_BEGIN;102103if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))104return 0;105else106return -1;107}108static __int64 LONG_TELL(FILE* file) {109LARGE_INTEGER off, newOff;110off.QuadPart = 0;111newOff.QuadPart = 0;112SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);113return newOff.QuadPart;114}115#else116# define LONG_SEEK fseek117# define LONG_TELL ftell118#endif119120#endif /* ZSTD_FILEIO_COMMON_H */121122123