From be4ae4d4010cb32a821de3098a5a1721581a126d Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 15 Mar 2012 01:58:39 -0400 Subject: [PATCH] Removed __PHYSFS_platformCvtToDependent(). It's only used by archive_dir.c, and it's trivial to make it into platform-independent code for that one module. Bonus: less malloc() pressure...now every access to the native filesystem doesn't require a temporary heap allocation. --- src/archiver_dir.c | 57 +++++++++++++++++++++++++++++++----------- src/physfs.c | 1 + src/physfs_internal.h | 14 ----------- src/platform_posix.c | 28 --------------------- src/platform_windows.c | 26 ------------------- 5 files changed, 44 insertions(+), 82 deletions(-) diff --git a/src/archiver_dir.c b/src/archiver_dir.c index 91861cb1..ede5a521 100644 --- a/src/archiver_dir.c +++ b/src/archiver_dir.c @@ -11,6 +11,31 @@ /* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */ + + +static char *cvtToDependent(const char *prepend, const char *path, char *buf) +{ + BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL); + sprintf(buf, "%s%s", prepend ? prepend : "", path); + + if (__PHYSFS_platformDirSeparator != '/') + { + char *p; + for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/')) + *p = __PHYSFS_platformDirSeparator; + } /* if */ + + return buf; +} /* cvtToDependent */ + + +#define CVT_TO_DEPENDENT(buf, pre, dir) { \ + const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \ + buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len)); \ +} + + + static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting) { PHYSFS_Stat statbuf; @@ -41,18 +66,18 @@ static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting) } /* DIR_openArchive */ -/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */ - static void DIR_enumerateFiles(dvoid *opaque, const char *dname, int omitSymLinks, PHYSFS_EnumFilesCallback cb, const char *origdir, void *callbackdata) { - char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL); + char *d; + + CVT_TO_DEPENDENT(d, opaque, dname); if (d != NULL) { __PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb, origdir, callbackdata); - allocator.Free(d); + __PHYSFS_smallFree(d); } /* if */ } /* DIR_enumerateFiles */ @@ -60,15 +85,16 @@ static void DIR_enumerateFiles(dvoid *opaque, const char *dname, static PHYSFS_Io *doOpen(dvoid *opaque, const char *name, const int mode, int *fileExists) { - char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL); + char *f; PHYSFS_Io *io = NULL; int existtmp = 0; + CVT_TO_DEPENDENT(f, opaque, name); + BAIL_IF_MACRO(f == NULL, NULL, NULL); + if (fileExists == NULL) fileExists = &existtmp; - BAIL_IF_MACRO(f == NULL, NULL, NULL); - io = __PHYSFS_createNativeIo(f, mode); if (io == NULL) { @@ -80,7 +106,7 @@ static PHYSFS_Io *doOpen(dvoid *opaque, const char *name, *fileExists = 1; } /* else */ - allocator.Free(f); + __PHYSFS_smallFree(f); return io; } /* doOpen */ @@ -106,24 +132,26 @@ static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename) static int DIR_remove(dvoid *opaque, const char *name) { - char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL); int retval; + char *f; + CVT_TO_DEPENDENT(f, opaque, name); BAIL_IF_MACRO(f == NULL, NULL, 0); retval = __PHYSFS_platformDelete(f); - allocator.Free(f); + __PHYSFS_smallFree(f); return retval; } /* DIR_remove */ static int DIR_mkdir(dvoid *opaque, const char *name) { - char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL); int retval; + char *f; + CVT_TO_DEPENDENT(f, opaque, name); BAIL_IF_MACRO(f == NULL, NULL, 0); retval = __PHYSFS_platformMkDir(f); - allocator.Free(f); + __PHYSFS_smallFree(f); return retval; } /* DIR_mkdir */ @@ -137,12 +165,13 @@ static void DIR_dirClose(dvoid *opaque) static int DIR_stat(dvoid *opaque, const char *name, int *exists, PHYSFS_Stat *stat) { - char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL); int retval = 0; + char *d; + CVT_TO_DEPENDENT(d, opaque, name); BAIL_IF_MACRO(d == NULL, NULL, 0); retval = __PHYSFS_platformStat(d, exists, stat); - allocator.Free(d); + __PHYSFS_smallFree(d); return retval; } /* DIR_stat */ diff --git a/src/physfs.c b/src/physfs.c index c2c500ca..62686bc6 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -9,6 +9,7 @@ */ /* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */ +/* !!! FIXME: use snprintf(), not sprintf(). */ #define __PHYSICSFS_INTERNAL__ #include "physfs_internal.h" diff --git a/src/physfs_internal.h b/src/physfs_internal.h index 4b1be1fd..31410f77 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -1243,20 +1243,6 @@ char *__PHYSFS_platformGetUserDir(void); */ void *__PHYSFS_platformGetThreadID(void); -/* - * Convert (dirName) to platform-dependent notation, then prepend (prepend) - * and append (append) to the converted string. - * - * So, on Win32, calling: - * __PHYSFS_platformCvtToDependent("C:\", "my/files", NULL); - * ...will return the string "C:\my\files". - * - * Be sure to allocator.Free() the return value when done with it. - */ -char *__PHYSFS_platformCvtToDependent(const char *prepend, - const char *dirName, - const char *append); - /* * Enumerate a directory of files. This follows the rules for the diff --git a/src/platform_posix.c b/src/platform_posix.c index 63df6d53..50b19691 100644 --- a/src/platform_posix.c +++ b/src/platform_posix.c @@ -113,34 +113,6 @@ char *__PHYSFS_platformGetUserDir(void) } /* __PHYSFS_platformGetUserDir */ -char *__PHYSFS_platformCvtToDependent(const char *prepend, - const char *dirName, - const char *append) -{ - int len = ((prepend) ? strlen(prepend) : 0) + - ((append) ? strlen(append) : 0) + - strlen(dirName) + 1; - char *retval = (char *) allocator.Malloc(len); - - BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); - - /* platform-independent notation is Unix-style already. :) */ - - if (prepend) - strcpy(retval, prepend); - else - retval[0] = '\0'; - - strcat(retval, dirName); - - if (append) - strcat(retval, append); - - return retval; -} /* __PHYSFS_platformCvtToDependent */ - - - void __PHYSFS_platformEnumerateFiles(const char *dirname, int omitSymLinks, PHYSFS_EnumFilesCallback callback, diff --git a/src/platform_windows.c b/src/platform_windows.c index d9c173b9..86dc59fc 100644 --- a/src/platform_windows.c +++ b/src/platform_windows.c @@ -54,10 +54,6 @@ } \ } \ -#ifndef _MSC_VER -#define _snprintf snprintf -#endif - /* !!! FIXME: this is wrong for UTF-16. */ static PHYSFS_uint64 wStrLen(const WCHAR *wstr) { @@ -467,28 +463,6 @@ static int isSymlinkAttrs(const DWORD attr, const DWORD tag) } /* isSymlinkAttrs */ -char *__PHYSFS_platformCvtToDependent(const char *prepend, - const char *dirName, - const char *append) -{ - const size_t len = ((prepend) ? strlen(prepend) : 0) + - ((append) ? strlen(append) : 0) + - strlen(dirName) + 1; - char *retval = (char *) allocator.Malloc(len); - char *p; - - BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); - - _snprintf(retval, len, "%s%s%s", - prepend ? prepend : "", dirName, append ? append : ""); - - for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/')) - *p = '\\'; - - return retval; -} /* __PHYSFS_platformCvtToDependent */ - - void __PHYSFS_platformEnumerateFiles(const char *dirname, int omitSymLinks, PHYSFS_EnumFilesCallback callback,