fileio_common.h 4.8 KB

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