Skip to content

Commit 75398f2

Browse files
[libc] Make time_t 64 bits long on all platforms but arm32
This patch changes the size of time_t to be an int64_t. This still follows the POSIX standard which only requires time_t to be an integer. Making time_t a 64-bit integer also fixes two cases in 32 bits platforms that use SYS_clock_nanosleep_time64 and SYS_clock_gettime64, as the name of these calls implies, they require a 64-bit time_t. For instance, in rv32, the 32-bit version of these syscalls is not available. We also follow glibc here, where time_t is still a 32-bit integer in arm32. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D159125
1 parent 1b7a095 commit 75398f2

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

libc/include/llvm-libc-types/time_t.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#ifndef __LLVM_LIBC_TYPES_TIME_T_H__
1010
#define __LLVM_LIBC_TYPES_TIME_T_H__
1111

12+
#if (defined(__arm__) || defined(_M_ARM))
1213
typedef __INTPTR_TYPE__ time_t;
14+
#else
15+
typedef __INT64_TYPE__ time_t;
16+
#endif
1317

1418
#endif // __LLVM_LIBC_TYPES_TIME_T_H__

libc/src/time/linux/clockGetTimeImpl.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "src/__support/error_or.h"
1515
#include "src/errno/libc_errno.h"
1616

17+
#include <stdint.h> // For int64_t.
1718
#include <sys/syscall.h> // For syscall numbers.
1819
#include <time.h>
1920

@@ -27,12 +28,12 @@ LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
2728
static_cast<long>(clockid),
2829
reinterpret_cast<long>(ts));
2930
#elif defined(SYS_clock_gettime64)
30-
struct timespec64 ts64;
31+
static_assert(
32+
sizeof(time_t) == sizeof(int64_t),
33+
"SYS_clock_gettime64 requires struct timespec with 64-bit members.");
3134
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
3235
static_cast<long>(clockid),
33-
reinterpret_cast<long>(&ts64));
34-
ts->tv_sec = static_cast<time_t>(ts64.tv_sec);
35-
ts->tv_nsec = static_cast<long>(ts64.tv_nsec);
36+
reinterpret_cast<long>(ts));
3637
#else
3738
#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
3839
#endif

libc/src/time/linux/nanosleep.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/common.h"
1313
#include "src/errno/libc_errno.h"
1414

15+
#include <stdint.h> // For int64_t.
1516
#include <sys/syscall.h> // For syscall numbers.
1617

1718
namespace __llvm_libc {
@@ -21,8 +22,11 @@ LLVM_LIBC_FUNCTION(int, nanosleep,
2122
#if SYS_nanosleep
2223
int ret = __llvm_libc::syscall_impl<int>(SYS_nanosleep, req, rem);
2324
#elif defined(SYS_clock_nanosleep_time64)
24-
int ret =
25-
__llvm_libc::syscall_impl<int>(SYS_clock_nanosleep_time64, req, rem);
25+
static_assert(
26+
sizeof(time_t) == sizeof(int64_t),
27+
"SYS_clock_gettime64 requires struct timespec with 64-bit members.");
28+
int ret = __llvm_libc::syscall_impl<int>(SYS_clock_nanosleep_time64,
29+
CLOCK_REALTIME, 0, req, rem);
2630
#else
2731
#error "SYS_nanosleep and SYS_clock_nanosleep_time64 syscalls not available."
2832
#endif

0 commit comments

Comments
 (0)