blob: c8be5f5fb574c747de7d48e3546104287c549c39 [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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01009 */
inikep69fcd7c2016-04-28 12:23:33 +020010
inikep69fcd7c2016-04-28 12:23:33 +020011#ifndef UTIL_H_MODULE
12#define UTIL_H_MODULE
13
14#if defined (__cplusplus)
15extern "C" {
16#endif
17
inikep9c22e572016-05-05 11:53:42 +020018
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010019
inikep69fcd7c2016-04-28 12:23:33 +020020/*-****************************************
21* Dependencies
22******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010023#include "platform.h" /* PLATFORM_POSIX_VERSION */
24#include <stdlib.h> /* malloc */
25#include <stddef.h> /* size_t, ptrdiff_t */
26#include <stdio.h> /* fprintf */
Sean Purcellafa48512017-04-13 12:28:28 -070027#include <string.h> /* strncmp */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010028#include <sys/types.h> /* stat, utime */
29#include <sys/stat.h> /* stat */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010030#if defined(_MSC_VER)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010031# include <sys/utime.h> /* utime */
32# include <io.h> /* _chmod */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010033#else
Yann Colletfda539f2016-12-12 01:03:23 +010034# include <unistd.h> /* chown, stat */
35# include <utime.h> /* utime */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010036#endif
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010037#include <time.h> /* time */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010038#include <errno.h>
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010039#include "mem.h" /* U32, U64 */
inikep69fcd7c2016-04-28 12:23:33 +020040
inikep31634032016-05-05 00:25:38 +020041
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010042/* ************************************************************
43* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
44***************************************************************/
45#if defined(_MSC_VER) && (_MSC_VER >= 1400)
46# define UTIL_fseek _fseeki64
47#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
48# define UTIL_fseek fseeko
49#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
50# define UTIL_fseek fseeko64
51#else
52# define UTIL_fseek fseek
53#endif
54
55
inikep9c22e572016-05-05 11:53:42 +020056/*-****************************************
57* Sleep functions: Windows - Posix - others
58******************************************/
inikep31634032016-05-05 00:25:38 +020059#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020060# include <windows.h>
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010061# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020062# define UTIL_sleep(s) Sleep(1000*s)
63# define UTIL_sleepMilli(milli) Sleep(milli)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010064#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
inikep31634032016-05-05 00:25:38 +020065# include <unistd.h>
66# include <sys/resource.h> /* setpriority */
67# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010068# if defined(PRIO_PROCESS)
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010069# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010070# else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010071# define SET_REALTIME_PRIORITY /* disabled */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010072# endif
inikep31634032016-05-05 00:25:38 +020073# define UTIL_sleep(s) sleep(s)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010074# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +020075# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
76# else
77# define UTIL_sleepMilli(milli) /* disabled */
78# endif
inikep83c76b42016-04-28 13:16:01 +020079#else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010080# define SET_REALTIME_PRIORITY /* disabled */
inikep31634032016-05-05 00:25:38 +020081# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020082# define UTIL_sleepMilli(milli) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020083#endif
84
inikep31634032016-05-05 00:25:38 +020085
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010086/* *************************************
87* Constants
88***************************************/
89#define LIST_SIZE_INCREASE (8*1024)
90
91
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010092/*-****************************************
93* Compiler specifics
94******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010095#if defined(__INTEL_COMPILER)
96# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
97#endif
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010098#if defined(__GNUC__)
99# define UTIL_STATIC static __attribute__((unused))
100#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
101# define UTIL_STATIC static inline
102#elif defined(_MSC_VER)
103# define UTIL_STATIC static __inline
104#else
105# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
106#endif
107
108
inikep31634032016-05-05 00:25:38 +0200109/*-****************************************
Yann Collet4299c272017-08-31 16:58:47 -0700110* Console log
111******************************************/
112static int g_utilDisplayLevel;
113#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
114#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
115
116
117/*-****************************************
inikep31634032016-05-05 00:25:38 +0200118* Time functions
119******************************************/
Przemyslaw Skibinski83775d92017-02-20 11:11:50 +0100120#if defined(_WIN32) /* Windows */
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700121 typedef LARGE_INTEGER UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700122 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
123 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
124 {
Yann Collet8f26dc32017-09-12 21:21:17 -0700125 static LARGE_INTEGER ticksPerSecond;
126 static int init = 0;
127 if (!init) {
Yann Colletc95c0c92017-09-12 18:12:46 -0700128 if (!QueryPerformanceFrequency(&ticksPerSecond))
129 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
Yann Collet8f26dc32017-09-12 21:21:17 -0700130 init = 1;
Yann Colletc95c0c92017-09-12 18:12:46 -0700131 }
132 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
133 }
134 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
135 {
Yann Collet8f26dc32017-09-12 21:21:17 -0700136 static LARGE_INTEGER ticksPerSecond;
137 static int init = 0;
138 if (!init) {
Yann Colletc95c0c92017-09-12 18:12:46 -0700139 if (!QueryPerformanceFrequency(&ticksPerSecond))
140 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
Yann Collet8f26dc32017-09-12 21:21:17 -0700141 init = 1;
Yann Colletc95c0c92017-09-12 18:12:46 -0700142 }
143 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
144 }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100145#elif defined(__APPLE__) && defined(__MACH__)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700146 #include <mach/mach_time.h>
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700147 typedef U64 UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700148 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
149 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
150 {
151 static mach_timebase_info_data_t rate;
152 static int init = 0;
153 if (!init) {
154 mach_timebase_info(&rate);
155 init = 1;
156 }
157 return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
158 }
159 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
160 {
161 static mach_timebase_info_data_t rate;
162 static int init = 0;
163 if (!init) {
164 mach_timebase_info(&rate);
165 init = 1;
166 }
167 return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
168 }
Przemyslaw Skibinski1b593332017-02-21 07:33:45 +0100169#elif (PLATFORM_POSIX_VERSION >= 200112L)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700170 #include <time.h>
171 typedef struct timespec UTIL_freq_t;
172 typedef struct timespec UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700173 UTIL_STATIC UTIL_time_t UTIL_getTime(void)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700174 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700175 UTIL_time_t time;
176 if (clock_gettime(CLOCK_MONOTONIC, &time))
177 UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n"); /* we could also exit() */
178 return time;
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700179 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700180 UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700181 {
182 UTIL_time_t diff;
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700183 if (end.tv_nsec < begin.tv_nsec) {
184 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
185 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
186 } else {
187 diff.tv_sec = end.tv_sec - begin.tv_sec;
188 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
189 }
190 return diff;
191 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700192 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700193 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700194 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700195 U64 micro = 0;
196 micro += 1000000ULL * diff.tv_sec;
197 micro += diff.tv_nsec / 1000ULL;
198 return micro;
199 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700200 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700201 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700202 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700203 U64 nano = 0;
204 nano += 1000000000ULL * diff.tv_sec;
205 nano += diff.tv_nsec;
206 return nano;
207 }
cyan49735fba09f2017-01-20 12:23:30 -0800208#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700209 typedef clock_t UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700210 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
211 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
212 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
inikep83c76b42016-04-28 13:16:01 +0200213#endif
214
215
inikep83c76b42016-04-28 13:16:01 +0200216/* returns time span in microseconds */
Yann Colletc95c0c92017-09-12 18:12:46 -0700217UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart )
inikep83c76b42016-04-28 13:16:01 +0200218{
Yann Colletc95c0c92017-09-12 18:12:46 -0700219 UTIL_time_t const clockEnd = UTIL_getTime();
220 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
inikep83c76b42016-04-28 13:16:01 +0200221}
222
223
Yann Colletbc41c7f2017-09-12 19:32:26 -0700224UTIL_STATIC void UTIL_waitForNextTick(void)
inikep83c76b42016-04-28 13:16:01 +0200225{
Yann Colletc95c0c92017-09-12 18:12:46 -0700226 UTIL_time_t const clockStart = UTIL_getTime();
227 UTIL_time_t clockEnd;
Yann Collete162ace2016-05-20 11:24:35 +0200228 do {
Yann Colletc95c0c92017-09-12 18:12:46 -0700229 clockEnd = UTIL_getTime();
230 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
inikep83c76b42016-04-28 13:16:01 +0200231}
232
233
inikep31634032016-05-05 00:25:38 +0200234
235/*-****************************************
236* File functions
237******************************************/
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100238#if defined(_MSC_VER)
Nick Terrell5152fb22017-03-29 18:51:58 -0700239 #define chmod _chmod
240 typedef struct __stat64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100241#else
242 typedef struct stat stat_t;
243#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100244
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100245
246UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
247{
248 int res = 0;
249 struct utimbuf timebuf;
250
Nick Terrell5152fb22017-03-29 18:51:58 -0700251 timebuf.actime = time(NULL);
252 timebuf.modtime = statbuf->st_mtime;
253 res += utime(filename, &timebuf); /* set access and modification times */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100254
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100255#if !defined(_WIN32)
256 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
257#endif
258
259 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100260
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100261 errno = 0;
262 return -res; /* number of errors is returned */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100263}
264
265
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100266UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100267{
268 int r;
269#if defined(_MSC_VER)
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100270 r = _stat64(infilename, statbuf);
271 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100272#else
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100273 r = stat(infilename, statbuf);
274 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100275#endif
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100276 return 1;
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100277}
278
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100279
Yann Collet6d4fef32017-05-17 18:36:15 -0700280UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800281{
282 stat_t statbuf;
283 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
284}
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100285
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100286
287UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
288{
289 int r;
290 stat_t statbuf;
291#if defined(_MSC_VER)
292 r = _stat64(infilename, &statbuf);
293 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
294#else
295 r = stat(infilename, &statbuf);
296 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
297#endif
298 return 0;
299}
300
Sean Purcell680e4e02017-03-23 11:52:09 -0700301UTIL_STATIC U32 UTIL_isLink(const char* infilename)
302{
303#if defined(_WIN32)
304 /* no symlinks on windows */
305 (void)infilename;
306#else
307 int r;
308 stat_t statbuf;
309 r = lstat(infilename, &statbuf);
310 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
311#endif
312 return 0;
313}
314
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100315
inikep69fcd7c2016-04-28 12:23:33 +0200316UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
317{
318 int r;
319#if defined(_MSC_VER)
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +0100320 struct __stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200321 r = _stat64(infilename, &statbuf);
322 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
ds7745f0c202017-02-10 18:37:57 +0100323#elif defined(__MINGW32__) && defined (__MSVCRT__)
324 struct _stati64 statbuf;
325 r = _stati64(infilename, &statbuf);
326 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
inikep69fcd7c2016-04-28 12:23:33 +0200327#else
328 struct stat statbuf;
329 r = stat(infilename, &statbuf);
330 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
331#endif
332 return (U64)statbuf.st_size;
333}
334
335
inikep55d047a2016-04-28 16:50:13 +0200336UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
337{
338 U64 total = 0;
339 unsigned n;
340 for (n=0; n<nbFiles; n++)
341 total += UTIL_getFileSize(fileNamesTable[n]);
342 return total;
343}
344
345
inikep61739312016-09-15 18:58:18 +0200346/*
347 * A modified version of realloc().
348 * If UTIL_realloc() fails the original block is freed.
349*/
350UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
351{
352 void *newptr = realloc(ptr, size);
353 if (newptr) return newptr;
354 free(ptr);
355 return NULL;
356}
357
inikep31634032016-05-05 00:25:38 +0200358#ifdef _WIN32
inikep9c22e572016-05-05 11:53:42 +0200359# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200360
Sean Purcell680e4e02017-03-23 11:52:09 -0700361UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200362{
inikep1c5ba8a2016-09-13 13:13:10 +0200363 char* path;
364 int dirLength, fnameLength, pathLength, nbFiles = 0;
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100365 WIN32_FIND_DATAA cFile;
inikep31634032016-05-05 00:25:38 +0200366 HANDLE hFile;
367
inikep9f25fcf2016-09-13 16:38:54 +0200368 dirLength = (int)strlen(dirName);
inikep1c5ba8a2016-09-13 13:13:10 +0200369 path = (char*) malloc(dirLength + 3);
370 if (!path) return 0;
371
372 memcpy(path, dirName, dirLength);
373 path[dirLength] = '\\';
374 path[dirLength+1] = '*';
375 path[dirLength+2] = 0;
inikep31634032016-05-05 00:25:38 +0200376
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100377 hFile=FindFirstFileA(path, &cFile);
inikep31634032016-05-05 00:25:38 +0200378 if (hFile == INVALID_HANDLE_VALUE) {
Yann Collet4299c272017-08-31 16:58:47 -0700379 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
inikep31634032016-05-05 00:25:38 +0200380 return 0;
381 }
inikep1c5ba8a2016-09-13 13:13:10 +0200382 free(path);
inikep31634032016-05-05 00:25:38 +0200383
inikep4dbf7f42016-05-11 14:11:00 +0200384 do {
inikep9f25fcf2016-09-13 16:38:54 +0200385 fnameLength = (int)strlen(cFile.cFileName);
inikep1c5ba8a2016-09-13 13:13:10 +0200386 path = (char*) malloc(dirLength + fnameLength + 2);
387 if (!path) { FindClose(hFile); return 0; }
388 memcpy(path, dirName, dirLength);
389 path[dirLength] = '\\';
390 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
391 pathLength = dirLength+1+fnameLength;
392 path[pathLength] = 0;
inikep31634032016-05-05 00:25:38 +0200393 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
394 if (strcmp (cFile.cFileName, "..") == 0 ||
inikep4dbf7f42016-05-11 14:11:00 +0200395 strcmp (cFile.cFileName, ".") == 0) continue;
inikepe416e302016-08-24 17:32:09 +0200396
Sean Purcell680e4e02017-03-23 11:52:09 -0700397 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200398 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep31634032016-05-05 00:25:38 +0200399 }
400 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
inikep4dbf7f42016-05-11 14:11:00 +0200401 if (*bufStart + *pos + pathLength >= *bufEnd) {
402 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200403 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200404 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200405 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200406 }
407 if (*bufStart + *pos + pathLength < *bufEnd) {
408 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
409 *pos += pathLength + 1;
410 nbFiles++;
411 }
inikep31634032016-05-05 00:25:38 +0200412 }
inikep1c5ba8a2016-09-13 13:13:10 +0200413 free(path);
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100414 } while (FindNextFileA(hFile, &cFile));
inikep31634032016-05-05 00:25:38 +0200415
416 FindClose(hFile);
417 return nbFiles;
418}
419
Przemyslaw Skibinski0b372052016-12-16 17:12:23 +0100420#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +0200421# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200422# include <dirent.h> /* opendir, readdir */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100423# include <string.h> /* strerror, memcpy */
inikep31634032016-05-05 00:25:38 +0200424
Sean Purcell680e4e02017-03-23 11:52:09 -0700425UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200426{
427 DIR *dir;
428 struct dirent *entry;
inikep1c5ba8a2016-09-13 13:13:10 +0200429 char* path;
430 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200431
inikep31634032016-05-05 00:25:38 +0200432 if (!(dir = opendir(dirName))) {
Yann Collet4299c272017-08-31 16:58:47 -0700433 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
inikep31634032016-05-05 00:25:38 +0200434 return 0;
435 }
Yann Collete162ace2016-05-20 11:24:35 +0200436
inikep9f25fcf2016-09-13 16:38:54 +0200437 dirLength = (int)strlen(dirName);
inikep7bc5c6b2016-07-26 11:07:37 +0200438 errno = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200439 while ((entry = readdir(dir)) != NULL) {
inikep0bd0fae2016-05-05 13:10:57 +0200440 if (strcmp (entry->d_name, "..") == 0 ||
441 strcmp (entry->d_name, ".") == 0) continue;
inikep9f25fcf2016-09-13 16:38:54 +0200442 fnameLength = (int)strlen(entry->d_name);
inikep1c5ba8a2016-09-13 13:13:10 +0200443 path = (char*) malloc(dirLength + fnameLength + 2);
444 if (!path) { closedir(dir); return 0; }
445 memcpy(path, dirName, dirLength);
Sean Purcell680e4e02017-03-23 11:52:09 -0700446
inikep1c5ba8a2016-09-13 13:13:10 +0200447 path[dirLength] = '/';
448 memcpy(path+dirLength+1, entry->d_name, fnameLength);
449 pathLength = dirLength+1+fnameLength;
450 path[pathLength] = 0;
451
Sean Purcell680e4e02017-03-23 11:52:09 -0700452 if (!followLinks && UTIL_isLink(path)) {
453 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
454 continue;
455 }
456
inikep0bd0fae2016-05-05 13:10:57 +0200457 if (UTIL_isDirectory(path)) {
Sean Purcell680e4e02017-03-23 11:52:09 -0700458 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200459 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep31634032016-05-05 00:25:38 +0200460 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200461 if (*bufStart + *pos + pathLength >= *bufEnd) {
462 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200463 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200464 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200465 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200466 }
467 if (*bufStart + *pos + pathLength < *bufEnd) {
468 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
469 *pos += pathLength + 1;
470 nbFiles++;
471 }
inikep31634032016-05-05 00:25:38 +0200472 }
inikep1c5ba8a2016-09-13 13:13:10 +0200473 free(path);
inikepe416e302016-08-24 17:32:09 +0200474 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
inikep31634032016-05-05 00:25:38 +0200475 }
476
inikep7bc5c6b2016-07-26 11:07:37 +0200477 if (errno != 0) {
Yann Collet4299c272017-08-31 16:58:47 -0700478 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
inikep7bc5c6b2016-07-26 11:07:37 +0200479 free(*bufStart);
480 *bufStart = NULL;
481 }
inikep31634032016-05-05 00:25:38 +0200482 closedir(dir);
483 return nbFiles;
484}
485
486#else
487
Sean Purcell680e4e02017-03-23 11:52:09 -0700488UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200489{
inikep4dbf7f42016-05-11 14:11:00 +0200490 (void)bufStart; (void)bufEnd; (void)pos;
Yann Collet4299c272017-08-31 16:58:47 -0700491 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
inikep31634032016-05-05 00:25:38 +0200492 return 0;
493}
494
inikepe416e302016-08-24 17:32:09 +0200495#endif /* #ifdef _WIN32 */
inikep31634032016-05-05 00:25:38 +0200496
Yann Collete162ace2016-05-20 11:24:35 +0200497/*
498 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
inikep0bdb6a82016-05-13 10:52:02 +0200499 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
500 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
501 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
502 */
Sean Purcell680e4e02017-03-23 11:52:09 -0700503UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
inikep31634032016-05-05 00:25:38 +0200504{
inikep4dbf7f42016-05-11 14:11:00 +0200505 size_t pos;
inikep0bdb6a82016-05-13 10:52:02 +0200506 unsigned i, nbFiles;
Yann Colletfda539f2016-12-12 01:03:23 +0100507 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
508 char* bufend = buf + LIST_SIZE_INCREASE;
inikep0bdb6a82016-05-13 10:52:02 +0200509 const char** fileTable;
inikep31634032016-05-05 00:25:38 +0200510
inikep0bdb6a82016-05-13 10:52:02 +0200511 if (!buf) return NULL;
inikep9c22e572016-05-05 11:53:42 +0200512
inikep0bdb6a82016-05-13 10:52:02 +0200513 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
inikep957823f2016-05-25 15:30:55 +0200514 if (!UTIL_isDirectory(inputNames[i])) {
Yann Colletfda539f2016-12-12 01:03:23 +0100515 size_t const len = strlen(inputNames[i]);
inikep4dbf7f42016-05-11 14:11:00 +0200516 if (buf + pos + len >= bufend) {
517 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200518 buf = (char*)UTIL_realloc(buf, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200519 bufend = buf + newListSize;
inikep0bdb6a82016-05-13 10:52:02 +0200520 if (!buf) return NULL;
inikep4dbf7f42016-05-11 14:11:00 +0200521 }
522 if (buf + pos + len < bufend) {
523 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
524 pos += len + 1;
525 nbFiles++;
526 }
Yann Collet415251c2016-08-01 14:26:49 +0200527 } else {
Sean Purcell680e4e02017-03-23 11:52:09 -0700528 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
inikep0bdb6a82016-05-13 10:52:02 +0200529 if (buf == NULL) return NULL;
Yann Collet415251c2016-08-01 14:26:49 +0200530 } }
inikep31634032016-05-05 00:25:38 +0200531
inikep0bdb6a82016-05-13 10:52:02 +0200532 if (nbFiles == 0) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200533
inikep0bdb6a82016-05-13 10:52:02 +0200534 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
535 if (!fileTable) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200536
Yann Colletfda539f2016-12-12 01:03:23 +0100537 for (i=0, pos=0; i<nbFiles; i++) {
inikep0bdb6a82016-05-13 10:52:02 +0200538 fileTable[i] = buf + pos;
539 pos += strlen(fileTable[i]) + 1;
inikep31634032016-05-05 00:25:38 +0200540 }
541
inikep0bdb6a82016-05-13 10:52:02 +0200542 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
543
544 *allocatedBuffer = buf;
545 *allocatedNamesNb = nbFiles;
546
547 return fileTable;
inikep31634032016-05-05 00:25:38 +0200548}
549
550
inikep0bdb6a82016-05-13 10:52:02 +0200551UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
inikep31634032016-05-05 00:25:38 +0200552{
inikep0bdb6a82016-05-13 10:52:02 +0200553 if (allocatedBuffer) free(allocatedBuffer);
554 if (filenameTable) free((void*)filenameTable);
inikep31634032016-05-05 00:25:38 +0200555}
556
Sean Purcellafa48512017-04-13 12:28:28 -0700557/* count the number of physical cores */
558#if defined(_WIN32) || defined(WIN32)
559
560#include <windows.h>
561
562typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
563
564UTIL_STATIC int UTIL_countPhysicalCores(void)
565{
Sean Purcelle4f32352017-04-13 16:34:28 -0700566 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700567 if (numPhysicalCores != 0) return numPhysicalCores;
568
569 { LPFN_GLPI glpi;
570 BOOL done = FALSE;
571 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
572 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
573 DWORD returnLength = 0;
574 size_t byteOffset = 0;
575
576 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
577 "GetLogicalProcessorInformation");
578
579 if (glpi == NULL) {
580 goto failed;
581 }
582
583 while(!done) {
584 DWORD rc = glpi(buffer, &returnLength);
585 if (FALSE == rc) {
586 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
587 if (buffer)
588 free(buffer);
589 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
590
591 if (buffer == NULL) {
592 perror("zstd");
593 exit(1);
594 }
595 } else {
596 /* some other error */
597 goto failed;
598 }
599 } else {
600 done = TRUE;
601 }
602 }
603
Sean Purcell3b6207d2017-04-13 14:03:56 -0700604 ptr = buffer;
Sean Purcellafa48512017-04-13 12:28:28 -0700605
Sean Purcell3b6207d2017-04-13 14:03:56 -0700606 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
607
608 if (ptr->Relationship == RelationProcessorCore) {
Sean Purcellafa48512017-04-13 12:28:28 -0700609 numPhysicalCores++;
610 }
Sean Purcell3b6207d2017-04-13 14:03:56 -0700611
612 ptr++;
613 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
Sean Purcellafa48512017-04-13 12:28:28 -0700614 }
615
616 free(buffer);
617
618 return numPhysicalCores;
619 }
620
621failed:
622 /* try to fall back on GetSystemInfo */
Sean Purcell3b6207d2017-04-13 14:03:56 -0700623 { SYSTEM_INFO sysinfo;
624 GetSystemInfo(&sysinfo);
625 numPhysicalCores = sysinfo.dwNumberOfProcessors;
626 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
627 }
Sean Purcellafa48512017-04-13 12:28:28 -0700628 return numPhysicalCores;
629}
630
631#elif defined(__APPLE__)
632
633#include <sys/sysctl.h>
634
635/* Use apple-provided syscall
636 * see: man 3 sysctl */
637UTIL_STATIC int UTIL_countPhysicalCores(void)
638{
Sean Purcelle4f32352017-04-13 16:34:28 -0700639 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
Sean Purcellafa48512017-04-13 12:28:28 -0700640 if (numPhysicalCores != 0) return numPhysicalCores;
641
Sean Purcellf876f122017-04-13 12:33:45 -0700642 { size_t size = sizeof(S32);
643 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
Sean Purcellafa48512017-04-13 12:28:28 -0700644 if (ret != 0) {
645 if (errno == ENOENT) {
646 /* entry not present, fall back on 1 */
647 numPhysicalCores = 1;
648 } else {
649 perror("zstd: can't get number of physical cpus");
650 exit(1);
651 }
652 }
653
654 return numPhysicalCores;
655 }
656}
657
658#elif defined(__linux__)
659
660/* parse /proc/cpuinfo
661 * siblings / cpu cores should give hyperthreading ratio
662 * otherwise fall back on sysconf */
663UTIL_STATIC int UTIL_countPhysicalCores(void)
664{
Sean Purcelle4f32352017-04-13 16:34:28 -0700665 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700666
667 if (numPhysicalCores != 0) return numPhysicalCores;
668
Sean Purcell9227aae2017-04-13 14:06:40 -0700669 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
Sean Purcellafa48512017-04-13 12:28:28 -0700670 if (numPhysicalCores == -1) {
671 /* value not queryable, fall back on 1 */
672 return numPhysicalCores = 1;
673 }
674
675 /* try to determine if there's hyperthreading */
676 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
Yann Collet46ac9ad2017-05-15 18:15:08 -0700677#define BUF_SIZE 80
Sean Purcellafa48512017-04-13 12:28:28 -0700678 char buff[BUF_SIZE];
679
680 int siblings = 0;
681 int cpu_cores = 0;
682 int ratio = 1;
683
684 if (cpuinfo == NULL) {
685 /* fall back on the sysconf value */
686 return numPhysicalCores;
687 }
688
689 /* assume the cpu cores/siblings values will be constant across all
690 * present processors */
691 while (!feof(cpuinfo)) {
692 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
693 if (strncmp(buff, "siblings", 8) == 0) {
694 const char* const sep = strchr(buff, ':');
695 if (*sep == '\0') {
696 /* formatting was broken? */
697 goto failed;
698 }
699
700 siblings = atoi(sep + 1);
701 }
702 if (strncmp(buff, "cpu cores", 9) == 0) {
703 const char* const sep = strchr(buff, ':');
704 if (*sep == '\0') {
705 /* formatting was broken? */
706 goto failed;
707 }
708
709 cpu_cores = atoi(sep + 1);
710 }
711 } else if (ferror(cpuinfo)) {
712 /* fall back on the sysconf value */
713 goto failed;
714 }
715 }
716 if (siblings && cpu_cores) {
717 ratio = siblings / cpu_cores;
718 }
719failed:
720 fclose(cpuinfo);
721 return numPhysicalCores = numPhysicalCores / ratio;
722 }
723}
724
Baptiste Daroussin7dd14d02017-04-15 16:25:08 +0200725#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
726
727/* Use apple-provided syscall
728 * see: man 3 sysctl */
729UTIL_STATIC int UTIL_countPhysicalCores(void)
730{
731 static int numPhysicalCores = 0;
732
733 if (numPhysicalCores != 0) return numPhysicalCores;
734
735 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
736 if (numPhysicalCores == -1) {
737 /* value not queryable, fall back on 1 */
738 return numPhysicalCores = 1;
739 }
740 return numPhysicalCores;
741}
742
Sean Purcellafa48512017-04-13 12:28:28 -0700743#else
744
745UTIL_STATIC int UTIL_countPhysicalCores(void)
746{
747 /* assume 1 */
748 return 1;
749}
750
751#endif
inikep31634032016-05-05 00:25:38 +0200752
inikep69fcd7c2016-04-28 12:23:33 +0200753#if defined (__cplusplus)
754}
755#endif
756
757#endif /* UTIL_H_MODULE */