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.
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"

View File

@ -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
@ -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;
@ -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;
}
@ -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;
@ -825,6 +835,7 @@ int fs_putfilep(FAR struct file *filep)
return ret;
}
#endif
/****************************************************************************
* Name: nx_dup2_from_tcb
@ -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.
*/
@ -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
}
/****************************************************************************

View File

@ -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 */
@ -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