Skip to content

Commit

Permalink
Custom devoptab wrappers (part 3).
Browse files Browse the repository at this point in the history
Add some minor tweaks to the devoptab macros.
  • Loading branch information
DarkMatterCore committed Dec 21, 2023
1 parent c392a9f commit e6caff3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 66 deletions.
50 changes: 24 additions & 26 deletions include/devoptab/nxdt_devoptab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 6 additions & 20 deletions source/devoptab/hfs_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -220,7 +214,6 @@ static off_t hfsdev_seek(struct _reent *r, void *fd, off_t pos, int dir)
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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 6 additions & 20 deletions source/devoptab/pfs_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -220,7 +214,6 @@ static off_t pfsdev_seek(struct _reent *r, void *fd, off_t pos, int dir)
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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e6caff3

Please sign in to comment.