diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index e61f432c52..40cb5f9911 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -154,7 +154,7 @@ static int files_extend(FAR struct filelist *list, size_t row) spin_unlock_irqrestore(&list->fl_lock, flags); - if (tmp != NULL) + if (tmp != NULL && tmp != &list->fl_prefile) { kmm_free(tmp); } @@ -298,7 +298,13 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, void files_initlist(FAR struct filelist *list) { - DEBUGASSERT(list); + /* The first row will reuse pre-allocated files, which will avoid + * unnecessary allocator accesses during file initialization. + */ + + list->fl_rows = 1; + list->fl_files = &list->fl_prefile; + list->fl_prefile = list->fl_prefiles; } /**************************************************************************** @@ -328,10 +334,16 @@ void files_releaselist(FAR struct filelist *list) file_close(&list->fl_files[i][j]); } - kmm_free(list->fl_files[i]); + if (i != 0) + { + kmm_free(list->fl_files[i]); + } } - kmm_free(list->fl_files); + if (list->fl_files != &list->fl_prefile) + { + kmm_free(list->fl_files); + } } /**************************************************************************** diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index d4f598cdab..3816e1501c 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -488,6 +488,15 @@ struct filelist spinlock_t fl_lock; /* Manage access to the file list */ uint8_t fl_rows; /* The number of rows of fl_files array */ FAR struct file **fl_files; /* The pointer of two layer file descriptors array */ + + /* Pre-allocated files to avoid allocator access during thread creation + * phase, For functional safety requirements, increase + * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK could also avoid allocator access + * caused by the file descriptor exceeding the limit. + */ + + FAR struct file *fl_prefile; + FAR struct file fl_prefiles[CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; }; /* The following structure defines the list of files used for standard C I/O.