|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* Check: a unit test framework for C
|
|
Packit |
f546b1 |
* Copyright (C) 2001, 2002 Arien Malec
|
|
Packit |
f546b1 |
*
|
|
Packit |
f546b1 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
f546b1 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
f546b1 |
* License as published by the Free Software Foundation; either
|
|
Packit |
f546b1 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
f546b1 |
*
|
|
Packit |
f546b1 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
f546b1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
f546b1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
f546b1 |
* Lesser General Public License for more details.
|
|
Packit |
f546b1 |
*
|
|
Packit |
f546b1 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
f546b1 |
* License along with this library; if not, write to the
|
|
Packit |
f546b1 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
|
Packit |
f546b1 |
* MA 02110-1301, USA.
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
#include "libcompat.h"
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
#ifdef __APPLE__
|
|
Packit |
f546b1 |
#include <mach/clock.h>
|
|
Packit |
f546b1 |
#include <mach/mach.h>
|
|
Packit |
f546b1 |
#include <mach/mach_time.h>
|
|
Packit |
f546b1 |
#include <TargetConditionals.h>
|
|
Packit |
f546b1 |
/* CoreServices.h is only available on macOS */
|
|
Packit |
f546b1 |
# if TARGET_OS_MAC && !TARGET_OS_IPHONE
|
|
Packit |
f546b1 |
# include <CoreServices/CoreServices.h>
|
|
Packit |
f546b1 |
# endif
|
|
Packit |
f546b1 |
#include <unistd.h>
|
|
Packit |
f546b1 |
#endif
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
#define NANOSECONDS_PER_SECOND 1000000000
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
int
|
|
Packit |
f546b1 |
clock_gettime (clockid_t clk_id CK_ATTRIBUTE_UNUSED, struct timespec *ts)
|
|
Packit |
f546b1 |
{
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
#ifdef __APPLE__
|
|
Packit |
f546b1 |
/* OS X does not have clock_gettime, use mach_absolute_time */
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
static mach_timebase_info_data_t sTimebaseInfo;
|
|
Packit |
f546b1 |
uint64_t rawTime;
|
|
Packit |
f546b1 |
uint64_t nanos;
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
rawTime = mach_absolute_time ();
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* OS X has a function to convert abs time to nano seconds: AbsoluteToNanoseconds
|
|
Packit |
f546b1 |
* However, the function may not be available as we may not have
|
|
Packit |
f546b1 |
* access to CoreServices. Because of this, we convert the abs time
|
|
Packit |
f546b1 |
* to nano seconds manually.
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* First grab the time base used on the system, if this is the first
|
|
Packit |
f546b1 |
* time we are being called. We can check if the value is uninitialized,
|
|
Packit |
f546b1 |
* as the denominator will be zero.
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
if (sTimebaseInfo.denom == 0) {
|
|
Packit |
f546b1 |
(void) mach_timebase_info (&sTimebaseInfo);
|
|
Packit |
f546b1 |
}
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* Do the conversion. We hope that the multiplication doesn't
|
|
Packit |
f546b1 |
* overflow; the price you pay for working in fixed point.
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
nanos = rawTime * sTimebaseInfo.numer / sTimebaseInfo.denom;
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* Fill in the timespec container
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
ts->tv_sec = nanos / NANOSECONDS_PER_SECOND;
|
|
Packit |
f546b1 |
ts->tv_nsec = nanos - (ts->tv_sec * NANOSECONDS_PER_SECOND);
|
|
Packit |
f546b1 |
#else
|
|
Packit |
f546b1 |
/*
|
|
Packit |
f546b1 |
* As there is no function to fall back onto to get the current
|
|
Packit |
f546b1 |
* time, zero out the time so the caller will have a sane value.
|
|
Packit |
f546b1 |
*/
|
|
Packit |
f546b1 |
ts->tv_sec = 0;
|
|
Packit |
f546b1 |
ts->tv_nsec = 0;
|
|
Packit |
f546b1 |
#endif
|
|
Packit |
f546b1 |
|
|
Packit |
f546b1 |
return 0;
|
|
Packit |
f546b1 |
}
|