summaryrefslogtreecommitdiffstats
path: root/programs/util.h
diff options
context:
space:
mode:
authorPrzemyslaw Skibinski <inikep@gmail.com>2017-02-15 16:16:15 (GMT)
committerPrzemyslaw Skibinski <inikep@gmail.com>2017-02-15 16:16:15 (GMT)
commit40ad1e85d49bd067a0b06b0e923ec47cc9930173 (patch)
tree65859b53cce120483f80847352a8460ead61f3fa /programs/util.h
parentc139eb40b0d78db390bdc37188eb696d9272f9c4 (diff)
downloadlz4-40ad1e85d49bd067a0b06b0e923ec47cc9930173.zip
lz4-40ad1e85d49bd067a0b06b0e923ec47cc9930173.tar.gz
lz4-40ad1e85d49bd067a0b06b0e923ec47cc9930173.tar.bz2
upgraded util.h
Diffstat (limited to 'programs/util.h')
-rw-r--r--programs/util.h103
1 files changed, 60 insertions, 43 deletions
diff --git a/programs/util.h b/programs/util.h
index f3ff1b2..044085f 100644
--- a/programs/util.h
+++ b/programs/util.h
@@ -70,12 +70,26 @@ extern "C" {
#endif
+/* ************************************************************
+* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
+***************************************************************/
+#if defined(_MSC_VER) && (_MSC_VER >= 1400)
+# define UTIL_fseek _fseeki64
+#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
+# define UTIL_fseek fseeko
+#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
+# define UTIL_fseek fseeko64
+#else
+# define UTIL_fseek fseek
+#endif
+
+
/*-****************************************
* Sleep functions: Windows - Posix - others
******************************************/
#if defined(_WIN32)
# include <windows.h>
-# define SET_HIGH_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
+# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
# define UTIL_sleep(s) Sleep(1000*s)
# define UTIL_sleepMilli(milli) Sleep(milli)
#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
@@ -83,9 +97,9 @@ extern "C" {
# include <sys/resource.h> /* setpriority */
# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
# if defined(PRIO_PROCESS)
-# define SET_HIGH_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
+# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
# else
-# define SET_HIGH_PRIORITY /* disabled */
+# define SET_REALTIME_PRIORITY /* disabled */
# endif
# define UTIL_sleep(s) sleep(s)
# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
@@ -94,7 +108,7 @@ extern "C" {
# define UTIL_sleepMilli(milli) /* disabled */
# endif
#else
-# define SET_HIGH_PRIORITY /* disabled */
+# define SET_REALTIME_PRIORITY /* disabled */
# define UTIL_sleep(s) /* disabled */
# define UTIL_sleepMilli(milli) /* disabled */
#endif
@@ -126,18 +140,26 @@ extern "C" {
/*-****************************************
* Time functions
******************************************/
-#if !defined(_WIN32)
- typedef clock_t UTIL_time_t;
- UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { *ticksPerSecond=0; }
- UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); }
- UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
- UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
-#else
+#if (PLATFORM_POSIX_VERSION >= 1)
+#include <unistd.h>
+#include <sys/times.h> /* times */
+ typedef U64 UTIL_time_t;
+ UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); }
+ UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { struct tms junk; clock_t newTicks = (clock_t) times(&junk); (void)junk; *x = (UTIL_time_t)newTicks; }
+ UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
+ UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
+#elif defined(_WIN32) /* Windows */
typedef LARGE_INTEGER UTIL_time_t;
UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { if (!QueryPerformanceFrequency(ticksPerSecond)) fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { QueryPerformanceCounter(x); }
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
+#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
+ typedef clock_t UTIL_time_t;
+ UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { *ticksPerSecond=0; }
+ UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); }
+ UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
+ UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
#endif
@@ -166,7 +188,7 @@ UTIL_STATIC void UTIL_waitForNextTick(UTIL_time_t ticksPerSecond)
******************************************/
#if defined(_MSC_VER)
#define chmod _chmod
- typedef struct _stat64 stat_t;
+ typedef struct __stat64 stat_t;
#else
typedef struct stat stat_t;
#endif
@@ -206,63 +228,58 @@ UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
}
-UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
+UTIL_STATIC int UTIL_isRegFile(const char* infilename)
+{
+ stat_t statbuf;
+ return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
+}
+
+
+UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
{
int r;
+ stat_t statbuf;
#if defined(_MSC_VER)
- struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
- if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
+ if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
#else
- struct stat statbuf;
r = stat(infilename, &statbuf);
- if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
+ if (!r && S_ISDIR(statbuf.st_mode)) return 1;
#endif
- return (U64)statbuf.st_size;
-}
-
-
-UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
-{
- U64 total = 0;
- unsigned n;
- for (n=0; n<nbFiles; n++)
- total += UTIL_getFileSize(fileNamesTable[n]);
- return total;
+ return 0;
}
-UTIL_STATIC int UTIL_doesFileExists(const char* infilename)
+UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
{
int r;
#if defined(_MSC_VER)
- struct _stat64 statbuf;
+ struct __stat64 statbuf;
r = _stat64(infilename, &statbuf);
if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
+#elif defined(__MINGW32__) && defined (__MSVCRT__)
+ struct _stati64 statbuf;
+ r = _stati64(infilename, &statbuf);
+ if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
#endif
- return 1;
+ return (U64)statbuf.st_size;
}
-UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
+UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
{
- int r;
-#if defined(_MSC_VER)
- struct _stat64 statbuf;
- r = _stat64(infilename, &statbuf);
- if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
-#else
- struct stat statbuf;
- r = stat(infilename, &statbuf);
- if (!r && S_ISDIR(statbuf.st_mode)) return 1;
-#endif
- return 0;
+ U64 total = 0;
+ unsigned n;
+ for (n=0; n<nbFiles; n++)
+ total += UTIL_getFileSize(fileNamesTable[n]);
+ return total;
}
+
/*
* A modified version of realloc().
* If UTIL_realloc() fails the original block is freed.