All pastes #962111 Raw Edit

Anonymous

public text v1 · immutable
#962111 ·published 2008-03-29 14:23 UTC
rendered paste body
Mac OS X implementation of Linux clock_gettime()
  -- noah

#ifdef __APPLE__
#include <mach/mach_time.h>
#include <sys/types.h>
#include <sys/time.h>
#include "library.h"

int clock_gettime(int unused, struct timespec *tp) {
       static mach_timebase_info_data_t mtid;
       static struct timeval tv;
       static uint64_t first_mat;
       uint64_t elapsed_ns;

       if(!mtid.denom) {
               mach_timebase_info(&mtid);
               gettimeofday(&tv, NULL);
               first_mat = mach_absolute_time();
       }

       elapsed_ns = (mach_absolute_time() - first_mat) * mtid.numer / mtid.denom;
       tp->tv_sec = tv.tv_sec + (long)elapsed_ns / 1000000000;
       tp->tv_nsec = tv.tv_usec*1000 + elapsed_ns % 1000000000;
       if(tp->tv_nsec >= 1000000000) {
               tp->tv_sec++;
               tp->tv_nsec -= 1000000000;
       }

       return 0;
}
#endif