Home | History | Annotate | Line # | Download | only in programs
fileio_common.h revision 1.1
      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 #if defined (__cplusplus)
     15 extern "C" {
     16 #endif
     17 
     18 #include "../lib/common/mem.h"     /* U32, U64 */
     19 #include "fileio_types.h"
     20 #include "platform.h"
     21 #include "timefn.h"     /* UTIL_getTime, UTIL_clockSpanMicro */
     22 
     23 /*-*************************************
     24 *  Macros
     25 ***************************************/
     26 #define KB *(1 <<10)
     27 #define MB *(1 <<20)
     28 #define GB *(1U<<30)
     29 #undef MAX
     30 #define MAX(a,b) ((a)>(b) ? (a) : (b))
     31 
     32 extern FIO_display_prefs_t g_display_prefs;
     33 
     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__); } }
     37 
     38 extern UTIL_time_t g_displayClock;
     39 
     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);       \
     49     }   }   }
     50 
     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__); } }
     58 
     59 #undef MIN  /* in case it would be already defined */
     60 #define MIN(a,b)    ((a) < (b) ? (a) : (b))
     61 
     62 
     63 #define EXM_THROW(error, ...)                                             \
     64 {                                                                         \
     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");                                               \
     70     exit(error);                                                          \
     71 }
     72 
     73 #define CHECK_V(v, f)                                \
     74     v = f;                                           \
     75     if (ZSTD_isError(v)) {                           \
     76         DISPLAYLEVEL(5, "%s \n", #f);                \
     77         EXM_THROW(11, "%s", ZSTD_getErrorName(v));   \
     78     }
     79 #define CHECK(f) { size_t err; CHECK_V(err, f); }
     80 
     81 
     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__)
     93 #   include <windows.h>
     94     static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
     95         LARGE_INTEGER off;
     96         DWORD method;
     97         off.QuadPart = offset;
     98         if (origin == SEEK_END)
     99             method = FILE_END;
    100         else if (origin == SEEK_CUR)
    101             method = FILE_CURRENT;
    102         else
    103             method = FILE_BEGIN;
    104 
    105         if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
    106             return 0;
    107         else
    108             return -1;
    109     }
    110     static __int64 LONG_TELL(FILE* file) {
    111         LARGE_INTEGER off, newOff;
    112         off.QuadPart = 0;
    113         newOff.QuadPart = 0;
    114         SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
    115         return newOff.QuadPart;
    116     }
    117 #else
    118 #   define LONG_SEEK fseek
    119 #   define LONG_TELL ftell
    120 #endif
    121 
    122 #if defined (__cplusplus)
    123 }
    124 #endif
    125 #endif /* ZSTD_FILEIO_COMMON_H */
    126