Register
handmade.network»Forums»CTIME for Mac OS X
RFH
AM
6 posts
CTIME for Mac OS X
3 years, 5 months ago Edited by AM on June 10, 2016, 7:07 p.m.
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
3 years, 5 months ago Edited by AM on June 10, 2016, 7:12 p.m.

Mārtiņš Možeiko
1983 posts / 1 project
CTIME for Mac OS X
3 years, 5 months ago
RFH
AM
6 posts
CTIME for Mac OS X
3 years, 5 months ago
Hah, good to know, sorry for the noise. I could not find a search function to see if this was discussed anywhere in here.