diff --git a/.gitignore b/.gitignore index cbf9b3d..fc6896e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.os *.so +build diff --git a/src/csnip/log.c b/src/csnip/log.c index 2ab3bf0..abc4af1 100644 --- a/src/csnip/log.c +++ b/src/csnip/log.c @@ -284,7 +284,7 @@ typedef enum { static const char* put_timestamp(char* buf, size_t bufSz, TsType tsType) { struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); + csnip_x_clock_gettime(CLOCK_REALTIME, &ts); struct tm broken_down; if (tsType == TS_LOCAL) { #ifdef WIN32 @@ -313,7 +313,7 @@ static const char* put_timestamp(char* buf, size_t bufSz, TsType tsType) static const char* put_timestampnum(char* buf, size_t bufSz, TsType tsType) { struct timespec ts; - clock_gettime(tsType == TS_MONO ? CLOCK_MONOTONIC : CLOCK_REALTIME, + csnip_x_clock_gettime(tsType == TS_MONO ? CLOCK_MONOTONIC : CLOCK_REALTIME, &ts); double ts_sec; time_Convert(ts, ts_sec); @@ -361,7 +361,7 @@ static const char* value_for_key(const char* keyStart, case 7: if (strncmp(keyStart, "timesec", 7) == 0) { struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + csnip_x_clock_gettime(CLOCK_MONOTONIC, &ts); snprintf(buf, bufSz, "%.16g", ts.tv_sec + ts.tv_nsec/1e9); return buf; diff --git a/src/csnip/log.h b/src/csnip/log.h index 8404287..2012346 100644 --- a/src/csnip/log.h +++ b/src/csnip/log.h @@ -173,7 +173,7 @@ inline const char* csnip_log__file(const char* filepath) { char* p = strrchr(filepath, '/'); char* q = NULL; -#ifdef WIN32 +#ifdef _WIN32 q = strrchr(filepath, '\\'); #endif if (p == NULL) { diff --git a/src/csnip/mem.c b/src/csnip/mem.c index a5219ca..21d3b60 100644 --- a/src/csnip/mem.c +++ b/src/csnip/mem.c @@ -115,11 +115,22 @@ void csnip_mem_aligned_free(void* mem) * / _aligned_free() pair of functions. */ -void* csnip_mem_aligned_alloc(size_t nAlign, size_t nSize, int* err_ret) +void* csnip_mem_aligned_alloc(size_t nAlign, size_t n, size_t size, int* err_ret) { + size_t alloc_sz = compute_alloc_amount(n, size); + if (alloc_sz == 0) { + if (err_ret) + *err_ret = csnip_err_RANGE; + return NULL; + } + if (err_ret) *err_ret = 0; - return _aligned_malloc(nSize, nAlign); + void* p_ret = _aligned_malloc(alloc_sz, nAlign); + if (p_ret == NULL && err_ret) { + *err_ret = csnip_err_NOMEM; + } + return p_ret; } void csnip_mem_aligned_free(void* mem) diff --git a/src/csnip/x.h b/src/csnip/x.h index bc46d0d..bbafcd9 100644 --- a/src/csnip/x.h +++ b/src/csnip/x.h @@ -80,7 +80,7 @@ int asprintf(char** strp, const char* fmt, ...); int csnip_x_asprintf_imp(char** strp, const char* format, ...); /** Wrapper for system fopencookie() or funopen(). */ -#if defined(CSNIP_CONF__HAVE_FOPENCOOKIE) || defined(CSNIP_CONF__HAVE_FUNOPEN) +#if defined(CSNIP_CONF__HAVE_FOPENCOOKIE) || defined(CSNIP_CONF__HAVE_FUNOPEN) || defined(_WIN32) typedef csnip_x_ssize_t csnip_x_cookie_read_function_t( void* cookie, char* buf, @@ -101,8 +101,14 @@ typedef struct { csnip_x_cookie_close_function_t* close; } csnip_x_cookie_io_functions_t; -FILE* csnip_x_fopencookie(void* __restrict__ cookie, - const char* __restrict__ mode, +#if defined(_WIN32) && defined(__cplusplus) +#define RESTRICT_CPP +#else +#define RESTRICT_CPP __restrict__ +#endif + +FILE* csnip_x_fopencookie(void* RESTRICT_CPP cookie_arg, + const char* RESTRICT_CPP mode, csnip_x_cookie_io_functions_t funcs); #endif @@ -275,7 +281,7 @@ csnip_x_ssize_t csnip_x_writev_imp(int fd, #define csnip_x_clock_gettime clock_gettime #if !defined(CSNIP_CONF__HAVE_CLOCK_GETTIME) #undef csnip_x_clock_gettime -#define csnip_x_clock_gettime csnip_x_clock_gettime_imp +#define csnip_x_clock_gettime x_csnip_clock_gettime_imp #endif /** Csnip's CLOCK_* constants. @@ -294,6 +300,13 @@ csnip_x_ssize_t csnip_x_writev_imp(int fd, # define CSNIP_X_CLOCK_MAYBE_MONOTONIC 1 #endif + +#if defined(_WIN32) && !defined(clockid_t) +typedef int clockid_t; +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 1 +#endif + /** clock_id_t */ #define csnip_x_clockid_t clockid_t #if !defined(CSNIP_CONF__HAVE_CLOCK_GETTIME) @@ -321,6 +334,8 @@ int x_csnip_clock_gettime_imp(csnip_x_clockid_t clk_id, #endif /* CSNIP_X_H */ + + #if defined(CSNIP_SHORT_NAMES) && !defined(CSNIP_X_HAVE_SHORT_NAMES) /* types */ diff --git a/src/csnip/x/clock_gettime.c b/src/csnip/x/clock_gettime.c index f1c11b7..22c332f 100644 --- a/src/csnip/x/clock_gettime.c +++ b/src/csnip/x/clock_gettime.c @@ -3,6 +3,10 @@ #include #include +#if defined(_WIN32) +#include +#endif + #ifdef CSNIP_CONF__HAVE_CLOCK_GETTIME int x_csnip_clock_gettime_imp(csnip_x_clockid_t clk_id, struct csnip_x_timespec* ts) @@ -20,11 +24,11 @@ int x_csnip_clock_gettime_imp(csnip_x_clockid_t clk_id, /* Didn't succeed; we don't really know why not... */ errno = EINVAL; - return -1 + return -1; } /* Wrong clk_id */ errno = EINVAL; return -1; } -#endif +#endif \ No newline at end of file diff --git a/src/csnip/x/fopencookie.c b/src/csnip/x/fopencookie.c index 8f5a0ae..82d9b60 100644 --- a/src/csnip/x/fopencookie.c +++ b/src/csnip/x/fopencookie.c @@ -8,8 +8,8 @@ #include #if defined(CSNIP_CONF__HAVE_FOPENCOOKIE) -FILE* x_fopencookie(void* restrict cookie, - const char* restrict mode, +FILE* x_fopencookie(void* RESTRICT_CPP cookie, + const char* RESTRICT_CPP mode, x_cookie_io_functions_t io_funcs) { cookie_io_functions_t io_funcs2 = { @@ -73,8 +73,8 @@ static int fun_close(void* cw_) } -FILE* x_fopencookie(void* restrict cookie, - const char* restrict mode, +FILE* x_fopencookie(void* RESTRICT_CPP cookie, + const char* RESTRICT_CPP mode, x_cookie_io_functions_t io_funcs) { /* Create the cookie wrapper */ @@ -96,4 +96,27 @@ FILE* x_fopencookie(void* restrict cookie, fun_close); } + +#elif defined(_WIN32) + +/* Windows stub implementation - fopencookie is not available on Windows */ + +#include + +FILE* x_fopencookie(void* RESTRICT_CPP cookie, + const char* RESTRICT_CPP mode, + x_cookie_io_functions_t io_funcs) +{ + /* fopencookie is not supported on Windows. + * This is a stub that prints a warning and returns NULL. + */ + (void)cookie; /* Suppress unused parameter warnings */ + (void)mode; + (void)io_funcs; + + fprintf(stderr, "WARNING: x_fopencookie() called but not supported on Windows\n"); + errno = ENOSYS; /* Function not implemented */ + return NULL; +} + #endif diff --git a/test/x_fopencookie_test.c b/test/x_fopencookie_test.c index c345999..a0761ea 100644 --- a/test/x_fopencookie_test.c +++ b/test/x_fopencookie_test.c @@ -34,7 +34,7 @@ typedef struct { size_t file_offs; } my_cookie_t; -ssize_t my_read_func(void* cookie_, char* buf, size_t size) +csnip_x_ssize_t my_read_func(void* cookie_, char* buf, size_t size) { my_cookie_t* cookie = (my_cookie_t*)cookie_; @@ -49,7 +49,7 @@ ssize_t my_read_func(void* cookie_, char* buf, size_t size) return s; } -ssize_t my_write_func(void* cookie_, const char* buf, size_t size) +csnip_x_ssize_t my_write_func(void* cookie_, const char* buf, size_t size) { my_cookie_t* cookie = cookie_;