[libc-commits] [libc] 75398f2 - [libc] Make time_t 64 bits long on all platforms but arm32

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Wed Sep 13 06:51:11 PDT 2023


Author: Mikhail R. Gadelha
Date: 2023-09-13T10:49:39-03:00
New Revision: 75398f28ebdb6008f028e413dffa4856a2523149

URL: https://github.com/llvm/llvm-project/commit/75398f28ebdb6008f028e413dffa4856a2523149
DIFF: https://github.com/llvm/llvm-project/commit/75398f28ebdb6008f028e413dffa4856a2523149.diff

LOG: [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

Added: 
    

Modified: 
    libc/include/llvm-libc-types/time_t.h
    libc/src/time/linux/clockGetTimeImpl.h
    libc/src/time/linux/nanosleep.cpp

Removed: 
    


################################################################################
diff  --git a/libc/include/llvm-libc-types/time_t.h b/libc/include/llvm-libc-types/time_t.h
index d66ee371ccbabb6..2b3ccd4d802471a 100644
--- a/libc/include/llvm-libc-types/time_t.h
+++ b/libc/include/llvm-libc-types/time_t.h
@@ -9,6 +9,10 @@
 #ifndef __LLVM_LIBC_TYPES_TIME_T_H__
 #define __LLVM_LIBC_TYPES_TIME_T_H__
 
+#if (defined(__arm__) || defined(_M_ARM))
 typedef __INTPTR_TYPE__ time_t;
+#else
+typedef __INT64_TYPE__ time_t;
+#endif
 
 #endif // __LLVM_LIBC_TYPES_TIME_T_H__

diff  --git a/libc/src/time/linux/clockGetTimeImpl.h b/libc/src/time/linux/clockGetTimeImpl.h
index a5b7d659c7c14c2..e519ebcdddbdb9e 100644
--- a/libc/src/time/linux/clockGetTimeImpl.h
+++ b/libc/src/time/linux/clockGetTimeImpl.h
@@ -14,6 +14,7 @@
 #include "src/__support/error_or.h"
 #include "src/errno/libc_errno.h"
 
+#include <stdint.h>      // For int64_t.
 #include <sys/syscall.h> // For syscall numbers.
 #include <time.h>
 
@@ -27,12 +28,12 @@ LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
                                            static_cast<long>(clockid),
                                            reinterpret_cast<long>(ts));
 #elif defined(SYS_clock_gettime64)
-  struct timespec64 ts64;
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
   int ret = __llvm_libc::syscall_impl<int>(SYS_clock_gettime64,
                                            static_cast<long>(clockid),
-                                           reinterpret_cast<long>(&ts64));
-  ts->tv_sec = static_cast<time_t>(ts64.tv_sec);
-  ts->tv_nsec = static_cast<long>(ts64.tv_nsec);
+                                           reinterpret_cast<long>(ts));
 #else
 #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
 #endif

diff  --git a/libc/src/time/linux/nanosleep.cpp b/libc/src/time/linux/nanosleep.cpp
index 84f571106837672..8c76dac75a176ad 100644
--- a/libc/src/time/linux/nanosleep.cpp
+++ b/libc/src/time/linux/nanosleep.cpp
@@ -12,6 +12,7 @@
 #include "src/__support/common.h"
 #include "src/errno/libc_errno.h"
 
+#include <stdint.h>      // For int64_t.
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace __llvm_libc {
@@ -21,8 +22,11 @@ LLVM_LIBC_FUNCTION(int, nanosleep,
 #if SYS_nanosleep
   int ret = __llvm_libc::syscall_impl<int>(SYS_nanosleep, req, rem);
 #elif defined(SYS_clock_nanosleep_time64)
-  int ret =
-      __llvm_libc::syscall_impl<int>(SYS_clock_nanosleep_time64, req, rem);
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
+  int ret = __llvm_libc::syscall_impl<int>(SYS_clock_nanosleep_time64,
+                                           CLOCK_REALTIME, 0, req, rem);
 #else
 #error "SYS_nanosleep and SYS_clock_nanosleep_time64 syscalls not available."
 #endif


        


More information about the libc-commits mailing list