Skip to content

Commit

Permalink
libc/chdir: replace heap alloc to path buffer to improve performance
Browse files Browse the repository at this point in the history
realloc path buffer to avoid alloc from realpath()

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao authored and xiaoxiang781216 committed Dec 12, 2024
1 parent bf27e4d commit 2ccdd94
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libs/libc/unistd/lib_chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

0 comments on commit 2ccdd94

Please sign in to comment.