From b1abb2fc3449aeab8ee25b42b8f32755219cab7f Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Tue, 23 Nov 2021 11:03:19 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15130 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/vfs/drivers/streams/drvstreams.c | 88 +++++++++++++++++++++++------ os/vfs/include/vfserrors.h | 2 +- os/vfs/include/vfsnodes.h | 2 +- os/vfs/src/vfsparser.c | 12 +--- os/vfs/src/vfssystem.c | 2 +- 5 files changed, 77 insertions(+), 29 deletions(-) diff --git a/os/vfs/drivers/streams/drvstreams.c b/os/vfs/drivers/streams/drvstreams.c index 23b8b6f19..f1a2a68b1 100644 --- a/os/vfs/drivers/streams/drvstreams.c +++ b/os/vfs/drivers/streams/drvstreams.c @@ -35,17 +35,36 @@ /*===========================================================================*/ /** - * @brief @p vfs_drv_streams_t specific methods. + * @brief Number of nodes pre-allocated in the pool. */ -#define __vfs_drv_streams_methods \ +#define DRV_NODES_NUM 2 + +/** + * @brief @p vfs_streams_driver_t specific methods. + */ +#define __vfs_streams_driver_methods \ __vfs_driver_methods /** - * @brief @p vfs_drv_streams_t specific data. + * @brief @p vfs_streams_driver_t specific data. */ -#define __vfs_drv_streams_data \ +#define __vfs_streams_driver_data \ __vfs_driver_data \ - const drv_stream_element_t *streams; + const drv_stream_element_t *streams; \ + memory_pool_t nodes_pool; + +/** + * @brief @p vfs_stream_file_node_t specific methods. + */ +#define __vfs_stream_file_node_methods \ + __vfs_file_node_methods + +/** + * @brief @p vfs_stream_file_node_t specific data. + */ +#define __vfs_stream_file_node_data \ + __vfs_file_node_data \ + BaseSequentialStream *stream; /*===========================================================================*/ /* Module exported variables. */ @@ -56,10 +75,10 @@ /*===========================================================================*/ /** - * @brief @p vfs_drv_streams_t virtual methods table. + * @brief @p vfs_streams_driver_t virtual methods table. */ -struct vfs_drv_streams_vmt { - __vfs_drv_streams_methods +struct vfs_streams_driver_vmt { + __vfs_streams_driver_methods }; /** @@ -69,9 +88,27 @@ typedef struct vfs_drv_streams { /** * @brief Virtual Methods Table. */ - const struct vfs_drv_streams_vmt *vmt; - __vfs_drv_streams_data -} vfs_drv_streams_t; + const struct vfs_streams_driver_vmt *vmt; + __vfs_streams_driver_data +} vfs_streams_driver_t; + +/** + * @brief @p vfs_file_node_t virtual methods table. + */ +struct vfs_stream_file_node_vmt { + __vfs_stream_file_node_methods +}; + +/** + * @brief Type of a structure representing a stream file VFS node. + */ +typedef struct vfs_stream_file_node { + /** + * @brief Virtual Methods Table. + */ + const struct vfs_stream_file_node_vmt *vmt; + __vfs_stream_file_node_data +} vfs_stream_file_node_t; /*===========================================================================*/ /* Module local variables. */ @@ -84,12 +121,13 @@ static msg_t drv_open_file(void *instance, const char *path, vfs_file_node_t **vfnpp); -static const struct vfs_drv_streams_vmt vmt = { +static const struct vfs_streams_driver_vmt vmt = { .open_dir = drv_open_dir, .open_file = drv_open_file }; -static vfs_drv_streams_t drv_streams; +static vfs_stream_file_node_t drv_file_nodes[DRV_NODES_NUM]; +static vfs_streams_driver_t drv_streams; /*===========================================================================*/ /* Module local functions. */ @@ -109,7 +147,7 @@ static msg_t drv_open_dir(void *instance, static msg_t drv_open_file(void *instance, const char *path, vfs_file_node_t **vfnpp) { - vfs_drv_streams_t *drvp = (vfs_drv_streams_t *)instance; + vfs_streams_driver_t *drvp = (vfs_streams_driver_t *)instance; const drv_stream_element_t *dsep; msg_t err; @@ -125,13 +163,25 @@ static msg_t drv_open_file(void *instance, err = vfs_parse_match_end(&path); VFS_BREAK_ON_ERROR(err); - dsep = &drvp->streams[0]; while (dsep->name != NULL) { if (strncmp(fname, dsep->name, VFS_CFG_MAX_NAMELEN) == 0) { + vfs_stream_file_node_t *sfnp; - *vfnpp = NULL; - return VFS_RET_SUCCESS; + sfnp = chPoolAlloc(&drv_streams.nodes_pool); + if (sfnp != NULL) { + + /* Node object initialization.*/ + sfnp->vmt = 0; + sfnp->refs = 1U; + sfnp->driver = (vfs_driver_t *)drvp; + sfnp->stream = dsep->stream; + + *vfnpp = (vfs_file_node_t *)sfnp; + return VFS_RET_SUCCESS; + } + + return VFS_RET_NO_RESOURCE; } dsep++; @@ -154,6 +204,10 @@ vfs_driver_t *drvStreamsInit(const char *rootname, drv_streams.vmt = &vmt; drv_streams.rootname = rootname; drv_streams.streams = streams; + chPoolObjectInit(&drv_streams.nodes_pool, + sizeof (vfs_stream_file_node_t), + chCoreAllocAligned); + chPoolLoadArray(&drv_streams.nodes_pool, drv_file_nodes, DRV_NODES_NUM); return (vfs_driver_t *)&drv_streams; } diff --git a/os/vfs/include/vfserrors.h b/os/vfs/include/vfserrors.h index 7b2515c08..02d0f6e99 100644 --- a/os/vfs/include/vfserrors.h +++ b/os/vfs/include/vfserrors.h @@ -39,7 +39,7 @@ #define VFS_RET_SUCCESS 0 #define VFS_RET_TIMEOUT -1 #define VFS_RET_EOF -2 -#define VFS_RET_PAST_LIMIT -3 +#define VFS_RET_NO_RESOURCE -3 #define VFS_RET_NO_DRIVER -4 #define VFS_RET_INVALID_PATH -5 #define VFS_RET_NOT_FOUND -6 diff --git a/os/vfs/include/vfsnodes.h b/os/vfs/include/vfsnodes.h index cec55a791..c6c1bfb97 100644 --- a/os/vfs/include/vfsnodes.h +++ b/os/vfs/include/vfsnodes.h @@ -106,7 +106,7 @@ typedef struct vfs_node vfs_node_t; #define __vfs_node_data \ _base_object_data \ /* Number of references to this node.*/ \ - unsigned references_counter; \ + unsigned refs; \ /* Driver handling this node.*/ \ vfs_driver_t *driver; diff --git a/os/vfs/src/vfsparser.c b/os/vfs/src/vfsparser.c index 526fe7958..51731106a 100644 --- a/os/vfs/src/vfsparser.c +++ b/os/vfs/src/vfsparser.c @@ -106,21 +106,14 @@ msg_t vfs_parse_filename(const char **pathp, char *fname) { n = 0U; while (true) { - char c = *p++; + char c = *p; /* Valid characters for path names.*/ if (!isalnum(c) && (c != '_') && (c != '-') && (c != '.')) { break; } - if (c == '/') { - *pathp = p + 1; - *fname = '\0'; - err = VFS_RET_SUCCESS; - break; - } - - if (c == '\0') { + if ((c == '/') || (c == '\0')) { *pathp = p; *fname = '\0'; err = VFS_RET_SUCCESS; @@ -132,6 +125,7 @@ msg_t vfs_parse_filename(const char **pathp, char *fname) { } *fname++ = c; + p++; n++; } diff --git a/os/vfs/src/vfssystem.c b/os/vfs/src/vfssystem.c index b4703c814..508fee8fa 100644 --- a/os/vfs/src/vfssystem.c +++ b/os/vfs/src/vfssystem.c @@ -117,7 +117,7 @@ msg_t vfsRegisterDriver(vfs_driver_t *vdp) { VFS_LOCK(); if (vfs.next_driver >= &vfs.drivers[VFS_CFG_MAX_DRIVERS]) { - err = VFS_RET_PAST_LIMIT; + err = VFS_RET_NO_RESOURCE; } else { *vfs.next_driver++ = vdp;