summaryrefslogtreecommitdiff
path: root/libc/src/__support/File/linux/file.cpp
diff options
context:
space:
mode:
authorSiva Chandra Reddy <sivachandra@google.com>2023-08-16 19:52:24 +0000
committerSiva Chandra Reddy <sivachandra@google.com>2023-08-17 19:48:35 +0000
commit0f6ef9bc2a14be0a5fdb0d05fc6bb2d5474bf0c6 (patch)
tree2c62333cb976f1786dc09aced554ce4dd159a916 /libc/src/__support/File/linux/file.cpp
parente2d06ffe19eefe0110be3b05e14a343bd1711443 (diff)
[libc][NFC] Put definitions of stdout and friends into their own object files.
This is done so that tests which only require platform file but not the platform streams can be run as unit tests. The tests which use platform streams can only be hermetic tests to avoid conflicts with the system-libc streams. Fixes: https://github.com/llvm/llvm-project/issues/64663 https://github.com/llvm/llvm-project/issues/63558 Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D158193
Diffstat (limited to 'libc/src/__support/File/linux/file.cpp')
-rw-r--r--libc/src/__support/File/linux/file.cpp62
1 files changed, 7 insertions, 55 deletions
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 9080a0f120f2..611d45ea7ac4 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -1,4 +1,4 @@
-//===--- Linux specialization of the File data structure ------------------===//
+//===--- Implementation of the Linux specialization of File ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#include "file.h"
+
#include "src/__support/File/file.h"
#include "src/__support/CPP/new.h"
@@ -18,31 +20,7 @@
namespace __llvm_libc {
-namespace {
-
-FileIOResult write_func(File *, const void *, size_t);
-FileIOResult read_func(File *, void *, size_t);
-ErrorOr<long> seek_func(File *, long, int);
-int close_func(File *);
-
-} // anonymous namespace
-
-class LinuxFile : public File {
- int fd;
-
-public:
- constexpr LinuxFile(int file_descriptor, uint8_t *buffer, size_t buffer_size,
- int buffer_mode, bool owned, File::ModeFlags modeflags)
- : File(&write_func, &read_func, &seek_func, &close_func, buffer,
- buffer_size, buffer_mode, owned, modeflags),
- fd(file_descriptor) {}
-
- int get_fd() const { return fd; }
-};
-
-namespace {
-
-FileIOResult write_func(File *f, const void *data, size_t size) {
+FileIOResult linux_file_write(File *f, const void *data, size_t size) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret = __llvm_libc::syscall_impl<int>(SYS_write, lf->get_fd(), data, size);
if (ret < 0) {
@@ -51,7 +29,7 @@ FileIOResult write_func(File *f, const void *data, size_t size) {
return ret;
}
-FileIOResult read_func(File *f, void *buf, size_t size) {
+FileIOResult linux_file_read(File *f, void *buf, size_t size) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret = __llvm_libc::syscall_impl<int>(SYS_read, lf->get_fd(), buf, size);
if (ret < 0) {
@@ -60,7 +38,7 @@ FileIOResult read_func(File *f, void *buf, size_t size) {
return ret;
}
-ErrorOr<long> seek_func(File *f, long offset, int whence) {
+ErrorOr<long> linux_file_seek(File *f, long offset, int whence) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
long result;
#ifdef SYS_lseek
@@ -90,7 +68,7 @@ ErrorOr<long> seek_func(File *f, long offset, int whence) {
return result;
}
-int close_func(File *f) {
+int linux_file_close(File *f) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret = __llvm_libc::syscall_impl<int>(SYS_close, lf->get_fd());
if (ret < 0) {
@@ -100,8 +78,6 @@ int close_func(File *f) {
return 0;
}
-} // anonymous namespace
-
ErrorOr<File *> openfile(const char *path, const char *mode) {
using ModeFlags = File::ModeFlags;
auto modeflags = File::mode_flags(mode);
@@ -166,28 +142,4 @@ int get_fileno(File *f) {
return lf->get_fd();
}
-constexpr size_t STDIN_BUFFER_SIZE = 512;
-uint8_t stdin_buffer[STDIN_BUFFER_SIZE];
-static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,
- File::ModeFlags(File::OpenMode::READ));
-File *stdin = &StdIn;
-
-constexpr size_t STDOUT_BUFFER_SIZE = 1024;
-uint8_t stdout_buffer[STDOUT_BUFFER_SIZE];
-static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,
- File::ModeFlags(File::OpenMode::APPEND));
-File *stdout = &StdOut;
-
-constexpr size_t STDERR_BUFFER_SIZE = 0;
-static LinuxFile StdErr(2, nullptr, STDERR_BUFFER_SIZE, _IONBF, false,
- File::ModeFlags(File::OpenMode::APPEND));
-File *stderr = &StdErr;
-
} // namespace __llvm_libc
-
-// Provide the external defintitions of the standard IO streams.
-extern "C" {
-FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc::StdIn);
-FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
-FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
-}