Skip to content

Commit

Permalink
Userland: Statically allocate the buffer for readdir
Browse files Browse the repository at this point in the history
Not sure if this is 100% POSIX compliant, because we invalidate the
directory entry with each 'readdir' call, but who cares.
  • Loading branch information
asynts committed Apr 17, 2021
1 parent c6c7d4c commit ac011b4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions Std/MemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions Userland/LibC/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Userland/LibC/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ac011b4

Please sign in to comment.