Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 1 | /** |
| 2 | * util.h - utility functions |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 3 | * |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 4 | * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This source code is licensed under the BSD-style license found in the |
| 8 | * LICENSE file in the root directory of this source tree. An additional grant |
| 9 | * of patent rights can be found in the PATENTS file in the same directory. |
| 10 | */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 11 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 12 | #ifndef UTIL_H_MODULE |
| 13 | #define UTIL_H_MODULE |
| 14 | |
| 15 | #if defined (__cplusplus) |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 19 | |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 20 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 21 | /*-**************************************** |
| 22 | * Dependencies |
| 23 | ******************************************/ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 24 | #include "platform.h" /* PLATFORM_POSIX_VERSION */ |
| 25 | #include <stdlib.h> /* malloc */ |
| 26 | #include <stddef.h> /* size_t, ptrdiff_t */ |
| 27 | #include <stdio.h> /* fprintf */ |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 28 | #include <string.h> /* strncmp */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 29 | #include <sys/types.h> /* stat, utime */ |
| 30 | #include <sys/stat.h> /* stat */ |
Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 31 | #if defined(_MSC_VER) |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 32 | # include <sys/utime.h> /* utime */ |
| 33 | # include <io.h> /* _chmod */ |
Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 34 | #else |
Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 35 | # include <unistd.h> /* chown, stat */ |
| 36 | # include <utime.h> /* utime */ |
Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 37 | #endif |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 38 | #include <time.h> /* time */ |
Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 39 | #include <errno.h> |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 40 | #include "mem.h" /* U32, U64 */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 41 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 42 | |
Przemyslaw Skibinski | 6e59b3c | 2017-02-15 17:03:16 +0100 | [diff] [blame] | 43 | /* ************************************************************ |
| 44 | * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW |
| 45 | ***************************************************************/ |
| 46 | #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 47 | # define UTIL_fseek _fseeki64 |
| 48 | #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */ |
| 49 | # define UTIL_fseek fseeko |
| 50 | #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) |
| 51 | # define UTIL_fseek fseeko64 |
| 52 | #else |
| 53 | # define UTIL_fseek fseek |
| 54 | #endif |
| 55 | |
| 56 | |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 57 | /*-**************************************** |
| 58 | * Sleep functions: Windows - Posix - others |
| 59 | ******************************************/ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 60 | #if defined(_WIN32) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 61 | # include <windows.h> |
Przemyslaw Skibinski | 94abd6a | 2017-02-07 16:36:19 +0100 | [diff] [blame] | 62 | # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 63 | # define UTIL_sleep(s) Sleep(1000*s) |
| 64 | # define UTIL_sleepMilli(milli) Sleep(milli) |
Przemyslaw Skibinski | b0f3663 | 2016-12-16 15:41:18 +0100 | [diff] [blame] | 65 | #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 66 | # include <unistd.h> |
| 67 | # include <sys/resource.h> /* setpriority */ |
| 68 | # include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */ |
Peter (Stig) Edwards | 04773ac | 2016-05-21 12:15:48 +0100 | [diff] [blame] | 69 | # if defined(PRIO_PROCESS) |
Przemyslaw Skibinski | 94abd6a | 2017-02-07 16:36:19 +0100 | [diff] [blame] | 70 | # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20) |
Peter (Stig) Edwards | 04773ac | 2016-05-21 12:15:48 +0100 | [diff] [blame] | 71 | # else |
Przemyslaw Skibinski | 94abd6a | 2017-02-07 16:36:19 +0100 | [diff] [blame] | 72 | # define SET_REALTIME_PRIORITY /* disabled */ |
Peter (Stig) Edwards | 04773ac | 2016-05-21 12:15:48 +0100 | [diff] [blame] | 73 | # endif |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 74 | # define UTIL_sleep(s) sleep(s) |
Przemyslaw Skibinski | b0f3663 | 2016-12-16 15:41:18 +0100 | [diff] [blame] | 75 | # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */ |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 76 | # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); } |
| 77 | # else |
| 78 | # define UTIL_sleepMilli(milli) /* disabled */ |
| 79 | # endif |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 80 | #else |
Przemyslaw Skibinski | 94abd6a | 2017-02-07 16:36:19 +0100 | [diff] [blame] | 81 | # define SET_REALTIME_PRIORITY /* disabled */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 82 | # define UTIL_sleep(s) /* disabled */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 83 | # define UTIL_sleepMilli(milli) /* disabled */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 84 | #endif |
| 85 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 86 | |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 87 | /* ************************************* |
| 88 | * Constants |
| 89 | ***************************************/ |
| 90 | #define LIST_SIZE_INCREASE (8*1024) |
| 91 | |
| 92 | |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 93 | /*-**************************************** |
| 94 | * Compiler specifics |
| 95 | ******************************************/ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 96 | #if defined(__INTEL_COMPILER) |
| 97 | # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */ |
| 98 | #endif |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 99 | #if defined(__GNUC__) |
| 100 | # define UTIL_STATIC static __attribute__((unused)) |
| 101 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
| 102 | # define UTIL_STATIC static inline |
| 103 | #elif defined(_MSC_VER) |
| 104 | # define UTIL_STATIC static __inline |
| 105 | #else |
| 106 | # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
| 107 | #endif |
| 108 | |
| 109 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 110 | /*-**************************************** |
| 111 | * Time functions |
| 112 | ******************************************/ |
Przemyslaw Skibinski | 83775d9 | 2017-02-20 11:11:50 +0100 | [diff] [blame] | 113 | #if defined(_WIN32) /* Windows */ |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 114 | typedef LARGE_INTEGER UTIL_freq_t; |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 115 | typedef LARGE_INTEGER UTIL_time_t; |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 116 | UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { if (!QueryPerformanceFrequency(ticksPerSecond)) fprintf(stderr, "ERROR: QueryPerformance not present\n"); } |
inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 117 | UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { QueryPerformanceCounter(x); } |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 118 | 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; } |
| 119 | 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 Skibinski | da4a0f3 | 2017-02-20 12:18:15 +0100 | [diff] [blame] | 120 | #elif defined(__APPLE__) && defined(__MACH__) |
| 121 | #include <mach/mach_time.h> |
| 122 | typedef mach_timebase_info_data_t UTIL_freq_t; |
| 123 | typedef U64 UTIL_time_t; |
Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 124 | UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* rate) { mach_timebase_info(rate); } |
Przemyslaw Skibinski | da4a0f3 | 2017-02-20 12:18:15 +0100 | [diff] [blame] | 125 | UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = mach_absolute_time(); } |
| 126 | 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; } |
| 127 | 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 Skibinski | 1b59333 | 2017-02-21 07:33:45 +0100 | [diff] [blame] | 128 | #elif (PLATFORM_POSIX_VERSION >= 200112L) |
| 129 | #include <sys/times.h> /* times */ |
| 130 | typedef U64 UTIL_freq_t; |
| 131 | typedef U64 UTIL_time_t; |
| 132 | UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); } |
| 133 | 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; } |
| 134 | UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; } |
| 135 | UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; } |
cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 136 | #else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */ |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 137 | typedef clock_t UTIL_freq_t; |
cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 138 | typedef clock_t UTIL_time_t; |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 139 | UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=0; } |
cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 140 | UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); } |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 141 | 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; } |
| 142 | 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; } |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 143 | #endif |
| 144 | |
| 145 | |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 146 | /* returns time span in microseconds */ |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 147 | UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart, UTIL_freq_t ticksPerSecond ) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 148 | { |
| 149 | UTIL_time_t clockEnd; |
inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 150 | UTIL_getTime(&clockEnd); |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 151 | return UTIL_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd); |
| 152 | } |
| 153 | |
| 154 | |
Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 155 | UTIL_STATIC void UTIL_waitForNextTick(UTIL_freq_t ticksPerSecond) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 156 | { |
| 157 | UTIL_time_t clockStart, clockEnd; |
inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 158 | UTIL_getTime(&clockStart); |
Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 159 | do { |
| 160 | UTIL_getTime(&clockEnd); |
inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 161 | } while (UTIL_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0); |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 165 | |
| 166 | /*-**************************************** |
| 167 | * File functions |
| 168 | ******************************************/ |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 169 | #if defined(_MSC_VER) |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 170 | #define chmod _chmod |
| 171 | typedef struct __stat64 stat_t; |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 172 | #else |
| 173 | typedef struct stat stat_t; |
| 174 | #endif |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 175 | |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 176 | |
| 177 | UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) |
| 178 | { |
| 179 | int res = 0; |
| 180 | struct utimbuf timebuf; |
| 181 | |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 182 | timebuf.actime = time(NULL); |
| 183 | timebuf.modtime = statbuf->st_mtime; |
| 184 | res += utime(filename, &timebuf); /* set access and modification times */ |
Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 185 | |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 186 | #if !defined(_WIN32) |
| 187 | res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ |
| 188 | #endif |
| 189 | |
| 190 | res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */ |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 191 | |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 192 | errno = 0; |
| 193 | return -res; /* number of errors is returned */ |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 197 | UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf) |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 198 | { |
| 199 | int r; |
| 200 | #if defined(_MSC_VER) |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 201 | r = _stat64(infilename, statbuf); |
| 202 | if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */ |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 203 | #else |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 204 | r = stat(infilename, statbuf); |
| 205 | if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */ |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 206 | #endif |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 207 | return 1; |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Przemyslaw Skibinski | 442c75f | 2017-02-14 09:38:51 +0100 | [diff] [blame] | 210 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 211 | UTIL_STATIC int UTIL_isRegularFile(const char* infilename) |
Sean Purcell | 0f5c95a | 2017-02-07 16:33:48 -0800 | [diff] [blame] | 212 | { |
| 213 | stat_t statbuf; |
| 214 | return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */ |
| 215 | } |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 216 | |
Przemyslaw Skibinski | 442c75f | 2017-02-14 09:38:51 +0100 | [diff] [blame] | 217 | |
| 218 | UTIL_STATIC U32 UTIL_isDirectory(const char* infilename) |
| 219 | { |
| 220 | int r; |
| 221 | stat_t statbuf; |
| 222 | #if defined(_MSC_VER) |
| 223 | r = _stat64(infilename, &statbuf); |
| 224 | if (!r && (statbuf.st_mode & _S_IFDIR)) return 1; |
| 225 | #else |
| 226 | r = stat(infilename, &statbuf); |
| 227 | if (!r && S_ISDIR(statbuf.st_mode)) return 1; |
| 228 | #endif |
| 229 | return 0; |
| 230 | } |
| 231 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 232 | UTIL_STATIC U32 UTIL_isLink(const char* infilename) |
| 233 | { |
| 234 | #if defined(_WIN32) |
| 235 | /* no symlinks on windows */ |
| 236 | (void)infilename; |
| 237 | #else |
| 238 | int r; |
| 239 | stat_t statbuf; |
| 240 | r = lstat(infilename, &statbuf); |
| 241 | if (!r && S_ISLNK(statbuf.st_mode)) return 1; |
| 242 | #endif |
| 243 | return 0; |
| 244 | } |
| 245 | |
Przemyslaw Skibinski | 442c75f | 2017-02-14 09:38:51 +0100 | [diff] [blame] | 246 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 247 | UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) |
| 248 | { |
| 249 | int r; |
| 250 | #if defined(_MSC_VER) |
Przemyslaw Skibinski | 35bf23c | 2017-02-13 13:57:29 +0100 | [diff] [blame] | 251 | struct __stat64 statbuf; |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 252 | r = _stat64(infilename, &statbuf); |
| 253 | if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ |
ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame] | 254 | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
| 255 | struct _stati64 statbuf; |
| 256 | r = _stati64(infilename, &statbuf); |
| 257 | if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 258 | #else |
| 259 | struct stat statbuf; |
| 260 | r = stat(infilename, &statbuf); |
| 261 | if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ |
| 262 | #endif |
| 263 | return (U64)statbuf.st_size; |
| 264 | } |
| 265 | |
| 266 | |
inikep | 55d047a | 2016-04-28 16:50:13 +0200 | [diff] [blame] | 267 | UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 268 | { |
| 269 | U64 total = 0; |
| 270 | unsigned n; |
| 271 | for (n=0; n<nbFiles; n++) |
| 272 | total += UTIL_getFileSize(fileNamesTable[n]); |
| 273 | return total; |
| 274 | } |
| 275 | |
| 276 | |
inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 277 | /* |
| 278 | * A modified version of realloc(). |
| 279 | * If UTIL_realloc() fails the original block is freed. |
| 280 | */ |
| 281 | UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size) |
| 282 | { |
| 283 | void *newptr = realloc(ptr, size); |
| 284 | if (newptr) return newptr; |
| 285 | free(ptr); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Sean Purcell | dee08ca | 2017-03-23 12:09:35 -0700 | [diff] [blame] | 289 | static int g_utilDisplayLevel; |
| 290 | #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 291 | #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } } |
| 292 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 293 | #ifdef _WIN32 |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 294 | # define UTIL_HAS_CREATEFILELIST |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 295 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 296 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 297 | { |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 298 | char* path; |
| 299 | int dirLength, fnameLength, pathLength, nbFiles = 0; |
Przemyslaw Skibinski | acb6e57 | 2017-02-15 17:13:35 +0100 | [diff] [blame] | 300 | WIN32_FIND_DATAA cFile; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 301 | HANDLE hFile; |
| 302 | |
inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 303 | dirLength = (int)strlen(dirName); |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 304 | path = (char*) malloc(dirLength + 3); |
| 305 | if (!path) return 0; |
| 306 | |
| 307 | memcpy(path, dirName, dirLength); |
| 308 | path[dirLength] = '\\'; |
| 309 | path[dirLength+1] = '*'; |
| 310 | path[dirLength+2] = 0; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 311 | |
Przemyslaw Skibinski | acb6e57 | 2017-02-15 17:13:35 +0100 | [diff] [blame] | 312 | hFile=FindFirstFileA(path, &cFile); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 313 | if (hFile == INVALID_HANDLE_VALUE) { |
| 314 | fprintf(stderr, "Cannot open directory '%s'\n", dirName); |
| 315 | return 0; |
| 316 | } |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 317 | free(path); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 318 | |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 319 | do { |
inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 320 | fnameLength = (int)strlen(cFile.cFileName); |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 321 | path = (char*) malloc(dirLength + fnameLength + 2); |
| 322 | if (!path) { FindClose(hFile); return 0; } |
| 323 | memcpy(path, dirName, dirLength); |
| 324 | path[dirLength] = '\\'; |
| 325 | memcpy(path+dirLength+1, cFile.cFileName, fnameLength); |
| 326 | pathLength = dirLength+1+fnameLength; |
| 327 | path[pathLength] = 0; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 328 | if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 329 | if (strcmp (cFile.cFileName, "..") == 0 || |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 330 | strcmp (cFile.cFileName, ".") == 0) continue; |
inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 331 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 332 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 333 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 334 | } |
| 335 | else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) { |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 336 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
| 337 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 338 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 339 | *bufEnd = *bufStart + newListSize; |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 340 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 341 | } |
| 342 | if (*bufStart + *pos + pathLength < *bufEnd) { |
| 343 | strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos)); |
| 344 | *pos += pathLength + 1; |
| 345 | nbFiles++; |
| 346 | } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 347 | } |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 348 | free(path); |
Przemyslaw Skibinski | acb6e57 | 2017-02-15 17:13:35 +0100 | [diff] [blame] | 349 | } while (FindNextFileA(hFile, &cFile)); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 350 | |
| 351 | FindClose(hFile); |
| 352 | return nbFiles; |
| 353 | } |
| 354 | |
Przemyslaw Skibinski | 0b37205 | 2016-12-16 17:12:23 +0100 | [diff] [blame] | 355 | #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 356 | # define UTIL_HAS_CREATEFILELIST |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 357 | # include <dirent.h> /* opendir, readdir */ |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 358 | # include <string.h> /* strerror, memcpy */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 359 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 360 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 361 | { |
| 362 | DIR *dir; |
| 363 | struct dirent *entry; |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 364 | char* path; |
| 365 | int dirLength, fnameLength, pathLength, nbFiles = 0; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 366 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 367 | if (!(dir = opendir(dirName))) { |
| 368 | fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); |
| 369 | return 0; |
| 370 | } |
Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 371 | |
inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 372 | dirLength = (int)strlen(dirName); |
inikep | 7bc5c6b | 2016-07-26 11:07:37 +0200 | [diff] [blame] | 373 | errno = 0; |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 374 | while ((entry = readdir(dir)) != NULL) { |
inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 375 | if (strcmp (entry->d_name, "..") == 0 || |
| 376 | strcmp (entry->d_name, ".") == 0) continue; |
inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 377 | fnameLength = (int)strlen(entry->d_name); |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 378 | path = (char*) malloc(dirLength + fnameLength + 2); |
| 379 | if (!path) { closedir(dir); return 0; } |
| 380 | memcpy(path, dirName, dirLength); |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 381 | |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 382 | path[dirLength] = '/'; |
| 383 | memcpy(path+dirLength+1, entry->d_name, fnameLength); |
| 384 | pathLength = dirLength+1+fnameLength; |
| 385 | path[pathLength] = 0; |
| 386 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 387 | if (!followLinks && UTIL_isLink(path)) { |
| 388 | UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path); |
| 389 | continue; |
| 390 | } |
| 391 | |
inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 392 | if (UTIL_isDirectory(path)) { |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 393 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 394 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 395 | } else { |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 396 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
| 397 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 398 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 399 | *bufEnd = *bufStart + newListSize; |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 400 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 401 | } |
| 402 | if (*bufStart + *pos + pathLength < *bufEnd) { |
| 403 | strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos)); |
| 404 | *pos += pathLength + 1; |
| 405 | nbFiles++; |
| 406 | } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 407 | } |
inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 408 | free(path); |
inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 409 | errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 410 | } |
| 411 | |
inikep | 7bc5c6b | 2016-07-26 11:07:37 +0200 | [diff] [blame] | 412 | if (errno != 0) { |
| 413 | fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno)); |
| 414 | free(*bufStart); |
| 415 | *bufStart = NULL; |
| 416 | } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 417 | closedir(dir); |
| 418 | return nbFiles; |
| 419 | } |
| 420 | |
| 421 | #else |
| 422 | |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 423 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 424 | { |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 425 | (void)bufStart; (void)bufEnd; (void)pos; |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 426 | fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 430 | #endif /* #ifdef _WIN32 */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 431 | |
Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 432 | /* |
| 433 | * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 434 | * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb). |
| 435 | * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) |
| 436 | * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. |
| 437 | */ |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 438 | UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks) |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 439 | { |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 440 | size_t pos; |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 441 | unsigned i, nbFiles; |
Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 442 | char* buf = (char*)malloc(LIST_SIZE_INCREASE); |
| 443 | char* bufend = buf + LIST_SIZE_INCREASE; |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 444 | const char** fileTable; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 445 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 446 | if (!buf) return NULL; |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 447 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 448 | for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { |
inikep | 957823f | 2016-05-25 15:30:55 +0200 | [diff] [blame] | 449 | if (!UTIL_isDirectory(inputNames[i])) { |
Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 450 | size_t const len = strlen(inputNames[i]); |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 451 | if (buf + pos + len >= bufend) { |
| 452 | ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; |
inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 453 | buf = (char*)UTIL_realloc(buf, newListSize); |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 454 | bufend = buf + newListSize; |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 455 | if (!buf) return NULL; |
inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 456 | } |
| 457 | if (buf + pos + len < bufend) { |
| 458 | strncpy(buf + pos, inputNames[i], bufend - (buf + pos)); |
| 459 | pos += len + 1; |
| 460 | nbFiles++; |
| 461 | } |
Yann Collet | 415251c | 2016-08-01 14:26:49 +0200 | [diff] [blame] | 462 | } else { |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 463 | nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks); |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 464 | if (buf == NULL) return NULL; |
Yann Collet | 415251c | 2016-08-01 14:26:49 +0200 | [diff] [blame] | 465 | } } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 466 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 467 | if (nbFiles == 0) { free(buf); return NULL; } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 468 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 469 | fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); |
| 470 | if (!fileTable) { free(buf); return NULL; } |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 471 | |
Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 472 | for (i=0, pos=0; i<nbFiles; i++) { |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 473 | fileTable[i] = buf + pos; |
| 474 | pos += strlen(fileTable[i]) + 1; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 475 | } |
| 476 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 477 | if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } |
| 478 | |
| 479 | *allocatedBuffer = buf; |
| 480 | *allocatedNamesNb = nbFiles; |
| 481 | |
| 482 | return fileTable; |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 486 | UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 487 | { |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 488 | if (allocatedBuffer) free(allocatedBuffer); |
| 489 | if (filenameTable) free((void*)filenameTable); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 492 | /* count the number of physical cores */ |
| 493 | #if defined(_WIN32) || defined(WIN32) |
| 494 | |
| 495 | #include <windows.h> |
| 496 | |
| 497 | typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); |
| 498 | |
| 499 | UTIL_STATIC int UTIL_countPhysicalCores(void) |
| 500 | { |
Sean Purcell | e4f3235 | 2017-04-13 16:34:28 -0700 | [diff] [blame] | 501 | static int numPhysicalCores = 0; |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 502 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 503 | |
| 504 | { LPFN_GLPI glpi; |
| 505 | BOOL done = FALSE; |
| 506 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; |
| 507 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; |
| 508 | DWORD returnLength = 0; |
| 509 | size_t byteOffset = 0; |
| 510 | |
| 511 | glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), |
| 512 | "GetLogicalProcessorInformation"); |
| 513 | |
| 514 | if (glpi == NULL) { |
| 515 | goto failed; |
| 516 | } |
| 517 | |
| 518 | while(!done) { |
| 519 | DWORD rc = glpi(buffer, &returnLength); |
| 520 | if (FALSE == rc) { |
| 521 | if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 522 | if (buffer) |
| 523 | free(buffer); |
| 524 | buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); |
| 525 | |
| 526 | if (buffer == NULL) { |
| 527 | perror("zstd"); |
| 528 | exit(1); |
| 529 | } |
| 530 | } else { |
| 531 | /* some other error */ |
| 532 | goto failed; |
| 533 | } |
| 534 | } else { |
| 535 | done = TRUE; |
| 536 | } |
| 537 | } |
| 538 | |
Sean Purcell | 3b6207d | 2017-04-13 14:03:56 -0700 | [diff] [blame] | 539 | ptr = buffer; |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 540 | |
Sean Purcell | 3b6207d | 2017-04-13 14:03:56 -0700 | [diff] [blame] | 541 | while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { |
| 542 | |
| 543 | if (ptr->Relationship == RelationProcessorCore) { |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 544 | numPhysicalCores++; |
| 545 | } |
Sean Purcell | 3b6207d | 2017-04-13 14:03:56 -0700 | [diff] [blame] | 546 | |
| 547 | ptr++; |
| 548 | byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | free(buffer); |
| 552 | |
| 553 | return numPhysicalCores; |
| 554 | } |
| 555 | |
| 556 | failed: |
| 557 | /* try to fall back on GetSystemInfo */ |
Sean Purcell | 3b6207d | 2017-04-13 14:03:56 -0700 | [diff] [blame] | 558 | { SYSTEM_INFO sysinfo; |
| 559 | GetSystemInfo(&sysinfo); |
| 560 | numPhysicalCores = sysinfo.dwNumberOfProcessors; |
| 561 | if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */ |
| 562 | } |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 563 | return numPhysicalCores; |
| 564 | } |
| 565 | |
| 566 | #elif defined(__APPLE__) |
| 567 | |
| 568 | #include <sys/sysctl.h> |
| 569 | |
| 570 | /* Use apple-provided syscall |
| 571 | * see: man 3 sysctl */ |
| 572 | UTIL_STATIC int UTIL_countPhysicalCores(void) |
| 573 | { |
Sean Purcell | e4f3235 | 2017-04-13 16:34:28 -0700 | [diff] [blame] | 574 | static S32 numPhysicalCores = 0; /* apple specifies int32_t */ |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 575 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 576 | |
Sean Purcell | f876f12 | 2017-04-13 12:33:45 -0700 | [diff] [blame] | 577 | { size_t size = sizeof(S32); |
| 578 | int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0); |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 579 | if (ret != 0) { |
| 580 | if (errno == ENOENT) { |
| 581 | /* entry not present, fall back on 1 */ |
| 582 | numPhysicalCores = 1; |
| 583 | } else { |
| 584 | perror("zstd: can't get number of physical cpus"); |
| 585 | exit(1); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return numPhysicalCores; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | #elif defined(__linux__) |
| 594 | |
| 595 | /* parse /proc/cpuinfo |
| 596 | * siblings / cpu cores should give hyperthreading ratio |
| 597 | * otherwise fall back on sysconf */ |
| 598 | UTIL_STATIC int UTIL_countPhysicalCores(void) |
| 599 | { |
Sean Purcell | e4f3235 | 2017-04-13 16:34:28 -0700 | [diff] [blame] | 600 | static int numPhysicalCores = 0; |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 601 | |
| 602 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 603 | |
Sean Purcell | 9227aae | 2017-04-13 14:06:40 -0700 | [diff] [blame] | 604 | numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 605 | if (numPhysicalCores == -1) { |
| 606 | /* value not queryable, fall back on 1 */ |
| 607 | return numPhysicalCores = 1; |
| 608 | } |
| 609 | |
| 610 | /* try to determine if there's hyperthreading */ |
| 611 | { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r"); |
Yann Collet | 46ac9ad | 2017-05-15 18:15:08 -0700 | [diff] [blame] | 612 | #define BUF_SIZE 80 |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 613 | char buff[BUF_SIZE]; |
| 614 | |
| 615 | int siblings = 0; |
| 616 | int cpu_cores = 0; |
| 617 | int ratio = 1; |
| 618 | |
| 619 | if (cpuinfo == NULL) { |
| 620 | /* fall back on the sysconf value */ |
| 621 | return numPhysicalCores; |
| 622 | } |
| 623 | |
| 624 | /* assume the cpu cores/siblings values will be constant across all |
| 625 | * present processors */ |
| 626 | while (!feof(cpuinfo)) { |
| 627 | if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) { |
| 628 | if (strncmp(buff, "siblings", 8) == 0) { |
| 629 | const char* const sep = strchr(buff, ':'); |
| 630 | if (*sep == '\0') { |
| 631 | /* formatting was broken? */ |
| 632 | goto failed; |
| 633 | } |
| 634 | |
| 635 | siblings = atoi(sep + 1); |
| 636 | } |
| 637 | if (strncmp(buff, "cpu cores", 9) == 0) { |
| 638 | const char* const sep = strchr(buff, ':'); |
| 639 | if (*sep == '\0') { |
| 640 | /* formatting was broken? */ |
| 641 | goto failed; |
| 642 | } |
| 643 | |
| 644 | cpu_cores = atoi(sep + 1); |
| 645 | } |
| 646 | } else if (ferror(cpuinfo)) { |
| 647 | /* fall back on the sysconf value */ |
| 648 | goto failed; |
| 649 | } |
| 650 | } |
| 651 | if (siblings && cpu_cores) { |
| 652 | ratio = siblings / cpu_cores; |
| 653 | } |
| 654 | failed: |
| 655 | fclose(cpuinfo); |
| 656 | return numPhysicalCores = numPhysicalCores / ratio; |
| 657 | } |
| 658 | } |
| 659 | |
Baptiste Daroussin | 7dd14d0 | 2017-04-15 16:25:08 +0200 | [diff] [blame] | 660 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
| 661 | |
| 662 | /* Use apple-provided syscall |
| 663 | * see: man 3 sysctl */ |
| 664 | UTIL_STATIC int UTIL_countPhysicalCores(void) |
| 665 | { |
| 666 | static int numPhysicalCores = 0; |
| 667 | |
| 668 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 669 | |
| 670 | numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 671 | if (numPhysicalCores == -1) { |
| 672 | /* value not queryable, fall back on 1 */ |
| 673 | return numPhysicalCores = 1; |
| 674 | } |
| 675 | return numPhysicalCores; |
| 676 | } |
| 677 | |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 678 | #else |
| 679 | |
| 680 | UTIL_STATIC int UTIL_countPhysicalCores(void) |
| 681 | { |
| 682 | /* assume 1 */ |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | #endif |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 687 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 688 | #if defined (__cplusplus) |
| 689 | } |
| 690 | #endif |
| 691 | |
| 692 | #endif /* UTIL_H_MODULE */ |