[libc-commits] [libc] [libc] Use RAII and DirScanResult in scandir (PR #240107)
Alexey Samsonov via libc-commits
libc-commits at lists.llvm.org
Sun Nov 8 18:49:47 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;
----------------
vonosmas wrote:
The API of this class is slightly confusing - the caller needs to call `ensure_capacity()` first to ensure there's a place for appending an element, and then call `push_back()`.
Can't we instead just have `LIBC_INLINE bool push_back(struct direntry* entry)` that would return false if a space for the new element can't be allocated? I don't think the order in which we do the checks inside the `scan_impl` below matters that much?
https://github.com/llvm/llvm-project/pull/240107
More information about the libc-commits
mailing list