Banished alloca().

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2946 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
Meep-Eep
2008-03-02 20:52:36 +00:00
parent 785c67bdf9
commit d3ca93c5f9
6 changed files with 22 additions and 21 deletions
+1
View File
@@ -1,4 +1,5 @@
Changes towards version 0.7: Changes towards version 0.7:
- Don't use alloca() in uio. - SvdB
- Replace PlayerOne/PlayerTwo by PlayerControls[0]/PlayerControls[1] - SvdB - Replace PlayerOne/PlayerTwo by PlayerControls[0]/PlayerControls[1] - SvdB
- Moved comm resources into starcon.ls2 - Michael - Moved comm resources into starcon.ls2 - Michael
- Repackaged static comm/ship data to it all uniquely named - Michael - Repackaged static comm/ship data to it all uniquely named - Michael
+11 -6
View File
@@ -667,6 +667,7 @@ listOneDir(DebugContext *debugContext, const char *arg) {
int i; int i;
const char *pattern; const char *pattern;
const char *cpath; const char *cpath;
char *buf = NULL;
if (arg[0] == '\0') { if (arg[0] == '\0') {
cpath = arg; cpath = arg;
@@ -674,21 +675,21 @@ listOneDir(DebugContext *debugContext, const char *arg) {
} else { } else {
pattern = strrchr(arg, '/'); pattern = strrchr(arg, '/');
if (pattern == NULL) { if (pattern == NULL) {
// No directory component in 'arg'.
cpath = ""; cpath = "";
pattern = arg; pattern = arg;
} else if (pattern[1] == '\0') { } else if (pattern[1] == '\0') {
// argument ends on / // 'arg' ends on /
cpath = arg; cpath = arg;
pattern = "*"; pattern = "*";
} else { } else {
if (pattern == arg) { if (pattern == arg) {
cpath = "/"; cpath = "/";
} else { } else {
char *path; buf = uio_malloc(pattern - arg + 1);
path = uio_alloca(pattern - arg + 1); memcpy(buf, arg, pattern - arg);
memcpy(path, arg, pattern - arg); buf[pattern - arg] = '\0';
path[pattern - arg] = '\0'; cpath = buf;
cpath = path;
} }
pattern++; pattern++;
} }
@@ -708,11 +709,15 @@ listOneDir(DebugContext *debugContext, const char *arg) {
if (dirList == NULL) { if (dirList == NULL) {
fprintf(debugContext->out, "Error in uio_getDirList(): %s.\n", fprintf(debugContext->out, "Error in uio_getDirList(): %s.\n",
strerror(errno)); strerror(errno));
if (buf != NULL)
uio_free(buf);
return 1; return 1;
} }
for (i = 0; i < dirList->numNames; i++) for (i = 0; i < dirList->numNames; i++)
fprintf(debugContext->out, "%s\n", dirList->names[i]); fprintf(debugContext->out, "%s\n", dirList->names[i]);
uio_DirList_free(dirList); uio_DirList_free(dirList);
if (buf != NULL)
uio_free(buf);
return 0; return 0;
} }
+3 -1
View File
@@ -223,7 +223,8 @@ uio_walkGPPath(uio_GPDir *startGPDir, const char *path,
int retVal; int retVal;
gPDir = startGPDir; 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; pathEnd = path + pathLen;
getFirstPathComponent(path, pathEnd, &partStart, &partEnd); getFirstPathComponent(path, pathEnd, &partStart, &partEnd);
while (1) { while (1) {
@@ -247,6 +248,7 @@ uio_walkGPPath(uio_GPDir *startGPDir, const char *path,
getNextPathComponent(pathEnd, &partStart, &partEnd); getNextPathComponent(pathEnd, &partStart, &partEnd);
} }
uio_free(tempBuf);
*pathRest = partStart; *pathRest = partStart;
*endGPDir = gPDir; *endGPDir = gPDir;
return retVal; return retVal;
+7 -2
View File
@@ -71,7 +71,8 @@ uio_walkPhysicalPath(uio_PDirHandle *startPDirHandle, const char *path,
uio_PDirHandle_ref(startPDirHandle); uio_PDirHandle_ref(startPDirHandle);
pDirHandle = 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; pathEnd = path + pathLen;
getFirstPathComponent(path, pathEnd, &partStart, &partEnd); getFirstPathComponent(path, pathEnd, &partStart, &partEnd);
for (;;) { for (;;) {
@@ -97,6 +98,7 @@ uio_walkPhysicalPath(uio_PDirHandle *startPDirHandle, const char *path,
getNextPathComponent(pathEnd, &partStart, &partEnd); getNextPathComponent(pathEnd, &partStart, &partEnd);
} }
uio_free(tempBuf);
*pathRest = partStart; *pathRest = partStart;
*endPDirHandle = pDirHandle; *endPDirHandle = pDirHandle;
return retVal; return retVal;
@@ -132,8 +134,9 @@ uio_makePath(uio_PDirHandle *pDirHandle, const char *path, size_t pathLen,
pathEnd = path + pathLen; pathEnd = path + pathLen;
buf = uio_alloca(pathLen + 1); buf = uio_malloc(pathLen + 1);
// worst case length // worst case length
// XXX: Use a dynamically allocated array when moving to C99.
uio_walkPhysicalPath(pDirHandle, path, pathLen, &pDirHandle, &rest); uio_walkPhysicalPath(pDirHandle, path, pathLen, &pDirHandle, &rest);
// The reference to the original pDirHandle is still kept // 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; int savedErrno = errno;
uio_PDirHandle_unref(pDirHandle); uio_PDirHandle_unref(pDirHandle);
errno = savedErrno; errno = savedErrno;
uio_free(buf);
return NULL; return NULL;
} }
uio_PDirHandle_unref(pDirHandle); uio_PDirHandle_unref(pDirHandle);
pDirHandle = newPDirHandle; pDirHandle = newPDirHandle;
getNextPathComponent(pathEnd, &start, &end); getNextPathComponent(pathEnd, &start, &end);
} }
uio_free(buf);
return pDirHandle; return pDirHandle;
} }
-1
View File
@@ -29,7 +29,6 @@
#define uio_realloc realloc #define uio_realloc realloc
#define uio_free free #define uio_free free
#define uio_calloc calloc #define uio_calloc calloc
#define uio_alloca alloca
#ifdef uio_MEM_DEBUG #ifdef uio_MEM_DEBUG
// When uio_strdup is defined to the libc strdup, there's no opportunity // When uio_strdup is defined to the libc strdup, there's no opportunity
-11
View File
@@ -145,17 +145,6 @@ typedef unsigned short mode_t;
# define S_IFDIR _S_IFDIR # define S_IFDIR _S_IFDIR
#endif #endif
// Memory related:
#ifdef WIN32
# ifdef __MINGW32__
# include <malloc.h>
# elif defined (_MSC_VER)
# define alloca _alloca
# endif
#elif defined(__linux__) || defined(__svr4__)
# include <alloca.h>
#endif
// String formatting // String formatting
#ifdef _MSC_VER #ifdef _MSC_VER
#define snprintf _snprintf #define snprintf _snprintf