2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 ZSTD_FILEIO_COMMON_H
12 #define ZSTD_FILEIO_COMMON_H
14 #if defined (__cplusplus)
18 #include "../lib/common/mem.h" /* U32, U64 */
19 #include "fileio_types.h"
21 #include "timefn.h" /* UTIL_getTime, UTIL_clockSpanMicro */
23 /*-*************************************
25 ***************************************/
30 #define MAX(a,b) ((a)>(b) ? (a) : (b))
32 extern FIO_display_prefs_t g_display_prefs;
34 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
35 #define DISPLAYOUT(...) fprintf(stdout, __VA_ARGS__)
36 #define DISPLAYLEVEL(l, ...) { if (g_display_prefs.displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
38 extern UTIL_time_t g_displayClock;
40 #define REFRESH_RATE ((U64)(SEC_TO_MICRO / 6))
41 #define READY_FOR_UPDATE() (UTIL_clockSpanMicro(g_displayClock) > REFRESH_RATE || g_display_prefs.displayLevel >= 4)
42 #define DELAY_NEXT_UPDATE() { g_displayClock = UTIL_getTime(); }
43 #define DISPLAYUPDATE(l, ...) { \
44 if (g_display_prefs.displayLevel>=l && (g_display_prefs.progressSetting != FIO_ps_never)) { \
45 if (READY_FOR_UPDATE()) { \
46 DELAY_NEXT_UPDATE(); \
47 DISPLAY(__VA_ARGS__); \
48 if (g_display_prefs.displayLevel>=4) fflush(stderr); \
51 #define SHOULD_DISPLAY_SUMMARY() \
52 (g_display_prefs.displayLevel >= 2 || g_display_prefs.progressSetting == FIO_ps_always)
53 #define SHOULD_DISPLAY_PROGRESS() \
54 (g_display_prefs.progressSetting != FIO_ps_never && SHOULD_DISPLAY_SUMMARY())
55 #define DISPLAY_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYLEVEL(1, __VA_ARGS__); }}
56 #define DISPLAYUPDATE_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYUPDATE(1, __VA_ARGS__); }}
57 #define DISPLAY_SUMMARY(...) { if (SHOULD_DISPLAY_SUMMARY()) { DISPLAYLEVEL(1, __VA_ARGS__); } }
59 #undef MIN /* in case it would be already defined */
60 #define MIN(a,b) ((a) < (b) ? (a) : (b))
63 #define EXM_THROW(error, ...) \
65 DISPLAYLEVEL(1, "zstd: "); \
66 DISPLAYLEVEL(5, "Error defined at %s, line %i : \n", __FILE__, __LINE__); \
67 DISPLAYLEVEL(1, "error %i : ", error); \
68 DISPLAYLEVEL(1, __VA_ARGS__); \
69 DISPLAYLEVEL(1, " \n"); \
73 #define CHECK_V(v, f) \
75 if (ZSTD_isError(v)) { \
76 DISPLAYLEVEL(5, "%s \n", #f); \
77 EXM_THROW(11, "%s", ZSTD_getErrorName(v)); \
79 #define CHECK(f) { size_t err; CHECK_V(err, f); }
82 /* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW */
83 #if defined(_MSC_VER) && _MSC_VER >= 1400
84 # define LONG_SEEK _fseeki64
85 # define LONG_TELL _ftelli64
86 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
87 # define LONG_SEEK fseeko
88 # define LONG_TELL ftello
89 #elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)
90 # define LONG_SEEK fseeko64
91 # define LONG_TELL ftello64
92 #elif defined(_WIN32) && !defined(__DJGPP__)
94 static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
97 off.QuadPart = offset;
98 if (origin == SEEK_END)
100 else if (origin == SEEK_CUR)
101 method = FILE_CURRENT;
105 if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
110 static __int64 LONG_TELL(FILE* file) {
111 LARGE_INTEGER off, newOff;
114 SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
115 return newOff.QuadPart;
118 # define LONG_SEEK fseek
119 # define LONG_TELL ftell
122 #if defined (__cplusplus)
125 #endif /* ZSTD_FILEIO_COMMON_H */