* @brief Allocate an available file descriptor slot in the file descriptor table * * @param[in,out] fdt Pointer to the file descriptor table to allocate from * @param[in] startfd The starting file descriptor index to begin searching * * @return int The allocated file descriptor index if successful (>= 0), * -1 if allocation failed (table expansion failed) * * @note If no empty slot
| 103 | * @note If no empty slot found, expand the table and return the new index. |
| 104 | */ |
| 105 | static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd) |
| 106 | { |
| 107 | int idx; |
| 108 | |
| 109 | /* find an empty fd slot */ |
| 110 | for (idx = startfd; idx < (int)fdt->maxfd; idx++) |
| 111 | { |
| 112 | if (fdt->fds[idx] == RT_NULL) |
| 113 | { |
| 114 | return idx; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | idx = fdt->maxfd; |
| 119 | if (idx < startfd) |
| 120 | { |
| 121 | idx = startfd; |
| 122 | } |
| 123 | |
| 124 | if (_fdt_slot_expand(fdt, idx) < 0) |
| 125 | { |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | return idx; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @brief Allocate a file descriptor from the file descriptor table |
no test coverage detected