[libc-commits] [libc] [llvm] [libc] Parametrize File with a platform (PR #245553)

via libc-commits libc-commits at lists.llvm.org
Tue Nov 24 01:28:15 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

<details>
<summary>Changes</summary>

This change replaces the File class with a templated class FileImpl which is parameterized by the platform. LinuxFile inherits from File providing concrete implementation for Linux using static dispatch. CustomFileImpl and CookieFileImpl are CRTP classes used to implement PopenFile, MemopenFile and CookieFile atop LinuxFile.

This reduces the size e.g. the LinuxFile is now 96B on x86-64 instead of 128B. When the dynamic backend (i.e. fopencookie or fmemopen) isn't used, we also avoid dynamic dispatch thus improving performance. This change also addresses several latent issues (e.g. leak in fopencookie close).

The parametrized platform also opens the path for reusing the FileImpl implementation for other platforms such as baremetal and GPU. This will be utilized in follow up changes.

Assisted-by: Gemini

---

Patch is 103.74 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/245553.diff


24 Files Affected:

- (modified) libc/src/__support/File/CMakeLists.txt (+7-3) 
- (modified) libc/src/__support/File/file.h (+267-54) 
- (renamed) libc/src/__support/File/file_impl.h (+139-48) 
- (modified) libc/src/__support/File/linux/CMakeLists.txt (+3-1) 
- (modified) libc/src/__support/File/linux/file.cpp (+33-26) 
- (modified) libc/src/__support/File/linux/file.h (+19-18) 
- (modified) libc/src/__support/File/linux/popen_file.cpp (+13-5) 
- (modified) libc/src/__support/File/linux/popen_file.h (+10-3) 
- (added) libc/src/__support/File/platform_file.h (+23) 
- (modified) libc/src/stdio/CMakeLists.txt (+4-3) 
- (modified) libc/src/stdio/fmemopen.cpp (+24-10) 
- (modified) libc/src/stdio/fopencookie.cpp (+12-74) 
- (modified) libc/src/stdio/generic/CMakeLists.txt (+2) 
- (modified) libc/src/wchar/CMakeLists.txt (+10) 
- (modified) libc/test/src/__support/File/CMakeLists.txt (+4) 
- (modified) libc/test/src/__support/File/file_test.cpp (+539-54) 
- (modified) libc/test/src/stdio/CMakeLists.txt (+11) 
- (modified) libc/test/src/stdio/fmemopen_test.cpp (+1) 
- (modified) libc/test/src/stdio/fopencookie_test.cpp (+37-2) 
- (modified) libc/test/src/stdio/freopen_test.cpp (+66) 
- (modified) libc/test/src/stdio/getdelim_impl_test.cpp (+7-5) 
- (modified) libc/test/src/stdio/popen_pclose_test.cpp (+73) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/File/BUILD.bazel (+6-3) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/stdio/BUILD.bazel (+16-4) 


``````````diff
diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt
index d3fbe37b94839b..c5802e1eeb3670 100644
--- a/libc/src/__support/File/CMakeLists.txt
+++ b/libc/src/__support/File/CMakeLists.txt
@@ -11,12 +11,12 @@ if(NOT (TARGET libc.src.__support.threads.mutex)
   return()
 endif()
 
-add_object_library(
+add_header_library(
   file
-  SRCS
-    file.cpp
   HDRS
     file.h
+    file_impl.h
+    file_mode.h
   DEPENDS
     .file_io_result
     libc.hdr.errno_macros
@@ -26,15 +26,19 @@ add_object_library(
     libc.hdr.types.char32_t
     libc.hdr.types.char8_t
     libc.hdr.types.off_t
+    libc.hdr.types.ssize_t
     libc.hdr.types.wchar_t
     libc.hdr.types.wint_t
     libc.hdr.wchar_macros
     libc.src.__support.alloc_checker
     libc.src.__support.CPP.new
     libc.src.__support.CPP.span
+    libc.src.__support.CPP.type_traits
     libc.src.__support.error_or
     libc.src.__support.libc_assert
     libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
     libc.src.__support.threads.mutex
     libc.src.__support.wchar.character_converter
     libc.src.__support.wchar.mbstate
diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 19b83e0e2fe10c..7134a1e46cd92e 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -14,16 +14,21 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_FILE_FILE_H
 #define LLVM_LIBC_SRC___SUPPORT_FILE_FILE_H
 
-#include "file_io_result.h"
-#include "file_mode.h"
+#include "hdr/errno_macros.h"
 #include "hdr/stdint_proxy.h"
 #include "hdr/stdio_macros.h"
 #include "hdr/types/off_t.h"
+#include "hdr/types/ssize_t.h"
 #include "hdr/types/wchar_t.h"
 #include "hdr/types/wint_t.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/File/file_io_result.h"
+#include "src/__support/File/file_mode.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
 #include "src/__support/threads/mutex.h"
 #include "src/__support/wchar/mbstate.h"
 
@@ -31,51 +36,109 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-// This a generic base class to encapsulate a platform independent file data
-// structure. Platform specific specializations should create a subclass as
-// suitable for their platform.
-class File {
+namespace internal {
+
+template <typename FileT>
+LIBC_INLINE FileIOResult default_write(FileT *, const void *, size_t size) {
+  return size;
+}
+
+template <typename FileT>
+LIBC_INLINE FileIOResult default_read(FileT *, void *, size_t) {
+  return 0;
+}
+
+template <typename FileT>
+LIBC_INLINE ErrorOr<off_t> default_seek(FileT *, off_t, int) {
+  return Error(ESPIPE);
+}
+
+template <typename FileT> LIBC_INLINE int default_close(FileT *) { return 0; }
+
+} // namespace internal
+
+template <typename FileT> struct FileOps {
+  using WriteFunc = FileIOResult(FileT *, const void *, size_t);
+  using ReadFunc = FileIOResult(FileT *, void *, size_t);
+  // The SeekFunc is expected to return the current offset of the external
+  // file position indicator.
+  using SeekFunc = ErrorOr<off_t>(FileT *, off_t, int);
+  using CloseFunc = int(FileT *);
+  // Optional hook invoked when reopening a CustomFile onto an OS file
+  // descriptor to tear down subclass-specific state without deleting |FileT|.
+  using DetachFunc = void(FileT *);
+
+  WriteFunc *write;
+  ReadFunc *read;
+  SeekFunc *seek;
+  CloseFunc *close;
+  DetachFunc *detach;
+
+  LIBC_INLINE constexpr FileOps(WriteFunc *w, ReadFunc *r, SeekFunc *s,
+                                CloseFunc *c, DetachFunc *d = nullptr)
+      : write(w != nullptr ? w : &internal::default_write<FileT>),
+        read(r != nullptr ? r : &internal::default_read<FileT>),
+        seek(s != nullptr ? s : &internal::default_seek<FileT>),
+        close(c != nullptr ? c : &internal::default_close<FileT>), detach(d) {}
+};
+
+struct CookieOps {
+  using ReadFunc = ssize_t(void *, char *, size_t);
+  using WriteFunc = ssize_t(void *, const char *, size_t);
+  using SeekFunc = int(void *, off_t *, int);
+  using CloseFunc = int(void *);
+
+  ReadFunc *read = nullptr;
+  WriteFunc *write = nullptr;
+  SeekFunc *seek = nullptr;
+  CloseFunc *close = nullptr;
+};
+
+// This is a generic base class template to encapsulate a platform independent
+// file data structure. Platform specific specializations provide a static
+// Platform policy and optionally subclass FileImpl as suitable for their
+// platform.
+template <typename Platform> class FileImpl {
 public:
-  static void add_file(File *f);
-  static void remove_file(File *f);
-  static File *get_first_file();
+  using PlatformType = Platform;
+  using FileBase = FileImpl;
+  using Ops = FileOps<FileImpl>;
+
+  enum class DispatchKind : uint8_t {
+    STATIC = 0,
+    CUSTOM = 1,
+    COOKIE = 2,
+  };
+
+  static void add_file(FileImpl *f);
+  static void remove_file(FileImpl *f);
+  static FileImpl *get_first_file();
   static void lock_list();
   static void unlock_list();
 
-  static File *list_all;
+  static FileImpl *list_all;
   static Mutex list_lock;
 
-  LIBC_INLINE File *get_next() const { return next; }
+  LIBC_INLINE FileImpl *get_next() const { return next; }
 
   static constexpr size_t DEFAULT_BUFFER_SIZE = 1024;
 
   enum class Orientation : uint8_t { UNORIENTED, BYTE, WIDE };
 
-  using LockFunc = void(File *);
-  using UnlockFunc = void(File *);
+  using LockFunc = void(FileImpl *);
+  using UnlockFunc = void(FileImpl *);
 
-  using WriteFunc = FileIOResult(File *, const void *, size_t);
-  using ReadFunc = FileIOResult(File *, void *, size_t);
-  // The SeekFunc is expected to return the current offset of the external
-  // file position indicator.
-  using SeekFunc = ErrorOr<off_t>(File *, off_t, int);
-  using CloseFunc = int(File *);
-
-  // Exposes the platform I/O callbacks so a platform backend can positively
-  // identify which concrete File subclass an object is (e.g. distinguishing
-  // a LinuxFile from a CookieFile/MemoryFile) without needing RTTI. See
-  // is_linux_file() in the Linux backend for the concrete use.
-  LIBC_INLINE ReadFunc *get_read_func() const { return platform_read; }
-  LIBC_INLINE WriteFunc *get_write_func() const { return platform_write; }
-  LIBC_INLINE SeekFunc *get_seek_func() const { return platform_seek; }
-  LIBC_INLINE CloseFunc *get_close_func() const { return platform_close; }
+  using WriteFunc = typename Ops::WriteFunc;
+  using ReadFunc = typename Ops::ReadFunc;
+  using SeekFunc = typename Ops::SeekFunc;
+  using CloseFunc = typename Ops::CloseFunc;
 
   // This is a convenience RAII class to lock and unlock file objects.
   class FileLock {
-    File *file;
+    FileImpl *file;
 
   public:
-    LIBC_INLINE explicit FileLock(File *f) : file(f) { file->lock(); }
+    LIBC_INLINE explicit FileLock(FileImpl *f) : file(f) { file->lock(); }
 
     LIBC_INLINE ~FileLock() { file->unlock(); }
 
@@ -86,14 +149,6 @@ class File {
 private:
   enum class FileOp : uint8_t { NONE, READ, WRITE, SEEK };
 
-  // Platform specific functions which create new file objects should initialize
-  // these fields suitably via the constructor. Typically, they should be simple
-  // syscall wrappers for the corresponding functionality.
-  WriteFunc *platform_write;
-  ReadFunc *platform_read;
-  SeekFunc *platform_seek;
-  CloseFunc *platform_close;
-
   uint8_t *buf;   // Pointer to the stream buffer for buffered streams
   size_t bufsize; // Size of the buffer pointed to by |buf|.
 
@@ -104,8 +159,8 @@ class File {
   // of the index to which the buffer can be read until.
   size_t read_limit;
 
-  File *prev;
-  File *next;
+  FileImpl *prev;
+  FileImpl *next;
 
   internal::mbstate mbstate;
 
@@ -113,7 +168,7 @@ class File {
   FileMode mode;
 
   // For files which are readable, we should be able to support one ungetc
-  // operation even if |buf| is nullptr. So, in the constructor of File, we
+  // operation even if |buf| is nullptr. So, in the constructor of FileImpl, we
   // set |buf| to point to this buffer character. It needs to be at least 4
   // bytes so we can store a widechar.
   uint8_t ungetc_buf[4];
@@ -132,6 +187,7 @@ class File {
   bool own_buf : 1;
   bool eof : 1;
   bool err : 1;
+  DispatchKind kind : 2;
 
 protected:
   LIBC_INLINE constexpr bool write_allowed() const {
@@ -154,7 +210,19 @@ class File {
     adjust_buf();
   }
 
-public:
+  LIBC_INLINE constexpr FileImpl(DispatchKind dispatch_kind, uint8_t *buffer,
+                                 size_t buffer_size, int buffer_mode,
+                                 bool owned, FileMode mode)
+      : buf(buffer), bufsize(buffer_size), pos(0), read_limit(0), prev(nullptr),
+        next(nullptr), mbstate(), mode(mode), ungetc_buf{},
+        mutex(/*timed=*/false, /*recursive=*/true,
+              /*robust=*/false, /*pshared=*/false),
+        bufmode(static_cast<int8_t>(buffer_mode)), prev_op(FileOp::NONE),
+        orientation(Orientation::UNORIENTED), own_buf(owned), eof(false),
+        err(false), kind(dispatch_kind) {
+    adjust_buf();
+  }
+
   // We want this constructor to be constexpr so that global file objects
   // like stdout do not require invocation of the constructor which can
   // potentially lead to static initialization order fiasco. Consequently,
@@ -162,18 +230,15 @@ class File {
   // meaningful - that is, |buffer| is nullptr if and only if |buffer_size|
   // is zero. This way, we will not have to employ the semantics of
   // the set_buffer method and allocate a buffer.
-  LIBC_INLINE constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf,
-                             CloseFunc *cf, uint8_t *buffer, size_t buffer_size,
-                             int buffer_mode, bool owned, FileMode mode)
-      : platform_write(wf), platform_read(rf), platform_seek(sf),
-        platform_close(cf), buf(buffer), bufsize(buffer_size), pos(0),
-        read_limit(0), prev(nullptr), next(nullptr), mbstate(), mode(mode),
-        ungetc_buf{}, mutex(/*timed=*/false, /*recursive=*/true,
-                            /*robust=*/false, /*pshared=*/false),
-        bufmode(static_cast<int8_t>(buffer_mode)), prev_op(FileOp::NONE),
-        orientation(Orientation::UNORIENTED), own_buf(owned), eof(false),
-        err(false) {
-    adjust_buf();
+  LIBC_INLINE constexpr FileImpl(uint8_t *buffer, size_t buffer_size,
+                                 int buffer_mode, bool owned, FileMode mode)
+      : FileImpl(DispatchKind::STATIC, buffer, buffer_size, buffer_mode, owned,
+                 mode) {}
+
+public:
+  LIBC_INLINE constexpr DispatchKind get_dispatch_kind() const { return kind; }
+  LIBC_INLINE constexpr bool is_dynamic() const {
+    return kind != DispatchKind::STATIC;
   }
 
   // Buffered write of |len| bytes from |data| without the file lock.
@@ -338,6 +403,41 @@ class File {
   }
 
 private:
+  LIBC_NOINLINE static FileIOResult
+  platform_write_dynamic(FileImpl *f, const void *data, size_t len);
+  LIBC_NOINLINE static FileIOResult
+  platform_read_dynamic(FileImpl *f, void *data, size_t len);
+  LIBC_NOINLINE static ErrorOr<off_t>
+  platform_seek_dynamic(FileImpl *f, off_t offset, int whence);
+  LIBC_NOINLINE static int platform_close_dynamic(FileImpl *f);
+
+  LIBC_INLINE static FileIOResult platform_write(FileImpl *f, const void *data,
+                                                 size_t len) {
+    if (LIBC_LIKELY(f->kind == DispatchKind::STATIC))
+      return Platform::write(f, data, len);
+    return platform_write_dynamic(f, data, len);
+  }
+
+  LIBC_INLINE static FileIOResult platform_read(FileImpl *f, void *data,
+                                                size_t len) {
+    if (LIBC_LIKELY(f->kind == DispatchKind::STATIC))
+      return Platform::read(f, data, len);
+    return platform_read_dynamic(f, data, len);
+  }
+
+  LIBC_INLINE static ErrorOr<off_t> platform_seek(FileImpl *f, off_t offset,
+                                                  int whence) {
+    if (LIBC_LIKELY(f->kind == DispatchKind::STATIC))
+      return Platform::seek(f, offset, whence);
+    return platform_seek_dynamic(f, offset, whence);
+  }
+
+  LIBC_INLINE static int platform_close(FileImpl *f) {
+    if (LIBC_LIKELY(f->kind == DispatchKind::STATIC))
+      return Platform::close(f);
+    return platform_close_dynamic(f);
+  }
+
   FileIOResult write_unlocked_impl(const void *data, size_t len);
   FileIOResult read_unlocked_impl(void *data, size_t len);
 
@@ -374,6 +474,119 @@ class File {
   }
 };
 
+struct FilePlatform;
+
+template <typename BaseFile> class CustomFileImpl : public BaseFile {
+  static_assert(
+      cpp::is_same_v<BaseFile, typename BaseFile::PlatformType::StreamBase>,
+      "BaseFile must be Platform::StreamBase");
+
+public:
+  using Ops = typename BaseFile::Ops;
+
+private:
+  Ops ops;
+  friend typename BaseFile::FileBase;
+
+  LIBC_INLINE FileIOResult custom_write(const void *data, size_t len) {
+    return ops.write(this, data, len);
+  }
+  LIBC_INLINE FileIOResult custom_read(void *data, size_t len) {
+    return ops.read(this, data, len);
+  }
+  LIBC_INLINE ErrorOr<off_t> custom_seek(off_t offset, int whence) {
+    return ops.seek(this, offset, whence);
+  }
+  LIBC_INLINE int custom_close() { return ops.close(this); }
+
+public:
+  template <typename... Args>
+  LIBC_INLINE constexpr CustomFileImpl(const Ops &fops, uint8_t *buffer,
+                                       size_t buffer_size, int buffer_mode,
+                                       bool owned, FileMode mode,
+                                       Args... base_args)
+      : BaseFile(BaseFile::DispatchKind::CUSTOM, buffer, buffer_size,
+                 buffer_mode, owned, mode, base_args...),
+        ops(fops) {}
+
+  // Tears down subclass-specific backing state (if any) and rebinds the I/O
+  // hooks while preserving |ops.close| so |close()| still deletes the exact
+  // derived dynamic type.
+  LIBC_INLINE void rebind_ops(typename Ops::WriteFunc *w,
+                              typename Ops::ReadFunc *r,
+                              typename Ops::SeekFunc *s) {
+    using FileBaseT = typename BaseFile::FileBase;
+    if (ops.detach != nullptr) {
+      ops.detach(this);
+      ops.detach = nullptr;
+    }
+    ops.write = w != nullptr ? w : &internal::default_write<FileBaseT>;
+    ops.read = r != nullptr ? r : &internal::default_read<FileBaseT>;
+    ops.seek = s != nullptr ? s : &internal::default_seek<FileBaseT>;
+  }
+};
+
+template <typename BaseFile> class CookieFileImpl final : public BaseFile {
+  static_assert(
+      cpp::is_same_v<BaseFile, typename BaseFile::PlatformType::StreamBase>,
+      "BaseFile must be Platform::StreamBase");
+
+  void *cookie;
+  CookieOps ops;
+  friend typename BaseFile::FileBase;
+
+  LIBC_INLINE FileIOResult cookie_write(const void *data, size_t size) {
+    if (ops.write == nullptr)
+      return size;
+    ssize_t result =
+        ops.write(cookie, reinterpret_cast<const char *>(data), size);
+    if (result < 0 || static_cast<size_t>(result) > size)
+      return {0, EIO};
+    size_t written = static_cast<size_t>(result);
+    return {written, written < size ? EIO : 0};
+  }
+
+  LIBC_INLINE FileIOResult cookie_read(void *data, size_t size) {
+    if (ops.read == nullptr)
+      return 0;
+    ssize_t result = ops.read(cookie, reinterpret_cast<char *>(data), size);
+    if (result < 0 || static_cast<size_t>(result) > size)
+      return {0, EIO};
+    return static_cast<size_t>(result);
+  }
+
+  LIBC_INLINE ErrorOr<off_t> cookie_seek(off_t offset, int whence) {
+    if (ops.seek == nullptr)
+      return Error(EINVAL);
+    if (ops.seek(cookie, &offset, whence) == 0)
+      return offset;
+    return Error(EIO);
+  }
+
+  LIBC_INLINE int cookie_close() {
+    BaseFile::FileBase::remove_file(this);
+    int retval = ops.close != nullptr ? ops.close(cookie) : 0;
+    delete this;
+    return retval == 0 ? 0 : EIO;
+  }
+
+public:
+  // CookieFileImpl instances must be heap-allocated because close() deletes
+  // |this| after invoking |ops.close(cookie)|.
+  template <typename... Args>
+  LIBC_INLINE constexpr CookieFileImpl(void *c, CookieOps cops, uint8_t *buffer,
+                                       size_t buffer_size, int buffer_mode,
+                                       bool owned, FileMode mode,
+                                       Args... base_args)
+      : BaseFile(BaseFile::DispatchKind::COOKIE, buffer, buffer_size,
+                 buffer_mode, owned, mode, base_args...),
+        cookie(c), ops(cops) {}
+};
+
+using File = FileImpl<FilePlatform>;
+
+extern template class FileImpl<FilePlatform>;
+
 // The implementation of this function is provided by the platform_file
 // library.
 ErrorOr<File *> openfile(const char *path, const char *mode);
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file_impl.h
similarity index 76%
rename from libc/src/__support/File/file.cpp
rename to libc/src/__support/File/file_impl.h
index bfd27e37bcfe52..267317cf5ef4bd 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file_impl.h
@@ -7,14 +7,13 @@
 //===----------------------------------------------------------------------===//
 ///
 /// \file
-/// Implementation of a platform independent file data structure.
+/// Out-of-line method definitions for FileImpl<Platform>.
 ///
 //===----------------------------------------------------------------------===//
 
-#include "file.h"
+#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IMPL_H
+#define LLVM_LIBC_SRC___SUPPORT_FILE_FILE_IMPL_H
 
-#include "file_io_result.h"
-#include "file_mode.h"
 #include "hdr/errno_macros.h"
 #include "hdr/func/realloc.h"
 #include "hdr/stdint_proxy.h"
@@ -27,6 +26,9 @@
 #include "hdr/wchar_macros.h"
 #include "src/__support/CPP/new.h"
 #include "src/__support/CPP/span.h"
+#include "src/__support/File/file.h"
+#include "src/__support/File/file_io_result.h"
+#include "src/__support/File/file_mode.h"
 #include "src/__support/alloc-checker.h"
 #include "src/__support/error_or.h"
 #include "src/__support/libc_assert.h"
@@ -40,40 +42,100 @@
 #include <stddef.h>
 
 namespace LIBC_NAMESPACE_DECL {
-File *File::list_all = nullptr;
-Mutex File::list_lock(/*timed=*/false, /*recursive=*/false, /*robust=*/false,
-                      /*pshared=*/false);
+
+template <typename Platform>
+FileImpl<Platform> *FileImpl<Platform>::list_all = nullptr;
+
+template <typename Platform>
+Mutex FileImpl<Platform>::list_lock(/*timed=*/false,
+                                    /*recursive=*/false,
+                                    /*robust=*/false,
+                                    /*pshared=*/false);
 
 // Adds a file to the global list of all open files.
-void File::add_file(File *f) {
-  File::list_lock.lock();
-  f->next = File::list_all;
+template <typename Platform> void FileImpl<Platform>::add_file(FileImpl *f) {
+  list_lock.lock();
+  f->next = list_all;
   f->prev = nullptr;
-  if (File::list_all != nullptr)
-    File::list_all->prev = f;
-  File::list_all = f;
-  File::list_lock.unlock();
+  if (list_all != nullptr)
+    list_all->prev = f;
+  list_all = f;
+  list_lock.unlock();
 }
 
 // Removes a file from the global list of all open files.
-void File::remove_file(File *f) {
-  File::list_lock.lock();
+template <typename Platform> void FileImpl<Platform>::remove_file(FileImpl *f) {
+  list_lock.lock();
   if (f->prev != nullptr)
     f->prev->next = f->next;
   if (f->next != nullptr)
     f->next->prev = f->prev;
-  if (File::list_all == f)
-    File::list_all = f->next;
+  if (list_all == f)
+    list_all = f->next;
   f->prev = nullptr;
   f->next = nullptr;
-  File::list_lock.unlock();
+  list_lock.unlock();
+}
+
+template <typename Platform>
+FileImpl<Platform> *FileImpl<Platform>::get_first_file() {
+  return list_all;
+}
+
+template <typename Platform> void FileImpl<Platform>::lock_list() {
+  list_lock.lock();
 }
 
-File *File::get_first_file() { return File::list_all; }
-void File::lock_list() { File::list_lock.lock(); }
-void File::unlock_list() { File::list_lock.unlock(); }
+template <typename Platform> void FileImpl<Platform>::unlock_list() {
+  list_lock.unlock();
+}
+
+template <typename Platform>
+FileIOResult FileImpl<Platform>::platform_write_dynamic(FileImpl ...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/245553


More information about the libc-commits mailing list