Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
3.9 kB · 142 lines
C++
at android
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143/*This file is part of Darling.
Copyright (C) 2013 Lubos Dolezel
Darling is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
Darling is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with Darling. If not, see <http://www.gnu.org/licenses/>.*/
#include "DriverServices.h"#include "time.h"#include <sys/sysctl.h>#include <sys/time.h>
AbsoluteTime UpTime(){ struct timeval boottime, now; size_t len = sizeof(boottime); int mib[2] = { CTL_KERN, KERN_BOOTTIME }; uint64_t value;
if (sysctl(mib, 2, &boottime, &len, NULL, 0) < 0) return { 0, 0};
gettimeofday(&now, NULL);
value = (now.tv_sec-boottime.tv_sec) * 1000000000ll + (now.tv_usec-boottime.tv_usec) * 1000ll; return *reinterpret_cast<AbsoluteTime*>(&value);}
Nanoseconds AbsoluteToNanoseconds(AbsoluteTime absTime){ return *reinterpret_cast<Nanoseconds*>(&absTime);}
Duration AbsoluteToDuration(AbsoluteTime absTime){ return Duration(*reinterpret_cast<int64_t*>(&absTime) / 1000000ll);}
AbsoluteTime NanosecondsToAbsolute(Nanoseconds ns){ uint64_t value = *reinterpret_cast<uint64_t*>(&ns); return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime DurationToAbsolute(Duration duration){ int64_t value = duration * 1000000ll; return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime AddAbsoluteToAbsolute(AbsoluteTime time1, AbsoluteTime time2){ int64_t value; value = *reinterpret_cast<int64_t*>(&time1); value += *reinterpret_cast<int64_t*>(&time2); return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime SubAbsoluteFromAbsolute(AbsoluteTime time1, AbsoluteTime time2){ int64_t value; value = *reinterpret_cast<int64_t*>(&time1); value -= *reinterpret_cast<int64_t*>(&time2); return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime AddNanosecondsToAbsolute(Nanoseconds ns, AbsoluteTime absTime){ int64_t value; value = *reinterpret_cast<int64_t*>(&absTime); value += *reinterpret_cast<uint64_t*>(&ns); return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime AddDurationToAbsolute(Duration duration, AbsoluteTime absTime){ int64_t value; AbsoluteTime at2 = DurationToAbsolute(duration);
value = *reinterpret_cast<int64_t*>(&absTime); value += *reinterpret_cast<int64_t*>(&at2);
return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime SubNanosecondsFromAbsolute(Nanoseconds ns, AbsoluteTime absTime){ int64_t value; value = *reinterpret_cast<int64_t*>(&absTime); value -= *reinterpret_cast<uint64_t*>(&ns); return *reinterpret_cast<AbsoluteTime*>(&value);}
AbsoluteTime SubDurationFromAbsolute(Duration duration, AbsoluteTime absTime){ int64_t value; AbsoluteTime at2 = DurationToAbsolute(duration);
value = *reinterpret_cast<int64_t*>(&absTime); value -= *reinterpret_cast<int64_t*>(&at2);
return *reinterpret_cast<AbsoluteTime*>(&value);}
Nanoseconds AbsoluteDeltaToNanoseconds(AbsoluteTime time1, AbsoluteTime time2){ int64_t value = *reinterpret_cast<int64_t*>(&time1) - *reinterpret_cast<int64_t*>(&time2);
return AbsoluteToNanoseconds(*reinterpret_cast<AbsoluteTime*>(&value));}
Duration AbsoluteDeltaToDuration(AbsoluteTime time1, AbsoluteTime time2){ return AbsoluteToDuration(SubAbsoluteFromAbsolute(time1, time2));}
Nanoseconds DurationToNanoseconds(Duration duration){ uint64_t value = duration * 1000000ll; return *reinterpret_cast<Nanoseconds*>(&value);}
Duration NanosecondsToDuration(Nanoseconds ns){ uint64_t value = *reinterpret_cast<uint64_t*>(&ns); return Duration(value / 1000000ll);}