blob: d6e1dfc200f3e40958eda9a93f5ab25cf272a884 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet232d62b2017-08-21 11:24:32 -07002 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01008 */
inikep69fcd7c2016-04-28 12:23:33 +02009
inikep69fcd7c2016-04-28 12:23:33 +020010#ifndef UTIL_H_MODULE
11#define UTIL_H_MODULE
12
13#if defined (__cplusplus)
14extern "C" {
15#endif
16
inikep9c22e572016-05-05 11:53:42 +020017
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010018
inikep69fcd7c2016-04-28 12:23:33 +020019/*-****************************************
20* Dependencies
21******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010022#include "platform.h" /* PLATFORM_POSIX_VERSION */
23#include <stdlib.h> /* malloc */
24#include <stddef.h> /* size_t, ptrdiff_t */
25#include <stdio.h> /* fprintf */
Sean Purcellafa48512017-04-13 12:28:28 -070026#include <string.h> /* strncmp */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010027#include <sys/types.h> /* stat, utime */
28#include <sys/stat.h> /* stat */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010029#if defined(_MSC_VER)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010030# include <sys/utime.h> /* utime */
31# include <io.h> /* _chmod */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010032#else
Yann Colletfda539f2016-12-12 01:03:23 +010033# include <unistd.h> /* chown, stat */
34# include <utime.h> /* utime */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010035#endif
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010036#include <time.h> /* time */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010037#include <errno.h>
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010038#include "mem.h" /* U32, U64 */
inikep69fcd7c2016-04-28 12:23:33 +020039
inikep31634032016-05-05 00:25:38 +020040
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010041/* ************************************************************
42* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
43***************************************************************/
44#if defined(_MSC_VER) && (_MSC_VER >= 1400)
45# define UTIL_fseek _fseeki64
46#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
47# define UTIL_fseek fseeko
48#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
49# define UTIL_fseek fseeko64
50#else
51# define UTIL_fseek fseek
52#endif
53
54
inikep9c22e572016-05-05 11:53:42 +020055/*-****************************************
56* Sleep functions: Windows - Posix - others
57******************************************/
inikep31634032016-05-05 00:25:38 +020058#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020059# include <windows.h>
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010060# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020061# define UTIL_sleep(s) Sleep(1000*s)
62# define UTIL_sleepMilli(milli) Sleep(milli)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010063#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
inikep31634032016-05-05 00:25:38 +020064# include <unistd.h>
65# include <sys/resource.h> /* setpriority */
66# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010067# if defined(PRIO_PROCESS)
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010068# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010069# else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010070# define SET_REALTIME_PRIORITY /* disabled */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010071# endif
inikep31634032016-05-05 00:25:38 +020072# define UTIL_sleep(s) sleep(s)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010073# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +020074# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
75# else
76# define UTIL_sleepMilli(milli) /* disabled */
77# endif
inikep83c76b42016-04-28 13:16:01 +020078#else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010079# define SET_REALTIME_PRIORITY /* disabled */
inikep31634032016-05-05 00:25:38 +020080# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020081# define UTIL_sleepMilli(milli) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020082#endif
83
inikep31634032016-05-05 00:25:38 +020084
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010085/* *************************************
86* Constants
87***************************************/
88#define LIST_SIZE_INCREASE (8*1024)
89
90
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010091/*-****************************************
92* Compiler specifics
93******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010094#if defined(__INTEL_COMPILER)
95# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
96#endif
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010097#if defined(__GNUC__)
98# define UTIL_STATIC static __attribute__((unused))
99#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
100# define UTIL_STATIC static inline
101#elif defined(_MSC_VER)
102# define UTIL_STATIC static __inline
103#else
104# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
105#endif
106
107
inikep31634032016-05-05 00:25:38 +0200108/*-****************************************
109* Time functions
110******************************************/
Przemyslaw Skibinski83775d92017-02-20 11:11:50 +0100111#if defined(_WIN32) /* Windows */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100112 typedef LARGE_INTEGER UTIL_freq_t;
inikep83c76b42016-04-28 13:16:01 +0200113 typedef LARGE_INTEGER UTIL_time_t;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100114 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { if (!QueryPerformanceFrequency(ticksPerSecond)) fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
inikepaaaf9232016-05-09 16:19:25 +0200115 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { QueryPerformanceCounter(x); }
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100116 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
117 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100118#elif defined(__APPLE__) && defined(__MACH__)
119 #include <mach/mach_time.h>
120 typedef mach_timebase_info_data_t UTIL_freq_t;
121 typedef U64 UTIL_time_t;
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100122 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* rate) { mach_timebase_info(rate); }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100123 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = mach_absolute_time(); }
124 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL; }
125 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom); }
Przemyslaw Skibinski1b593332017-02-21 07:33:45 +0100126#elif (PLATFORM_POSIX_VERSION >= 200112L)
127 #include <sys/times.h> /* times */
128 typedef U64 UTIL_freq_t;
129 typedef U64 UTIL_time_t;
130 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); }
131 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; }
132 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
133 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
cyan49735fba09f2017-01-20 12:23:30 -0800134#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100135 typedef clock_t UTIL_freq_t;
cyan49735fba09f2017-01-20 12:23:30 -0800136 typedef clock_t UTIL_time_t;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100137 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=0; }
cyan49735fba09f2017-01-20 12:23:30 -0800138 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); }
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100139 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
140 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
inikep83c76b42016-04-28 13:16:01 +0200141#endif
142
143
inikep83c76b42016-04-28 13:16:01 +0200144/* returns time span in microseconds */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100145UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart, UTIL_freq_t ticksPerSecond )
inikep83c76b42016-04-28 13:16:01 +0200146{
147 UTIL_time_t clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200148 UTIL_getTime(&clockEnd);
inikep83c76b42016-04-28 13:16:01 +0200149 return UTIL_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd);
150}
151
152
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100153UTIL_STATIC void UTIL_waitForNextTick(UTIL_freq_t ticksPerSecond)
inikep83c76b42016-04-28 13:16:01 +0200154{
155 UTIL_time_t clockStart, clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200156 UTIL_getTime(&clockStart);
Yann Collete162ace2016-05-20 11:24:35 +0200157 do {
158 UTIL_getTime(&clockEnd);
inikepd5ff2c32016-04-28 14:40:45 +0200159 } while (UTIL_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
inikep83c76b42016-04-28 13:16:01 +0200160}
161
162
inikep31634032016-05-05 00:25:38 +0200163
164/*-****************************************
165* File functions
166******************************************/
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100167#if defined(_MSC_VER)
Nick Terrell5152fb22017-03-29 18:51:58 -0700168 #define chmod _chmod
169 typedef struct __stat64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100170#else
171 typedef struct stat stat_t;
172#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100173
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100174
175UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
176{
177 int res = 0;
178 struct utimbuf timebuf;
179
Nick Terrell5152fb22017-03-29 18:51:58 -0700180 timebuf.actime = time(NULL);
181 timebuf.modtime = statbuf->st_mtime;
182 res += utime(filename, &timebuf); /* set access and modification times */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100183
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100184#if !defined(_WIN32)
185 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
186#endif
187
188 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100189
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100190 errno = 0;
191 return -res; /* number of errors is returned */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100192}
193
194
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100195UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100196{
197 int r;
198#if defined(_MSC_VER)
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100199 r = _stat64(infilename, statbuf);
200 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100201#else
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100202 r = stat(infilename, statbuf);
203 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100204#endif
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100205 return 1;
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100206}
207
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100208
Yann Collet6d4fef32017-05-17 18:36:15 -0700209UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800210{
211 stat_t statbuf;
212 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
213}
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100214
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100215
216UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
217{
218 int r;
219 stat_t statbuf;
220#if defined(_MSC_VER)
221 r = _stat64(infilename, &statbuf);
222 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
223#else
224 r = stat(infilename, &statbuf);
225 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
226#endif
227 return 0;
228}
229
Sean Purcell680e4e02017-03-23 11:52:09 -0700230UTIL_STATIC U32 UTIL_isLink(const char* infilename)
231{
232#if defined(_WIN32)
233 /* no symlinks on windows */
234 (void)infilename;
235#else
236 int r;
237 stat_t statbuf;
238 r = lstat(infilename, &statbuf);
239 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
240#endif
241 return 0;
242}
243
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100244
inikep69fcd7c2016-04-28 12:23:33 +0200245UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
246{
247 int r;
248#if defined(_MSC_VER)
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +0100249 struct __stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200250 r = _stat64(infilename, &statbuf);
251 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
ds7745f0c202017-02-10 18:37:57 +0100252#elif defined(__MINGW32__) && defined (__MSVCRT__)
253 struct _stati64 statbuf;
254 r = _stati64(infilename, &statbuf);
255 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
inikep69fcd7c2016-04-28 12:23:33 +0200256#else
257 struct stat statbuf;
258 r = stat(infilename, &statbuf);
259 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
260#endif
261 return (U64)statbuf.st_size;
262}
263
264
inikep55d047a2016-04-28 16:50:13 +0200265UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
266{
267 U64 total = 0;
268 unsigned n;
269 for (n=0; n<nbFiles; n++)
270 total += UTIL_getFileSize(fileNamesTable[n]);
271 return total;
272}
273
274
inikep61739312016-09-15 18:58:18 +0200275/*
276 * A modified version of realloc().
277 * If UTIL_realloc() fails the original block is freed.
278*/
279UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
280{
281 void *newptr = realloc(ptr, size);
282 if (newptr) return newptr;
283 free(ptr);
284 return NULL;
285}
286
Sean Purcelldee08ca2017-03-23 12:09:35 -0700287static int g_utilDisplayLevel;
288#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
289#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
290
inikep31634032016-05-05 00:25:38 +0200291#ifdef _WIN32
inikep9c22e572016-05-05 11:53:42 +0200292# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200293
Sean Purcell680e4e02017-03-23 11:52:09 -0700294UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200295{
inikep1c5ba8a2016-09-13 13:13:10 +0200296 char* path;
297 int dirLength, fnameLength, pathLength, nbFiles = 0;
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100298 WIN32_FIND_DATAA cFile;
inikep31634032016-05-05 00:25:38 +0200299 HANDLE hFile;
300
inikep9f25fcf2016-09-13 16:38:54 +0200301 dirLength = (int)strlen(dirName);
inikep1c5ba8a2016-09-13 13:13:10 +0200302 path = (char*) malloc(dirLength + 3);
303 if (!path) return 0;
304
305 memcpy(path, dirName, dirLength);
306 path[dirLength] = '\\';
307 path[dirLength+1] = '*';
308 path[dirLength+2] = 0;
inikep31634032016-05-05 00:25:38 +0200309
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100310 hFile=FindFirstFileA(path, &cFile);
inikep31634032016-05-05 00:25:38 +0200311 if (hFile == INVALID_HANDLE_VALUE) {
312 fprintf(stderr, "Cannot open directory '%s'\n", dirName);
313 return 0;
314 }
inikep1c5ba8a2016-09-13 13:13:10 +0200315 free(path);
inikep31634032016-05-05 00:25:38 +0200316
inikep4dbf7f42016-05-11 14:11:00 +0200317 do {
inikep9f25fcf2016-09-13 16:38:54 +0200318 fnameLength = (int)strlen(cFile.cFileName);
inikep1c5ba8a2016-09-13 13:13:10 +0200319 path = (char*) malloc(dirLength + fnameLength + 2);
320 if (!path) { FindClose(hFile); return 0; }
321 memcpy(path, dirName, dirLength);
322 path[dirLength] = '\\';
323 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
324 pathLength = dirLength+1+fnameLength;
325 path[pathLength] = 0;
inikep31634032016-05-05 00:25:38 +0200326 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
327 if (strcmp (cFile.cFileName, "..") == 0 ||
inikep4dbf7f42016-05-11 14:11:00 +0200328 strcmp (cFile.cFileName, ".") == 0) continue;
inikepe416e302016-08-24 17:32:09 +0200329
Sean Purcell680e4e02017-03-23 11:52:09 -0700330 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200331 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep31634032016-05-05 00:25:38 +0200332 }
333 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
inikep4dbf7f42016-05-11 14:11:00 +0200334 if (*bufStart + *pos + pathLength >= *bufEnd) {
335 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200336 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200337 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200338 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200339 }
340 if (*bufStart + *pos + pathLength < *bufEnd) {
341 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
342 *pos += pathLength + 1;
343 nbFiles++;
344 }
inikep31634032016-05-05 00:25:38 +0200345 }
inikep1c5ba8a2016-09-13 13:13:10 +0200346 free(path);
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100347 } while (FindNextFileA(hFile, &cFile));
inikep31634032016-05-05 00:25:38 +0200348
349 FindClose(hFile);
350 return nbFiles;
351}
352
Przemyslaw Skibinski0b372052016-12-16 17:12:23 +0100353#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +0200354# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200355# include <dirent.h> /* opendir, readdir */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100356# include <string.h> /* strerror, memcpy */
inikep31634032016-05-05 00:25:38 +0200357
Sean Purcell680e4e02017-03-23 11:52:09 -0700358UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200359{
360 DIR *dir;
361 struct dirent *entry;
inikep1c5ba8a2016-09-13 13:13:10 +0200362 char* path;
363 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200364
inikep31634032016-05-05 00:25:38 +0200365 if (!(dir = opendir(dirName))) {
366 fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
367 return 0;
368 }
Yann Collete162ace2016-05-20 11:24:35 +0200369
inikep9f25fcf2016-09-13 16:38:54 +0200370 dirLength = (int)strlen(dirName);
inikep7bc5c6b2016-07-26 11:07:37 +0200371 errno = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200372 while ((entry = readdir(dir)) != NULL) {
inikep0bd0fae2016-05-05 13:10:57 +0200373 if (strcmp (entry->d_name, "..") == 0 ||
374 strcmp (entry->d_name, ".") == 0) continue;
inikep9f25fcf2016-09-13 16:38:54 +0200375 fnameLength = (int)strlen(entry->d_name);
inikep1c5ba8a2016-09-13 13:13:10 +0200376 path = (char*) malloc(dirLength + fnameLength + 2);
377 if (!path) { closedir(dir); return 0; }
378 memcpy(path, dirName, dirLength);
Sean Purcell680e4e02017-03-23 11:52:09 -0700379
inikep1c5ba8a2016-09-13 13:13:10 +0200380 path[dirLength] = '/';
381 memcpy(path+dirLength+1, entry->d_name, fnameLength);
382 pathLength = dirLength+1+fnameLength;
383 path[pathLength] = 0;
384
Sean Purcell680e4e02017-03-23 11:52:09 -0700385 if (!followLinks && UTIL_isLink(path)) {
386 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
387 continue;
388 }
389
inikep0bd0fae2016-05-05 13:10:57 +0200390 if (UTIL_isDirectory(path)) {
Sean Purcell680e4e02017-03-23 11:52:09 -0700391 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200392 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep31634032016-05-05 00:25:38 +0200393 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200394 if (*bufStart + *pos + pathLength >= *bufEnd) {
395 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200396 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200397 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200398 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200399 }
400 if (*bufStart + *pos + pathLength < *bufEnd) {
401 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
402 *pos += pathLength + 1;
403 nbFiles++;
404 }
inikep31634032016-05-05 00:25:38 +0200405 }
inikep1c5ba8a2016-09-13 13:13:10 +0200406 free(path);
inikepe416e302016-08-24 17:32:09 +0200407 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
inikep31634032016-05-05 00:25:38 +0200408 }
409
inikep7bc5c6b2016-07-26 11:07:37 +0200410 if (errno != 0) {
411 fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
412 free(*bufStart);
413 *bufStart = NULL;
414 }
inikep31634032016-05-05 00:25:38 +0200415 closedir(dir);
416 return nbFiles;
417}
418
419#else
420
Sean Purcell680e4e02017-03-23 11:52:09 -0700421UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200422{
inikep4dbf7f42016-05-11 14:11:00 +0200423 (void)bufStart; (void)bufEnd; (void)pos;
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100424 fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
inikep31634032016-05-05 00:25:38 +0200425 return 0;
426}
427
inikepe416e302016-08-24 17:32:09 +0200428#endif /* #ifdef _WIN32 */
inikep31634032016-05-05 00:25:38 +0200429
Yann Collete162ace2016-05-20 11:24:35 +0200430/*
431 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
inikep0bdb6a82016-05-13 10:52:02 +0200432 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
433 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
434 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
435 */
Sean Purcell680e4e02017-03-23 11:52:09 -0700436UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
inikep31634032016-05-05 00:25:38 +0200437{
inikep4dbf7f42016-05-11 14:11:00 +0200438 size_t pos;
inikep0bdb6a82016-05-13 10:52:02 +0200439 unsigned i, nbFiles;
Yann Colletfda539f2016-12-12 01:03:23 +0100440 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
441 char* bufend = buf + LIST_SIZE_INCREASE;
inikep0bdb6a82016-05-13 10:52:02 +0200442 const char** fileTable;
inikep31634032016-05-05 00:25:38 +0200443
inikep0bdb6a82016-05-13 10:52:02 +0200444 if (!buf) return NULL;
inikep9c22e572016-05-05 11:53:42 +0200445
inikep0bdb6a82016-05-13 10:52:02 +0200446 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
inikep957823f2016-05-25 15:30:55 +0200447 if (!UTIL_isDirectory(inputNames[i])) {
Yann Colletfda539f2016-12-12 01:03:23 +0100448 size_t const len = strlen(inputNames[i]);
inikep4dbf7f42016-05-11 14:11:00 +0200449 if (buf + pos + len >= bufend) {
450 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200451 buf = (char*)UTIL_realloc(buf, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200452 bufend = buf + newListSize;
inikep0bdb6a82016-05-13 10:52:02 +0200453 if (!buf) return NULL;
inikep4dbf7f42016-05-11 14:11:00 +0200454 }
455 if (buf + pos + len < bufend) {
456 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
457 pos += len + 1;
458 nbFiles++;
459 }
Yann Collet415251c2016-08-01 14:26:49 +0200460 } else {
Sean Purcell680e4e02017-03-23 11:52:09 -0700461 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
inikep0bdb6a82016-05-13 10:52:02 +0200462 if (buf == NULL) return NULL;
Yann Collet415251c2016-08-01 14:26:49 +0200463 } }
inikep31634032016-05-05 00:25:38 +0200464
inikep0bdb6a82016-05-13 10:52:02 +0200465 if (nbFiles == 0) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200466
inikep0bdb6a82016-05-13 10:52:02 +0200467 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
468 if (!fileTable) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200469
Yann Colletfda539f2016-12-12 01:03:23 +0100470 for (i=0, pos=0; i<nbFiles; i++) {
inikep0bdb6a82016-05-13 10:52:02 +0200471 fileTable[i] = buf + pos;
472 pos += strlen(fileTable[i]) + 1;
inikep31634032016-05-05 00:25:38 +0200473 }
474
inikep0bdb6a82016-05-13 10:52:02 +0200475 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
476
477 *allocatedBuffer = buf;
478 *allocatedNamesNb = nbFiles;
479
480 return fileTable;
inikep31634032016-05-05 00:25:38 +0200481}
482
483
inikep0bdb6a82016-05-13 10:52:02 +0200484UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
inikep31634032016-05-05 00:25:38 +0200485{
inikep0bdb6a82016-05-13 10:52:02 +0200486 if (allocatedBuffer) free(allocatedBuffer);
487 if (filenameTable) free((void*)filenameTable);
inikep31634032016-05-05 00:25:38 +0200488}
489
Sean Purcellafa48512017-04-13 12:28:28 -0700490/* count the number of physical cores */
491#if defined(_WIN32) || defined(WIN32)
492
493#include <windows.h>
494
495typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
496
497UTIL_STATIC int UTIL_countPhysicalCores(void)
498{
Sean Purcelle4f32352017-04-13 16:34:28 -0700499 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700500 if (numPhysicalCores != 0) return numPhysicalCores;
501
502 { LPFN_GLPI glpi;
503 BOOL done = FALSE;
504 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
505 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
506 DWORD returnLength = 0;
507 size_t byteOffset = 0;
508
509 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
510 "GetLogicalProcessorInformation");
511
512 if (glpi == NULL) {
513 goto failed;
514 }
515
516 while(!done) {
517 DWORD rc = glpi(buffer, &returnLength);
518 if (FALSE == rc) {
519 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
520 if (buffer)
521 free(buffer);
522 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
523
524 if (buffer == NULL) {
525 perror("zstd");
526 exit(1);
527 }
528 } else {
529 /* some other error */
530 goto failed;
531 }
532 } else {
533 done = TRUE;
534 }
535 }
536
Sean Purcell3b6207d2017-04-13 14:03:56 -0700537 ptr = buffer;
Sean Purcellafa48512017-04-13 12:28:28 -0700538
Sean Purcell3b6207d2017-04-13 14:03:56 -0700539 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
540
541 if (ptr->Relationship == RelationProcessorCore) {
Sean Purcellafa48512017-04-13 12:28:28 -0700542 numPhysicalCores++;
543 }
Sean Purcell3b6207d2017-04-13 14:03:56 -0700544
545 ptr++;
546 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
Sean Purcellafa48512017-04-13 12:28:28 -0700547 }
548
549 free(buffer);
550
551 return numPhysicalCores;
552 }
553
554failed:
555 /* try to fall back on GetSystemInfo */
Sean Purcell3b6207d2017-04-13 14:03:56 -0700556 { SYSTEM_INFO sysinfo;
557 GetSystemInfo(&sysinfo);
558 numPhysicalCores = sysinfo.dwNumberOfProcessors;
559 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
560 }
Sean Purcellafa48512017-04-13 12:28:28 -0700561 return numPhysicalCores;
562}
563
564#elif defined(__APPLE__)
565
566#include <sys/sysctl.h>
567
568/* Use apple-provided syscall
569 * see: man 3 sysctl */
570UTIL_STATIC int UTIL_countPhysicalCores(void)
571{
Sean Purcelle4f32352017-04-13 16:34:28 -0700572 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
Sean Purcellafa48512017-04-13 12:28:28 -0700573 if (numPhysicalCores != 0) return numPhysicalCores;
574
Sean Purcellf876f122017-04-13 12:33:45 -0700575 { size_t size = sizeof(S32);
576 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
Sean Purcellafa48512017-04-13 12:28:28 -0700577 if (ret != 0) {
578 if (errno == ENOENT) {
579 /* entry not present, fall back on 1 */
580 numPhysicalCores = 1;
581 } else {
582 perror("zstd: can't get number of physical cpus");
583 exit(1);
584 }
585 }
586
587 return numPhysicalCores;
588 }
589}
590
591#elif defined(__linux__)
592
593/* parse /proc/cpuinfo
594 * siblings / cpu cores should give hyperthreading ratio
595 * otherwise fall back on sysconf */
596UTIL_STATIC int UTIL_countPhysicalCores(void)
597{
Sean Purcelle4f32352017-04-13 16:34:28 -0700598 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700599
600 if (numPhysicalCores != 0) return numPhysicalCores;
601
Sean Purcell9227aae2017-04-13 14:06:40 -0700602 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
Sean Purcellafa48512017-04-13 12:28:28 -0700603 if (numPhysicalCores == -1) {
604 /* value not queryable, fall back on 1 */
605 return numPhysicalCores = 1;
606 }
607
608 /* try to determine if there's hyperthreading */
609 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
Yann Collet46ac9ad2017-05-15 18:15:08 -0700610#define BUF_SIZE 80
Sean Purcellafa48512017-04-13 12:28:28 -0700611 char buff[BUF_SIZE];
612
613 int siblings = 0;
614 int cpu_cores = 0;
615 int ratio = 1;
616
617 if (cpuinfo == NULL) {
618 /* fall back on the sysconf value */
619 return numPhysicalCores;
620 }
621
622 /* assume the cpu cores/siblings values will be constant across all
623 * present processors */
624 while (!feof(cpuinfo)) {
625 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
626 if (strncmp(buff, "siblings", 8) == 0) {
627 const char* const sep = strchr(buff, ':');
628 if (*sep == '\0') {
629 /* formatting was broken? */
630 goto failed;
631 }
632
633 siblings = atoi(sep + 1);
634 }
635 if (strncmp(buff, "cpu cores", 9) == 0) {
636 const char* const sep = strchr(buff, ':');
637 if (*sep == '\0') {
638 /* formatting was broken? */
639 goto failed;
640 }
641
642 cpu_cores = atoi(sep + 1);
643 }
644 } else if (ferror(cpuinfo)) {
645 /* fall back on the sysconf value */
646 goto failed;
647 }
648 }
649 if (siblings && cpu_cores) {
650 ratio = siblings / cpu_cores;
651 }
652failed:
653 fclose(cpuinfo);
654 return numPhysicalCores = numPhysicalCores / ratio;
655 }
656}
657
Baptiste Daroussin7dd14d02017-04-15 16:25:08 +0200658#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
659
660/* Use apple-provided syscall
661 * see: man 3 sysctl */
662UTIL_STATIC int UTIL_countPhysicalCores(void)
663{
664 static int numPhysicalCores = 0;
665
666 if (numPhysicalCores != 0) return numPhysicalCores;
667
668 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
669 if (numPhysicalCores == -1) {
670 /* value not queryable, fall back on 1 */
671 return numPhysicalCores = 1;
672 }
673 return numPhysicalCores;
674}
675
Sean Purcellafa48512017-04-13 12:28:28 -0700676#else
677
678UTIL_STATIC int UTIL_countPhysicalCores(void)
679{
680 /* assume 1 */
681 return 1;
682}
683
684#endif
inikep31634032016-05-05 00:25:38 +0200685
inikep69fcd7c2016-04-28 12:23:33 +0200686#if defined (__cplusplus)
687}
688#endif
689
690#endif /* UTIL_H_MODULE */