mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-06 17:12:01 +08:00
@@ -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
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user