• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
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.
9  */
10 
11 #ifndef ZSTD_FILEIO_COMMON_H
12 #define ZSTD_FILEIO_COMMON_H
13 
14 #include "../lib/common/mem.h"     /* U32, U64 */
15 #include "fileio_types.h"
16 #include "platform.h"
17 #include "timefn.h"     /* UTIL_getTime, UTIL_clockSpanMicro */
18 
19 /*-*************************************
20 *  Macros
21 ***************************************/
22 #define KB *(1 <<10)
23 #define MB *(1 <<20)
24 #define GB *(1U<<30)
25 #undef MAX
26 #define MAX(a,b) ((a)>(b) ? (a) : (b))
27 #undef MIN  /* in case it would be already defined */
28 #define MIN(a,b)    ((a) < (b) ? (a) : (b))
29 
30 extern FIO_display_prefs_t g_display_prefs;
31 
32 #define DISPLAY_F(f, ...)    fprintf((f), __VA_ARGS__)
33 #define DISPLAYOUT(...)      DISPLAY_F(stdout, __VA_ARGS__)
34 #define DISPLAY(...)         DISPLAY_F(stderr, __VA_ARGS__)
35 #define DISPLAYLEVEL(l, ...) { if (g_display_prefs.displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
36 
37 extern UTIL_time_t g_displayClock;
38 
39 #define REFRESH_RATE  ((U64)(SEC_TO_MICRO / 6))
40 #define READY_FOR_UPDATE() (UTIL_clockSpanMicro(g_displayClock) > REFRESH_RATE || g_display_prefs.displayLevel >= 4)
41 #define DELAY_NEXT_UPDATE() { g_displayClock = UTIL_getTime(); }
42 #define DISPLAYUPDATE(l, ...) {                              \
43         if (g_display_prefs.displayLevel>=l && (g_display_prefs.progressSetting != FIO_ps_never)) { \
44             if (READY_FOR_UPDATE()) { \
45                 DELAY_NEXT_UPDATE();                         \
46                 DISPLAY(__VA_ARGS__);                        \
47                 if (g_display_prefs.displayLevel>=4) fflush(stderr);       \
48     }   }   }
49 
50 #define SHOULD_DISPLAY_SUMMARY() \
51     (g_display_prefs.displayLevel >= 2 || g_display_prefs.progressSetting == FIO_ps_always)
52 #define SHOULD_DISPLAY_PROGRESS()                       \
53     (g_display_prefs.progressSetting != FIO_ps_never && SHOULD_DISPLAY_SUMMARY())
54 #define DISPLAY_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYLEVEL(1, __VA_ARGS__); }}
55 #define DISPLAYUPDATE_PROGRESS(...) { if (SHOULD_DISPLAY_PROGRESS()) { DISPLAYUPDATE(1, __VA_ARGS__); }}
56 #define DISPLAY_SUMMARY(...) { if (SHOULD_DISPLAY_SUMMARY()) { DISPLAYLEVEL(1, __VA_ARGS__); } }
57 
58 #define EXM_THROW(error, ...)                                             \
59 {                                                                         \
60     DISPLAYLEVEL(1, "zstd: ");                                            \
61     DISPLAYLEVEL(5, "Error defined at %s, line %i : \n", __FILE__, __LINE__); \
62     DISPLAYLEVEL(1, "error %i : ", error);                                \
63     DISPLAYLEVEL(1, __VA_ARGS__);                                         \
64     DISPLAYLEVEL(1, " \n");                                               \
65     exit(error);                                                          \
66 }
67 
68 #define CHECK_V(v, f)                                \
69     v = f;                                           \
70     if (ZSTD_isError(v)) {                           \
71         DISPLAYLEVEL(5, "%s \n", #f);                \
72         EXM_THROW(11, "%s", ZSTD_getErrorName(v));   \
73     }
74 #define CHECK(f) { size_t err; CHECK_V(err, f); }
75 
76 
77 /* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW */
78 #if defined(LIBC_NO_FSEEKO)
79 /* Some older libc implementations don't include these functions (e.g. Bionic < 24) */
80 #   define LONG_SEEK fseek
81 #   define LONG_TELL ftell
82 #elif defined(_MSC_VER) && _MSC_VER >= 1400
83 #   define LONG_SEEK _fseeki64
84 #   define LONG_TELL _ftelli64
85 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
86 #   define LONG_SEEK fseeko
87 #   define LONG_TELL ftello
88 #elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)
89 #   define LONG_SEEK fseeko64
90 #   define LONG_TELL ftello64
91 #elif defined(_WIN32) && !defined(__DJGPP__)
92 #   include <windows.h>
LONG_SEEK(FILE * file,__int64 offset,int origin)93     static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
94         LARGE_INTEGER off;
95         DWORD method;
96         off.QuadPart = offset;
97         if (origin == SEEK_END)
98             method = FILE_END;
99         else if (origin == SEEK_CUR)
100             method = FILE_CURRENT;
101         else
102             method = FILE_BEGIN;
103 
104         if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
105             return 0;
106         else
107             return -1;
108     }
LONG_TELL(FILE * file)109     static __int64 LONG_TELL(FILE* file) {
110         LARGE_INTEGER off, newOff;
111         off.QuadPart = 0;
112         newOff.QuadPart = 0;
113         SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
114         return newOff.QuadPart;
115     }
116 #else
117 #   define LONG_SEEK fseek
118 #   define LONG_TELL ftell
119 #endif
120 
121 #endif /* ZSTD_FILEIO_COMMON_H */
122