From 57ab766dd213f869c7fafad829687588b36cf13b Mon Sep 17 00:00:00 2001 From: chao an Date: Tue, 7 Nov 2023 21:49:09 +0800 Subject: [PATCH] fs/inode: minor change to save the code size Before: text data bss dec hex filename 360641 7889 4064 372594 5af72 nuttx After: text data bss dec hex filename 360313 7889 4064 372266 5ae2a nuttx Signed-off-by: chao an --- fs/inode/fs_files.c | 61 +++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index bcb7638288166..9f04b2197c134 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -51,6 +51,16 @@ * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: files_fget + ****************************************************************************/ + +static FAR struct file *files_fget(int fd, FAR struct filelist *list) +{ + return &list->fl_files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] + [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; +} + /**************************************************************************** * Name: files_extend ****************************************************************************/ @@ -201,16 +211,13 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, } } - filep = &list->fl_files[fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] - [fd2 % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; + filep = files_fget(fd2, list); memcpy(&file, filep, sizeof(struct file)); memset(filep, 0, sizeof(struct file)); /* Perform the dup3 operation */ - ret = file_dup3(&list->fl_files[fd1 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] - [fd1 % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK], - filep, flags); + ret = file_dup3(files_fget(fd1, list), filep, flags); #ifdef CONFIG_FDSAN filep->f_tag = file.f_tag; @@ -341,25 +348,9 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode, { do { - if (!list->fl_files[i][j].f_inode) + if (list->fl_files[i][j].f_inode == NULL) { - list->fl_files[i][j].f_oflags = oflags; - list->fl_files[i][j].f_pos = pos; - list->fl_files[i][j].f_inode = inode; - list->fl_files[i][j].f_priv = priv; - nxmutex_unlock(&list->fl_lock); - - if (addref) - { - inode_addref(inode); - } - -#ifdef CONFIG_FDCHECK - return - fdcheck_protect(i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j); -#else - return i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j; -#endif + goto found; } } while (++j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK); @@ -377,10 +368,13 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode, return ret; } - list->fl_files[i][0].f_oflags = oflags; - list->fl_files[i][0].f_pos = pos; - list->fl_files[i][0].f_inode = inode; - list->fl_files[i][0].f_priv = priv; + j = 0; +found: + + list->fl_files[i][j].f_oflags = oflags; + list->fl_files[i][j].f_pos = pos; + list->fl_files[i][j].f_inode = inode; + list->fl_files[i][j].f_priv = priv; nxmutex_unlock(&list->fl_lock); if (addref) @@ -389,9 +383,9 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode, } #ifdef CONFIG_FDCHECK - return fdcheck_protect(i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK); + return fdcheck_protect(i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j); #else - return i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; + return i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j; #endif } @@ -609,8 +603,7 @@ int fs_getfilep(int fd, FAR struct file **filep) return ret; } - *filep = &list->fl_files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] - [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; + *filep = files_fget(fd, list); /* if f_inode is NULL, fd was closed */ @@ -765,15 +758,13 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd) /* If the file was properly opened, there should be an inode assigned */ if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK || - !list->fl_files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] - [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK].f_inode) + files_fget(fd, list)->f_inode == NULL) { nxmutex_unlock(&list->fl_lock); return -EBADF; } - filep = &list->fl_files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] - [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; + filep = files_fget(fd, list); memcpy(&file, filep, sizeof(struct file)); memset(filep, 0, sizeof(struct file));