[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:44:17 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;
- if (filter != nullptr && !filter(entry))
- continue;
+ constexpr size_t INITIAL_CAPACITY = 8;
+ constexpr size_t MAX_CAPACITY =
+ cpp::numeric_limits<size_t>::max() / sizeof(struct dirent *);
+ if (LIBC_UNLIKELY(capacity >= MAX_CAPACITY / 2))
+ return false;
- // Scandir must return the number of entries as int, anything
- // above that is non-representable.
- if (count == cpp::numeric_limits<int>::max()) {
- error_code = EOVERFLOW;
- break;
- }
+ size_t new_capacity = (capacity == 0) ? INITIAL_CAPACITY : capacity * 2;
- if (count >= buffer_capacity) {
- size_t new_capacity = (buffer_capacity == 0) ? 8 : buffer_capacity * 2;
-
- // Overflow check
- if (new_capacity >
- cpp::numeric_limits<size_t>::max() / sizeof(struct dirent *)) {
- error_code = ENOMEM;
- break;
- }
-
- struct dirent **bigger_buffer = static_cast<struct dirent **>(
- ::realloc(entries, new_capacity * sizeof(struct dirent *)));
- if (bigger_buffer == nullptr) {
- error_code = ENOMEM;
- break;
- }
- buffer_capacity = new_capacity;
- entries = bigger_buffer;
+ AllocChecker ac;
+ struct dirent **bigger_buffer = new (ac) struct dirent *[new_capacity];
+ if (LIBC_UNLIKELY(!ac))
+ return false;
+
+ if (entries != nullptr) {
+ inline_memcpy(bigger_buffer, entries, count * sizeof(struct dirent *));
+ delete[] entries;
}
- size_t reclen = DirType::reclen(entry);
+ capacity = new_capacity;
+ entries = bigger_buffer;
+ return true;
+ }
- struct dirent *new_entry = static_cast<struct dirent *>(::malloc(reclen));
- if (new_entry == nullptr) {
- error_code = ENOMEM;
- break;
- }
- inline_memcpy(new_entry, entry, reclen);
+ LIBC_INLINE void push_back(struct dirent *entry) { entries[count++] = entry; }
- entries[count] = new_entry;
- count++;
+ LIBC_INLINE struct dirent **release() {
+ struct dirent **res = entries;
+ entries = nullptr;
+ count = 0;
+ capacity = 0;
+ return res;
}
+};
+
+template <typename DirType>
+LIBC_INLINE ErrorOr<DirScanResult> scan_impl(
+ const char *name, int (*filter)(const struct dirent *) = nullptr,
+ int (*compare)(const struct dirent **, const struct dirent **) = nullptr) {
+ const auto res_open = DirType::open(name);
+ if (LIBC_UNLIKELY(!res_open))
+ return LIBC_NAMESPACE::Error(res_open.error());
- dir->close();
+ cpp::unique_ptr<DirType, DirCloser<DirType>> dir(res_open.value());
+ ScannedEntries entries;
- if (error_code != 0) {
- free_entries();
- return LIBC_NAMESPACE::Error(error_code);
+ constexpr size_t MAX_ENTRIES =
+ static_cast<size_t>(cpp::numeric_limits<int>::max());
+
+ while (true) {
+ const auto res_read = dir->read();
+ if (LIBC_UNLIKELY(!res_read))
+ return LIBC_NAMESPACE::Error(res_read.error());
+
+ struct dirent *const entry = res_read.value();
+ if (entry == nullptr)
+ break;
+
+ if (filter != nullptr && !filter(entry))
+ continue;
+
+ // Scandir must return the number of entries as int, anything
+ // above that is non-representable.
+ if (LIBC_UNLIKELY(entries.size() == MAX_ENTRIES))
+ return LIBC_NAMESPACE::Error(EOVERFLOW);
+
+ if (LIBC_UNLIKELY(!entries.ensure_capacity()))
+ return LIBC_NAMESPACE::Error(ENOMEM);
+
+ const size_t reclen = DirType::reclen(entry);
+ AllocChecker ac;
+ // Allocate only the variable-length record size (reclen) to save memory
+ // whilst heap allocation guarantees max_align_t alignment for struct
+ // dirent.
+ cpp::unique_ptr<uint8_t[]> new_entry_buf(new (ac) uint8_t[reclen]);
+ if (LIBC_UNLIKELY(!ac))
+ return LIBC_NAMESPACE::Error(ENOMEM);
+
+ inline_memcpy(new_entry_buf.get(), entry, reclen);
+ entries.push_back(
+ reinterpret_cast<struct dirent *>(new_entry_buf.release()));
}
- if (compare != nullptr && count > 1) {
+ dir.reset();
+
+ const size_t NUM_ENTRIES = entries.size();
----------------
vonosmas wrote:
This is not a compile-time constant, can it be `num_entries` instead?
https://github.com/llvm/llvm-project/pull/240107
More information about the libc-commits
mailing list