* @brief Expand the file descriptor table to accommodate a specified file descriptor * * This function ensures that the file descriptor table in the given `dfs_fdtable` structure * has sufficient capacity to include the specified file descriptor `fd`. If the table * needs to be expanded, it reallocates memory and initializes new slots to `NULL`. * * @param[in,out] fdt Pointer to the file des
| 55 | * @note Expand table size to next multiple of 4 (but not exceeding DFS_FD_MAX) |
| 56 | */ |
| 57 | static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd) |
| 58 | { |
| 59 | int nr; |
| 60 | int index; |
| 61 | struct dfs_file **fds = NULL; |
| 62 | |
| 63 | if (fd < fdt->maxfd) |
| 64 | { |
| 65 | return fd; |
| 66 | } |
| 67 | if (fd >= DFS_FD_MAX) |
| 68 | { |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | nr = ((fd + 4) & ~3); |
| 73 | if (nr > DFS_FD_MAX) |
| 74 | { |
| 75 | nr = DFS_FD_MAX; |
| 76 | } |
| 77 | fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *)); |
| 78 | if (!fds) |
| 79 | { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | /* clean the new allocated fds */ |
| 84 | for (index = fdt->maxfd; index < nr; index++) |
| 85 | { |
| 86 | fds[index] = NULL; |
| 87 | } |
| 88 | fdt->fds = fds; |
| 89 | fdt->maxfd = nr; |
| 90 | |
| 91 | return fd; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @brief Allocate an available file descriptor slot in the file descriptor table |
no test coverage detected