[dfs]mmap的文件在关闭后释放file指针 (#9917)

* [dfs]mmap的文件在关闭后释放file指针
This commit is contained in:
heyuanjie87
2025-02-09 15:55:30 +08:00
committed by GitHub
parent d83d71cc05
commit d3d33ff983
3 changed files with 33 additions and 12 deletions

View File

@@ -141,6 +141,9 @@ struct dfs_fdtable *dfs_fdtable_get_global(void);
int dfs_dup(int oldfd, int startfd);
#endif /* DFS_USING_POSIX */
struct dfs_file* dfs_file_create(void);
void dfs_file_destroy(struct dfs_file *file);
#ifdef __cplusplus
}
#endif

View File

@@ -190,6 +190,33 @@ int dfs_init(void)
}
INIT_PREV_EXPORT(dfs_init);
struct dfs_file* dfs_file_create(void)
{
struct dfs_file *file;
file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
if (file)
{
file->magic = DFS_FD_MAGIC;
file->ref_count = 1;
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
}
return file;
}
void dfs_file_destroy(struct dfs_file *file)
{
rt_mutex_detach(&file->pos_lock);
if (file->mmap_context)
{
rt_free(file->mmap_context);
}
rt_free(file);
}
/**
* @ingroup Fd
* This function will allocate a file descriptor.
@@ -217,13 +244,10 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
{
struct dfs_file *file;
file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
file = dfs_file_create();
if (file)
{
file->magic = DFS_FD_MAGIC;
file->ref_count = 1;
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
fdt->fds[idx] = file;
LOG_D("allocate a new fd @ %d", idx);
@@ -255,14 +279,7 @@ void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
if (file && file->ref_count == 1)
{
rt_mutex_detach(&file->pos_lock);
if (file->mmap_context)
{
rt_free(file->mmap_context);
}
rt_free(file);
dfs_file_destroy(file);
}
else
{

View File

@@ -152,6 +152,7 @@ static void on_varea_close(struct rt_varea *varea)
if (rt_atomic_load(&(file->ref_count)) == 1)
{
dfs_file_close(file);
dfs_file_destroy(file);
}
else
{