/*
 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

#ifndef UTIL_H_MODULE
#define UTIL_H_MODULE

#if defined (__cplusplus)
extern "C" {
#endif


/*-****************************************
*  Dependencies
******************************************/
#include <stdlib.h>       /* malloc, realloc, free */
#include <stddef.h>       /* size_t, ptrdiff_t */
#include <stdio.h>        /* fprintf */
#include <sys/types.h>    /* stat, utime */
#include <sys/stat.h>     /* stat, chmod */
#if defined(_WIN32)
#  include <sys/utime.h>  /* utime */
#  include <io.h>         /* _chmod */
#else
#  include <unistd.h>     /* chown, stat */
#if PLATFORM_POSIX_VERSION < 200809L
#  include <utime.h>      /* utime */
#else
#  include <fcntl.h>      /* AT_FDCWD */
#  include <sys/stat.h>   /* utimensat */
#endif
#endif
#include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */


/*-****************************************
*  Compiler specifics
******************************************/
#if defined(__INTEL_COMPILER)
#  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
#endif
#if defined(__GNUC__)
#  define UTIL_STATIC static __attribute__((unused))
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
#  define UTIL_STATIC static inline
#elif defined(_MSC_VER)
#  define UTIL_STATIC static __inline
#else
#  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
#endif


/*-****************************************
*  File functions
******************************************/
#if defined(_MSC_VER)
    #define chmod _chmod
    typedef struct __stat64 stat_t;
#else
    typedef struct stat stat_t;
#endif

#if defined(_WIN32) || defined(WIN32)
 static int numLogicalCores = 1;
 static time_t lastTimeCached = 0;
 static int util_cpuCoresCacheTTL = 60;
#else
__attribute__((unused)) static int numLogicalCores = 1;
__attribute__((unused)) static time_t lastTimeCached = 0;
__attribute__((unused)) static int util_cpuCoresCacheTTL = 60;
#endif
int UTIL_countAvailableCores(void);
int UTIL_setCpuCoresCacheTTL(int cacheTTL);
int UTIL_stopCpuCoresCache(void);


#if defined (__cplusplus)
}
#endif

#endif /* UTIL_H_MODULE */
