From d3ca93c5f93db3afd878b63d6c34bf4058bbc4f0 Mon Sep 17 00:00:00 2001 From: Meep-Eep Date: Sun, 2 Mar 2008 20:52:36 +0000 Subject: [PATCH] Banished alloca(). git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2946 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 1 + sc2/src/sc2code/libs/uio/debug.c | 17 +++++++++++------ sc2/src/sc2code/libs/uio/gphys.c | 4 +++- sc2/src/sc2code/libs/uio/ioaux.c | 9 +++++++-- sc2/src/sc2code/libs/uio/mem.h | 1 - sc2/src/sc2code/libs/uio/uioport.h | 11 ----------- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 6a29c0c86..b39acd4f3 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.7: +- Don't use alloca() in uio. - SvdB - Replace PlayerOne/PlayerTwo by PlayerControls[0]/PlayerControls[1] - SvdB - Moved comm resources into starcon.ls2 - Michael - Repackaged static comm/ship data to it all uniquely named - Michael diff --git a/sc2/src/sc2code/libs/uio/debug.c b/sc2/src/sc2code/libs/uio/debug.c index 69d9b0b63..4485602a1 100644 --- a/sc2/src/sc2code/libs/uio/debug.c +++ b/sc2/src/sc2code/libs/uio/debug.c @@ -667,6 +667,7 @@ listOneDir(DebugContext *debugContext, const char *arg) { int i; const char *pattern; const char *cpath; + char *buf = NULL; if (arg[0] == '\0') { cpath = arg; @@ -674,21 +675,21 @@ listOneDir(DebugContext *debugContext, const char *arg) { } else { pattern = strrchr(arg, '/'); if (pattern == NULL) { + // No directory component in 'arg'. cpath = ""; pattern = arg; } else if (pattern[1] == '\0') { - // argument ends on / + // 'arg' ends on / cpath = arg; pattern = "*"; } else { if (pattern == arg) { cpath = "/"; } else { - char *path; - path = uio_alloca(pattern - arg + 1); - memcpy(path, arg, pattern - arg); - path[pattern - arg] = '\0'; - cpath = path; + buf = uio_malloc(pattern - arg + 1); + memcpy(buf, arg, pattern - arg); + buf[pattern - arg] = '\0'; + cpath = buf; } pattern++; } @@ -708,11 +709,15 @@ listOneDir(DebugContext *debugContext, const char *arg) { if (dirList == NULL) { fprintf(debugContext->out, "Error in uio_getDirList(): %s.\n", strerror(errno)); + if (buf != NULL) + uio_free(buf); return 1; } for (i = 0; i < dirList->numNames; i++) fprintf(debugContext->out, "%s\n", dirList->names[i]); uio_DirList_free(dirList); + if (buf != NULL) + uio_free(buf); return 0; } diff --git a/sc2/src/sc2code/libs/uio/gphys.c b/sc2/src/sc2code/libs/uio/gphys.c index c9136b89c..f25f5574e 100644 --- a/sc2/src/sc2code/libs/uio/gphys.c +++ b/sc2/src/sc2code/libs/uio/gphys.c @@ -223,7 +223,8 @@ uio_walkGPPath(uio_GPDir *startGPDir, const char *path, int retVal; gPDir = startGPDir; - tempBuf = uio_alloca(strlen(path) + 1); + tempBuf = uio_malloc(strlen(path) + 1); + // XXX: Use a dynamically allocated array when moving to C99. pathEnd = path + pathLen; getFirstPathComponent(path, pathEnd, &partStart, &partEnd); while (1) { @@ -247,6 +248,7 @@ uio_walkGPPath(uio_GPDir *startGPDir, const char *path, getNextPathComponent(pathEnd, &partStart, &partEnd); } + uio_free(tempBuf); *pathRest = partStart; *endGPDir = gPDir; return retVal; diff --git a/sc2/src/sc2code/libs/uio/ioaux.c b/sc2/src/sc2code/libs/uio/ioaux.c index 411a81b35..0ec975358 100644 --- a/sc2/src/sc2code/libs/uio/ioaux.c +++ b/sc2/src/sc2code/libs/uio/ioaux.c @@ -71,7 +71,8 @@ uio_walkPhysicalPath(uio_PDirHandle *startPDirHandle, const char *path, uio_PDirHandle_ref(startPDirHandle); pDirHandle = startPDirHandle; - tempBuf = uio_alloca(strlen(path) + 1); + tempBuf = uio_malloc(strlen(path) + 1); + // XXX: Use a dynamically allocated array when moving to C99. pathEnd = path + pathLen; getFirstPathComponent(path, pathEnd, &partStart, &partEnd); for (;;) { @@ -97,6 +98,7 @@ uio_walkPhysicalPath(uio_PDirHandle *startPDirHandle, const char *path, getNextPathComponent(pathEnd, &partStart, &partEnd); } + uio_free(tempBuf); *pathRest = partStart; *endPDirHandle = pDirHandle; return retVal; @@ -132,8 +134,9 @@ uio_makePath(uio_PDirHandle *pDirHandle, const char *path, size_t pathLen, pathEnd = path + pathLen; - buf = uio_alloca(pathLen + 1); + buf = uio_malloc(pathLen + 1); // worst case length + // XXX: Use a dynamically allocated array when moving to C99. uio_walkPhysicalPath(pDirHandle, path, pathLen, &pDirHandle, &rest); // The reference to the original pDirHandle is still kept @@ -152,12 +155,14 @@ uio_makePath(uio_PDirHandle *pDirHandle, const char *path, size_t pathLen, int savedErrno = errno; uio_PDirHandle_unref(pDirHandle); errno = savedErrno; + uio_free(buf); return NULL; } uio_PDirHandle_unref(pDirHandle); pDirHandle = newPDirHandle; getNextPathComponent(pathEnd, &start, &end); } + uio_free(buf); return pDirHandle; } diff --git a/sc2/src/sc2code/libs/uio/mem.h b/sc2/src/sc2code/libs/uio/mem.h index 6fff349e7..2ec279125 100644 --- a/sc2/src/sc2code/libs/uio/mem.h +++ b/sc2/src/sc2code/libs/uio/mem.h @@ -29,7 +29,6 @@ #define uio_realloc realloc #define uio_free free #define uio_calloc calloc -#define uio_alloca alloca #ifdef uio_MEM_DEBUG // When uio_strdup is defined to the libc strdup, there's no opportunity diff --git a/sc2/src/sc2code/libs/uio/uioport.h b/sc2/src/sc2code/libs/uio/uioport.h index 1eb6cf58e..2887a96c7 100644 --- a/sc2/src/sc2code/libs/uio/uioport.h +++ b/sc2/src/sc2code/libs/uio/uioport.h @@ -145,17 +145,6 @@ typedef unsigned short mode_t; # define S_IFDIR _S_IFDIR #endif -// Memory related: -#ifdef WIN32 -# ifdef __MINGW32__ -# include -# elif defined (_MSC_VER) -# define alloca _alloca -# endif -#elif defined(__linux__) || defined(__svr4__) -# include -#endif - // String formatting #ifdef _MSC_VER #define snprintf _snprintf