From 6fba8aa8acdfe347f8b78ff087e58147250a9de8 Mon Sep 17 00:00:00 2001 From: Moinak Ghosh Date: Mon, 28 Apr 2014 00:12:51 +0530 Subject: [PATCH] More OSX compatibility code. Fix new warnings with Gcc 4.8. --- adaptive_compress.c | 2 ++ allocator.c | 8 +++--- allocator.h | 4 +-- lzma_compress.c | 15 ++++++++--- pcompress.c | 5 ---- ppmd_compress.c | 2 +- utils/cpuid.c | 2 +- utils/cpuid.h | 4 +-- utils/utils.c | 62 ++++++++++++++++++++++++++++++++++++++++++--- utils/utils.h | 46 ++++++++++++++++++++++++++------- 10 files changed, 119 insertions(+), 31 deletions(-) diff --git a/adaptive_compress.c b/adaptive_compress.c index 990a0d0..4ef924a 100644 --- a/adaptive_compress.c +++ b/adaptive_compress.c @@ -27,11 +27,13 @@ #include #include #include +/* #if defined(sun) || defined(__sun) #include #else #include #endif +*/ #include #include #include diff --git a/allocator.c b/allocator.c index 2879930..a463975 100644 --- a/allocator.c +++ b/allocator.c @@ -280,7 +280,7 @@ slab_cleanup(int quiet) } void * -slab_calloc(void *p, uint64_t items, uint64_t size) { +slab_calloc(void *p, size_t items, size_t size) { void *ptr; if (bypass) return(calloc(items, size)); @@ -376,7 +376,7 @@ slab_cache_add(uint64_t size) } void * -slab_alloc(void *p, uint64_t size) +slab_alloc(void *p, size_t size) { uint64_t div; struct slabentry *slab; @@ -517,13 +517,13 @@ void slab_cleanup(int quiet) {} void -*slab_alloc(void *p, uint64_t size) +*slab_alloc(void *p, size_t size) { return (malloc(size)); } void -*slab_calloc(void *p, uint64_t items, uint64_t size) +*slab_calloc(void *p, size_t items, size_t size) { return (calloc(items, size)); } diff --git a/allocator.h b/allocator.h index e23b09d..933d419 100644 --- a/allocator.h +++ b/allocator.h @@ -31,8 +31,8 @@ void slab_init(); void slab_cleanup(int quiet); -void *slab_alloc(void *p, uint64_t size); -void *slab_calloc(void *p, uint64_t items, uint64_t size); +void *slab_alloc(void *p, size_t size); +void *slab_calloc(void *p, size_t items, size_t size); void slab_free(void *p, void *address); void slab_release(void *p, void *address); int slab_cache_add(uint64_t size); diff --git a/lzma_compress.c b/lzma_compress.c index c7589a2..52c168a 100644 --- a/lzma_compress.c +++ b/lzma_compress.c @@ -201,10 +201,11 @@ int lzma_compress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen, int level, uchar_t chdr, int btype, void *data) { - uint64_t props_len = LZMA_PROPS_SIZE; + SizeT props_len = LZMA_PROPS_SIZE; SRes res; Byte *_dst; CLzmaEncProps *props = (CLzmaEncProps *)data; + SizeT dlen; if (*dstlen < LZMA_PROPS_SIZE) { lzerr(SZ_ERROR_DESTLEN, 1); @@ -217,8 +218,10 @@ lzma_compress(void *src, uint64_t srclen, void *dst, _dst = (Byte *)dst; *dstlen -= LZMA_PROPS_SIZE; - res = LzmaEncode(_dst + LZMA_PROPS_SIZE, dstlen, (const uchar_t *)src, srclen, + dlen = *dstlen; + res = LzmaEncode(_dst + LZMA_PROPS_SIZE, &dlen, (const uchar_t *)src, srclen, props, (uchar_t *)_dst, &props_len, 0, NULL, &g_Alloc, &g_Alloc); + *dstlen = dlen; if (res != 0) { lzerr(res, 1); @@ -233,20 +236,24 @@ int lzma_decompress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen, int level, uchar_t chdr, int btype, void *data) { - uint64_t _srclen; + SizeT _srclen; const uchar_t *_src; SRes res; ELzmaStatus status; + SizeT dlen; _srclen = srclen - LZMA_PROPS_SIZE; _src = (uchar_t *)src + LZMA_PROPS_SIZE; + dlen = *dstlen; - if ((res = LzmaDecode((uchar_t *)dst, dstlen, _src, &_srclen, + if ((res = LzmaDecode((uchar_t *)dst, &dlen, _src, &_srclen, (uchar_t *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &g_Alloc)) != SZ_OK) { + *dstlen = dlen; lzerr(res, 0); return (-1); } + *dstlen = dlen; return (0); } diff --git a/pcompress.c b/pcompress.c index 260bd5a..72272fa 100644 --- a/pcompress.c +++ b/pcompress.c @@ -38,11 +38,6 @@ #include #include #include -#if defined(sun) || defined(__sun) -#include -#else -#include -#endif #include #include #include diff --git a/ppmd_compress.c b/ppmd_compress.c index 14aa6d5..46c638e 100644 --- a/ppmd_compress.c +++ b/ppmd_compress.c @@ -174,7 +174,7 @@ ppmd_decompress(void *src, uint64_t srclen, void *dst, CPpmd8 *_ppmd = (CPpmd8 *)data; Byte *_src = (Byte *)src; Byte *_dst = (Byte *)dst; - uint64_t i; + SizeT i; int res; _ppmd->buf = (Byte *)_src; diff --git a/utils/cpuid.c b/utils/cpuid.c index 6a927b3..c5e957a 100644 --- a/utils/cpuid.c +++ b/utils/cpuid.c @@ -133,7 +133,7 @@ NOINLINE_ATTR cpuid_get_raw_data(struct cpu_raw_data_t* data) } void -cpuid_basic_identify(processor_info_t *pc) +cpuid_basic_identify(processor_cap_t *pc) { struct cpu_raw_data_t raw; cpuid_get_raw_data(&raw); diff --git a/utils/cpuid.h b/utils/cpuid.h index 26edbdf..a1ca118 100644 --- a/utils/cpuid.h +++ b/utils/cpuid.h @@ -72,7 +72,7 @@ typedef struct { int xop_avail; int aes_avail; proc_type_t proc_type; -} processor_info_t; +} processor_cap_t; /** * This contains only the most basic CPU data, required to do identification @@ -94,7 +94,7 @@ struct cpu_raw_data_t { }; void cpuid_get_raw_data(struct cpu_raw_data_t* data); -void cpuid_basic_identify(processor_info_t *pc); +void cpuid_basic_identify(processor_cap_t *pc); #endif /* __x86_64__ */ diff --git a/utils/utils.c b/utils/utils.c index 34087be..bc25edc 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -50,12 +50,17 @@ #include #else #include +#include +#include +#include + +static mach_timebase_info_data_t sTimebaseInfo; #endif #define _IN_UTILS_ #include "utils.h" -processor_info_t proc_info; +processor_cap_t proc_info; pthread_mutex_t f_mutex = PTHREAD_MUTEX_INITIALIZER; static int cur_log_level = 2; static log_dest_t ldest = {LOG_OUTPUT, LOG_INFO, NULL}; @@ -66,6 +71,9 @@ void init_pcompress() { cpuid_basic_identify(&proc_info); XXH32_module_init(); +#ifdef __APPLE__ + (void) mach_timebase_info(&sTimebaseInfo); +#endif } /* @@ -349,13 +357,38 @@ set_threadcounts(algo_props_t *props, int *nthreads, int nprocs, algo_threads_ty uint64_t get_total_ram() { +#ifndef __APPLE__ uint64_t phys_pages, page_size; page_size = sysconf(_SC_PAGESIZE); phys_pages = sysconf(_SC_PHYS_PAGES); return (phys_pages * page_size); +#else + int mib[2]; + int64_t size; + size_t len; + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + size = 0; + len = sizeof (size); + if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) + return (uint64_t)size; + return (ONE_GB); +#endif } +#ifdef __APPLE__ +int +clock_gettime(int clk_id, struct timespec *ts) +{ + if (clk_id == CLOCK_MONOTONIC) { + uint64_t abstime = mach_absolute_time(); + return (abstime * sTimebaseInfo.numer / sTimebaseInfo.denom); + } + return (0); +} +#endif + double get_wtime_millis(void) { @@ -380,15 +413,37 @@ get_mb_s(uint64_t bytes, double strt, double en) void get_sys_limits(my_sysinfo *msys_info) { - struct sysinfo sys_info; unsigned long totram; int rv; char *val; +#ifdef __APPLE__ + mach_port_t host_port = mach_host_self(); + unsigned int host_size = HOST_VM_INFO64_COUNT; + vm_size_t pagesize; + vm_statistics64_data_t vm_stat; + + host_page_size(host_port, &pagesize); + rv = host_statistics64(host_port, HOST_VM_INFO64, (host_info64_t)&vm_stat, &host_size); + if (rv != KERN_SUCCESS) { + vm_stat.free_count = (100 * 1024 * 1024) / pagesize; // 100M arbitrary + } + uint64_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize; + msys_info->freeram = vm_stat.free_count * pagesize; + msys_info->totalram = mem_used + msys_info->freeram; + msys_info->totalswap = 0; + msys_info->freeswap = 0; + msys_info->mem_unit = pagesize; + msys_info->sharedram = vm_stat.wire_count * pagesize; + +#else + struct sysinfo sys_info; rv = sysinfo(&sys_info); if (rv == -1) { + memset(&sys_info, 0, sizeof (struct sysinfo)); sys_info.freeram = 100 * 1024 * 1024; // 100M arbitrary + sys_info.mem_unit = 1; } msys_info->totalram = sys_info.totalram * sys_info.mem_unit; msys_info->freeram = sys_info.freeram * sys_info.mem_unit + sys_info.bufferram * sys_info.mem_unit; @@ -396,13 +451,14 @@ get_sys_limits(my_sysinfo *msys_info) msys_info->freeswap = sys_info.freeswap * sys_info.mem_unit; msys_info->mem_unit = sys_info.mem_unit; msys_info->sharedram = sys_info.sharedram * sys_info.mem_unit; +#endif /* * If free memory is less than half of total memory (excluding shared allocations), * and at least 75% of swap is free then adjust free memory value to 75% of * total memory excluding shared allocations. */ - totram = msys_info->totalram - sys_info.sharedram; + totram = msys_info->totalram - msys_info->sharedram; if (msys_info->freeram <= (totram >> 1) && msys_info->freeswap >= ((msys_info->totalswap >> 1) + (msys_info->totalswap >> 2))) { msys_info->freeram = (totram >> 1) + (totram >> 2); diff --git a/utils/utils.h b/utils/utils.h index 17c31d5..e8dde54 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -40,6 +40,13 @@ #include #include #include +#if defined(sun) || defined(__sun) +#include +#elif defined(__APPLE__) +#include +#else +#include +#endif #ifdef __cplusplus extern "C" { @@ -52,6 +59,7 @@ extern "C" { #define ONE_PB (1125899906842624ULL) #define ONE_TB (1099511627776ULL) +#define ONE_GB (1024UL * 1024UL * 1024UL) #define TWO_MB (2UL * 1024UL * 1024UL) #define FOUR_MB FOURM #define EIGHT_MB EIGHTM @@ -89,9 +97,15 @@ typedef int32_t bsize_t; # define ntohll(x) (x) # endif # if !defined(sun) && !defined (__sun) -# define LE64(x) __bswap_64(x) -# define LE32(x) __bswap_32(x) -# define LE16(x) __bswap_16(x) +# if defined(__APPLE__) +# define LE64(x) OSSwapInt64(x) +# define LE32(x) OSSwapInt32(x) +# define LE16(x) OSSwapInt16(x) +# else +# define LE64(x) __bswap_64(x) +# define LE32(x) __bswap_32(x) +# define LE16(x) __bswap_16(x) +# endif # else # define LE64(x) BSWAP_64(x) # define LE32(x) BSWAP_32(x) @@ -99,11 +113,20 @@ typedef int32_t bsize_t; # endif #else # if !defined(sun) && !defined (__sun) -# ifndef htonll -# define htonll(x) __bswap_64(x) -# endif -# ifndef ntohll -# define ntohll(x) __bswap_64(x) +# if defined(__APPLE__) +# ifndef htonll +# define htonll(x) OSSwapInt64(x) +# endif +# ifndef ntohll +# define ntohll(x) OSSwapInt64(x) +# endif +# else +# ifndef htonll +# define htonll(x) __bswap_64(x) +# endif +# ifndef ntohll +# define ntohll(x) __bswap_64(x) +# endif # endif # endif # define LE64(x) (x) @@ -164,6 +187,11 @@ typedef int32_t bsize_t; #define I32_P(x) *((int32_t *)(x)) #define I16_P(x) *((int16_t *)(x)) +#ifdef __APPLE__ +#define CLOCK_MONOTONIC 0 +#define CLOCK_REALTIME 1 +#endif + /* * Public checksum properties. CKSUM_MAX_BYTES must be updated if a * newer larger checksum is added to the list. @@ -295,7 +323,7 @@ typedef enum { #define PC_TYPE(x) ((x) & PC_TYPE_MASK) #ifndef _IN_UTILS_ -extern processor_info_t proc_info; +extern processor_cap_t proc_info; #endif extern void err_exit(int show_errno, const char *format, ...);