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