Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.o
*.os
*.so
build
6 changes: 3 additions & 3 deletions src/csnip/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/csnip/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 13 additions & 2 deletions src/csnip/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions src/csnip/x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 6 additions & 2 deletions src/csnip/x/clock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <csnip/csnip_conf.h>
#include <csnip/x.h>

#if defined(_WIN32)
#include<errno.h>
#endif

#ifdef CSNIP_CONF__HAVE_CLOCK_GETTIME
int x_csnip_clock_gettime_imp(csnip_x_clockid_t clk_id,
struct csnip_x_timespec* ts)
Expand All @@ -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
31 changes: 27 additions & 4 deletions src/csnip/x/fopencookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <csnip/x.h>

#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 = {
Expand Down Expand Up @@ -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 */
Expand All @@ -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 <stdio.h>

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
4 changes: 2 additions & 2 deletions test/x_fopencookie_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_;

Expand All @@ -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_;

Expand Down