2017-08-18 23:52:05 +00:00
|
|
|
/*
|
2017-08-21 18:24:32 +00:00
|
|
|
* Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
|
2016-12-21 14:08:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 23:52:05 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-12-21 14:08:44 +00:00
|
|
|
*/
|
2016-05-09 14:19:25 +00:00
|
|
|
|
2016-04-28 10:23:33 +00:00
|
|
|
#ifndef UTIL_H_MODULE
|
|
|
|
#define UTIL_H_MODULE
|
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-05-05 09:53:42 +00:00
|
|
|
|
2016-04-28 10:23:33 +00:00
|
|
|
/*-****************************************
|
|
|
|
* Dependencies
|
|
|
|
******************************************/
|
2018-10-03 22:34:41 +00:00
|
|
|
#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
|
2018-12-20 02:30:57 +00:00
|
|
|
#include <stdlib.h> /* malloc, realloc, free */
|
2016-12-21 12:23:34 +00:00
|
|
|
#include <stddef.h> /* size_t, ptrdiff_t */
|
|
|
|
#include <stdio.h> /* fprintf */
|
|
|
|
#include <sys/types.h> /* stat, utime */
|
2018-10-03 21:54:33 +00:00
|
|
|
#include <sys/stat.h> /* stat, chmod */
|
2016-11-03 08:54:53 +00:00
|
|
|
#if defined(_MSC_VER)
|
2016-12-21 12:23:34 +00:00
|
|
|
# include <sys/utime.h> /* utime */
|
|
|
|
# include <io.h> /* _chmod */
|
2016-11-03 08:54:53 +00:00
|
|
|
#else
|
2016-12-12 00:03:23 +00:00
|
|
|
# include <unistd.h> /* chown, stat */
|
|
|
|
# include <utime.h> /* utime */
|
2016-11-03 08:54:53 +00:00
|
|
|
#endif
|
2017-12-05 00:02:42 +00:00
|
|
|
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
|
2016-12-21 12:23:34 +00:00
|
|
|
#include "mem.h" /* U32, U64 */
|
2016-04-28 10:23:33 +00:00
|
|
|
|
2016-05-04 22:25:38 +00:00
|
|
|
|
2018-10-12 00:34:47 +00:00
|
|
|
/*-************************************************************
|
2018-06-09 19:31:17 +00:00
|
|
|
* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
|
2017-02-15 16:03:16 +00:00
|
|
|
***************************************************************/
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
2018-10-03 21:54:33 +00:00
|
|
|
/*-*************************************************
|
|
|
|
* Sleep & priority functions: Windows - Posix - others
|
|
|
|
***************************************************/
|
2016-05-04 22:25:38 +00:00
|
|
|
#if defined(_WIN32)
|
2016-04-28 11:16:01 +00:00
|
|
|
# include <windows.h>
|
2017-02-07 15:36:19 +00:00
|
|
|
# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
|
2016-04-28 11:16:01 +00:00
|
|
|
# define UTIL_sleep(s) Sleep(1000*s)
|
|
|
|
# define UTIL_sleepMilli(milli) Sleep(milli)
|
2018-10-03 21:54:33 +00:00
|
|
|
|
|
|
|
#elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
|
|
|
|
# include <unistd.h> /* sleep */
|
2016-05-04 22:25:38 +00:00
|
|
|
# define UTIL_sleep(s) sleep(s)
|
2018-10-03 22:34:41 +00:00
|
|
|
# if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */
|
2016-05-05 09:53:42 +00:00
|
|
|
# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
|
|
|
|
# else
|
|
|
|
# define UTIL_sleepMilli(milli) /* disabled */
|
|
|
|
# endif
|
2018-10-03 21:54:33 +00:00
|
|
|
# if ZSTD_SETPRIORITY_SUPPORT
|
|
|
|
# include <sys/resource.h> /* setpriority */
|
|
|
|
# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
|
|
|
|
# else
|
|
|
|
# define SET_REALTIME_PRIORITY /* disabled */
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else /* unknown non-unix operating systen */
|
2016-05-04 22:25:38 +00:00
|
|
|
# define UTIL_sleep(s) /* disabled */
|
2016-04-28 11:16:01 +00:00
|
|
|
# define UTIL_sleepMilli(milli) /* disabled */
|
2018-10-03 21:54:33 +00:00
|
|
|
# define SET_REALTIME_PRIORITY /* disabled */
|
2016-04-28 11:16:01 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-04 22:25:38 +00:00
|
|
|
|
2018-10-12 00:34:47 +00:00
|
|
|
/*-*************************************
|
2016-12-21 08:04:59 +00:00
|
|
|
* Constants
|
|
|
|
***************************************/
|
|
|
|
#define LIST_SIZE_INCREASE (8*1024)
|
|
|
|
|
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Compiler specifics
|
|
|
|
******************************************/
|
2016-12-21 12:23:34 +00:00
|
|
|
#if defined(__INTEL_COMPILER)
|
|
|
|
# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
|
|
|
|
#endif
|
2016-12-21 08:04:59 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define UTIL_STATIC static __attribute__((unused))
|
|
|
|
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
|
|
|
# define UTIL_STATIC static inline
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# define UTIL_STATIC static __inline
|
|
|
|
#else
|
|
|
|
# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-08-31 23:58:47 +00:00
|
|
|
/*-****************************************
|
|
|
|
* Console log
|
|
|
|
******************************************/
|
2018-10-11 23:51:29 +00:00
|
|
|
extern int g_utilDisplayLevel;
|
2017-08-31 23:58:47 +00:00
|
|
|
#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
|
|
|
|
|
|
|
|
|
2016-05-04 22:25:38 +00:00
|
|
|
/*-****************************************
|
|
|
|
* Time functions
|
|
|
|
******************************************/
|
2017-02-20 10:11:50 +00:00
|
|
|
#if defined(_WIN32) /* Windows */
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2017-11-30 03:11:12 +00:00
|
|
|
#define UTIL_TIME_INITIALIZER { { 0, 0 } }
|
2017-09-12 20:09:08 +00:00
|
|
|
typedef LARGE_INTEGER UTIL_time_t;
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2017-02-20 11:18:15 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2017-09-12 20:09:08 +00:00
|
|
|
#include <mach/mach_time.h>
|
2017-11-30 03:11:12 +00:00
|
|
|
#define UTIL_TIME_INITIALIZER 0
|
2017-09-12 20:09:08 +00:00
|
|
|
typedef U64 UTIL_time_t;
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2018-09-21 21:46:09 +00:00
|
|
|
#elif (PLATFORM_POSIX_VERSION >= 200112L) \
|
|
|
|
&& (defined(__UCLIBC__) \
|
|
|
|
|| (defined(__GLIBC__) \
|
|
|
|
&& ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) \
|
|
|
|
|| (__GLIBC__ > 2))))
|
2018-02-20 22:48:09 +00:00
|
|
|
|
2017-11-30 03:11:12 +00:00
|
|
|
#define UTIL_TIME_INITIALIZER { 0, 0 }
|
2017-09-12 20:09:08 +00:00
|
|
|
typedef struct timespec UTIL_freq_t;
|
|
|
|
typedef struct timespec UTIL_time_t;
|
2018-10-03 21:54:33 +00:00
|
|
|
|
2018-10-11 22:07:12 +00:00
|
|
|
UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end);
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2017-01-20 20:23:30 +00:00
|
|
|
#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2017-09-12 20:09:08 +00:00
|
|
|
typedef clock_t UTIL_time_t;
|
2017-11-30 03:11:12 +00:00
|
|
|
#define UTIL_TIME_INITIALIZER 0
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2016-04-28 11:16:01 +00:00
|
|
|
#endif
|
2018-12-20 22:02:50 +00:00
|
|
|
|
2018-10-12 00:34:47 +00:00
|
|
|
UTIL_time_t UTIL_getTime(void);
|
|
|
|
U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
|
|
|
|
U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
|
2016-04-28 11:16:01 +00:00
|
|
|
|
2017-12-01 03:40:53 +00:00
|
|
|
#define SEC_TO_MICRO 1000000
|
2016-04-28 11:16:01 +00:00
|
|
|
|
|
|
|
/* returns time span in microseconds */
|
2018-10-11 22:07:12 +00:00
|
|
|
U64 UTIL_clockSpanMicro(UTIL_time_t clockStart);
|
2016-04-28 11:16:01 +00:00
|
|
|
|
2018-02-20 22:48:09 +00:00
|
|
|
/* returns time span in microseconds */
|
2018-10-11 22:07:12 +00:00
|
|
|
U64 UTIL_clockSpanNano(UTIL_time_t clockStart);
|
|
|
|
void UTIL_waitForNextTick(void);
|
2016-04-28 11:16:01 +00:00
|
|
|
|
2016-05-04 22:25:38 +00:00
|
|
|
/*-****************************************
|
|
|
|
* File functions
|
|
|
|
******************************************/
|
2016-11-02 13:08:07 +00:00
|
|
|
#if defined(_MSC_VER)
|
2017-03-30 01:51:58 +00:00
|
|
|
#define chmod _chmod
|
|
|
|
typedef struct __stat64 stat_t;
|
2016-11-02 13:08:07 +00:00
|
|
|
#else
|
|
|
|
typedef struct stat stat_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-12-20 02:30:57 +00:00
|
|
|
int UTIL_fileExist(const char* filename);
|
2018-10-11 22:07:12 +00:00
|
|
|
int UTIL_isRegularFile(const char* infilename);
|
2018-12-20 02:30:57 +00:00
|
|
|
int UTIL_setFileStat(const char* filename, stat_t* statbuf);
|
2018-10-11 19:52:19 +00:00
|
|
|
U32 UTIL_isDirectory(const char* infilename);
|
2018-12-20 02:30:57 +00:00
|
|
|
int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
|
2018-10-11 19:52:19 +00:00
|
|
|
|
|
|
|
U32 UTIL_isLink(const char* infilename);
|
|
|
|
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
|
|
|
|
U64 UTIL_getFileSize(const char* infilename);
|
|
|
|
|
|
|
|
U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles);
|
|
|
|
|
2016-09-15 16:58:18 +00:00
|
|
|
/*
|
|
|
|
* A modified version of realloc().
|
|
|
|
* If UTIL_realloc() fails the original block is freed.
|
|
|
|
*/
|
2018-12-20 02:30:57 +00:00
|
|
|
UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
|
2016-09-15 16:58:18 +00:00
|
|
|
{
|
|
|
|
void *newptr = realloc(ptr, size);
|
|
|
|
if (newptr) return newptr;
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-20 02:30:57 +00:00
|
|
|
int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
|
2016-05-04 22:25:38 +00:00
|
|
|
#ifdef _WIN32
|
2016-05-05 09:53:42 +00:00
|
|
|
# define UTIL_HAS_CREATEFILELIST
|
2016-12-16 16:12:23 +00:00
|
|
|
#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
|
2016-05-05 09:53:42 +00:00
|
|
|
# define UTIL_HAS_CREATEFILELIST
|
2016-05-04 22:25:38 +00:00
|
|
|
# include <dirent.h> /* opendir, readdir */
|
2016-12-21 08:04:59 +00:00
|
|
|
# include <string.h> /* strerror, memcpy */
|
2016-05-04 22:25:38 +00:00
|
|
|
#else
|
2016-08-24 15:32:09 +00:00
|
|
|
#endif /* #ifdef _WIN32 */
|
2016-05-04 22:25:38 +00:00
|
|
|
|
2016-05-20 09:24:35 +00:00
|
|
|
/*
|
|
|
|
* UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
|
2016-05-13 08:52:02 +00:00
|
|
|
* and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
|
|
|
|
* After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
|
|
|
|
* In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
|
|
|
|
*/
|
2018-10-11 22:51:57 +00:00
|
|
|
const char**
|
2018-08-30 18:02:08 +00:00
|
|
|
UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
|
|
|
|
char** allocatedBuffer, unsigned* allocatedNamesNb,
|
2018-10-11 22:51:57 +00:00
|
|
|
int followLinks);
|
2016-05-04 22:25:38 +00:00
|
|
|
|
2016-05-13 08:52:02 +00:00
|
|
|
UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
|
2016-05-04 22:25:38 +00:00
|
|
|
{
|
2016-05-13 08:52:02 +00:00
|
|
|
if (allocatedBuffer) free(allocatedBuffer);
|
|
|
|
if (filenameTable) free((void*)filenameTable);
|
2016-05-04 22:25:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 00:34:47 +00:00
|
|
|
int UTIL_countPhysicalCores(void);
|
2016-05-04 22:25:38 +00:00
|
|
|
|
2016-04-28 10:23:33 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* UTIL_H_MODULE */
|