Skip to content

Commit d3d33ff

Browse files
authored
[dfs]mmap的文件在关闭后释放file指针 (#9917)
* [dfs]mmap的文件在关闭后释放file指针
1 parent d83d71c commit d3d33ff

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

components/dfs/dfs_v2/include/dfs.h

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ struct dfs_fdtable *dfs_fdtable_get_global(void);
141141
int dfs_dup(int oldfd, int startfd);
142142
#endif /* DFS_USING_POSIX */
143143

144+
struct dfs_file* dfs_file_create(void);
145+
void dfs_file_destroy(struct dfs_file *file);
146+
144147
#ifdef __cplusplus
145148
}
146149
#endif

components/dfs/dfs_v2/src/dfs.c

+29-12
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@ int dfs_init(void)
190190
}
191191
INIT_PREV_EXPORT(dfs_init);
192192

193+
struct dfs_file* dfs_file_create(void)
194+
{
195+
struct dfs_file *file;
196+
197+
file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
198+
if (file)
199+
{
200+
file->magic = DFS_FD_MAGIC;
201+
file->ref_count = 1;
202+
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
203+
}
204+
205+
return file;
206+
}
207+
208+
void dfs_file_destroy(struct dfs_file *file)
209+
{
210+
rt_mutex_detach(&file->pos_lock);
211+
212+
if (file->mmap_context)
213+
{
214+
rt_free(file->mmap_context);
215+
}
216+
217+
rt_free(file);
218+
}
219+
193220
/**
194221
* @ingroup Fd
195222
* This function will allocate a file descriptor.
@@ -217,13 +244,10 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
217244
{
218245
struct dfs_file *file;
219246

220-
file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
247+
file = dfs_file_create();
221248

222249
if (file)
223250
{
224-
file->magic = DFS_FD_MAGIC;
225-
file->ref_count = 1;
226-
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
227251
fdt->fds[idx] = file;
228252

229253
LOG_D("allocate a new fd @ %d", idx);
@@ -255,14 +279,7 @@ void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
255279

256280
if (file && file->ref_count == 1)
257281
{
258-
rt_mutex_detach(&file->pos_lock);
259-
260-
if (file->mmap_context)
261-
{
262-
rt_free(file->mmap_context);
263-
}
264-
265-
rt_free(file);
282+
dfs_file_destroy(file);
266283
}
267284
else
268285
{

components/dfs/dfs_v2/src/dfs_file_mmap.c

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static void on_varea_close(struct rt_varea *varea)
152152
if (rt_atomic_load(&(file->ref_count)) == 1)
153153
{
154154
dfs_file_close(file);
155+
dfs_file_destroy(file);
155156
}
156157
else
157158
{

0 commit comments

Comments
 (0)