From 26a0ee4af3f8ed033fa55b786372b0917fd36be8 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 20 Oct 2019 02:47:37 +0200 Subject: [PATCH] fsdev/romfsdev: Reduce TLS usage by sharing the path buffer --- nx/source/runtime/devices/fs_dev.c | 28 +++++++++++++-------------- nx/source/runtime/devices/path_buf.c | 3 +++ nx/source/runtime/devices/path_buf.h | 4 ++++ nx/source/runtime/devices/romfs_dev.c | 14 +++++++------- 4 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 nx/source/runtime/devices/path_buf.c create mode 100644 nx/source/runtime/devices/path_buf.h diff --git a/nx/source/runtime/devices/fs_dev.c b/nx/source/runtime/devices/fs_dev.c index 2036b886..e1bac69a 100644 --- a/nx/source/runtime/devices/fs_dev.c +++ b/nx/source/runtime/devices/fs_dev.c @@ -14,6 +14,7 @@ #include "services/fs.h" #include "services/time.h" +#include "path_buf.h" /*! @internal * @@ -109,7 +110,6 @@ static fsdev_fsdevice fsdev_fsdevices[32]; /*! @endcond */ static char __cwd[PATH_MAX+1] = "/"; -static __thread char __fixedpath[PATH_MAX+1]; __attribute__((weak)) u32 __nx_fsdev_direntry_cache_size = 32; @@ -202,17 +202,17 @@ fsdev_fixpath(struct _reent *r, } while(code != 0); if(path[0] == '/') - strncpy(__fixedpath, path, PATH_MAX); + strncpy(__nx_dev_path_buf, path, PATH_MAX); else { - strncpy(__fixedpath, __cwd, PATH_MAX); - __fixedpath[PATH_MAX] = '\0'; - strncat(__fixedpath, path, PATH_MAX - strlen(__cwd)); + strncpy(__nx_dev_path_buf, __cwd, PATH_MAX); + __nx_dev_path_buf[PATH_MAX] = '\0'; + strncat(__nx_dev_path_buf, path, PATH_MAX - strlen(__cwd)); } - if(__fixedpath[PATH_MAX] != 0) + if(__nx_dev_path_buf[PATH_MAX] != 0) { - __fixedpath[PATH_MAX] = 0; + __nx_dev_path_buf[PATH_MAX] = 0; r->_errno = ENAMETOOLONG; return NULL; } @@ -236,7 +236,7 @@ fsdev_fixpath(struct _reent *r, } } - return __fixedpath; + return __nx_dev_path_buf; } static int @@ -248,7 +248,7 @@ fsdev_getfspath(struct _reent *r, if(fsdev_fixpath(r, path, device) == NULL) return -1; - memcpy(outpath, __fixedpath,FS_MAX_PATH-1); + memcpy(outpath, __nx_dev_path_buf,FS_MAX_PATH-1); outpath[FS_MAX_PATH-1] = '\0'; return 0; @@ -462,15 +462,15 @@ Result fsdevMountSdmc(void) { if(FindDevice(__system_argv[0]) == dev) { - strncpy(__fixedpath,__system_argv[0],PATH_MAX); - if(__fixedpath[PATH_MAX] != 0) + strncpy(__nx_dev_path_buf,__system_argv[0],PATH_MAX); + if(__nx_dev_path_buf[PATH_MAX] != 0) { - __fixedpath[PATH_MAX] = 0; + __nx_dev_path_buf[PATH_MAX] = 0; } else { char *last_slash = NULL; - p = __fixedpath; + p = __nx_dev_path_buf; do { units = decode_utf8(&code, (const uint8_t*)p); @@ -489,7 +489,7 @@ Result fsdevMountSdmc(void) if(last_slash != NULL) { last_slash[0] = 0; - chdir(__fixedpath); + chdir(__nx_dev_path_buf); } } } diff --git a/nx/source/runtime/devices/path_buf.c b/nx/source/runtime/devices/path_buf.c new file mode 100644 index 00000000..b03dbb53 --- /dev/null +++ b/nx/source/runtime/devices/path_buf.c @@ -0,0 +1,3 @@ +#include "path_buf.h" + +char __thread __nx_dev_path_buf[PATH_MAX+1]; diff --git a/nx/source/runtime/devices/path_buf.h b/nx/source/runtime/devices/path_buf.h new file mode 100644 index 00000000..dcf0b1eb --- /dev/null +++ b/nx/source/runtime/devices/path_buf.h @@ -0,0 +1,4 @@ +#pragma once +#include + +extern char __thread __nx_dev_path_buf[PATH_MAX+1]; diff --git a/nx/source/runtime/devices/romfs_dev.c b/nx/source/runtime/devices/romfs_dev.c index 459f6f63..43b74510 100644 --- a/nx/source/runtime/devices/romfs_dev.c +++ b/nx/source/runtime/devices/romfs_dev.c @@ -15,6 +15,8 @@ #include "runtime/env.h" #include "nro.h" +#include "path_buf.h" + typedef enum { RomfsSource_FsFile, RomfsSource_FsStorage, @@ -40,8 +42,6 @@ typedef struct romfs_mount extern int __system_argc; extern char** __system_argv; -static char __thread __component[PATH_MAX+1]; - #define romFS_root(m) ((romfs_dir*)(m)->dirTable) #define romFS_dir(m,x) ((romfs_dir*) ((u8*)(m)->dirTable + (x))) #define romFS_file(m,x) ((romfs_file*)((u8*)(m)->fileTable + (x))) @@ -233,10 +233,10 @@ Result romfsMount(const char *name) filename += 5; else if (strncmp(filename, "nxlink:/", 8) == 0) { - strncpy(__component, "/switch", PATH_MAX); - strncat(__component, filename+7, PATH_MAX); - __component[PATH_MAX] = 0; - filename = __component; + strncpy(__nx_dev_path_buf, "/switch", PATH_MAX); + strncat(__nx_dev_path_buf, filename+7, PATH_MAX); + __nx_dev_path_buf[PATH_MAX] = 0; + filename = __nx_dev_path_buf; } else { @@ -501,7 +501,7 @@ static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPa while (**pPath) { char* slashPos = strchr(*pPath, '/'); - char* component = __component; + char* component = __nx_dev_path_buf; if (slashPos) {