From 2ccdd947a1062cff425eb050102e933fb8fa29eb Mon Sep 17 00:00:00 2001 From: chao an Date: Wed, 11 Dec 2024 09:18:53 +0800 Subject: [PATCH] libc/chdir: replace heap alloc to path buffer to improve performance realloc path buffer to avoid alloc from realpath() Signed-off-by: chao an --- libs/libc/unistd/lib_chdir.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libs/libc/unistd/lib_chdir.c b/libs/libc/unistd/lib_chdir.c index dd14d5b345b56..65016bfe013ec 100644 --- a/libs/libc/unistd/lib_chdir.c +++ b/libs/libc/unistd/lib_chdir.c @@ -72,9 +72,7 @@ int chdir(FAR const char *path) { -#ifndef CONFIG_DISABLE_ENVIRON - FAR char *oldpwd; -#endif /* !CONFIG_DISABLE_ENVIRON */ + FAR char *oldpwd = NULL; FAR char *abspath; struct stat buf; int ret; @@ -99,9 +97,15 @@ int chdir(FAR const char *path) * Remove any trailing '/' characters from the path */ - abspath = realpath(path, NULL); - if (abspath == NULL) + abspath = lib_get_pathbuffer(); + if (abspath != NULL) + { + oldpwd = realpath(path, abspath); + } + + if (abspath == NULL || oldpwd == NULL) { + lib_put_pathbuffer(abspath); return ERROR; } @@ -124,7 +128,7 @@ int chdir(FAR const char *path) ret = setenv("PWD", abspath, TRUE); #endif /* !CONFIG_DISABLE_ENVIRON */ - lib_free(abspath); + lib_put_pathbuffer(abspath); return ret; }