You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/02/11 05:46:13 UTC
[incubator-nuttx] branch master updated: romfs: add FAR for all pointer and optimize some code
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new bff02fc romfs: add FAR for all pointer and optimize some code
bff02fc is described below
commit bff02fcd7927431c5e01a50a868048647f9d5d9c
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Thu Feb 10 11:41:50 2022 +0800
romfs: add FAR for all pointer and optimize some code
Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
fs/romfs/fs_romfs.c | 52 ++++++++----------------------
fs/romfs/fs_romfs.h | 52 +++++++++++++++---------------
fs/romfs/fs_romfsutil.c | 85 +++++++++++++++++++++++++------------------------
3 files changed, 82 insertions(+), 107 deletions(-)
diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c
index 637166a..5c20c9d 100644
--- a/fs/romfs/fs_romfs.c
+++ b/fs/romfs/fs_romfs.c
@@ -28,8 +28,6 @@
#include <sys/statfs.h>
#include <sys/stat.h>
-#include <stdint.h>
-#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
@@ -43,7 +41,6 @@
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/fs/dirent.h>
-#include <nuttx/mtd/mtd.h>
#include "fs_romfs.h"
@@ -231,7 +228,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath,
* file.
*/
- rf = (FAR struct romfs_file_s *)kmm_zalloc(sizeof(struct romfs_file_s));
+ rf = kmm_zalloc(sizeof(struct romfs_file_s));
if (!rf)
{
ferr("ERROR: Failed to allocate private data\n");
@@ -273,11 +270,6 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath,
rm->rm_refs++;
- romfs_semgive(rm);
- return OK;
-
- /* Error exits */
-
errout_with_semaphore:
romfs_semgive(rm);
return ret;
@@ -348,7 +340,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer,
FAR struct romfs_mountpt_s *rm;
FAR struct romfs_file_s *rf;
unsigned int bytesread;
- unsigned int readsize;
+ unsigned int readsize = 0;
unsigned int nsectors;
uint32_t offset;
size_t bytesleft;
@@ -402,7 +394,6 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer,
* error occurs.
*/
- readsize = 0;
while (buflen > 0)
{
/* Get the first sector and index to read from. */
@@ -474,12 +465,9 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer,
buflen -= bytesread;
}
- romfs_semgive(rm);
- return readsize;
-
errout_with_semaphore:
romfs_semgive(rm);
- return ret;
+ return ret < 0 ? ret : readsize;
}
/****************************************************************************
@@ -560,9 +548,6 @@ static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence)
filep->f_pos = position;
finfo("New file position: %jd\n", (intmax_t)filep->f_pos);
- romfs_semgive(rm);
- return OK;
-
errout_with_semaphore:
romfs_semgive(rm);
return ret;
@@ -630,7 +615,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp)
* structure
*/
- rm = (FAR struct romfs_mountpt_s *)newp->f_inode->i_private;
+ rm = newp->f_inode->i_private;
DEBUGASSERT(rm != NULL);
/* Check if the mount is still healthy */
@@ -656,7 +641,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp)
* dup'ed file.
*/
- newrf = (FAR struct romfs_file_s *)kmm_malloc(sizeof(struct romfs_file_s));
+ newrf = kmm_malloc(sizeof(struct romfs_file_s));
if (!newrf)
{
ferr("ERROR: Failed to allocate private data\n");
@@ -685,11 +670,6 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp)
rm->rm_refs++;
- romfs_semgive(rm);
- return OK;
-
- /* Error exits */
-
errout_with_semaphore:
romfs_semgive(rm);
return ret;
@@ -721,7 +701,7 @@ static int romfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
*/
rf = filep->f_priv;
- rm = (FAR struct romfs_mountpt_s *)filep->f_inode->i_private;
+ rm = filep->f_inode->i_private;
DEBUGASSERT(rm != NULL);
/* Check if the mount is still healthy */
@@ -757,7 +737,7 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct fs_dirent_s *dir)
{
FAR struct romfs_mountpt_s *rm;
- FAR struct romfs_dirinfo_s dirinfo;
+ struct romfs_dirinfo_s dirinfo;
int ret;
finfo("relpath: '%s'\n", relpath);
@@ -808,8 +788,6 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
/* The entry is a directory */
memcpy(&dir->u.romfs, &dirinfo.rd_dir, sizeof(struct fs_romfsdir_s));
- romfs_semgive(rm);
- return OK;
errout_with_semaphore:
romfs_semgive(rm);
@@ -1001,8 +979,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
/* Create an instance of the mountpt state structure */
- rm = (FAR struct romfs_mountpt_s *)
- kmm_zalloc(sizeof(struct romfs_mountpt_s));
+ rm = kmm_zalloc(sizeof(struct romfs_mountpt_s));
if (!rm)
{
ferr("ERROR: Failed to allocate mountpoint structure\n");
@@ -1014,8 +991,8 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* have to addref() here (but does have to release in ubind().
*/
- nxsem_init(&rm->rm_sem, 0, 0); /* Initialize the semaphore that controls access */
- rm->rm_blkdriver = blkdriver; /* Save the block driver reference */
+ nxsem_init(&rm->rm_sem, 0, 0); /* Initialize the semaphore that controls access */
+ rm->rm_blkdriver = blkdriver; /* Save the block driver reference */
/* Get the hardware configuration and setup buffering appropriately */
@@ -1066,7 +1043,7 @@ errout_with_sem:
static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags)
{
- FAR struct romfs_mountpt_s *rm = (FAR struct romfs_mountpt_s *)handle;
+ FAR struct romfs_mountpt_s *rm = handle;
int ret;
finfo("Entry\n");
@@ -1104,7 +1081,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
if (rm->rm_blkdriver)
{
- struct inode *inode = rm->rm_blkdriver;
+ FAR struct inode *inode = rm->rm_blkdriver;
if (inode)
{
if (INODE_IS_BLOCK(inode) && inode->u.i_bops->close != NULL)
@@ -1194,9 +1171,6 @@ static int romfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
buf->f_bavail = 0;
buf->f_namelen = NAME_MAX;
- romfs_semgive(rm);
- return OK;
-
errout_with_semaphore:
romfs_semgive(rm);
return ret;
@@ -1269,7 +1243,7 @@ static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct stat *buf)
{
FAR struct romfs_mountpt_s *rm;
- FAR struct romfs_dirinfo_s dirinfo;
+ struct romfs_dirinfo_s dirinfo;
uint8_t type;
int ret;
diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h
index 9b8591d..5fd2a63 100644
--- a/fs/romfs/fs_romfs.h
+++ b/fs/romfs/fs_romfs.h
@@ -126,17 +126,17 @@
struct romfs_file_s;
struct romfs_mountpt_s
{
- struct inode *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */
- bool rm_mounted; /* true: The file system is ready */
- uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver */
- sem_t rm_sem; /* Used to assume thread-safe access */
- uint32_t rm_refs; /* The references for all files opened on this mountpoint */
- uint32_t rm_rootoffset; /* Saved offset to the first root directory entry */
- uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */
- uint32_t rm_volsize; /* Size of the ROMFS volume */
- uint32_t rm_cachesector; /* Current sector in the rm_buffer */
- uint8_t *rm_xipbase; /* Base address of directly accessible media */
- uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */
+ FAR struct inode *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */
+ bool rm_mounted; /* true: The file system is ready */
+ uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver */
+ sem_t rm_sem; /* Used to assume thread-safe access */
+ uint32_t rm_refs; /* The references for all files opened on this mountpoint */
+ uint32_t rm_rootoffset; /* Saved offset to the first root directory entry */
+ uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */
+ uint32_t rm_volsize; /* Size of the ROMFS volume */
+ uint32_t rm_cachesector; /* Current sector in the rm_buffer */
+ FAR uint8_t *rm_xipbase; /* Base address of directly accessible media */
+ FAR uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */
};
/* This structure represents on open file under the mountpoint. An instance
@@ -146,11 +146,11 @@ struct romfs_mountpt_s
struct romfs_file_s
{
- uint32_t rf_startoffset; /* Offset to the start of the file data */
- uint32_t rf_size; /* Size of the file in bytes */
- uint32_t rf_cachesector; /* Current sector in the rf_buffer */
- uint8_t *rf_buffer; /* File sector buffer, allocated if rm_xipbase==0 */
- uint8_t rf_type; /* File type (for fstat()) */
+ uint32_t rf_startoffset; /* Offset to the start of the file data */
+ uint32_t rf_size; /* Size of the file in bytes */
+ uint32_t rf_cachesector; /* Current sector in the rf_buffer */
+ FAR uint8_t *rf_buffer; /* File sector buffer, allocated if rm_xipbase==0 */
+ uint8_t rf_type; /* File type (for fstat()) */
};
/* This structure is used internally for describing the result of
@@ -192,24 +192,24 @@ extern "C"
int romfs_semtake(FAR struct romfs_mountpt_s *rm);
void romfs_semgive(FAR struct romfs_mountpt_s *rm);
int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
- uint32_t sector, unsigned int nsectors);
+ uint32_t sector, unsigned int nsectors);
int romfs_filecacheread(FAR struct romfs_mountpt_s *rm,
- FAR struct romfs_file_s *rf, uint32_t sector);
+ FAR struct romfs_file_s *rf, uint32_t sector);
int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm);
int romfs_fsconfigure(FAR struct romfs_mountpt_s *rm);
int romfs_fileconfigure(FAR struct romfs_mountpt_s *rm,
- FAR struct romfs_file_s *rf);
+ FAR struct romfs_file_s *rf);
int romfs_checkmount(FAR struct romfs_mountpt_s *rm);
int romfs_finddirentry(FAR struct romfs_mountpt_s *rm,
- FAR struct romfs_dirinfo_s *dirinfo,
- FAR const char *path);
-int romfs_parsedirentry(FAR struct romfs_mountpt_s *rm,
- uint32_t offset, FAR uint32_t *poffset, FAR uint32_t *pnext,
- FAR uint32_t *pinfo, FAR uint32_t *psize);
+ FAR struct romfs_dirinfo_s *dirinfo,
+ FAR const char *path);
+int romfs_parsedirentry(FAR struct romfs_mountpt_s *rm, uint32_t offset,
+ FAR uint32_t *poffset, FAR uint32_t *pnext,
+ FAR uint32_t *pinfo, FAR uint32_t *psize);
int romfs_parsefilename(FAR struct romfs_mountpt_s *rm, uint32_t offset,
- FAR char *pname);
+ FAR char *pname);
int romfs_datastart(FAR struct romfs_mountpt_s *rm, uint32_t offset,
- FAR uint32_t *start);
+ FAR uint32_t *start);
#undef EXTERN
#if defined(__cplusplus)
diff --git a/fs/romfs/fs_romfsutil.c b/fs/romfs/fs_romfsutil.c
index d4af535..8a57ca6 100644
--- a/fs/romfs/fs_romfsutil.c
+++ b/fs/romfs/fs_romfsutil.c
@@ -28,8 +28,6 @@
#include <sys/types.h>
#include <inttypes.h>
-#include <stdint.h>
-#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
@@ -66,7 +64,7 @@
*
****************************************************************************/
-static uint32_t romfs_devread32(struct romfs_mountpt_s *rm, int ndx)
+static uint32_t romfs_devread32(FAR struct romfs_mountpt_s *rm, int ndx)
{
/* This should not read past the end of the sector since the directory
* entries are aligned at 16-byte boundaries.
@@ -86,10 +84,10 @@ static uint32_t romfs_devread32(struct romfs_mountpt_s *rm, int ndx)
*
****************************************************************************/
-static inline int romfs_checkentry(struct romfs_mountpt_s *rm,
- uint32_t offset, const char *entryname,
- int entrylen,
- struct romfs_dirinfo_s *dirinfo)
+static inline int romfs_checkentry(FAR struct romfs_mountpt_s *rm,
+ uint32_t offset,
+ FAR const char *entryname, int entrylen,
+ FAR struct romfs_dirinfo_s *dirinfo)
{
char name[NAME_MAX + 1];
uint32_t linkoffset;
@@ -163,7 +161,8 @@ static inline int romfs_checkentry(struct romfs_mountpt_s *rm,
*
****************************************************************************/
-int16_t romfs_devcacheread(struct romfs_mountpt_s *rm, uint32_t offset)
+static int16_t romfs_devcacheread(FAR struct romfs_mountpt_s *rm,
+ uint32_t offset)
{
uint32_t sector;
int ret;
@@ -222,8 +221,8 @@ int16_t romfs_devcacheread(struct romfs_mountpt_s *rm, uint32_t offset)
*
****************************************************************************/
-static int romfs_followhardlinks(struct romfs_mountpt_s *rm, uint32_t offset,
- uint32_t *poffset)
+static int romfs_followhardlinks(FAR struct romfs_mountpt_s *rm,
+ uint32_t offset, FAR uint32_t *poffset)
{
uint32_t next;
int16_t ndx;
@@ -271,9 +270,9 @@ static int romfs_followhardlinks(struct romfs_mountpt_s *rm, uint32_t offset,
*
****************************************************************************/
-static inline int romfs_searchdir(struct romfs_mountpt_s *rm,
- const char *entryname, int entrylen,
- struct romfs_dirinfo_s *dirinfo)
+static inline int romfs_searchdir(FAR struct romfs_mountpt_s *rm,
+ FAR const char *entryname, int entrylen,
+ FAR struct romfs_dirinfo_s *dirinfo)
{
uint32_t offset;
uint32_t next;
@@ -336,7 +335,7 @@ static inline int romfs_searchdir(struct romfs_mountpt_s *rm,
* Name: romfs_semtake
****************************************************************************/
-int romfs_semtake(struct romfs_mountpt_s *rm)
+int romfs_semtake(FAR struct romfs_mountpt_s *rm)
{
return nxsem_wait_uninterruptible(&rm->rm_sem);
}
@@ -345,7 +344,7 @@ int romfs_semtake(struct romfs_mountpt_s *rm)
* Name: romfs_semgive
****************************************************************************/
-void romfs_semgive(struct romfs_mountpt_s *rm)
+void romfs_semgive(FAR struct romfs_mountpt_s *rm)
{
nxsem_post(&rm->rm_sem);
}
@@ -357,7 +356,7 @@ void romfs_semgive(struct romfs_mountpt_s *rm)
*
****************************************************************************/
-int romfs_hwread(struct romfs_mountpt_s *rm, uint8_t *buffer,
+int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
uint32_t sector, unsigned int nsectors)
{
int ret = OK;
@@ -369,14 +368,14 @@ int romfs_hwread(struct romfs_mountpt_s *rm, uint8_t *buffer,
/* In XIP mode, we just copy the requested data */
memcpy(buffer,
- rm->rm_xipbase + sector*rm->rm_hwsectorsize,
- nsectors*rm->rm_hwsectorsize);
+ rm->rm_xipbase + sector * rm->rm_hwsectorsize,
+ nsectors * rm->rm_hwsectorsize);
}
else
{
/* In non-XIP mode, we have to read the data from the device */
- struct inode *inode = rm->rm_blkdriver;
+ FAR struct inode *inode = rm->rm_blkdriver;
ssize_t nsectorsread = -ENODEV;
DEBUGASSERT(inode);
@@ -412,8 +411,8 @@ int romfs_hwread(struct romfs_mountpt_s *rm, uint8_t *buffer,
*
****************************************************************************/
-int romfs_filecacheread(struct romfs_mountpt_s *rm, struct romfs_file_s *rf,
- uint32_t sector)
+int romfs_filecacheread(FAR struct romfs_mountpt_s *rm,
+ FAR struct romfs_file_s *rf, uint32_t sector)
{
int ret;
@@ -472,9 +471,9 @@ int romfs_filecacheread(struct romfs_mountpt_s *rm, struct romfs_file_s *rf,
*
****************************************************************************/
-int romfs_hwconfigure(struct romfs_mountpt_s *rm)
+int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm)
{
- struct inode *inode = rm->rm_blkdriver;
+ FAR struct inode *inode = rm->rm_blkdriver;
int ret;
/* Get the underlying device geometry */
@@ -556,7 +555,7 @@ int romfs_hwconfigure(struct romfs_mountpt_s *rm)
/* Allocate the device cache buffer for normal sector accesses */
- rm->rm_buffer = (FAR uint8_t *)kmm_malloc(rm->rm_hwsectorsize);
+ rm->rm_buffer = kmm_malloc(rm->rm_hwsectorsize);
if (!rm->rm_buffer)
{
return -ENOMEM;
@@ -576,10 +575,10 @@ int romfs_hwconfigure(struct romfs_mountpt_s *rm)
*
****************************************************************************/
-int romfs_fsconfigure(struct romfs_mountpt_s *rm)
+int romfs_fsconfigure(FAR struct romfs_mountpt_s *rm)
{
- const char *name;
- int16_t ndx;
+ FAR const char *name;
+ int16_t ndx;
/* Then get information about the ROMFS filesystem on the devices managed
* by this block driver. Read sector zero which contains the volume header.
@@ -623,7 +622,8 @@ int romfs_fsconfigure(struct romfs_mountpt_s *rm)
*
****************************************************************************/
-int romfs_fileconfigure(struct romfs_mountpt_s *rm, struct romfs_file_s *rf)
+int romfs_fileconfigure(FAR struct romfs_mountpt_s *rm,
+ FAR struct romfs_file_s *rf)
{
/* Check if XIP access mode is supported. If so, then we do not need
* to allocate anything.
@@ -663,9 +663,9 @@ int romfs_fileconfigure(struct romfs_mountpt_s *rm, struct romfs_file_s *rf)
*
****************************************************************************/
-int romfs_checkmount(struct romfs_mountpt_s *rm)
+int romfs_checkmount(FAR struct romfs_mountpt_s *rm)
{
- struct inode *inode;
+ FAR struct inode *inode;
struct geometry geo;
int ret;
@@ -713,11 +713,12 @@ int romfs_checkmount(struct romfs_mountpt_s *rm)
*
****************************************************************************/
-int romfs_finddirentry(struct romfs_mountpt_s *rm,
- struct romfs_dirinfo_s *dirinfo, const char *path)
+int romfs_finddirentry(FAR struct romfs_mountpt_s *rm,
+ FAR struct romfs_dirinfo_s *dirinfo,
+ FAR const char *path)
{
- const char *entryname;
- const char *terminator;
+ FAR const char *entryname;
+ FAR const char *terminator;
int entrylen;
int ret;
@@ -737,7 +738,7 @@ int romfs_finddirentry(struct romfs_mountpt_s *rm,
/* Then loop for each directory/file component in the full path */
- entryname = path;
+ entryname = path;
terminator = NULL;
for (; ; )
@@ -817,9 +818,9 @@ int romfs_finddirentry(struct romfs_mountpt_s *rm,
*
****************************************************************************/
-int romfs_parsedirentry(struct romfs_mountpt_s *rm, uint32_t offset,
- uint32_t *poffset, uint32_t *pnext, uint32_t *pinfo,
- uint32_t *psize)
+int romfs_parsedirentry(FAR struct romfs_mountpt_s *rm, uint32_t offset,
+ FAR uint32_t *poffset, uint32_t *pnext,
+ FAR uint32_t *pinfo, FAR uint32_t *psize)
{
uint32_t save;
uint32_t next;
@@ -884,8 +885,8 @@ int romfs_parsedirentry(struct romfs_mountpt_s *rm, uint32_t offset,
*
****************************************************************************/
-int romfs_parsefilename(struct romfs_mountpt_s *rm, uint32_t offset,
- char *pname)
+int romfs_parsefilename(FAR struct romfs_mountpt_s *rm, uint32_t offset,
+ FAR char *pname)
{
int16_t ndx;
uint16_t namelen;
@@ -952,8 +953,8 @@ int romfs_parsefilename(struct romfs_mountpt_s *rm, uint32_t offset,
*
****************************************************************************/
-int romfs_datastart(struct romfs_mountpt_s *rm, uint32_t offset,
- uint32_t *start)
+int romfs_datastart(FAR struct romfs_mountpt_s *rm, uint32_t offset,
+ FAR uint32_t *start)
{
int16_t ndx;
int ret;