xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 0f743729abbfc5c9d78a713f72241a4d4bd601ec)
10c16b537SWarner Losh /*
20c16b537SWarner Losh  * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh #ifndef UTIL_H_MODULE
120c16b537SWarner Losh #define UTIL_H_MODULE
130c16b537SWarner Losh 
140c16b537SWarner Losh #if defined (__cplusplus)
150c16b537SWarner Losh extern "C" {
160c16b537SWarner Losh #endif
170c16b537SWarner Losh 
180c16b537SWarner Losh 
190c16b537SWarner Losh 
200c16b537SWarner Losh /*-****************************************
210c16b537SWarner Losh *  Dependencies
220c16b537SWarner Losh ******************************************/
23*0f743729SConrad Meyer #include "platform.h"     /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
240c16b537SWarner Losh #include <stdlib.h>       /* malloc */
250c16b537SWarner Losh #include <stddef.h>       /* size_t, ptrdiff_t */
260c16b537SWarner Losh #include <stdio.h>        /* fprintf */
270c16b537SWarner Losh #include <string.h>       /* strncmp */
280c16b537SWarner Losh #include <sys/types.h>    /* stat, utime */
29*0f743729SConrad Meyer #include <sys/stat.h>     /* stat, chmod */
300c16b537SWarner Losh #if defined(_MSC_VER)
310c16b537SWarner Losh #  include <sys/utime.h>  /* utime */
320c16b537SWarner Losh #  include <io.h>         /* _chmod */
330c16b537SWarner Losh #else
340c16b537SWarner Losh #  include <unistd.h>     /* chown, stat */
350c16b537SWarner Losh #  include <utime.h>      /* utime */
360c16b537SWarner Losh #endif
37052d3c12SConrad Meyer #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
380c16b537SWarner Losh #include <errno.h>
390c16b537SWarner Losh #include "mem.h"          /* U32, U64 */
400c16b537SWarner Losh 
410c16b537SWarner Losh 
420c16b537SWarner Losh /* ************************************************************
43*0f743729SConrad Meyer * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
440c16b537SWarner Losh ***************************************************************/
450c16b537SWarner Losh #if defined(_MSC_VER) && (_MSC_VER >= 1400)
460c16b537SWarner Losh #   define UTIL_fseek _fseeki64
470c16b537SWarner Losh #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
480c16b537SWarner Losh #  define UTIL_fseek fseeko
490c16b537SWarner Losh #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
500c16b537SWarner Losh #   define UTIL_fseek fseeko64
510c16b537SWarner Losh #else
520c16b537SWarner Losh #   define UTIL_fseek fseek
530c16b537SWarner Losh #endif
540c16b537SWarner Losh 
550c16b537SWarner Losh 
56*0f743729SConrad Meyer /*-*************************************************
57*0f743729SConrad Meyer *  Sleep & priority functions: Windows - Posix - others
58*0f743729SConrad Meyer ***************************************************/
590c16b537SWarner Losh #if defined(_WIN32)
600c16b537SWarner Losh #  include <windows.h>
610c16b537SWarner Losh #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
620c16b537SWarner Losh #  define UTIL_sleep(s) Sleep(1000*s)
630c16b537SWarner Losh #  define UTIL_sleepMilli(milli) Sleep(milli)
64*0f743729SConrad Meyer 
65*0f743729SConrad Meyer #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
66*0f743729SConrad Meyer #  include <unistd.h>   /* sleep */
670c16b537SWarner Losh #  define UTIL_sleep(s) sleep(s)
68*0f743729SConrad Meyer #  if ZSTD_NANOSLEEP_SUPPORT   /* necessarily defined in platform.h */
690c16b537SWarner Losh #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
700c16b537SWarner Losh #  else
710c16b537SWarner Losh #      define UTIL_sleepMilli(milli) /* disabled */
720c16b537SWarner Losh #  endif
73*0f743729SConrad Meyer #  if ZSTD_SETPRIORITY_SUPPORT
74*0f743729SConrad Meyer #    include <sys/resource.h> /* setpriority */
75*0f743729SConrad Meyer #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
760c16b537SWarner Losh #  else
770c16b537SWarner Losh #    define SET_REALTIME_PRIORITY /* disabled */
78*0f743729SConrad Meyer #  endif
79*0f743729SConrad Meyer 
80*0f743729SConrad Meyer #else  /* unknown non-unix operating systen */
810c16b537SWarner Losh #  define UTIL_sleep(s)          /* disabled */
820c16b537SWarner Losh #  define UTIL_sleepMilli(milli) /* disabled */
83*0f743729SConrad Meyer #  define SET_REALTIME_PRIORITY  /* disabled */
840c16b537SWarner Losh #endif
850c16b537SWarner Losh 
860c16b537SWarner Losh 
870c16b537SWarner Losh /* *************************************
880c16b537SWarner Losh *  Constants
890c16b537SWarner Losh ***************************************/
900c16b537SWarner Losh #define LIST_SIZE_INCREASE   (8*1024)
910c16b537SWarner Losh 
920c16b537SWarner Losh 
930c16b537SWarner Losh /*-****************************************
940c16b537SWarner Losh *  Compiler specifics
950c16b537SWarner Losh ******************************************/
960c16b537SWarner Losh #if defined(__INTEL_COMPILER)
970c16b537SWarner Losh #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
980c16b537SWarner Losh #endif
990c16b537SWarner Losh #if defined(__GNUC__)
1000c16b537SWarner Losh #  define UTIL_STATIC static __attribute__((unused))
1010c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1020c16b537SWarner Losh #  define UTIL_STATIC static inline
1030c16b537SWarner Losh #elif defined(_MSC_VER)
1040c16b537SWarner Losh #  define UTIL_STATIC static __inline
1050c16b537SWarner Losh #else
1060c16b537SWarner Losh #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
1070c16b537SWarner Losh #endif
1080c16b537SWarner Losh 
1090c16b537SWarner Losh 
1100c16b537SWarner Losh /*-****************************************
1110c16b537SWarner Losh *  Console log
1120c16b537SWarner Losh ******************************************/
1130c16b537SWarner Losh static int g_utilDisplayLevel;
1140c16b537SWarner Losh #define UTIL_DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
1150c16b537SWarner Losh #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
1160c16b537SWarner Losh 
1170c16b537SWarner Losh 
1180c16b537SWarner Losh /*-****************************************
1190c16b537SWarner Losh *  Time functions
1200c16b537SWarner Losh ******************************************/
1210c16b537SWarner Losh #if defined(_WIN32)   /* Windows */
122052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER { { 0, 0 } }
1230c16b537SWarner Losh     typedef LARGE_INTEGER UTIL_time_t;
124*0f743729SConrad Meyer 
1250c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
1260c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1270c16b537SWarner Losh     {
1280c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
1290c16b537SWarner Losh         static int init = 0;
1300c16b537SWarner Losh         if (!init) {
1310c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
1320c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
1330c16b537SWarner Losh             init = 1;
1340c16b537SWarner Losh         }
1350c16b537SWarner Losh         return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
1360c16b537SWarner Losh     }
1370c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1380c16b537SWarner Losh     {
1390c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
1400c16b537SWarner Losh         static int init = 0;
1410c16b537SWarner Losh         if (!init) {
1420c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
1430c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
1440c16b537SWarner Losh             init = 1;
1450c16b537SWarner Losh         }
1460c16b537SWarner Losh         return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
1470c16b537SWarner Losh     }
14819fcbaf1SConrad Meyer 
1490c16b537SWarner Losh #elif defined(__APPLE__) && defined(__MACH__)
15019fcbaf1SConrad Meyer 
1510c16b537SWarner Losh     #include <mach/mach_time.h>
152052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER 0
1530c16b537SWarner Losh     typedef U64 UTIL_time_t;
154*0f743729SConrad Meyer 
1550c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
1560c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1570c16b537SWarner Losh     {
1580c16b537SWarner Losh         static mach_timebase_info_data_t rate;
1590c16b537SWarner Losh         static int init = 0;
1600c16b537SWarner Losh         if (!init) {
1610c16b537SWarner Losh             mach_timebase_info(&rate);
1620c16b537SWarner Losh             init = 1;
1630c16b537SWarner Losh         }
1640c16b537SWarner Losh         return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
1650c16b537SWarner Losh     }
1660c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1670c16b537SWarner Losh     {
1680c16b537SWarner Losh         static mach_timebase_info_data_t rate;
1690c16b537SWarner Losh         static int init = 0;
1700c16b537SWarner Losh         if (!init) {
1710c16b537SWarner Losh             mach_timebase_info(&rate);
1720c16b537SWarner Losh             init = 1;
1730c16b537SWarner Losh         }
1740c16b537SWarner Losh         return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
1750c16b537SWarner Losh     }
17619fcbaf1SConrad Meyer 
177*0f743729SConrad Meyer #elif (PLATFORM_POSIX_VERSION >= 200112L) \
178*0f743729SConrad Meyer    && (defined(__UCLIBC__)                \
179*0f743729SConrad Meyer       || (defined(__GLIBC__)              \
180*0f743729SConrad Meyer           && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) \
181*0f743729SConrad Meyer              || (__GLIBC__ > 2))))
18219fcbaf1SConrad Meyer 
183052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER { 0, 0 }
1840c16b537SWarner Losh     typedef struct timespec UTIL_freq_t;
1850c16b537SWarner Losh     typedef struct timespec UTIL_time_t;
186*0f743729SConrad Meyer 
1870c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void)
1880c16b537SWarner Losh     {
1890c16b537SWarner Losh         UTIL_time_t time;
1900c16b537SWarner Losh         if (clock_gettime(CLOCK_MONOTONIC, &time))
1910c16b537SWarner Losh             UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n");   /* we could also exit() */
1920c16b537SWarner Losh         return time;
1930c16b537SWarner Losh     }
194*0f743729SConrad Meyer 
1950c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
1960c16b537SWarner Losh     {
1970c16b537SWarner Losh         UTIL_time_t diff;
1980c16b537SWarner Losh         if (end.tv_nsec < begin.tv_nsec) {
1990c16b537SWarner Losh             diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
2000c16b537SWarner Losh             diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
2010c16b537SWarner Losh         } else {
2020c16b537SWarner Losh             diff.tv_sec = end.tv_sec - begin.tv_sec;
2030c16b537SWarner Losh             diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
2040c16b537SWarner Losh         }
2050c16b537SWarner Losh         return diff;
2060c16b537SWarner Losh     }
207*0f743729SConrad Meyer 
2080c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
2090c16b537SWarner Losh     {
2100c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
2110c16b537SWarner Losh         U64 micro = 0;
2120c16b537SWarner Losh         micro += 1000000ULL * diff.tv_sec;
2130c16b537SWarner Losh         micro += diff.tv_nsec / 1000ULL;
2140c16b537SWarner Losh         return micro;
2150c16b537SWarner Losh     }
216*0f743729SConrad Meyer 
2170c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
2180c16b537SWarner Losh     {
2190c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
2200c16b537SWarner Losh         U64 nano = 0;
2210c16b537SWarner Losh         nano += 1000000000ULL * diff.tv_sec;
2220c16b537SWarner Losh         nano += diff.tv_nsec;
2230c16b537SWarner Losh         return nano;
2240c16b537SWarner Losh     }
225*0f743729SConrad Meyer 
2260c16b537SWarner Losh #else   /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
2270c16b537SWarner Losh     typedef clock_t UTIL_time_t;
228052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER 0
2290c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
2300c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
2310c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
2320c16b537SWarner Losh #endif
2330c16b537SWarner Losh 
234052d3c12SConrad Meyer #define SEC_TO_MICRO 1000000
2350c16b537SWarner Losh 
2360c16b537SWarner Losh /* returns time span in microseconds */
2370c16b537SWarner Losh UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart )
2380c16b537SWarner Losh {
2390c16b537SWarner Losh     UTIL_time_t const clockEnd = UTIL_getTime();
2400c16b537SWarner Losh     return UTIL_getSpanTimeMicro(clockStart, clockEnd);
2410c16b537SWarner Losh }
2420c16b537SWarner Losh 
24319fcbaf1SConrad Meyer /* returns time span in microseconds */
24419fcbaf1SConrad Meyer UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart )
24519fcbaf1SConrad Meyer {
24619fcbaf1SConrad Meyer     UTIL_time_t const clockEnd = UTIL_getTime();
24719fcbaf1SConrad Meyer     return UTIL_getSpanTimeNano(clockStart, clockEnd);
24819fcbaf1SConrad Meyer }
2490c16b537SWarner Losh 
2500c16b537SWarner Losh UTIL_STATIC void UTIL_waitForNextTick(void)
2510c16b537SWarner Losh {
2520c16b537SWarner Losh     UTIL_time_t const clockStart = UTIL_getTime();
2530c16b537SWarner Losh     UTIL_time_t clockEnd;
2540c16b537SWarner Losh     do {
2550c16b537SWarner Losh         clockEnd = UTIL_getTime();
2560c16b537SWarner Losh     } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
2570c16b537SWarner Losh }
2580c16b537SWarner Losh 
2590c16b537SWarner Losh 
2600c16b537SWarner Losh 
2610c16b537SWarner Losh /*-****************************************
2620c16b537SWarner Losh *  File functions
2630c16b537SWarner Losh ******************************************/
2640c16b537SWarner Losh #if defined(_MSC_VER)
2650c16b537SWarner Losh     #define chmod _chmod
2660c16b537SWarner Losh     typedef struct __stat64 stat_t;
2670c16b537SWarner Losh #else
2680c16b537SWarner Losh     typedef struct stat stat_t;
2690c16b537SWarner Losh #endif
2700c16b537SWarner Losh 
2710c16b537SWarner Losh 
27219fcbaf1SConrad Meyer UTIL_STATIC int UTIL_isRegularFile(const char* infilename);
27319fcbaf1SConrad Meyer 
27419fcbaf1SConrad Meyer 
2750c16b537SWarner Losh UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
2760c16b537SWarner Losh {
2770c16b537SWarner Losh     int res = 0;
2780c16b537SWarner Losh     struct utimbuf timebuf;
2790c16b537SWarner Losh 
28019fcbaf1SConrad Meyer     if (!UTIL_isRegularFile(filename))
28119fcbaf1SConrad Meyer         return -1;
28219fcbaf1SConrad Meyer 
2830c16b537SWarner Losh     timebuf.actime = time(NULL);
2840c16b537SWarner Losh     timebuf.modtime = statbuf->st_mtime;
2850c16b537SWarner Losh     res += utime(filename, &timebuf);  /* set access and modification times */
2860c16b537SWarner Losh 
2870c16b537SWarner Losh #if !defined(_WIN32)
2880c16b537SWarner Losh     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
2890c16b537SWarner Losh #endif
2900c16b537SWarner Losh 
2910c16b537SWarner Losh     res += chmod(filename, statbuf->st_mode & 07777);  /* Copy file permissions */
2920c16b537SWarner Losh 
2930c16b537SWarner Losh     errno = 0;
2940c16b537SWarner Losh     return -res; /* number of errors is returned */
2950c16b537SWarner Losh }
2960c16b537SWarner Losh 
2970c16b537SWarner Losh 
2980c16b537SWarner Losh UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
2990c16b537SWarner Losh {
3000c16b537SWarner Losh     int r;
3010c16b537SWarner Losh #if defined(_MSC_VER)
3020c16b537SWarner Losh     r = _stat64(infilename, statbuf);
3030c16b537SWarner Losh     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
3040c16b537SWarner Losh #else
3050c16b537SWarner Losh     r = stat(infilename, statbuf);
3060c16b537SWarner Losh     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
3070c16b537SWarner Losh #endif
3080c16b537SWarner Losh     return 1;
3090c16b537SWarner Losh }
3100c16b537SWarner Losh 
3110c16b537SWarner Losh 
3120c16b537SWarner Losh UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
3130c16b537SWarner Losh {
3140c16b537SWarner Losh     stat_t statbuf;
3150c16b537SWarner Losh     return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
3160c16b537SWarner Losh }
3170c16b537SWarner Losh 
3180c16b537SWarner Losh 
3190c16b537SWarner Losh UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
3200c16b537SWarner Losh {
3210c16b537SWarner Losh     int r;
3220c16b537SWarner Losh     stat_t statbuf;
3230c16b537SWarner Losh #if defined(_MSC_VER)
3240c16b537SWarner Losh     r = _stat64(infilename, &statbuf);
3250c16b537SWarner Losh     if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
3260c16b537SWarner Losh #else
3270c16b537SWarner Losh     r = stat(infilename, &statbuf);
3280c16b537SWarner Losh     if (!r && S_ISDIR(statbuf.st_mode)) return 1;
3290c16b537SWarner Losh #endif
3300c16b537SWarner Losh     return 0;
3310c16b537SWarner Losh }
3320c16b537SWarner Losh 
3330c16b537SWarner Losh UTIL_STATIC U32 UTIL_isLink(const char* infilename)
3340c16b537SWarner Losh {
335*0f743729SConrad Meyer /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
336*0f743729SConrad Meyer #ifndef __STRICT_ANSI__
337*0f743729SConrad Meyer #if defined(_BSD_SOURCE) \
338*0f743729SConrad Meyer     || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \
339*0f743729SConrad Meyer     || (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) \
340*0f743729SConrad Meyer     || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \
341*0f743729SConrad Meyer     || (defined(__APPLE__) && defined(__MACH__))
3420c16b537SWarner Losh     int r;
3430c16b537SWarner Losh     stat_t statbuf;
3440c16b537SWarner Losh     r = lstat(infilename, &statbuf);
3450c16b537SWarner Losh     if (!r && S_ISLNK(statbuf.st_mode)) return 1;
3460c16b537SWarner Losh #endif
347*0f743729SConrad Meyer #endif
348*0f743729SConrad Meyer     (void)infilename;
3490c16b537SWarner Losh     return 0;
3500c16b537SWarner Losh }
3510c16b537SWarner Losh 
3520c16b537SWarner Losh 
353052d3c12SConrad Meyer #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
3540c16b537SWarner Losh UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
3550c16b537SWarner Losh {
356052d3c12SConrad Meyer     if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN;
357052d3c12SConrad Meyer     {   int r;
3580c16b537SWarner Losh #if defined(_MSC_VER)
3590c16b537SWarner Losh         struct __stat64 statbuf;
3600c16b537SWarner Losh         r = _stat64(infilename, &statbuf);
361052d3c12SConrad Meyer         if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
3620c16b537SWarner Losh #elif defined(__MINGW32__) && defined (__MSVCRT__)
3630c16b537SWarner Losh         struct _stati64 statbuf;
3640c16b537SWarner Losh         r = _stati64(infilename, &statbuf);
365052d3c12SConrad Meyer         if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
3660c16b537SWarner Losh #else
3670c16b537SWarner Losh         struct stat statbuf;
3680c16b537SWarner Losh         r = stat(infilename, &statbuf);
369052d3c12SConrad Meyer         if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN;
3700c16b537SWarner Losh #endif
3710c16b537SWarner Losh         return (U64)statbuf.st_size;
3720c16b537SWarner Losh     }
373052d3c12SConrad Meyer }
3740c16b537SWarner Losh 
3750c16b537SWarner Losh 
376052d3c12SConrad Meyer UTIL_STATIC U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles)
3770c16b537SWarner Losh {
3780c16b537SWarner Losh     U64 total = 0;
379052d3c12SConrad Meyer     int error = 0;
3800c16b537SWarner Losh     unsigned n;
381052d3c12SConrad Meyer     for (n=0; n<nbFiles; n++) {
382052d3c12SConrad Meyer         U64 const size = UTIL_getFileSize(fileNamesTable[n]);
383052d3c12SConrad Meyer         error |= (size == UTIL_FILESIZE_UNKNOWN);
384052d3c12SConrad Meyer         total += size;
385052d3c12SConrad Meyer     }
386052d3c12SConrad Meyer     return error ? UTIL_FILESIZE_UNKNOWN : total;
3870c16b537SWarner Losh }
3880c16b537SWarner Losh 
3890c16b537SWarner Losh 
3900c16b537SWarner Losh /*
3910c16b537SWarner Losh  * A modified version of realloc().
3920c16b537SWarner Losh  * If UTIL_realloc() fails the original block is freed.
3930c16b537SWarner Losh */
3940c16b537SWarner Losh UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
3950c16b537SWarner Losh {
3960c16b537SWarner Losh     void *newptr = realloc(ptr, size);
3970c16b537SWarner Losh     if (newptr) return newptr;
3980c16b537SWarner Losh     free(ptr);
3990c16b537SWarner Losh     return NULL;
4000c16b537SWarner Losh }
4010c16b537SWarner Losh 
4020c16b537SWarner Losh #ifdef _WIN32
4030c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
4040c16b537SWarner Losh 
4050c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
4060c16b537SWarner Losh {
4070c16b537SWarner Losh     char* path;
4080c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
4090c16b537SWarner Losh     WIN32_FIND_DATAA cFile;
4100c16b537SWarner Losh     HANDLE hFile;
4110c16b537SWarner Losh 
4120c16b537SWarner Losh     dirLength = (int)strlen(dirName);
4130c16b537SWarner Losh     path = (char*) malloc(dirLength + 3);
4140c16b537SWarner Losh     if (!path) return 0;
4150c16b537SWarner Losh 
4160c16b537SWarner Losh     memcpy(path, dirName, dirLength);
4170c16b537SWarner Losh     path[dirLength] = '\\';
4180c16b537SWarner Losh     path[dirLength+1] = '*';
4190c16b537SWarner Losh     path[dirLength+2] = 0;
4200c16b537SWarner Losh 
4210c16b537SWarner Losh     hFile=FindFirstFileA(path, &cFile);
4220c16b537SWarner Losh     if (hFile == INVALID_HANDLE_VALUE) {
4230c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
4240c16b537SWarner Losh         return 0;
4250c16b537SWarner Losh     }
4260c16b537SWarner Losh     free(path);
4270c16b537SWarner Losh 
4280c16b537SWarner Losh     do {
4290c16b537SWarner Losh         fnameLength = (int)strlen(cFile.cFileName);
4300c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
4310c16b537SWarner Losh         if (!path) { FindClose(hFile); return 0; }
4320c16b537SWarner Losh         memcpy(path, dirName, dirLength);
4330c16b537SWarner Losh         path[dirLength] = '\\';
4340c16b537SWarner Losh         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
4350c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
4360c16b537SWarner Losh         path[pathLength] = 0;
4370c16b537SWarner Losh         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
4380c16b537SWarner Losh             if (strcmp (cFile.cFileName, "..") == 0 ||
4390c16b537SWarner Losh                 strcmp (cFile.cFileName, ".") == 0) continue;
4400c16b537SWarner Losh 
4410c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
4420c16b537SWarner Losh             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
4430c16b537SWarner Losh         }
4440c16b537SWarner Losh         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
4450c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
4460c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
4470c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
4480c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
4490c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
4500c16b537SWarner Losh             }
4510c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
4520c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
4530c16b537SWarner Losh                 *pos += pathLength + 1;
4540c16b537SWarner Losh                 nbFiles++;
4550c16b537SWarner Losh             }
4560c16b537SWarner Losh         }
4570c16b537SWarner Losh         free(path);
4580c16b537SWarner Losh     } while (FindNextFileA(hFile, &cFile));
4590c16b537SWarner Losh 
4600c16b537SWarner Losh     FindClose(hFile);
4610c16b537SWarner Losh     return nbFiles;
4620c16b537SWarner Losh }
4630c16b537SWarner Losh 
4640c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
4650c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
4660c16b537SWarner Losh #  include <dirent.h>       /* opendir, readdir */
4670c16b537SWarner Losh #  include <string.h>       /* strerror, memcpy */
4680c16b537SWarner Losh 
4690c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
4700c16b537SWarner Losh {
4710c16b537SWarner Losh     DIR *dir;
4720c16b537SWarner Losh     struct dirent *entry;
4730c16b537SWarner Losh     char* path;
4740c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
4750c16b537SWarner Losh 
4760c16b537SWarner Losh     if (!(dir = opendir(dirName))) {
4770c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
4780c16b537SWarner Losh         return 0;
4790c16b537SWarner Losh     }
4800c16b537SWarner Losh 
4810c16b537SWarner Losh     dirLength = (int)strlen(dirName);
4820c16b537SWarner Losh     errno = 0;
4830c16b537SWarner Losh     while ((entry = readdir(dir)) != NULL) {
4840c16b537SWarner Losh         if (strcmp (entry->d_name, "..") == 0 ||
4850c16b537SWarner Losh             strcmp (entry->d_name, ".") == 0) continue;
4860c16b537SWarner Losh         fnameLength = (int)strlen(entry->d_name);
4870c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
4880c16b537SWarner Losh         if (!path) { closedir(dir); return 0; }
4890c16b537SWarner Losh         memcpy(path, dirName, dirLength);
4900c16b537SWarner Losh 
4910c16b537SWarner Losh         path[dirLength] = '/';
4920c16b537SWarner Losh         memcpy(path+dirLength+1, entry->d_name, fnameLength);
4930c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
4940c16b537SWarner Losh         path[pathLength] = 0;
4950c16b537SWarner Losh 
4960c16b537SWarner Losh         if (!followLinks && UTIL_isLink(path)) {
4970c16b537SWarner Losh             UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
4980c16b537SWarner Losh             continue;
4990c16b537SWarner Losh         }
5000c16b537SWarner Losh 
5010c16b537SWarner Losh         if (UTIL_isDirectory(path)) {
5020c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
5030c16b537SWarner Losh             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
5040c16b537SWarner Losh         } else {
5050c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
5060c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
5070c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
5080c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
5090c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
5100c16b537SWarner Losh             }
5110c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
5120c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
5130c16b537SWarner Losh                 *pos += pathLength + 1;
5140c16b537SWarner Losh                 nbFiles++;
5150c16b537SWarner Losh             }
5160c16b537SWarner Losh         }
5170c16b537SWarner Losh         free(path);
5180c16b537SWarner Losh         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
5190c16b537SWarner Losh     }
5200c16b537SWarner Losh 
5210c16b537SWarner Losh     if (errno != 0) {
5220c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
5230c16b537SWarner Losh         free(*bufStart);
5240c16b537SWarner Losh         *bufStart = NULL;
5250c16b537SWarner Losh     }
5260c16b537SWarner Losh     closedir(dir);
5270c16b537SWarner Losh     return nbFiles;
5280c16b537SWarner Losh }
5290c16b537SWarner Losh 
5300c16b537SWarner Losh #else
5310c16b537SWarner Losh 
5320c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
5330c16b537SWarner Losh {
534*0f743729SConrad Meyer     (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
5350c16b537SWarner Losh     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
5360c16b537SWarner Losh     return 0;
5370c16b537SWarner Losh }
5380c16b537SWarner Losh 
5390c16b537SWarner Losh #endif /* #ifdef _WIN32 */
5400c16b537SWarner Losh 
5410c16b537SWarner Losh /*
5420c16b537SWarner Losh  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
5430c16b537SWarner Losh  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
5440c16b537SWarner Losh  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
5450c16b537SWarner Losh  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
5460c16b537SWarner Losh  */
547*0f743729SConrad Meyer UTIL_STATIC const char**
548*0f743729SConrad Meyer UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
549*0f743729SConrad Meyer                     char** allocatedBuffer, unsigned* allocatedNamesNb,
550*0f743729SConrad Meyer                     int followLinks)
5510c16b537SWarner Losh {
5520c16b537SWarner Losh     size_t pos;
5530c16b537SWarner Losh     unsigned i, nbFiles;
5540c16b537SWarner Losh     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
5550c16b537SWarner Losh     char* bufend = buf + LIST_SIZE_INCREASE;
5560c16b537SWarner Losh     const char** fileTable;
5570c16b537SWarner Losh 
5580c16b537SWarner Losh     if (!buf) return NULL;
5590c16b537SWarner Losh 
5600c16b537SWarner Losh     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
5610c16b537SWarner Losh         if (!UTIL_isDirectory(inputNames[i])) {
5620c16b537SWarner Losh             size_t const len = strlen(inputNames[i]);
5630c16b537SWarner Losh             if (buf + pos + len >= bufend) {
5640c16b537SWarner Losh                 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
5650c16b537SWarner Losh                 buf = (char*)UTIL_realloc(buf, newListSize);
5660c16b537SWarner Losh                 bufend = buf + newListSize;
5670c16b537SWarner Losh                 if (!buf) return NULL;
5680c16b537SWarner Losh             }
5690c16b537SWarner Losh             if (buf + pos + len < bufend) {
5700c16b537SWarner Losh                 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
5710c16b537SWarner Losh                 pos += len + 1;
5720c16b537SWarner Losh                 nbFiles++;
5730c16b537SWarner Losh             }
5740c16b537SWarner Losh         } else {
5750c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
5760c16b537SWarner Losh             if (buf == NULL) return NULL;
5770c16b537SWarner Losh     }   }
5780c16b537SWarner Losh 
5790c16b537SWarner Losh     if (nbFiles == 0) { free(buf); return NULL; }
5800c16b537SWarner Losh 
5810c16b537SWarner Losh     fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
5820c16b537SWarner Losh     if (!fileTable) { free(buf); return NULL; }
5830c16b537SWarner Losh 
5840c16b537SWarner Losh     for (i=0, pos=0; i<nbFiles; i++) {
5850c16b537SWarner Losh         fileTable[i] = buf + pos;
5860c16b537SWarner Losh         pos += strlen(fileTable[i]) + 1;
5870c16b537SWarner Losh     }
5880c16b537SWarner Losh 
5890c16b537SWarner Losh     if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
5900c16b537SWarner Losh 
5910c16b537SWarner Losh     *allocatedBuffer = buf;
5920c16b537SWarner Losh     *allocatedNamesNb = nbFiles;
5930c16b537SWarner Losh 
5940c16b537SWarner Losh     return fileTable;
5950c16b537SWarner Losh }
5960c16b537SWarner Losh 
5970c16b537SWarner Losh 
5980c16b537SWarner Losh UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
5990c16b537SWarner Losh {
6000c16b537SWarner Losh     if (allocatedBuffer) free(allocatedBuffer);
6010c16b537SWarner Losh     if (filenameTable) free((void*)filenameTable);
6020c16b537SWarner Losh }
6030c16b537SWarner Losh 
6040c16b537SWarner Losh /* count the number of physical cores */
6050c16b537SWarner Losh #if defined(_WIN32) || defined(WIN32)
6060c16b537SWarner Losh 
6070c16b537SWarner Losh #include <windows.h>
6080c16b537SWarner Losh 
6090c16b537SWarner Losh typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
6100c16b537SWarner Losh 
6110c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
6120c16b537SWarner Losh {
6130c16b537SWarner Losh     static int numPhysicalCores = 0;
6140c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
6150c16b537SWarner Losh 
6160c16b537SWarner Losh     {   LPFN_GLPI glpi;
6170c16b537SWarner Losh         BOOL done = FALSE;
6180c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
6190c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
6200c16b537SWarner Losh         DWORD returnLength = 0;
6210c16b537SWarner Losh         size_t byteOffset = 0;
6220c16b537SWarner Losh 
6230c16b537SWarner Losh         glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
6240c16b537SWarner Losh                                          "GetLogicalProcessorInformation");
6250c16b537SWarner Losh 
6260c16b537SWarner Losh         if (glpi == NULL) {
6270c16b537SWarner Losh             goto failed;
6280c16b537SWarner Losh         }
6290c16b537SWarner Losh 
6300c16b537SWarner Losh         while(!done) {
6310c16b537SWarner Losh             DWORD rc = glpi(buffer, &returnLength);
6320c16b537SWarner Losh             if (FALSE == rc) {
6330c16b537SWarner Losh                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
6340c16b537SWarner Losh                     if (buffer)
6350c16b537SWarner Losh                         free(buffer);
6360c16b537SWarner Losh                     buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
6370c16b537SWarner Losh 
6380c16b537SWarner Losh                     if (buffer == NULL) {
6390c16b537SWarner Losh                         perror("zstd");
6400c16b537SWarner Losh                         exit(1);
6410c16b537SWarner Losh                     }
6420c16b537SWarner Losh                 } else {
6430c16b537SWarner Losh                     /* some other error */
6440c16b537SWarner Losh                     goto failed;
6450c16b537SWarner Losh                 }
6460c16b537SWarner Losh             } else {
6470c16b537SWarner Losh                 done = TRUE;
6480c16b537SWarner Losh             }
6490c16b537SWarner Losh         }
6500c16b537SWarner Losh 
6510c16b537SWarner Losh         ptr = buffer;
6520c16b537SWarner Losh 
6530c16b537SWarner Losh         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
6540c16b537SWarner Losh 
6550c16b537SWarner Losh             if (ptr->Relationship == RelationProcessorCore) {
6560c16b537SWarner Losh                 numPhysicalCores++;
6570c16b537SWarner Losh             }
6580c16b537SWarner Losh 
6590c16b537SWarner Losh             ptr++;
6600c16b537SWarner Losh             byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
6610c16b537SWarner Losh         }
6620c16b537SWarner Losh 
6630c16b537SWarner Losh         free(buffer);
6640c16b537SWarner Losh 
6650c16b537SWarner Losh         return numPhysicalCores;
6660c16b537SWarner Losh     }
6670c16b537SWarner Losh 
6680c16b537SWarner Losh failed:
6690c16b537SWarner Losh     /* try to fall back on GetSystemInfo */
6700c16b537SWarner Losh     {   SYSTEM_INFO sysinfo;
6710c16b537SWarner Losh         GetSystemInfo(&sysinfo);
6720c16b537SWarner Losh         numPhysicalCores = sysinfo.dwNumberOfProcessors;
6730c16b537SWarner Losh         if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
6740c16b537SWarner Losh     }
6750c16b537SWarner Losh     return numPhysicalCores;
6760c16b537SWarner Losh }
6770c16b537SWarner Losh 
6780c16b537SWarner Losh #elif defined(__APPLE__)
6790c16b537SWarner Losh 
6800c16b537SWarner Losh #include <sys/sysctl.h>
6810c16b537SWarner Losh 
6820c16b537SWarner Losh /* Use apple-provided syscall
6830c16b537SWarner Losh  * see: man 3 sysctl */
6840c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
6850c16b537SWarner Losh {
6860c16b537SWarner Losh     static S32 numPhysicalCores = 0; /* apple specifies int32_t */
6870c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
6880c16b537SWarner Losh 
6890c16b537SWarner Losh     {   size_t size = sizeof(S32);
6900c16b537SWarner Losh         int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
6910c16b537SWarner Losh         if (ret != 0) {
6920c16b537SWarner Losh             if (errno == ENOENT) {
6930c16b537SWarner Losh                 /* entry not present, fall back on 1 */
6940c16b537SWarner Losh                 numPhysicalCores = 1;
6950c16b537SWarner Losh             } else {
6960c16b537SWarner Losh                 perror("zstd: can't get number of physical cpus");
6970c16b537SWarner Losh                 exit(1);
6980c16b537SWarner Losh             }
6990c16b537SWarner Losh         }
7000c16b537SWarner Losh 
7010c16b537SWarner Losh         return numPhysicalCores;
7020c16b537SWarner Losh     }
7030c16b537SWarner Losh }
7040c16b537SWarner Losh 
7050c16b537SWarner Losh #elif defined(__linux__)
7060c16b537SWarner Losh 
7070c16b537SWarner Losh /* parse /proc/cpuinfo
7080c16b537SWarner Losh  * siblings / cpu cores should give hyperthreading ratio
7090c16b537SWarner Losh  * otherwise fall back on sysconf */
7100c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
7110c16b537SWarner Losh {
7120c16b537SWarner Losh     static int numPhysicalCores = 0;
7130c16b537SWarner Losh 
7140c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
7150c16b537SWarner Losh 
7160c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
7170c16b537SWarner Losh     if (numPhysicalCores == -1) {
7180c16b537SWarner Losh         /* value not queryable, fall back on 1 */
7190c16b537SWarner Losh         return numPhysicalCores = 1;
7200c16b537SWarner Losh     }
7210c16b537SWarner Losh 
7220c16b537SWarner Losh     /* try to determine if there's hyperthreading */
7230c16b537SWarner Losh     {   FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
7240c16b537SWarner Losh #define BUF_SIZE 80
7250c16b537SWarner Losh         char buff[BUF_SIZE];
7260c16b537SWarner Losh 
7270c16b537SWarner Losh         int siblings = 0;
7280c16b537SWarner Losh         int cpu_cores = 0;
7290c16b537SWarner Losh         int ratio = 1;
7300c16b537SWarner Losh 
7310c16b537SWarner Losh         if (cpuinfo == NULL) {
7320c16b537SWarner Losh             /* fall back on the sysconf value */
7330c16b537SWarner Losh             return numPhysicalCores;
7340c16b537SWarner Losh         }
7350c16b537SWarner Losh 
7360c16b537SWarner Losh         /* assume the cpu cores/siblings values will be constant across all
7370c16b537SWarner Losh          * present processors */
7380c16b537SWarner Losh         while (!feof(cpuinfo)) {
7390c16b537SWarner Losh             if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
7400c16b537SWarner Losh                 if (strncmp(buff, "siblings", 8) == 0) {
7410c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
7420c16b537SWarner Losh                     if (*sep == '\0') {
7430c16b537SWarner Losh                         /* formatting was broken? */
7440c16b537SWarner Losh                         goto failed;
7450c16b537SWarner Losh                     }
7460c16b537SWarner Losh 
7470c16b537SWarner Losh                     siblings = atoi(sep + 1);
7480c16b537SWarner Losh                 }
7490c16b537SWarner Losh                 if (strncmp(buff, "cpu cores", 9) == 0) {
7500c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
7510c16b537SWarner Losh                     if (*sep == '\0') {
7520c16b537SWarner Losh                         /* formatting was broken? */
7530c16b537SWarner Losh                         goto failed;
7540c16b537SWarner Losh                     }
7550c16b537SWarner Losh 
7560c16b537SWarner Losh                     cpu_cores = atoi(sep + 1);
7570c16b537SWarner Losh                 }
7580c16b537SWarner Losh             } else if (ferror(cpuinfo)) {
7590c16b537SWarner Losh                 /* fall back on the sysconf value */
7600c16b537SWarner Losh                 goto failed;
7610c16b537SWarner Losh             }
7620c16b537SWarner Losh         }
7630c16b537SWarner Losh         if (siblings && cpu_cores) {
7640c16b537SWarner Losh             ratio = siblings / cpu_cores;
7650c16b537SWarner Losh         }
7660c16b537SWarner Losh failed:
7670c16b537SWarner Losh         fclose(cpuinfo);
7680c16b537SWarner Losh         return numPhysicalCores = numPhysicalCores / ratio;
7690c16b537SWarner Losh     }
7700c16b537SWarner Losh }
7710c16b537SWarner Losh 
7720c16b537SWarner Losh #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
7730c16b537SWarner Losh 
7740c16b537SWarner Losh /* Use apple-provided syscall
7750c16b537SWarner Losh  * see: man 3 sysctl */
7760c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
7770c16b537SWarner Losh {
7780c16b537SWarner Losh     static int numPhysicalCores = 0;
7790c16b537SWarner Losh 
7800c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
7810c16b537SWarner Losh 
7820c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
7830c16b537SWarner Losh     if (numPhysicalCores == -1) {
7840c16b537SWarner Losh         /* value not queryable, fall back on 1 */
7850c16b537SWarner Losh         return numPhysicalCores = 1;
7860c16b537SWarner Losh     }
7870c16b537SWarner Losh     return numPhysicalCores;
7880c16b537SWarner Losh }
7890c16b537SWarner Losh 
7900c16b537SWarner Losh #else
7910c16b537SWarner Losh 
7920c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
7930c16b537SWarner Losh {
7940c16b537SWarner Losh     /* assume 1 */
7950c16b537SWarner Losh     return 1;
7960c16b537SWarner Losh }
7970c16b537SWarner Losh 
7980c16b537SWarner Losh #endif
7990c16b537SWarner Losh 
8000c16b537SWarner Losh #if defined (__cplusplus)
8010c16b537SWarner Losh }
8020c16b537SWarner Losh #endif
8030c16b537SWarner Losh 
8040c16b537SWarner Losh #endif /* UTIL_H_MODULE */
805