Skip to content

Commit 43c9dc8

Browse files
committed
Merge branch 'master' of git://github.com/habnabit/libcurvecpr into add-os-x-support
2 parents 1225869 + 895ffe0 commit 43c9dc8

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

configure.ac

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ AC_TYPE_UINT32_T
5555
AC_CHECK_TYPE([struct timespec], [], [AC_MSG_ERROR([missing struct timespec])], [[#include <time.h>]])
5656

5757
# Checks for functions.
58-
AC_CHECK_FUNCS([clock_gettime], [], [AC_MSG_ERROR([missing clock_gettime])])
58+
AC_CHECK_FUNCS([clock_gettime], [AC_DEFINE([HAVE_CLOCK_GETTIME])], [
59+
AC_CHECK_FUNCS([host_get_clock_service],
60+
[AC_DEFINE([HAVE_HOST_GET_CLOCK_SERVICE])],
61+
[AC_MSG_ERROR([no clock_gettime or host_get_clock_service])])
62+
])
5963

6064
# Checks for compiler flags.
6165
CCHECKFLAGS="-Wno-error"

libcurvecpr/lib/util.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <sodium/randombytes.h>
1010

11+
#ifdef HAVE_HOST_GET_CLOCK_SERVICE
12+
#include <mach/clock.h>
13+
#include <mach/mach.h>
14+
#endif
15+
1116
/* XXX: Current implementation is limited to n < 2^55. */
1217
long long curvecpr_util_random_mod_n (long long n)
1318
{
@@ -29,9 +34,19 @@ long long curvecpr_util_random_mod_n (long long n)
2934
/* XXX: Nanosecond granularity limits users to 1 terabyte per second. */
3035
long long curvecpr_util_nanoseconds (void)
3136
{
37+
#if defined(HAVE_CLOCK_GETTIME)
3238
struct timespec t;
3339
if (clock_gettime(CLOCK_REALTIME, &t) != 0)
3440
return -1;
41+
#elif defined(HAVE_HOST_GET_CLOCK_SERVICE)
42+
clock_serv_t clock;
43+
mach_timespec_t t;
44+
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clock);
45+
clock_get_time(clock, &t);
46+
mach_port_deallocate(mach_task_self(), clock);
47+
#else
48+
#error "no function for getting microseconds"
49+
#endif
3550

3651
return t.tv_sec * 1000000000LL + t.tv_nsec;
3752
}

0 commit comments

Comments
 (0)