Skip to content

Commit

Permalink
Move clock_gettime & mmap ports to win.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tairov committed Jul 27, 2023
1 parent 2fc0b18 commit 6e43d0e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 1 addition & 6 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Then run with:
#include <fcntl.h>

#if defined(_WIN32)
#include "mmap-windows.c"
#include "win.h"
#else
#include <unistd.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -364,17 +364,12 @@ int argmax(float* v, int n) {
long time_in_ms() {
struct timespec time;
// Get the current time with nanosecond precision
#if defined(_WIN32)
timespec_get(&time, TIME_UTC);
return time.tv_sec * 1000 + time.tv_nsec / 1000000;
#else
if (clock_gettime(CLOCK_REALTIME, &time) == 0) {
return time.tv_sec * 1000 + time.tv_nsec / 1000000;
} else {
perror("clock_gettime");
return -1; // Return -1 to indicate an error
}
#endif
}

int main(int argc, char *argv[]) {
Expand Down
15 changes: 15 additions & 0 deletions mmap-windows.c → win.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Windows Compatibility headers
* This source contains compatible versions of mmap & clock_gettime functions
* ported for Windows OS
*/

/* mmap() replacement for Windows
*
* Author: Mike Frysinger <[email protected]>
Expand Down Expand Up @@ -98,3 +104,12 @@ static void munmap(void *addr, size_t length)

#undef DWORD_HI
#undef DWORD_LO

// define undeclared constant
#define CLOCK_REALTIME 0

// define simple clock_gettime alternative for Windows
int clock_gettime(int _, struct timespec *tv)
{
return timespec_get(tv, TIME_UTC) != 0 ? 0 : 1;
}

0 comments on commit 6e43d0e

Please sign in to comment.