Files
mkrtos-real/mkrtos_user/lib/libc_backend/src/fd_map.c

183 lines
4.0 KiB
C
Raw Normal View History

2024-08-27 23:02:54 +08:00
/**
* @file fd_map.c
* @author your name (1358745329@qq.com)
* @brief fd map layer
2024-08-27 23:02:54 +08:00
* @version 0.1
* @date 2024-08-08
*
* @copyright Copyright (c) ATShining 2024
*
*/
2023-11-18 22:58:10 +08:00
#include <u_types.h>
#include <assert.h>
#include <malloc.h>
#include <errno.h>
#include <pthread.h>
2023-11-18 23:31:00 +08:00
#include <fd_map.h>
#include <string.h>
2023-11-18 22:58:10 +08:00
2023-12-05 00:01:26 +08:00
#define FD_MAP_TOTAL (CONFIG_FD_MAP_ROW_CN * CONFIG_FD_MAP_ROW_NR)
2023-11-18 22:58:10 +08:00
typedef struct fd_map_row
{
2023-12-05 00:01:26 +08:00
fd_map_entry_t entry[CONFIG_FD_MAP_ROW_CN];
2023-11-18 22:58:10 +08:00
} fd_map_row_t;
typedef struct fd_map
{
2023-12-05 00:01:26 +08:00
fd_map_row_t *row[CONFIG_FD_MAP_ROW_NR];
2023-11-18 22:58:10 +08:00
pthread_spinlock_t lock;
uint16_t free_fd;
} fd_map_t;
static fd_map_t fd_map;
2024-08-27 23:02:54 +08:00
/**
* @brief fd
*
* @param svr_fd
* @param priv_fd
* @param type
* @return int
*/
2023-12-08 23:55:00 +08:00
int fd_map_alloc(uint32_t svr_fd, uint32_t priv_fd, enum fd_type type)
2023-11-18 22:58:10 +08:00
{
int alloc_fd = 0;
pthread_spin_lock(&fd_map.lock);
if (fd_map.free_fd >= FD_MAP_TOTAL)
{
// 没有可用的fd了尝试循环查找
2023-12-05 00:01:26 +08:00
for (int i = 0; i < CONFIG_FD_MAP_ROW_NR; i++)
2023-11-18 22:58:10 +08:00
{
2023-12-05 00:01:26 +08:00
for (int j = 0; j < CONFIG_FD_MAP_ROW_CN; j++)
2023-11-18 22:58:10 +08:00
{
if (fd_map.row[i]->entry[j].flags == 0)
{
2023-12-05 00:01:26 +08:00
alloc_fd = i * CONFIG_FD_MAP_ROW_CN + j;
2023-11-18 22:58:10 +08:00
goto next;
}
}
}
pthread_spin_unlock(&fd_map.lock);
// 没有可用的了
return -EAGAIN;
}
else
{
alloc_fd = fd_map.free_fd;
}
next:;
2023-12-05 00:01:26 +08:00
int row_inx = alloc_fd / CONFIG_FD_MAP_ROW_CN;
int inx = alloc_fd % CONFIG_FD_MAP_ROW_CN;
2023-11-18 22:58:10 +08:00
2023-12-05 00:01:26 +08:00
assert(row_inx < CONFIG_FD_MAP_ROW_NR);
2023-11-18 22:58:10 +08:00
if (fd_map.row[row_inx] == NULL)
{
fd_map.row[row_inx] = malloc(sizeof(fd_map_row_t));
if (fd_map.row[row_inx] == NULL)
{
pthread_spin_unlock(&fd_map.lock);
return -EAGAIN;
}
2023-11-18 23:31:00 +08:00
memset(fd_map.row[row_inx], 0, sizeof(fd_map_row_t));
2023-11-18 22:58:10 +08:00
}
assert(fd_map.row[row_inx]->entry[inx].flags == 0);
fd_map.row[row_inx]->entry[inx].flags = 1;
fd_map.row[row_inx]->entry[inx].svr_fd = svr_fd;
fd_map.row[row_inx]->entry[inx].priv_fd = priv_fd;
2023-11-18 23:31:00 +08:00
fd_map.row[row_inx]->entry[inx].type = type;
fd_map.free_fd++;
2023-11-18 22:58:10 +08:00
pthread_spin_unlock(&fd_map.lock);
return alloc_fd;
}
2024-08-27 23:02:54 +08:00
/**
* @brief fd
*
* @param fd
* @param new_entry
* @return int
*/
2023-11-18 23:31:00 +08:00
int fd_map_get(int fd, fd_map_entry_t *new_entry)
{
assert(new_entry);
if (fd >= FD_MAP_TOTAL)
{
return -1;
}
2023-12-05 00:01:26 +08:00
int row_inx = fd / CONFIG_FD_MAP_ROW_CN;
int inx = fd % CONFIG_FD_MAP_ROW_CN;
pthread_spin_lock(&fd_map.lock);
2023-11-18 22:58:10 +08:00
2024-04-07 15:04:30 +00:00
if (fd_map.row[row_inx] == NULL)
{
pthread_spin_unlock(&fd_map.lock);
2024-04-07 15:04:30 +00:00
return -1;
}
2023-11-18 23:31:00 +08:00
*new_entry = fd_map.row[row_inx]->entry[inx];
pthread_spin_unlock(&fd_map.lock);
2023-11-18 23:31:00 +08:00
return 0;
}
2024-08-27 23:02:54 +08:00
/**
* @brief fd
*
* @param fd
* @param new_entry
* @return int
*/
2023-11-18 22:58:10 +08:00
int fd_map_update(int fd, fd_map_entry_t *new_entry)
{
if (fd >= FD_MAP_TOTAL)
{
return -1;
}
2023-12-05 00:01:26 +08:00
int row_inx = fd / CONFIG_FD_MAP_ROW_CN;
int inx = fd % CONFIG_FD_MAP_ROW_CN;
2023-11-18 22:58:10 +08:00
pthread_spin_lock(&fd_map.lock);
int flags = fd_map.row[row_inx]->entry[inx].flags;
fd_map.row[row_inx]->entry[inx] = *new_entry;
fd_map.row[row_inx]->entry[inx].flags = flags;
pthread_spin_unlock(&fd_map.lock);
return 0;
2023-11-18 22:58:10 +08:00
}
2024-08-27 23:02:54 +08:00
/**
* @brief fd
*
* @param fd
* @param ret_entry
* @return int
*/
2023-11-18 22:58:10 +08:00
int fd_map_free(int fd, fd_map_entry_t *ret_entry)
{
if (fd >= FD_MAP_TOTAL)
{
return -1;
}
2023-12-05 00:01:26 +08:00
int row_inx = fd / CONFIG_FD_MAP_ROW_CN;
int inx = fd % CONFIG_FD_MAP_ROW_CN;
2023-11-18 22:58:10 +08:00
pthread_spin_lock(&fd_map.lock);
if (fd_map.row[row_inx]->entry[inx].flags == 1)
{
if (ret_entry)
{
*ret_entry = fd_map.row[row_inx]->entry[inx];
}
fd_map.row[row_inx]->entry[inx].flags = 0;
2023-11-18 23:31:00 +08:00
if (fd_map.free_fd > fd)
{
fd_map.free_fd = fd;
}
2023-11-18 22:58:10 +08:00
pthread_spin_unlock(&fd_map.lock);
}
else
{
pthread_spin_unlock(&fd_map.lock);
return -1;
}
return 0;
}