From ac011b4a5ca2db9f71d8ddf7af3f4be5efc3a657 Mon Sep 17 00:00:00 2001 From: Paul Scharnofske Date: Sat, 17 Apr 2021 10:41:49 +0200 Subject: [PATCH] Userland: Statically allocate the buffer for readdir Not sure if this is 100% POSIX compliant, because we invalidate the directory entry with each 'readdir' call, but who cares. --- Std/MemoryAllocator.cpp | 1 + Userland/LibC/dirent.c | 6 ++---- Userland/LibC/dirent.h | 3 +++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Std/MemoryAllocator.cpp b/Std/MemoryAllocator.cpp index d3b5a0d..f83af7c 100644 --- a/Std/MemoryAllocator.cpp +++ b/Std/MemoryAllocator.cpp @@ -36,6 +36,7 @@ namespace Std entry->m_next = nullptr; entry->m_size = round_to_word(size); + VERIFY(usize(entry->m_data) % 4 == 0); return entry->m_data; } diff --git a/Userland/LibC/dirent.c b/Userland/LibC/dirent.c index 8c946cc..57dd6ff 100644 --- a/Userland/LibC/dirent.c +++ b/Userland/LibC/dirent.c @@ -18,16 +18,14 @@ DIR* opendir(const char *path) struct dirent* readdir(DIR *dirp) { - struct dirent *entry = malloc(sizeof(struct dirent)); - - ssize_t retval = read(dirp->fd, entry, sizeof(struct dirent)); + ssize_t retval = read(dirp->fd, &dirp->entry, sizeof(struct dirent)); if (retval == 0) return NULL; assert(retval == sizeof(struct dirent)); - return entry; + return &dirp->entry; } int closedir(DIR *dirp) diff --git a/Userland/LibC/dirent.h b/Userland/LibC/dirent.h index c295ed0..ce51f96 100644 --- a/Userland/LibC/dirent.h +++ b/Userland/LibC/dirent.h @@ -5,6 +5,9 @@ typedef struct { int fd; + + // We return a pointer to this in each readdir call + struct dirent entry; } DIR; DIR* opendir(const char *path);