static int filemap_alloc(int fd, struct file **file_store) { struct file *file = get_filemap(); if (fd == NO_FD) { for (fd = 0; fd < FS_STRUCT_NENTRY; fd++, file++) { if (file->status == FD_NONE) { goto found; } } return -E_MAX_OPEN; } else { if (testfd(fd)) { file += fd; if (file->status == FD_NONE) { goto found; } return -E_BUSY; } return -E_INVAL; } found: assert(fopen_count(file) == 0); file->status = FD_INIT, file->node = NULL; *file_store = file; return 0; }
void filemap_release(struct file *file) { assert(file->status == FD_OPENED || file->status == FD_CLOSED); assert(fopen_count(file) > 0); if (fopen_count_dec(file) == 0) { filemap_free(file); } }
static void filemap_free(struct file *file) { assert(file->status == FD_INIT || file->status == FD_CLOSED); assert(fopen_count(file) == 0); if (file->status == FD_CLOSED) { vfs_close(file->node); } file->status = FD_NONE; }
// fd_array_close - file's open_count--; if file's open_count-- == 0 , then call fd_array_free to free this file item void fd_array_close(struct file *file) { assert(file->status == FD_OPENED); assert(fopen_count(file) > 0); file->status = FD_CLOSED; if (fopen_count_dec(file) == 0) { fd_array_free(file); } }
void filemap_release(struct file *file) { assert(fopen_count(file) > 0); fopen_count_dec(file); }