[libc-commits] [libc] 8cd4ecf - [libc] Unify lseek implementations

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Wed Sep 6 07:56:51 PDT 2023


Author: Mikhail R. Gadelha
Date: 2023-09-06T11:56:41-03:00
New Revision: 8cd4ecfa6001eed7486fa3618bbd6bde47a0b075

URL: https://github.com/llvm/llvm-project/commit/8cd4ecfa6001eed7486fa3618bbd6bde47a0b075
DIFF: https://github.com/llvm/llvm-project/commit/8cd4ecfa6001eed7486fa3618bbd6bde47a0b075.diff

LOG: [libc] Unify lseek implementations

In patch D157792, the calls to SYS_llseek/SYS_llseek for 32-bit systems
were fixed in lseek.cpp but there was another implementation in file.cpp
that was missed.

To reduce the code duplication, this patch unifies both call sites to
use a new lseekimpl function.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159208

Added: 
    libc/src/__support/File/linux/lseekImpl.h

Modified: 
    libc/src/__support/File/linux/file.cpp
    libc/src/unistd/linux/lseek.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 611d45ea7ac4d61..2d4481bc4b56370 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/File/file.h"
 
 #include "src/__support/CPP/new.h"
+#include "src/__support/File/linux/lseekImpl.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/errno/libc_errno.h"         // For error macros
 
@@ -40,32 +41,10 @@ FileIOResult linux_file_read(File *f, void *buf, size_t size) {
 
 ErrorOr<long> linux_file_seek(File *f, long offset, int whence) {
   auto *lf = reinterpret_cast<LinuxFile *>(f);
-  long result;
-#ifdef SYS_lseek
-  int ret =
-      __llvm_libc::syscall_impl<int>(SYS_lseek, lf->get_fd(), offset, whence);
-  result = ret;
-#elif defined(SYS_llseek)
-  int ret = __llvm_libc::syscall_impl<int>(SYS_llseek, lf->get_fd(),
-                                           (long)(((uint64_t)(offset)) >> 32),
-                                           (long)offset, &result, whence);
-  result = ret;
-#elif defined(SYS_llseek)
-  int ret = __llvm_libc::syscall_impl<int>(SYS_llseek, lf->get_fd(),
-                                           (long)(((uint64_t)(offset)) >> 32),
-                                           (long)offset, &result, whence);
-  result = ret;
-#elif defined(SYS__llseek)
-  int ret = __llvm_libc::syscall_impl<int>(
-      SYS__llseek, lf->get_fd(), offset >> 32, offset, &result, whence);
-#else
-#error "lseek, llseek and _llseek syscalls not available."
-#endif
-
-  if (ret < 0)
-    return Error(-ret);
-
-  return result;
+  auto result = internal::lseekimpl(lf->get_fd(), offset, whence);
+  if (!result.has_value())
+    return result.error();
+  return result.value();
 }
 
 int linux_file_close(File *f) {

diff  --git a/libc/src/__support/File/linux/lseekImpl.h b/libc/src/__support/File/linux/lseekImpl.h
new file mode 100644
index 000000000000000..8594cd3536fcdca
--- /dev/null
+++ b/libc/src/__support/File/linux/lseekImpl.h
@@ -0,0 +1,50 @@
+//===-- Linux implementation of lseek -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_FILE_LINUX_LSEEKIMPL_H
+#define LLVM_LIBC_SRC_SUPPORT_FILE_LINUX_LSEEKIMPL_H
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/errno/libc_errno.h"
+
+#include <stdint.h>      // For uint64_t.
+#include <sys/syscall.h> // For syscall numbers.
+#include <unistd.h>      // For off_t.
+
+namespace __llvm_libc {
+namespace internal {
+
+LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
+  off_t result;
+#ifdef SYS_lseek
+  int ret = __llvm_libc::syscall_impl<int>(SYS_lseek, fd, offset, whence);
+  result = ret;
+#elif defined(SYS_llseek) || defined(SYS__llseek)
+  static_assert(sizeof(size_t) == 4, "size_t must be 32 bits.");
+#ifdef SYS_llseek
+  constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;
+#elif defined(SYS__llseek)
+  constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;
+#endif
+  off_t offset_64 = offset;
+  int ret = __llvm_libc::syscall_impl<int>(
+      LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);
+#else
+#error "lseek, llseek and _llseek syscalls not available."
+#endif
+  if (ret < 0)
+    return Error(-ret);
+  return result;
+}
+
+} // namespace internal
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SUPPORT_FILE_LINUX_LSEEKIMPL_H

diff  --git a/libc/src/unistd/linux/lseek.cpp b/libc/src/unistd/linux/lseek.cpp
index 87900e2f8c08f50..3ab0084269e2db9 100644
--- a/libc/src/unistd/linux/lseek.cpp
+++ b/libc/src/unistd/linux/lseek.cpp
@@ -9,38 +9,22 @@
 #include "src/unistd/lseek.h"
 #include "src/errno/libc_errno.h"
 
+#include "src/__support/File/linux/lseekImpl.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
-#include <stdint.h>
 #include <sys/syscall.h> // For syscall numbers.
-#include <unistd.h>
+#include <unistd.h>      // For off_t.
 
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(off_t, lseek, (int fd, off_t offset, int whence)) {
-  off_t result;
-#ifdef SYS_lseek
-  int ret = __llvm_libc::syscall_impl<int>(SYS_lseek, fd, offset, whence);
-  result = ret;
-#elif defined(SYS_llseek) || defined(SYS__llseek)
-#ifdef SYS_llseek
-  constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;
-#elif defined(SYS__llseek)
-  constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;
-#endif
-  uint64_t offset_64 = static_cast<uint64_t>(offset);
-  int ret = __llvm_libc::syscall_impl<int>(
-      LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);
-#else
-#error "lseek, llseek and _llseek syscalls not available."
-#endif
-
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = internal::lseekimpl(fd, offset, whence);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return result;
+  return result.value();
 }
 
 } // namespace __llvm_libc


        


More information about the libc-commits mailing list