Browse Source

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

* [dfs]mmap的文件在关闭后释放file指针
heyuanjie87 2 months ago
parent
commit
d3d33ff983

+ 3 - 0
components/dfs/dfs_v2/include/dfs.h

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

+ 29 - 12
components/dfs/dfs_v2/src/dfs.c

@@ -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
         {

+ 1 - 0
components/dfs/dfs_v2/src/dfs_file_mmap.c

@@ -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
         {