xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 19fcbaf1424b464269f1a7621fab747bb75afc36)
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 ******************************************/
230c16b537SWarner Losh #include "platform.h"     /* PLATFORM_POSIX_VERSION */
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 */
290c16b537SWarner Losh #include <sys/stat.h>     /* stat */
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 /* ************************************************************
430c16b537SWarner Losh * 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 
560c16b537SWarner Losh /*-****************************************
570c16b537SWarner Losh *  Sleep functions: Windows - Posix - others
580c16b537SWarner Losh ******************************************/
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)
640c16b537SWarner Losh #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
650c16b537SWarner Losh #  include <unistd.h>
660c16b537SWarner Losh #  include <sys/resource.h> /* setpriority */
670c16b537SWarner Losh #  if defined(PRIO_PROCESS)
680c16b537SWarner Losh #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
690c16b537SWarner Losh #  else
700c16b537SWarner Losh #    define SET_REALTIME_PRIORITY /* disabled */
710c16b537SWarner Losh #  endif
720c16b537SWarner Losh #  define UTIL_sleep(s) sleep(s)
730c16b537SWarner Losh #  if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L)  /* nanosleep requires POSIX.1-2001 */
740c16b537SWarner Losh #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
750c16b537SWarner Losh #  else
760c16b537SWarner Losh #      define UTIL_sleepMilli(milli) /* disabled */
770c16b537SWarner Losh #  endif
780c16b537SWarner Losh #else
790c16b537SWarner Losh #  define SET_REALTIME_PRIORITY      /* disabled */
800c16b537SWarner Losh #  define UTIL_sleep(s)          /* disabled */
810c16b537SWarner Losh #  define UTIL_sleepMilli(milli) /* disabled */
820c16b537SWarner Losh #endif
830c16b537SWarner Losh 
840c16b537SWarner Losh 
850c16b537SWarner Losh /* *************************************
860c16b537SWarner Losh *  Constants
870c16b537SWarner Losh ***************************************/
880c16b537SWarner Losh #define LIST_SIZE_INCREASE   (8*1024)
890c16b537SWarner Losh 
900c16b537SWarner Losh 
910c16b537SWarner Losh /*-****************************************
920c16b537SWarner Losh *  Compiler specifics
930c16b537SWarner Losh ******************************************/
940c16b537SWarner Losh #if defined(__INTEL_COMPILER)
950c16b537SWarner Losh #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
960c16b537SWarner Losh #endif
970c16b537SWarner Losh #if defined(__GNUC__)
980c16b537SWarner Losh #  define UTIL_STATIC static __attribute__((unused))
990c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1000c16b537SWarner Losh #  define UTIL_STATIC static inline
1010c16b537SWarner Losh #elif defined(_MSC_VER)
1020c16b537SWarner Losh #  define UTIL_STATIC static __inline
1030c16b537SWarner Losh #else
1040c16b537SWarner Losh #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
1050c16b537SWarner Losh #endif
1060c16b537SWarner Losh 
1070c16b537SWarner Losh 
1080c16b537SWarner Losh /*-****************************************
1090c16b537SWarner Losh *  Console log
1100c16b537SWarner Losh ******************************************/
1110c16b537SWarner Losh static int g_utilDisplayLevel;
1120c16b537SWarner Losh #define UTIL_DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
1130c16b537SWarner Losh #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
1140c16b537SWarner Losh 
1150c16b537SWarner Losh 
1160c16b537SWarner Losh /*-****************************************
1170c16b537SWarner Losh *  Time functions
1180c16b537SWarner Losh ******************************************/
1190c16b537SWarner Losh #if defined(_WIN32)   /* Windows */
120052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER { { 0, 0 } }
1210c16b537SWarner Losh     typedef LARGE_INTEGER UTIL_time_t;
1220c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
1230c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1240c16b537SWarner Losh     {
1250c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
1260c16b537SWarner Losh         static int init = 0;
1270c16b537SWarner Losh         if (!init) {
1280c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
1290c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
1300c16b537SWarner Losh             init = 1;
1310c16b537SWarner Losh         }
1320c16b537SWarner Losh         return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
1330c16b537SWarner Losh     }
1340c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1350c16b537SWarner Losh     {
1360c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
1370c16b537SWarner Losh         static int init = 0;
1380c16b537SWarner Losh         if (!init) {
1390c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
1400c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
1410c16b537SWarner Losh             init = 1;
1420c16b537SWarner Losh         }
1430c16b537SWarner Losh         return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
1440c16b537SWarner Losh     }
145*19fcbaf1SConrad Meyer 
1460c16b537SWarner Losh #elif defined(__APPLE__) && defined(__MACH__)
147*19fcbaf1SConrad Meyer 
1480c16b537SWarner Losh     #include <mach/mach_time.h>
149052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER 0
1500c16b537SWarner Losh     typedef U64 UTIL_time_t;
1510c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
1520c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1530c16b537SWarner Losh     {
1540c16b537SWarner Losh         static mach_timebase_info_data_t rate;
1550c16b537SWarner Losh         static int init = 0;
1560c16b537SWarner Losh         if (!init) {
1570c16b537SWarner Losh             mach_timebase_info(&rate);
1580c16b537SWarner Losh             init = 1;
1590c16b537SWarner Losh         }
1600c16b537SWarner Losh         return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
1610c16b537SWarner Losh     }
1620c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
1630c16b537SWarner Losh     {
1640c16b537SWarner Losh         static mach_timebase_info_data_t rate;
1650c16b537SWarner Losh         static int init = 0;
1660c16b537SWarner Losh         if (!init) {
1670c16b537SWarner Losh             mach_timebase_info(&rate);
1680c16b537SWarner Losh             init = 1;
1690c16b537SWarner Losh         }
1700c16b537SWarner Losh         return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
1710c16b537SWarner Losh     }
172*19fcbaf1SConrad Meyer 
173052d3c12SConrad Meyer #elif (PLATFORM_POSIX_VERSION >= 200112L) && (defined __UCLIBC__ || ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2))
174*19fcbaf1SConrad Meyer 
175052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER { 0, 0 }
1760c16b537SWarner Losh     typedef struct timespec UTIL_freq_t;
1770c16b537SWarner Losh     typedef struct timespec UTIL_time_t;
1780c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void)
1790c16b537SWarner Losh     {
1800c16b537SWarner Losh         UTIL_time_t time;
1810c16b537SWarner Losh         if (clock_gettime(CLOCK_MONOTONIC, &time))
1820c16b537SWarner Losh             UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n");   /* we could also exit() */
1830c16b537SWarner Losh         return time;
1840c16b537SWarner Losh     }
1850c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
1860c16b537SWarner Losh     {
1870c16b537SWarner Losh         UTIL_time_t diff;
1880c16b537SWarner Losh         if (end.tv_nsec < begin.tv_nsec) {
1890c16b537SWarner Losh             diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
1900c16b537SWarner Losh             diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
1910c16b537SWarner Losh         } else {
1920c16b537SWarner Losh             diff.tv_sec = end.tv_sec - begin.tv_sec;
1930c16b537SWarner Losh             diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
1940c16b537SWarner Losh         }
1950c16b537SWarner Losh         return diff;
1960c16b537SWarner Losh     }
1970c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
1980c16b537SWarner Losh     {
1990c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
2000c16b537SWarner Losh         U64 micro = 0;
2010c16b537SWarner Losh         micro += 1000000ULL * diff.tv_sec;
2020c16b537SWarner Losh         micro += diff.tv_nsec / 1000ULL;
2030c16b537SWarner Losh         return micro;
2040c16b537SWarner Losh     }
2050c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
2060c16b537SWarner Losh     {
2070c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
2080c16b537SWarner Losh         U64 nano = 0;
2090c16b537SWarner Losh         nano += 1000000000ULL * diff.tv_sec;
2100c16b537SWarner Losh         nano += diff.tv_nsec;
2110c16b537SWarner Losh         return nano;
2120c16b537SWarner Losh     }
2130c16b537SWarner Losh #else   /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
2140c16b537SWarner Losh     typedef clock_t UTIL_time_t;
215052d3c12SConrad Meyer     #define UTIL_TIME_INITIALIZER 0
2160c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
2170c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
2180c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
2190c16b537SWarner Losh #endif
2200c16b537SWarner Losh 
221052d3c12SConrad Meyer #define SEC_TO_MICRO 1000000
2220c16b537SWarner Losh 
2230c16b537SWarner Losh /* returns time span in microseconds */
2240c16b537SWarner Losh UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart )
2250c16b537SWarner Losh {
2260c16b537SWarner Losh     UTIL_time_t const clockEnd = UTIL_getTime();
2270c16b537SWarner Losh     return UTIL_getSpanTimeMicro(clockStart, clockEnd);
2280c16b537SWarner Losh }
2290c16b537SWarner Losh 
230*19fcbaf1SConrad Meyer /* returns time span in microseconds */
231*19fcbaf1SConrad Meyer UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart )
232*19fcbaf1SConrad Meyer {
233*19fcbaf1SConrad Meyer     UTIL_time_t const clockEnd = UTIL_getTime();
234*19fcbaf1SConrad Meyer     return UTIL_getSpanTimeNano(clockStart, clockEnd);
235*19fcbaf1SConrad Meyer }
2360c16b537SWarner Losh 
2370c16b537SWarner Losh UTIL_STATIC void UTIL_waitForNextTick(void)
2380c16b537SWarner Losh {
2390c16b537SWarner Losh     UTIL_time_t const clockStart = UTIL_getTime();
2400c16b537SWarner Losh     UTIL_time_t clockEnd;
2410c16b537SWarner Losh     do {
2420c16b537SWarner Losh         clockEnd = UTIL_getTime();
2430c16b537SWarner Losh     } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
2440c16b537SWarner Losh }
2450c16b537SWarner Losh 
2460c16b537SWarner Losh 
2470c16b537SWarner Losh 
2480c16b537SWarner Losh /*-****************************************
2490c16b537SWarner Losh *  File functions
2500c16b537SWarner Losh ******************************************/
2510c16b537SWarner Losh #if defined(_MSC_VER)
2520c16b537SWarner Losh     #define chmod _chmod
2530c16b537SWarner Losh     typedef struct __stat64 stat_t;
2540c16b537SWarner Losh #else
2550c16b537SWarner Losh     typedef struct stat stat_t;
2560c16b537SWarner Losh #endif
2570c16b537SWarner Losh 
2580c16b537SWarner Losh 
259*19fcbaf1SConrad Meyer UTIL_STATIC int UTIL_isRegularFile(const char* infilename);
260*19fcbaf1SConrad Meyer 
261*19fcbaf1SConrad Meyer 
2620c16b537SWarner Losh UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
2630c16b537SWarner Losh {
2640c16b537SWarner Losh     int res = 0;
2650c16b537SWarner Losh     struct utimbuf timebuf;
2660c16b537SWarner Losh 
267*19fcbaf1SConrad Meyer     if (!UTIL_isRegularFile(filename))
268*19fcbaf1SConrad Meyer         return -1;
269*19fcbaf1SConrad Meyer 
2700c16b537SWarner Losh     timebuf.actime = time(NULL);
2710c16b537SWarner Losh     timebuf.modtime = statbuf->st_mtime;
2720c16b537SWarner Losh     res += utime(filename, &timebuf);  /* set access and modification times */
2730c16b537SWarner Losh 
2740c16b537SWarner Losh #if !defined(_WIN32)
2750c16b537SWarner Losh     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
2760c16b537SWarner Losh #endif
2770c16b537SWarner Losh 
2780c16b537SWarner Losh     res += chmod(filename, statbuf->st_mode & 07777);  /* Copy file permissions */
2790c16b537SWarner Losh 
2800c16b537SWarner Losh     errno = 0;
2810c16b537SWarner Losh     return -res; /* number of errors is returned */
2820c16b537SWarner Losh }
2830c16b537SWarner Losh 
2840c16b537SWarner Losh 
2850c16b537SWarner Losh UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
2860c16b537SWarner Losh {
2870c16b537SWarner Losh     int r;
2880c16b537SWarner Losh #if defined(_MSC_VER)
2890c16b537SWarner Losh     r = _stat64(infilename, statbuf);
2900c16b537SWarner Losh     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
2910c16b537SWarner Losh #else
2920c16b537SWarner Losh     r = stat(infilename, statbuf);
2930c16b537SWarner Losh     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
2940c16b537SWarner Losh #endif
2950c16b537SWarner Losh     return 1;
2960c16b537SWarner Losh }
2970c16b537SWarner Losh 
2980c16b537SWarner Losh 
2990c16b537SWarner Losh UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
3000c16b537SWarner Losh {
3010c16b537SWarner Losh     stat_t statbuf;
3020c16b537SWarner Losh     return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
3030c16b537SWarner Losh }
3040c16b537SWarner Losh 
3050c16b537SWarner Losh 
3060c16b537SWarner Losh UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
3070c16b537SWarner Losh {
3080c16b537SWarner Losh     int r;
3090c16b537SWarner Losh     stat_t statbuf;
3100c16b537SWarner Losh #if defined(_MSC_VER)
3110c16b537SWarner Losh     r = _stat64(infilename, &statbuf);
3120c16b537SWarner Losh     if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
3130c16b537SWarner Losh #else
3140c16b537SWarner Losh     r = stat(infilename, &statbuf);
3150c16b537SWarner Losh     if (!r && S_ISDIR(statbuf.st_mode)) return 1;
3160c16b537SWarner Losh #endif
3170c16b537SWarner Losh     return 0;
3180c16b537SWarner Losh }
3190c16b537SWarner Losh 
3200c16b537SWarner Losh UTIL_STATIC U32 UTIL_isLink(const char* infilename)
3210c16b537SWarner Losh {
3220c16b537SWarner Losh #if defined(_WIN32)
3230c16b537SWarner Losh     /* no symlinks on windows */
3240c16b537SWarner Losh     (void)infilename;
3250c16b537SWarner Losh #else
3260c16b537SWarner Losh     int r;
3270c16b537SWarner Losh     stat_t statbuf;
3280c16b537SWarner Losh     r = lstat(infilename, &statbuf);
3290c16b537SWarner Losh     if (!r && S_ISLNK(statbuf.st_mode)) return 1;
3300c16b537SWarner Losh #endif
3310c16b537SWarner Losh     return 0;
3320c16b537SWarner Losh }
3330c16b537SWarner Losh 
3340c16b537SWarner Losh 
335052d3c12SConrad Meyer #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
3360c16b537SWarner Losh UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
3370c16b537SWarner Losh {
338052d3c12SConrad Meyer     if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN;
339052d3c12SConrad Meyer     {   int r;
3400c16b537SWarner Losh #if defined(_MSC_VER)
3410c16b537SWarner Losh         struct __stat64 statbuf;
3420c16b537SWarner Losh         r = _stat64(infilename, &statbuf);
343052d3c12SConrad Meyer         if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
3440c16b537SWarner Losh #elif defined(__MINGW32__) && defined (__MSVCRT__)
3450c16b537SWarner Losh         struct _stati64 statbuf;
3460c16b537SWarner Losh         r = _stati64(infilename, &statbuf);
347052d3c12SConrad Meyer         if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
3480c16b537SWarner Losh #else
3490c16b537SWarner Losh         struct stat statbuf;
3500c16b537SWarner Losh         r = stat(infilename, &statbuf);
351052d3c12SConrad Meyer         if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN;
3520c16b537SWarner Losh #endif
3530c16b537SWarner Losh         return (U64)statbuf.st_size;
3540c16b537SWarner Losh     }
355052d3c12SConrad Meyer }
3560c16b537SWarner Losh 
3570c16b537SWarner Losh 
358052d3c12SConrad Meyer UTIL_STATIC U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles)
3590c16b537SWarner Losh {
3600c16b537SWarner Losh     U64 total = 0;
361052d3c12SConrad Meyer     int error = 0;
3620c16b537SWarner Losh     unsigned n;
363052d3c12SConrad Meyer     for (n=0; n<nbFiles; n++) {
364052d3c12SConrad Meyer         U64 const size = UTIL_getFileSize(fileNamesTable[n]);
365052d3c12SConrad Meyer         error |= (size == UTIL_FILESIZE_UNKNOWN);
366052d3c12SConrad Meyer         total += size;
367052d3c12SConrad Meyer     }
368052d3c12SConrad Meyer     return error ? UTIL_FILESIZE_UNKNOWN : total;
3690c16b537SWarner Losh }
3700c16b537SWarner Losh 
3710c16b537SWarner Losh 
3720c16b537SWarner Losh /*
3730c16b537SWarner Losh  * A modified version of realloc().
3740c16b537SWarner Losh  * If UTIL_realloc() fails the original block is freed.
3750c16b537SWarner Losh */
3760c16b537SWarner Losh UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
3770c16b537SWarner Losh {
3780c16b537SWarner Losh     void *newptr = realloc(ptr, size);
3790c16b537SWarner Losh     if (newptr) return newptr;
3800c16b537SWarner Losh     free(ptr);
3810c16b537SWarner Losh     return NULL;
3820c16b537SWarner Losh }
3830c16b537SWarner Losh 
3840c16b537SWarner Losh #ifdef _WIN32
3850c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
3860c16b537SWarner Losh 
3870c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
3880c16b537SWarner Losh {
3890c16b537SWarner Losh     char* path;
3900c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
3910c16b537SWarner Losh     WIN32_FIND_DATAA cFile;
3920c16b537SWarner Losh     HANDLE hFile;
3930c16b537SWarner Losh 
3940c16b537SWarner Losh     dirLength = (int)strlen(dirName);
3950c16b537SWarner Losh     path = (char*) malloc(dirLength + 3);
3960c16b537SWarner Losh     if (!path) return 0;
3970c16b537SWarner Losh 
3980c16b537SWarner Losh     memcpy(path, dirName, dirLength);
3990c16b537SWarner Losh     path[dirLength] = '\\';
4000c16b537SWarner Losh     path[dirLength+1] = '*';
4010c16b537SWarner Losh     path[dirLength+2] = 0;
4020c16b537SWarner Losh 
4030c16b537SWarner Losh     hFile=FindFirstFileA(path, &cFile);
4040c16b537SWarner Losh     if (hFile == INVALID_HANDLE_VALUE) {
4050c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
4060c16b537SWarner Losh         return 0;
4070c16b537SWarner Losh     }
4080c16b537SWarner Losh     free(path);
4090c16b537SWarner Losh 
4100c16b537SWarner Losh     do {
4110c16b537SWarner Losh         fnameLength = (int)strlen(cFile.cFileName);
4120c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
4130c16b537SWarner Losh         if (!path) { FindClose(hFile); return 0; }
4140c16b537SWarner Losh         memcpy(path, dirName, dirLength);
4150c16b537SWarner Losh         path[dirLength] = '\\';
4160c16b537SWarner Losh         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
4170c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
4180c16b537SWarner Losh         path[pathLength] = 0;
4190c16b537SWarner Losh         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
4200c16b537SWarner Losh             if (strcmp (cFile.cFileName, "..") == 0 ||
4210c16b537SWarner Losh                 strcmp (cFile.cFileName, ".") == 0) continue;
4220c16b537SWarner Losh 
4230c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
4240c16b537SWarner Losh             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
4250c16b537SWarner Losh         }
4260c16b537SWarner Losh         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
4270c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
4280c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
4290c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
4300c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
4310c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
4320c16b537SWarner Losh             }
4330c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
4340c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
4350c16b537SWarner Losh                 *pos += pathLength + 1;
4360c16b537SWarner Losh                 nbFiles++;
4370c16b537SWarner Losh             }
4380c16b537SWarner Losh         }
4390c16b537SWarner Losh         free(path);
4400c16b537SWarner Losh     } while (FindNextFileA(hFile, &cFile));
4410c16b537SWarner Losh 
4420c16b537SWarner Losh     FindClose(hFile);
4430c16b537SWarner Losh     return nbFiles;
4440c16b537SWarner Losh }
4450c16b537SWarner Losh 
4460c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
4470c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
4480c16b537SWarner Losh #  include <dirent.h>       /* opendir, readdir */
4490c16b537SWarner Losh #  include <string.h>       /* strerror, memcpy */
4500c16b537SWarner Losh 
4510c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
4520c16b537SWarner Losh {
4530c16b537SWarner Losh     DIR *dir;
4540c16b537SWarner Losh     struct dirent *entry;
4550c16b537SWarner Losh     char* path;
4560c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
4570c16b537SWarner Losh 
4580c16b537SWarner Losh     if (!(dir = opendir(dirName))) {
4590c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
4600c16b537SWarner Losh         return 0;
4610c16b537SWarner Losh     }
4620c16b537SWarner Losh 
4630c16b537SWarner Losh     dirLength = (int)strlen(dirName);
4640c16b537SWarner Losh     errno = 0;
4650c16b537SWarner Losh     while ((entry = readdir(dir)) != NULL) {
4660c16b537SWarner Losh         if (strcmp (entry->d_name, "..") == 0 ||
4670c16b537SWarner Losh             strcmp (entry->d_name, ".") == 0) continue;
4680c16b537SWarner Losh         fnameLength = (int)strlen(entry->d_name);
4690c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
4700c16b537SWarner Losh         if (!path) { closedir(dir); return 0; }
4710c16b537SWarner Losh         memcpy(path, dirName, dirLength);
4720c16b537SWarner Losh 
4730c16b537SWarner Losh         path[dirLength] = '/';
4740c16b537SWarner Losh         memcpy(path+dirLength+1, entry->d_name, fnameLength);
4750c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
4760c16b537SWarner Losh         path[pathLength] = 0;
4770c16b537SWarner Losh 
4780c16b537SWarner Losh         if (!followLinks && UTIL_isLink(path)) {
4790c16b537SWarner Losh             UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
4800c16b537SWarner Losh             continue;
4810c16b537SWarner Losh         }
4820c16b537SWarner Losh 
4830c16b537SWarner Losh         if (UTIL_isDirectory(path)) {
4840c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
4850c16b537SWarner Losh             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
4860c16b537SWarner Losh         } else {
4870c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
4880c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
4890c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
4900c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
4910c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
4920c16b537SWarner Losh             }
4930c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
4940c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
4950c16b537SWarner Losh                 *pos += pathLength + 1;
4960c16b537SWarner Losh                 nbFiles++;
4970c16b537SWarner Losh             }
4980c16b537SWarner Losh         }
4990c16b537SWarner Losh         free(path);
5000c16b537SWarner Losh         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
5010c16b537SWarner Losh     }
5020c16b537SWarner Losh 
5030c16b537SWarner Losh     if (errno != 0) {
5040c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
5050c16b537SWarner Losh         free(*bufStart);
5060c16b537SWarner Losh         *bufStart = NULL;
5070c16b537SWarner Losh     }
5080c16b537SWarner Losh     closedir(dir);
5090c16b537SWarner Losh     return nbFiles;
5100c16b537SWarner Losh }
5110c16b537SWarner Losh 
5120c16b537SWarner Losh #else
5130c16b537SWarner Losh 
5140c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
5150c16b537SWarner Losh {
5160c16b537SWarner Losh     (void)bufStart; (void)bufEnd; (void)pos;
5170c16b537SWarner Losh     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
5180c16b537SWarner Losh     return 0;
5190c16b537SWarner Losh }
5200c16b537SWarner Losh 
5210c16b537SWarner Losh #endif /* #ifdef _WIN32 */
5220c16b537SWarner Losh 
5230c16b537SWarner Losh /*
5240c16b537SWarner Losh  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
5250c16b537SWarner Losh  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
5260c16b537SWarner Losh  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
5270c16b537SWarner Losh  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
5280c16b537SWarner Losh  */
5290c16b537SWarner Losh UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
5300c16b537SWarner Losh {
5310c16b537SWarner Losh     size_t pos;
5320c16b537SWarner Losh     unsigned i, nbFiles;
5330c16b537SWarner Losh     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
5340c16b537SWarner Losh     char* bufend = buf + LIST_SIZE_INCREASE;
5350c16b537SWarner Losh     const char** fileTable;
5360c16b537SWarner Losh 
5370c16b537SWarner Losh     if (!buf) return NULL;
5380c16b537SWarner Losh 
5390c16b537SWarner Losh     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
5400c16b537SWarner Losh         if (!UTIL_isDirectory(inputNames[i])) {
5410c16b537SWarner Losh             size_t const len = strlen(inputNames[i]);
5420c16b537SWarner Losh             if (buf + pos + len >= bufend) {
5430c16b537SWarner Losh                 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
5440c16b537SWarner Losh                 buf = (char*)UTIL_realloc(buf, newListSize);
5450c16b537SWarner Losh                 bufend = buf + newListSize;
5460c16b537SWarner Losh                 if (!buf) return NULL;
5470c16b537SWarner Losh             }
5480c16b537SWarner Losh             if (buf + pos + len < bufend) {
5490c16b537SWarner Losh                 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
5500c16b537SWarner Losh                 pos += len + 1;
5510c16b537SWarner Losh                 nbFiles++;
5520c16b537SWarner Losh             }
5530c16b537SWarner Losh         } else {
5540c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
5550c16b537SWarner Losh             if (buf == NULL) return NULL;
5560c16b537SWarner Losh     }   }
5570c16b537SWarner Losh 
5580c16b537SWarner Losh     if (nbFiles == 0) { free(buf); return NULL; }
5590c16b537SWarner Losh 
5600c16b537SWarner Losh     fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
5610c16b537SWarner Losh     if (!fileTable) { free(buf); return NULL; }
5620c16b537SWarner Losh 
5630c16b537SWarner Losh     for (i=0, pos=0; i<nbFiles; i++) {
5640c16b537SWarner Losh         fileTable[i] = buf + pos;
5650c16b537SWarner Losh         pos += strlen(fileTable[i]) + 1;
5660c16b537SWarner Losh     }
5670c16b537SWarner Losh 
5680c16b537SWarner Losh     if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
5690c16b537SWarner Losh 
5700c16b537SWarner Losh     *allocatedBuffer = buf;
5710c16b537SWarner Losh     *allocatedNamesNb = nbFiles;
5720c16b537SWarner Losh 
5730c16b537SWarner Losh     return fileTable;
5740c16b537SWarner Losh }
5750c16b537SWarner Losh 
5760c16b537SWarner Losh 
5770c16b537SWarner Losh UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
5780c16b537SWarner Losh {
5790c16b537SWarner Losh     if (allocatedBuffer) free(allocatedBuffer);
5800c16b537SWarner Losh     if (filenameTable) free((void*)filenameTable);
5810c16b537SWarner Losh }
5820c16b537SWarner Losh 
5830c16b537SWarner Losh /* count the number of physical cores */
5840c16b537SWarner Losh #if defined(_WIN32) || defined(WIN32)
5850c16b537SWarner Losh 
5860c16b537SWarner Losh #include <windows.h>
5870c16b537SWarner Losh 
5880c16b537SWarner Losh typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
5890c16b537SWarner Losh 
5900c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
5910c16b537SWarner Losh {
5920c16b537SWarner Losh     static int numPhysicalCores = 0;
5930c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
5940c16b537SWarner Losh 
5950c16b537SWarner Losh     {   LPFN_GLPI glpi;
5960c16b537SWarner Losh         BOOL done = FALSE;
5970c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
5980c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
5990c16b537SWarner Losh         DWORD returnLength = 0;
6000c16b537SWarner Losh         size_t byteOffset = 0;
6010c16b537SWarner Losh 
6020c16b537SWarner Losh         glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
6030c16b537SWarner Losh                                          "GetLogicalProcessorInformation");
6040c16b537SWarner Losh 
6050c16b537SWarner Losh         if (glpi == NULL) {
6060c16b537SWarner Losh             goto failed;
6070c16b537SWarner Losh         }
6080c16b537SWarner Losh 
6090c16b537SWarner Losh         while(!done) {
6100c16b537SWarner Losh             DWORD rc = glpi(buffer, &returnLength);
6110c16b537SWarner Losh             if (FALSE == rc) {
6120c16b537SWarner Losh                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
6130c16b537SWarner Losh                     if (buffer)
6140c16b537SWarner Losh                         free(buffer);
6150c16b537SWarner Losh                     buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
6160c16b537SWarner Losh 
6170c16b537SWarner Losh                     if (buffer == NULL) {
6180c16b537SWarner Losh                         perror("zstd");
6190c16b537SWarner Losh                         exit(1);
6200c16b537SWarner Losh                     }
6210c16b537SWarner Losh                 } else {
6220c16b537SWarner Losh                     /* some other error */
6230c16b537SWarner Losh                     goto failed;
6240c16b537SWarner Losh                 }
6250c16b537SWarner Losh             } else {
6260c16b537SWarner Losh                 done = TRUE;
6270c16b537SWarner Losh             }
6280c16b537SWarner Losh         }
6290c16b537SWarner Losh 
6300c16b537SWarner Losh         ptr = buffer;
6310c16b537SWarner Losh 
6320c16b537SWarner Losh         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
6330c16b537SWarner Losh 
6340c16b537SWarner Losh             if (ptr->Relationship == RelationProcessorCore) {
6350c16b537SWarner Losh                 numPhysicalCores++;
6360c16b537SWarner Losh             }
6370c16b537SWarner Losh 
6380c16b537SWarner Losh             ptr++;
6390c16b537SWarner Losh             byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
6400c16b537SWarner Losh         }
6410c16b537SWarner Losh 
6420c16b537SWarner Losh         free(buffer);
6430c16b537SWarner Losh 
6440c16b537SWarner Losh         return numPhysicalCores;
6450c16b537SWarner Losh     }
6460c16b537SWarner Losh 
6470c16b537SWarner Losh failed:
6480c16b537SWarner Losh     /* try to fall back on GetSystemInfo */
6490c16b537SWarner Losh     {   SYSTEM_INFO sysinfo;
6500c16b537SWarner Losh         GetSystemInfo(&sysinfo);
6510c16b537SWarner Losh         numPhysicalCores = sysinfo.dwNumberOfProcessors;
6520c16b537SWarner Losh         if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
6530c16b537SWarner Losh     }
6540c16b537SWarner Losh     return numPhysicalCores;
6550c16b537SWarner Losh }
6560c16b537SWarner Losh 
6570c16b537SWarner Losh #elif defined(__APPLE__)
6580c16b537SWarner Losh 
6590c16b537SWarner Losh #include <sys/sysctl.h>
6600c16b537SWarner Losh 
6610c16b537SWarner Losh /* Use apple-provided syscall
6620c16b537SWarner Losh  * see: man 3 sysctl */
6630c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
6640c16b537SWarner Losh {
6650c16b537SWarner Losh     static S32 numPhysicalCores = 0; /* apple specifies int32_t */
6660c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
6670c16b537SWarner Losh 
6680c16b537SWarner Losh     {   size_t size = sizeof(S32);
6690c16b537SWarner Losh         int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
6700c16b537SWarner Losh         if (ret != 0) {
6710c16b537SWarner Losh             if (errno == ENOENT) {
6720c16b537SWarner Losh                 /* entry not present, fall back on 1 */
6730c16b537SWarner Losh                 numPhysicalCores = 1;
6740c16b537SWarner Losh             } else {
6750c16b537SWarner Losh                 perror("zstd: can't get number of physical cpus");
6760c16b537SWarner Losh                 exit(1);
6770c16b537SWarner Losh             }
6780c16b537SWarner Losh         }
6790c16b537SWarner Losh 
6800c16b537SWarner Losh         return numPhysicalCores;
6810c16b537SWarner Losh     }
6820c16b537SWarner Losh }
6830c16b537SWarner Losh 
6840c16b537SWarner Losh #elif defined(__linux__)
6850c16b537SWarner Losh 
6860c16b537SWarner Losh /* parse /proc/cpuinfo
6870c16b537SWarner Losh  * siblings / cpu cores should give hyperthreading ratio
6880c16b537SWarner Losh  * otherwise fall back on sysconf */
6890c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
6900c16b537SWarner Losh {
6910c16b537SWarner Losh     static int numPhysicalCores = 0;
6920c16b537SWarner Losh 
6930c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
6940c16b537SWarner Losh 
6950c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
6960c16b537SWarner Losh     if (numPhysicalCores == -1) {
6970c16b537SWarner Losh         /* value not queryable, fall back on 1 */
6980c16b537SWarner Losh         return numPhysicalCores = 1;
6990c16b537SWarner Losh     }
7000c16b537SWarner Losh 
7010c16b537SWarner Losh     /* try to determine if there's hyperthreading */
7020c16b537SWarner Losh     {   FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
7030c16b537SWarner Losh #define BUF_SIZE 80
7040c16b537SWarner Losh         char buff[BUF_SIZE];
7050c16b537SWarner Losh 
7060c16b537SWarner Losh         int siblings = 0;
7070c16b537SWarner Losh         int cpu_cores = 0;
7080c16b537SWarner Losh         int ratio = 1;
7090c16b537SWarner Losh 
7100c16b537SWarner Losh         if (cpuinfo == NULL) {
7110c16b537SWarner Losh             /* fall back on the sysconf value */
7120c16b537SWarner Losh             return numPhysicalCores;
7130c16b537SWarner Losh         }
7140c16b537SWarner Losh 
7150c16b537SWarner Losh         /* assume the cpu cores/siblings values will be constant across all
7160c16b537SWarner Losh          * present processors */
7170c16b537SWarner Losh         while (!feof(cpuinfo)) {
7180c16b537SWarner Losh             if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
7190c16b537SWarner Losh                 if (strncmp(buff, "siblings", 8) == 0) {
7200c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
7210c16b537SWarner Losh                     if (*sep == '\0') {
7220c16b537SWarner Losh                         /* formatting was broken? */
7230c16b537SWarner Losh                         goto failed;
7240c16b537SWarner Losh                     }
7250c16b537SWarner Losh 
7260c16b537SWarner Losh                     siblings = atoi(sep + 1);
7270c16b537SWarner Losh                 }
7280c16b537SWarner Losh                 if (strncmp(buff, "cpu cores", 9) == 0) {
7290c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
7300c16b537SWarner Losh                     if (*sep == '\0') {
7310c16b537SWarner Losh                         /* formatting was broken? */
7320c16b537SWarner Losh                         goto failed;
7330c16b537SWarner Losh                     }
7340c16b537SWarner Losh 
7350c16b537SWarner Losh                     cpu_cores = atoi(sep + 1);
7360c16b537SWarner Losh                 }
7370c16b537SWarner Losh             } else if (ferror(cpuinfo)) {
7380c16b537SWarner Losh                 /* fall back on the sysconf value */
7390c16b537SWarner Losh                 goto failed;
7400c16b537SWarner Losh             }
7410c16b537SWarner Losh         }
7420c16b537SWarner Losh         if (siblings && cpu_cores) {
7430c16b537SWarner Losh             ratio = siblings / cpu_cores;
7440c16b537SWarner Losh         }
7450c16b537SWarner Losh failed:
7460c16b537SWarner Losh         fclose(cpuinfo);
7470c16b537SWarner Losh         return numPhysicalCores = numPhysicalCores / ratio;
7480c16b537SWarner Losh     }
7490c16b537SWarner Losh }
7500c16b537SWarner Losh 
7510c16b537SWarner Losh #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
7520c16b537SWarner Losh 
7530c16b537SWarner Losh /* Use apple-provided syscall
7540c16b537SWarner Losh  * see: man 3 sysctl */
7550c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
7560c16b537SWarner Losh {
7570c16b537SWarner Losh     static int numPhysicalCores = 0;
7580c16b537SWarner Losh 
7590c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
7600c16b537SWarner Losh 
7610c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
7620c16b537SWarner Losh     if (numPhysicalCores == -1) {
7630c16b537SWarner Losh         /* value not queryable, fall back on 1 */
7640c16b537SWarner Losh         return numPhysicalCores = 1;
7650c16b537SWarner Losh     }
7660c16b537SWarner Losh     return numPhysicalCores;
7670c16b537SWarner Losh }
7680c16b537SWarner Losh 
7690c16b537SWarner Losh #else
7700c16b537SWarner Losh 
7710c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
7720c16b537SWarner Losh {
7730c16b537SWarner Losh     /* assume 1 */
7740c16b537SWarner Losh     return 1;
7750c16b537SWarner Losh }
7760c16b537SWarner Losh 
7770c16b537SWarner Losh #endif
7780c16b537SWarner Losh 
7790c16b537SWarner Losh #if defined (__cplusplus)
7800c16b537SWarner Losh }
7810c16b537SWarner Losh #endif
7820c16b537SWarner Losh 
7830c16b537SWarner Losh #endif /* UTIL_H_MODULE */
784