Handmade Network»Forums
RFH
AM
6 posts
CTIME for Mac OS X
Edited by AM on
Apologies if this has been posted before. I just wanted to share the code for making Casey's CTIME application compile in the Mac OS X. All you have to do is add the following lines to the top of the ctime.cpp file, below the other includes:

1
2
3
4
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif


and then replace the #endif at the end of this part of the code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#ifdef _WIN32

#include <windows.h>

static int unsigned
GetClock(void)
{
    if(sizeof(int unsigned) != sizeof(DWORD))
    {
        fprintf(stderr, "ERROR: Unexpected integer size - timing will not work on this platform!\n");
    }

    return(timeGetTime());
}

#endif


with this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#elif defined(__MACH__)

static int unsigned
GetClock(void)
{
   // from:
   // http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
   //
   struct timespec TimeSpec;
   clock_serv_t CClock;
   mach_timespec_t Mts;
   int unsigned Result;
   
   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &CClock);
   clock_get_time(CClock, &Mts);
   mach_port_deallocate(mach_task_self(), CClock);
   TimeSpec.tv_sec = Mts.tv_sec;
   TimeSpec.tv_nsec = Mts.tv_nsec;
   Result = TimeSpec.tv_sec * 1000 + TimeSpec.tv_nsec / 1000000;

   return Result;
}

#endif


Hope this helps somebody.

A.
RFH
AM
6 posts
CTIME for Mac OS X
Edited by AM on

Mārtiņš Možeiko
2559 posts / 2 projects
CTIME for Mac OS X
There is OSX port available here: https://gist.github.com/nil-ableton/80294aad65abdf9e1dc764e12f4e2472
It's mentioned in comments for gist from Casey: https://gist.github.com/cmuratori/8c909975de4bb071056b4ec1651077e8
RFH
AM
6 posts
CTIME for Mac OS X
Hah, good to know, sorry for the noise. I could not find a search function to see if this was discussed anywhere in here.