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.
This commit is contained in:
Augustin Cavalier 2025-01-03 12:28:25 -05:00
parent 66f51cb3b9
commit 5911165316

View File

@ -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) {