[libc-commits] [libc] [libc] Use RAII and DirScanResult in scandir (PR #240107)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 23:48:29 PST 2026


================
@@ -11,123 +11,171 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_SCAN_IMPL_H
-#define LLVM_LIBC_SRC___SUPPORT_FILE_SCAN_IMPL_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_DIR_SCAN_IMPL_H
+#define LLVM_LIBC_SRC___SUPPORT_FILE_DIR_SCAN_IMPL_H
 
 #include "hdr/errno_macros.h"
-#include "hdr/func/free.h"
-#include "hdr/func/malloc.h"
-#include "hdr/func/realloc.h"
+#include "hdr/stdint_proxy.h"
 #include "hdr/types/struct_dirent.h"
 #include "src/__support/CPP/limits.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/CPP/unique_ptr.h"
+#include "src/__support/File/dir_scan_result.h"
+#include "src/__support/alloc-checker.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/stdlib/qsort_util.h"
 #include "src/string/memory_utils/inline_memcpy.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
 
-template <typename DirType>
-ErrorOr<int> scan_impl(const char *name, struct dirent ***namelist,
-                       int (*filter)(const struct dirent *),
-                       int (*compare)(const struct dirent **,
-                                      const struct dirent **)) {
-  auto res_open = DirType::open(name);
-  if (!res_open)
-    return LIBC_NAMESPACE::Error(res_open.error());
+template <typename DirType> struct DirCloser {
+  LIBC_INLINE constexpr DirCloser() = default;
 
-  DirType *dir = res_open.value();
+  LIBC_INLINE constexpr void operator()(DirType *d) const {
+    if (d != nullptr)
+      (void)d->close();
+  }
+};
 
-  int error_code = 0;
+// TODO: Replace with a dynamic array container once available.
+class ScannedEntries {
   struct dirent **entries = nullptr;
   size_t count = 0;
-  size_t buffer_capacity = 0;
+  size_t capacity = 0;
 
-  auto free_entries = [&entries, &count]() {
+public:
+  LIBC_INLINE constexpr ScannedEntries() = default;
+  ScannedEntries(const ScannedEntries &) = delete;
+  ScannedEntries &operator=(const ScannedEntries &) = delete;
+  ScannedEntries(ScannedEntries &&) = delete;
+  ScannedEntries &operator=(ScannedEntries &&) = delete;
+
+  LIBC_INLINE ~ScannedEntries() {
     if (entries == nullptr)
       return;
 
+    // Match the uint8_t[] dynamic allocation type when freeing entries.
     for (size_t i = 0; i < count; ++i)
-      ::free(entries[i]);
+      delete[] reinterpret_cast<uint8_t *>(entries[i]);
 
-    ::free(entries);
-  };
+    delete[] entries;
+  }
 
-  while (true) {
-    auto res_read = dir->read();
-    if (!res_read) {
-      error_code = res_read.error();
-      break;
-    }
+  LIBC_INLINE constexpr size_t size() const { return count; }
+  LIBC_INLINE constexpr struct dirent **data() const { return entries; }
 
-    struct dirent *entry = res_read.value();
-    if (entry == nullptr)
-      break;
+  LIBC_INLINE bool ensure_capacity() {
+    if (LIBC_LIKELY(count < capacity))
+      return true;
----------------
kaladron wrote:

Updated in the latest commit. I made ensure_capacity() private and moved the growth check into push_back(), though I changed the parameter to cpp::unique_ptr<uint8_t[]> by value instead of a raw struct dirent *.

Passing the unique_ptr avoids releasing ownership in scan_impl before we know capacity growth succeeded (so the buffer is automatically freed with the right uint8_t[] deleter if ensure_capacity() fails), keeps both uint8_t * / struct dirent * casts encapsulated inside ScannedEntries, and preserves EOVERFLOW checking before record allocation. Since cpp::unique_ptr is LIBC_TRIVIAL_ABI, passing it by value compiles down the same as a raw pointer.

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


More information about the libc-commits mailing list