rpmsgfs: remove memcpy in rpmsgfs open/close

improve the rpmsgfs performance

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2023-12-18 22:51:09 +08:00 committed by Mateusz Szafoni
parent 3f47fd767a
commit 5613675834

View File

@ -169,8 +169,8 @@ static const rpmsg_ept_cb g_rpmsgfs_handler[] =
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
static int rpmsgfs_attach_file(FAR struct rpmsgfs_server_s *priv, static int rpmsgfs_alloc_file(FAR struct rpmsgfs_server_s *priv,
FAR struct file *filep) FAR struct file **filep)
{ {
FAR struct file **tmp; FAR struct file **tmp;
int ret; int ret;
@ -185,9 +185,7 @@ static int rpmsgfs_attach_file(FAR struct rpmsgfs_server_s *priv,
{ {
if (priv->files[i][j].f_inode == NULL) if (priv->files[i][j].f_inode == NULL)
{ {
memcpy(&priv->files[i][j], filep, sizeof(*filep)); goto found;
ret = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j;
goto out;
} }
} }
} }
@ -213,34 +211,18 @@ static int rpmsgfs_attach_file(FAR struct rpmsgfs_server_s *priv,
priv->files = tmp; priv->files = tmp;
priv->file_rows++; priv->file_rows++;
memcpy(&priv->files[i][0], filep, sizeof(*filep)); j = 0;
ret = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
found:
priv->files[i][j].f_inode = (FAR struct inode *)-1;
*filep = &priv->files[i][j];
ret = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j;
out: out:
nxmutex_unlock(&priv->lock); nxmutex_unlock(&priv->lock);
return ret; return ret;
} }
static int rpmsgfs_detach_file(FAR struct rpmsgfs_server_s *priv,
int fd, FAR struct file *filep)
{
struct file *tfilep;
if (fd < 0 || fd >= priv->file_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)
{
return -EBADF;
}
nxmutex_lock(&priv->lock);
tfilep = &priv->files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]
[fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK];
memcpy(filep, tfilep, sizeof(*filep));
memset(tfilep, 0, sizeof(*tfilep));
nxmutex_unlock(&priv->lock);
return 0;
}
static FAR struct file *rpmsgfs_get_file( static FAR struct file *rpmsgfs_get_file(
FAR struct rpmsgfs_server_s *priv, FAR struct rpmsgfs_server_s *priv,
int fd) int fd)
@ -334,20 +316,24 @@ static int rpmsgfs_open_handler(FAR struct rpmsg_endpoint *ept,
uint32_t src, FAR void *priv) uint32_t src, FAR void *priv)
{ {
FAR struct rpmsgfs_open_s *msg = data; FAR struct rpmsgfs_open_s *msg = data;
struct file file; FAR struct file *filep;
int ret; int ret;
int fd;
ret = file_open(&file, msg->pathname, msg->flags, msg->mode); ret = fd = rpmsgfs_alloc_file(priv, &filep);
if (ret >= 0) if (ret < 0)
{ {
ret = rpmsgfs_attach_file(priv, &file); goto out;
if (ret < 0)
{
file_close(&file);
}
} }
msg->header.result = ret; ret = file_open(filep, msg->pathname, msg->flags, msg->mode);
if (ret < 0)
{
filep->f_inode = NULL;
}
out:
msg->header.result = ret < 0 ? ret : fd;
return rpmsg_send(ept, msg, sizeof(*msg)); return rpmsg_send(ept, msg, sizeof(*msg));
} }
@ -356,13 +342,13 @@ static int rpmsgfs_close_handler(FAR struct rpmsg_endpoint *ept,
uint32_t src, FAR void *priv) uint32_t src, FAR void *priv)
{ {
FAR struct rpmsgfs_close_s *msg = data; FAR struct rpmsgfs_close_s *msg = data;
struct file file; FAR struct file *filep;
int ret; int ret = -ENOENT;
ret = rpmsgfs_detach_file(priv, msg->fd, &file); filep = rpmsgfs_get_file(priv, msg->fd);
if (ret >= 0) if (filep)
{ {
ret = file_close(&file); ret = file_close(filep);
} }
msg->header.result = ret; msg->header.result = ret;
@ -511,25 +497,23 @@ static int rpmsgfs_dup_handler(FAR struct rpmsg_endpoint *ept,
uint32_t src, FAR void *priv) uint32_t src, FAR void *priv)
{ {
FAR struct rpmsgfs_dup_s *msg = data; FAR struct rpmsgfs_dup_s *msg = data;
FAR struct file *newfilep = NULL;
FAR struct file *filep; FAR struct file *filep;
struct file newfile; int ret;
int ret = -ENOENT; int fd;
filep = rpmsgfs_get_file(priv, msg->fd); filep = rpmsgfs_get_file(priv, msg->fd);
if (filep != NULL) ret = fd = rpmsgfs_alloc_file(priv, &newfilep);
if (filep != NULL && ret >= 0)
{ {
ret = file_dup2(filep, &newfile); ret = file_dup2(filep, newfilep);
if (ret >= 0) if (ret < 0)
{ {
ret = rpmsgfs_attach_file(priv, &newfile); file_close(newfilep);
if (ret < 0)
{
file_close(&newfile);
}
} }
} }
msg->header.result = ret; msg->header.result = ret < 0 ? ret : fd;
return rpmsg_send(ept, msg, sizeof(*msg)); return rpmsg_send(ept, msg, sizeof(*msg));
} }