Skip to content

Commit

Permalink
Add reference count configuration
Browse files Browse the repository at this point in the history
Signed-off-by: zhangshoukui <[email protected]>
  • Loading branch information
Zhangshoukui authored and xiaoxiang781216 committed Sep 17, 2024
1 parent 0b08430 commit 13dd7bb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions fs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ config FS_HEAPSIZE
Support for shm/tmpfs/fs_pseudofile.c ram based fs memory.
default 0 to use kmm directly. independent heap disabled

config FS_REFCOUNT
bool "File reference count"
default !DEFAULT_SMALL
---help---
Enable will Records the number of filep references. The file is
actually closed when the count reaches 0

source "fs/vfs/Kconfig"
source "fs/aio/Kconfig"
source "fs/semaphore/Kconfig"
Expand Down
16 changes: 16 additions & 0 deletions fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
flags = spin_lock_irqsave(NULL);

filep = &list->fl_files[l1][l2];
#ifdef CONFIG_FS_REFCOUNT
if (filep->f_inode != NULL)
{
/* When the reference count is zero but the inode has not yet been
Expand Down Expand Up @@ -99,6 +100,12 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
filep->f_refs = 2;
*new = true;
}
#else
if (filep->f_inode == NULL && new == NULL)
{
filep = NULL;
}
#endif

spin_unlock_irqrestore(NULL, flags);
return filep;
Expand Down Expand Up @@ -583,7 +590,9 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
filep->f_pos = pos;
filep->f_inode = inode;
filep->f_priv = priv;
#ifdef CONFIG_FS_REFCOUNT
filep->f_refs = 1;
#endif

goto found;
}
Expand Down Expand Up @@ -799,6 +808,7 @@ int fs_getfilep(int fd, FAR struct file **filep)
* file' instance.
****************************************************************************/

#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep)
{
irqstate_t flags;
Expand All @@ -825,6 +835,7 @@ int fs_putfilep(FAR struct file *filep)

return ret;
}
#endif

/****************************************************************************
* Name: nx_dup2_from_tcb
Expand Down Expand Up @@ -969,6 +980,8 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
return -EBADF;
}

#ifdef CONFIG_FS_REFCOUNT

/* files_fget will increase the reference count, there call fs_putfilep
* reduce reference count.
*/
Expand All @@ -978,6 +991,9 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
/* Undo the last reference count from file_allocate_from_tcb */

return fs_putfilep(filep);
#else
return file_close(filep);
#endif
}

/****************************************************************************
Expand Down
6 changes: 6 additions & 0 deletions include/nuttx/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ typedef struct cookie_io_functions_t
struct file
{
int f_oflags; /* Open mode flags */
#ifdef CONFIG_FS_REFCOUNT
int f_refs; /* Reference count */
#endif
off_t f_pos; /* File position */
FAR struct inode *f_inode; /* Driver or file system interface */
FAR void *f_priv; /* Per file driver private data */
Expand Down Expand Up @@ -1165,7 +1167,11 @@ int fs_getfilep(int fd, FAR struct file **filep);
*
****************************************************************************/

#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep);
#else
# define fs_putfilep(f)
#endif

/****************************************************************************
* Name: file_close
Expand Down

0 comments on commit 13dd7bb

Please sign in to comment.