@@ -190,6 +190,33 @@ int dfs_init(void)
190
190
}
191
191
INIT_PREV_EXPORT (dfs_init );
192
192
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
+
193
220
/**
194
221
* @ingroup Fd
195
222
* This function will allocate a file descriptor.
@@ -217,13 +244,10 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
217
244
{
218
245
struct dfs_file * file ;
219
246
220
- file = ( struct dfs_file * ) rt_calloc ( 1 , sizeof ( struct dfs_file ) );
247
+ file = dfs_file_create ( );
221
248
222
249
if (file )
223
250
{
224
- file -> magic = DFS_FD_MAGIC ;
225
- file -> ref_count = 1 ;
226
- rt_mutex_init (& file -> pos_lock , "fpos" , RT_IPC_FLAG_PRIO );
227
251
fdt -> fds [idx ] = file ;
228
252
229
253
LOG_D ("allocate a new fd @ %d" , idx );
@@ -255,14 +279,7 @@ void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
255
279
256
280
if (file && file -> ref_count == 1 )
257
281
{
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 );
266
283
}
267
284
else
268
285
{
0 commit comments