From 64b0b49a1f23da9faa680f8158cb8a12cd4d118c Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Thu, 3 Oct 2024 14:21:12 +0200 Subject: libc: Add readdir_multi This is a faster, zero allocation function for reading directory entries. It has massive speed increases for certain applications since malloc(especially my implementation) is really slow in comparison. --- userland/libc/dirent/readdir.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'userland/libc/dirent/readdir.c') diff --git a/userland/libc/dirent/readdir.c b/userland/libc/dirent/readdir.c index 8b825cb..9a9f8fa 100644 --- a/userland/libc/dirent/readdir.c +++ b/userland/libc/dirent/readdir.c @@ -1,6 +1,19 @@ #include #include +int readdir_multi(DIR *dir, struct dirent *entries, int num_entries) { + size_t offset = dir->dir_num * sizeof(struct dirent); + int rc; + if (-1 == (rc = pread(dir->fd, entries, num_entries * sizeof(struct dirent), + offset))) { + return -1; + } + + int num_read_entries = rc / sizeof(struct dirent); + dir->dir_num += num_read_entries; + return num_read_entries; +} + struct dirent *readdir(DIR *dir) { size_t offset = dir->dir_num * sizeof(struct dirent); int rc; -- cgit v1.2.3