From 59111653162ee8f0b07b19b705ec81bde9972dfb Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Fri, 3 Jan 2025 12:28:25 -0500 Subject: [PATCH] kernel/fs: Consolidate allocation of FD tables. Adjust vfs_resize_fd_table to support allocating tables when none have been allocated before, and then just use it in vfs_new_io_context rather than doing the same calculations and allocations. No behavioral change intended. --- src/system/kernel/fs/vfs.cpp | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/system/kernel/fs/vfs.cpp b/src/system/kernel/fs/vfs.cpp index 675a1827be..fd75bbb357 100644 --- a/src/system/kernel/fs/vfs.cpp +++ b/src/system/kernel/fs/vfs.cpp @@ -4934,6 +4934,7 @@ vfs_new_io_context(const io_context* parentContext, bool purgeCloseOnExec) memset(context, 0, sizeof(io_context)); context->ref_count = 1; + rw_lock_init(&context->lock, "I/O context"); ReadLocker parentLocker; @@ -4944,25 +4945,11 @@ vfs_new_io_context(const io_context* parentContext, bool purgeCloseOnExec) } else tableSize = DEFAULT_FD_TABLE_SIZE; - // allocate space for FDs and their close-on-exec flag - context->fds = (file_descriptor**)malloc( - sizeof(struct file_descriptor*) * tableSize - + sizeof(struct select_info**) * tableSize - + (tableSize + 7) / 8); - if (context->fds == NULL) { + if (vfs_resize_fd_table(context, tableSize) != B_OK) { free(context); return NULL; } - context->select_infos = (select_info**)(context->fds + tableSize); - context->fds_close_on_exec = (uint8*)(context->select_infos + tableSize); - - memset(context->fds, 0, sizeof(struct file_descriptor*) * tableSize - + sizeof(struct select_info**) * tableSize - + (tableSize + 7) / 8); - - rw_lock_init(&context->lock, "I/O context"); - // Copy all parent file descriptors if (parentContext != NULL) { @@ -5010,8 +4997,6 @@ vfs_new_io_context(const io_context* parentContext, bool purgeCloseOnExec) inc_vnode_ref_count(context->cwd); } - context->table_size = tableSize; - list_init(&context->node_monitors); context->max_monitors = DEFAULT_NODE_MONITORS; @@ -5074,13 +5059,15 @@ vfs_resize_fd_table(struct io_context* context, uint32 newSize) context->fds_close_on_exec = (uint8*)(context->select_infos + newSize); context->table_size = newSize; - // copy entries from old tables - uint32 toCopy = min_c(oldSize, newSize); + if (oldSize != 0) { + // copy entries from old tables + uint32 toCopy = min_c(oldSize, newSize); - memcpy(context->fds, oldFDs, sizeof(void*) * toCopy); - memcpy(context->select_infos, oldSelectInfos, sizeof(void*) * toCopy); - memcpy(context->fds_close_on_exec, oldCloseOnExecTable, - min_c(oldCloseOnExitBitmapSize, newCloseOnExitBitmapSize)); + memcpy(context->fds, oldFDs, sizeof(void*) * toCopy); + memcpy(context->select_infos, oldSelectInfos, sizeof(void*) * toCopy); + memcpy(context->fds_close_on_exec, oldCloseOnExecTable, + min_c(oldCloseOnExitBitmapSize, newCloseOnExitBitmapSize)); + } // clear additional entries, if the tables grow if (newSize > oldSize) {