From e6caff3b170874614c84efc1248e0f3a6d8cd120 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Thu, 21 Dec 2023 02:20:37 +0100 Subject: [PATCH] Custom devoptab wrappers (part 3). Add some minor tweaks to the devoptab macros. --- include/devoptab/nxdt_devoptab.h | 50 +++++++++++++++----------------- source/devoptab/hfs_dev.c | 26 ++++------------- source/devoptab/pfs_dev.c | 26 ++++------------- 3 files changed, 36 insertions(+), 66 deletions(-) diff --git a/include/devoptab/nxdt_devoptab.h b/include/devoptab/nxdt_devoptab.h index d2d9ab5..6397484 100644 --- a/include/devoptab/nxdt_devoptab.h +++ b/include/devoptab/nxdt_devoptab.h @@ -32,44 +32,42 @@ extern "C" { #endif -#define DEVOPTAB_MOUNT_NAME_LENGTH 32 // Including NULL terminator. +#define DEVOPTAB_MOUNT_NAME_LENGTH 32 // Including NULL terminator. -#define DEVOPTAB_DECL_ERROR_STATE int _errno = 0 -#define DEVOPTAB_DECL_DEV_CTX DevoptabDeviceContext *dev_ctx = (DevoptabDeviceContext*)r->deviceData -#define DEVOPTAB_DECL_FS_CTX(type) type *fs_ctx = (type*)dev_ctx->fs_ctx -#define DEVOPTAB_DECL_FILE_STATE(type) type *file = (type*)fd -#define DEVOPTAB_DECL_DIR_STATE(type) type *dir = (type*)dirState->dirStruct +#define DEVOPTAB_DECL_ERROR_STATE int _errno = 0 +#define DEVOPTAB_DECL_DEV_CTX DevoptabDeviceContext *dev_ctx = (DevoptabDeviceContext*)r->deviceData +#define DEVOPTAB_DECL_FS_CTX(type) type *fs_ctx = (type*)dev_ctx->fs_ctx +#define DEVOPTAB_DECL_FILE_STATE(type) type *file = (type*)fd +#define DEVOPTAB_DECL_DIR_STATE(type) type *dir = (type*)dirState->dirStruct -#define DEVOPTAB_SET_ERROR(x) r->_errno = _errno = (x) -#define DEVOPTAB_IS_ERROR_SET (_errno != 0) +#define DEVOPTAB_SET_ERROR(x) r->_errno = _errno = (x) +#define DEVOPTAB_IS_ERROR_SET (_errno != 0) -#define DEVOPTAB_EXIT goto end -#define DEVOPTAB_SET_ERROR_AND_EXIT(x) \ +#define DEVOPTAB_EXIT goto end +#define DEVOPTAB_SET_ERROR_AND_EXIT(x) \ do { \ DEVOPTAB_SET_ERROR(x); \ DEVOPTAB_EXIT; \ } while(0) -#define DEVOPTAB_RETURN_INT(x) return (DEVOPTAB_IS_ERROR_SET ? -1 : (x)) -#define DEVOPTAB_RETURN_PTR(x) return (DEVOPTAB_IS_ERROR_SET ? NULL : (x)) -#define DEVOPTAB_RETURN_BOOL return (DEVOPTAB_IS_ERROR_SET ? false : true) -#define DEVOPTAB_RETURN_UNSUPPORTED_OP r->_errno = ENOSYS; \ - return -1; +#define DEVOPTAB_RETURN_INT(x) return (DEVOPTAB_IS_ERROR_SET ? -1 : (x)) +#define DEVOPTAB_RETURN_PTR(x) return (DEVOPTAB_IS_ERROR_SET ? NULL : (x)) +#define DEVOPTAB_RETURN_BOOL return (DEVOPTAB_IS_ERROR_SET ? false : true) +#define DEVOPTAB_RETURN_UNSUPPORTED_OP r->_errno = ENOSYS; \ + return -1; -#define DEVOPTAB_INIT_VARS(type, decl) devoptabControlMutex(true); \ - DEVOPTAB_DECL_ERROR_STATE; \ - decl +#define DEVOPTAB_INIT_VARS(type) devoptabControlMutex(true); \ + DEVOPTAB_DECL_ERROR_STATE; \ + DEVOPTAB_DECL_DEV_CTX; \ + if (!dev_ctx->initialized) DEVOPTAB_SET_ERROR_AND_EXIT(ENODEV); -#define DEVOPTAB_INIT_VARS_WITH_FILE_STATE(fs_type, file_type) DEVOPTAB_INIT_VARS(fs_type,); \ - DEVOPTAB_DECL_FILE_STATE(file_type) +#define DEVOPTAB_INIT_FILE_VARS(fs_type, file_type) DEVOPTAB_INIT_VARS(fs_type); \ + DEVOPTAB_DECL_FILE_STATE(file_type) -#define DEVOPTAB_INIT_VARS_WITH_DIR_STATE(fs_type, dir_type) DEVOPTAB_INIT_VARS(fs_type,); \ - DEVOPTAB_DECL_DIR_STATE(dir_type) +#define DEVOPTAB_INIT_DIR_VARS(fs_type, dir_type) DEVOPTAB_INIT_VARS(fs_type); \ + DEVOPTAB_DECL_DIR_STATE(dir_type) -#define DEVOPTAB_INIT_FS_ACCESS(type) DEVOPTAB_DECL_DEV_CTX; \ - DEVOPTAB_DECL_FS_CTX(type) - -#define DEVOPTAB_DEINIT_VARS devoptabControlMutex(false) +#define DEVOPTAB_DEINIT_VARS devoptabControlMutex(false) typedef struct { bool initialized; ///< Device initialization flag. diff --git a/source/devoptab/hfs_dev.c b/source/devoptab/hfs_dev.c index 0752c7e..16f5131 100644 --- a/source/devoptab/hfs_dev.c +++ b/source/devoptab/hfs_dev.c @@ -27,10 +27,10 @@ /* Helper macros. */ -#define HFS_DEV_INIT_VARS DEVOPTAB_INIT_VARS(HashFileSystemContext,) -#define HFS_DEV_INIT_FILE_VARS DEVOPTAB_INIT_VARS_WITH_FILE_STATE(HashFileSystemContext, HashFileSystemFileState) -#define HFS_DEV_INIT_DIR_VARS DEVOPTAB_INIT_VARS_WITH_DIR_STATE(HashFileSystemContext, HashFileSystemDirectoryState) -#define HFS_DEV_INIT_FS_ACCESS DEVOPTAB_INIT_FS_ACCESS(HashFileSystemContext) +#define HFS_DEV_INIT_VARS DEVOPTAB_INIT_VARS(HashFileSystemContext) +#define HFS_DEV_INIT_FILE_VARS DEVOPTAB_INIT_FILE_VARS(HashFileSystemContext, HashFileSystemFileState) +#define HFS_DEV_INIT_DIR_VARS DEVOPTAB_INIT_DIR_VARS(HashFileSystemContext, HashFileSystemDirectoryState) +#define HFS_DEV_INIT_FS_ACCESS DEVOPTAB_DECL_FS_CTX(HashFileSystemContext) /* Type definitions. */ @@ -137,10 +137,7 @@ static int hfsdev_close(struct _reent *r, void *fd) /* Sanity check. */ if (!file) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Closing \"%s:/%s\".", dev_ctx->name, file->name); -#endif /* Reset file descriptor. */ memset(file, 0, sizeof(HashFileSystemFileState)); @@ -204,10 +201,7 @@ static off_t hfsdev_seek(struct _reent *r, void *fd, off_t pos, int dir) /* Don't allow positive seeks beyond the end of file. */ if (offset > (off_t)file->hfs_entry->size) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Seeking to offset 0x%lX from \"%s:/%s\".", offset, dev_ctx->name, file->name); -#endif /* Adjust offset. */ file->offset = (u64)offset; @@ -220,7 +214,6 @@ end: static int hfsdev_fstat(struct _reent *r, void *fd, struct stat *st) { HFS_DEV_INIT_FILE_VARS; - DEVOPTAB_DECL_DEV_CTX; /* Sanity check. */ if (!file || !st) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); @@ -272,10 +265,7 @@ static DIR_ITER *hfsdev_diropen(struct _reent *r, DIR_ITER *dirState, const char if (!(path = hfsdev_get_truncated_path(r, path))) DEVOPTAB_EXIT; if (*path) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Opening directory \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ memset(dir, 0, sizeof(HashFileSystemDirectoryState)); @@ -292,14 +282,12 @@ static int hfsdev_dirreset(struct _reent *r, DIR_ITER *dirState) { HFS_DEV_INIT_DIR_VARS; -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Resetting directory state for \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ dir->index = 0; +end: DEVOPTAB_DEINIT_VARS; DEVOPTAB_RETURN_INT(0); } @@ -341,14 +329,12 @@ static int hfsdev_dirclose(struct _reent *r, DIR_ITER *dirState) { HFS_DEV_INIT_DIR_VARS; -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Closing directory \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ memset(dir, 0, sizeof(HashFileSystemDirectoryState)); +end: DEVOPTAB_DEINIT_VARS; DEVOPTAB_RETURN_INT(0); } diff --git a/source/devoptab/pfs_dev.c b/source/devoptab/pfs_dev.c index 9a87dab..bb13e92 100644 --- a/source/devoptab/pfs_dev.c +++ b/source/devoptab/pfs_dev.c @@ -27,10 +27,10 @@ /* Helper macros. */ -#define PFS_DEV_INIT_VARS DEVOPTAB_INIT_VARS(PartitionFileSystemContext,) -#define PFS_DEV_INIT_FILE_VARS DEVOPTAB_INIT_VARS_WITH_FILE_STATE(PartitionFileSystemContext, PartitionFileSystemFileState) -#define PFS_DEV_INIT_DIR_VARS DEVOPTAB_INIT_VARS_WITH_DIR_STATE(PartitionFileSystemContext, PartitionFileSystemDirectoryState) -#define PFS_DEV_INIT_FS_ACCESS DEVOPTAB_INIT_FS_ACCESS(PartitionFileSystemContext) +#define PFS_DEV_INIT_VARS DEVOPTAB_INIT_VARS(PartitionFileSystemContext) +#define PFS_DEV_INIT_FILE_VARS DEVOPTAB_INIT_FILE_VARS(PartitionFileSystemContext, PartitionFileSystemFileState) +#define PFS_DEV_INIT_DIR_VARS DEVOPTAB_INIT_DIR_VARS(PartitionFileSystemContext, PartitionFileSystemDirectoryState) +#define PFS_DEV_INIT_FS_ACCESS DEVOPTAB_DECL_FS_CTX(PartitionFileSystemContext) /* Type definitions. */ @@ -137,10 +137,7 @@ static int pfsdev_close(struct _reent *r, void *fd) /* Sanity check. */ if (!file) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Closing \"%s:/%s\".", dev_ctx->name, file->name); -#endif /* Reset file descriptor. */ memset(file, 0, sizeof(PartitionFileSystemFileState)); @@ -204,10 +201,7 @@ static off_t pfsdev_seek(struct _reent *r, void *fd, off_t pos, int dir) /* Don't allow positive seeks beyond the end of file. */ if (offset > (off_t)file->pfs_entry->size) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Seeking to offset 0x%lX from \"%s:/%s\".", offset, dev_ctx->name, file->name); -#endif /* Adjust offset. */ file->offset = (u64)offset; @@ -220,7 +214,6 @@ end: static int pfsdev_fstat(struct _reent *r, void *fd, struct stat *st) { PFS_DEV_INIT_FILE_VARS; - DEVOPTAB_DECL_DEV_CTX; /* Sanity check. */ if (!file || !st) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); @@ -272,10 +265,7 @@ static DIR_ITER *pfsdev_diropen(struct _reent *r, DIR_ITER *dirState, const char if (!(path = pfsdev_get_truncated_path(r, path))) DEVOPTAB_EXIT; if (*path) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Opening directory \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ memset(dir, 0, sizeof(PartitionFileSystemDirectoryState)); @@ -292,14 +282,12 @@ static int pfsdev_dirreset(struct _reent *r, DIR_ITER *dirState) { PFS_DEV_INIT_DIR_VARS; -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Resetting directory state for \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ dir->index = 0; +end: DEVOPTAB_DEINIT_VARS; DEVOPTAB_RETURN_INT(0); } @@ -341,14 +329,12 @@ static int pfsdev_dirclose(struct _reent *r, DIR_ITER *dirState) { PFS_DEV_INIT_DIR_VARS; -#if LOG_LEVEL == LOG_LEVEL_DEBUG - DEVOPTAB_DECL_DEV_CTX; LOG_MSG_DEBUG("Closing directory \"%s:/\".", dev_ctx->name); -#endif /* Reset directory state. */ memset(dir, 0, sizeof(PartitionFileSystemDirectoryState)); +end: DEVOPTAB_DEINIT_VARS; DEVOPTAB_RETURN_INT(0); }