Skip to content

Commit

Permalink
fs/inode: minor change to save the code size
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
anchao committed Nov 7, 2023
1 parent 6403965 commit 57ab766
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit 57ab766

Please sign in to comment.