Skip to content

Commit

Permalink
[dfs]mmap的文件在关闭后释放file指针
Browse files Browse the repository at this point in the history
  • Loading branch information
heyuanjie87 committed Jan 14, 2025
1 parent 4e37047 commit 8ae9131
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions components/dfs/dfs_v2/include/dfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_empty_file_alloc(void);
void dfs_file_free(struct dfs_file *file);

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 28 additions & 4 deletions components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ int dfs_init(void)
}
INIT_PREV_EXPORT(dfs_init);

struct dfs_file* dfs_empty_file_alloc(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_free(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.
Expand Down Expand Up @@ -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_empty_file_alloc();

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);
Expand Down
1 change: 1 addition & 0 deletions components/dfs/dfs_v2/src/dfs_file_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_free(file);
}
else
{
Expand Down

0 comments on commit 8ae9131

Please sign in to comment.