Add reference count configuration

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
This commit is contained in:
zhangshoukui 2024-09-02 14:30:20 +08:00 committed by Xiang Xiao
parent 0b084308ba
commit 13dd7bbfb3
3 changed files with 29 additions and 0 deletions

View File

@ -119,6 +119,13 @@ config FS_HEAPSIZE
Support for shm/tmpfs/fs_pseudofile.c ram based fs memory. Support for shm/tmpfs/fs_pseudofile.c ram based fs memory.
default 0 to use kmm directly. independent heap disabled 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/vfs/Kconfig"
source "fs/aio/Kconfig" source "fs/aio/Kconfig"
source "fs/semaphore/Kconfig" source "fs/semaphore/Kconfig"

View File

@ -71,6 +71,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(NULL);
filep = &list->fl_files[l1][l2]; filep = &list->fl_files[l1][l2];
#ifdef CONFIG_FS_REFCOUNT
if (filep->f_inode != NULL) if (filep->f_inode != NULL)
{ {
/* When the reference count is zero but the inode has not yet been /* When the reference count is zero but the inode has not yet been
@ -99,6 +100,12 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
filep->f_refs = 2; filep->f_refs = 2;
*new = true; *new = true;
} }
#else
if (filep->f_inode == NULL && new == NULL)
{
filep = NULL;
}
#endif
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(NULL, flags);
return filep; return filep;
@ -583,7 +590,9 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
filep->f_pos = pos; filep->f_pos = pos;
filep->f_inode = inode; filep->f_inode = inode;
filep->f_priv = priv; filep->f_priv = priv;
#ifdef CONFIG_FS_REFCOUNT
filep->f_refs = 1; filep->f_refs = 1;
#endif
goto found; goto found;
} }
@ -799,6 +808,7 @@ int fs_getfilep(int fd, FAR struct file **filep)
* file' instance. * file' instance.
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep) int fs_putfilep(FAR struct file *filep)
{ {
irqstate_t flags; irqstate_t flags;
@ -825,6 +835,7 @@ int fs_putfilep(FAR struct file *filep)
return ret; return ret;
} }
#endif
/**************************************************************************** /****************************************************************************
* Name: nx_dup2_from_tcb * Name: nx_dup2_from_tcb
@ -969,6 +980,8 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
return -EBADF; return -EBADF;
} }
#ifdef CONFIG_FS_REFCOUNT
/* files_fget will increase the reference count, there call fs_putfilep /* files_fget will increase the reference count, there call fs_putfilep
* reduce reference count. * reduce reference count.
*/ */
@ -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 */ /* Undo the last reference count from file_allocate_from_tcb */
return fs_putfilep(filep); return fs_putfilep(filep);
#else
return file_close(filep);
#endif
} }
/**************************************************************************** /****************************************************************************

View File

@ -465,7 +465,9 @@ typedef struct cookie_io_functions_t
struct file struct file
{ {
int f_oflags; /* Open mode flags */ int f_oflags; /* Open mode flags */
#ifdef CONFIG_FS_REFCOUNT
int f_refs; /* Reference count */ int f_refs; /* Reference count */
#endif
off_t f_pos; /* File position */ off_t f_pos; /* File position */
FAR struct inode *f_inode; /* Driver or file system interface */ FAR struct inode *f_inode; /* Driver or file system interface */
FAR void *f_priv; /* Per file driver private data */ FAR void *f_priv; /* Per file driver private data */
@ -1165,7 +1167,11 @@ int fs_getfilep(int fd, FAR struct file **filep);
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_FS_REFCOUNT
int fs_putfilep(FAR struct file *filep); int fs_putfilep(FAR struct file *filep);
#else
# define fs_putfilep(f)
#endif
/**************************************************************************** /****************************************************************************
* Name: file_close * Name: file_close