Skip to content

Commit

Permalink
Replaced some Malloc and all the alloca() calls with __PHYSFS_smallAl…
Browse files Browse the repository at this point in the history
…loc(),

 which will stack allocate small (128 or less bytes) blocks and Malloc the
 rest...naturally these now have to be paired with __PHYSFS_smallFree() calls,
 so you can't be as lazy as a basic alloca() would let you be. The benefit is
 both less malloc pressure for those temporary allocations and better stack
 overflow safety (so if some jerk tries to push a 78 megabyte string through
 the library as a filename, we won't try to strcpy it to the stack).
  • Loading branch information
icculus committed Mar 24, 2007
1 parent 80ec45d commit 7386320
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 210 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.txt
@@ -1,6 +1,18 @@
/*
* CHANGELOG.
*/

03222007 - Replaced some Malloc and all the alloca() calls with
__PHYSFS_smallAlloc(), which will stack allocate small (128 or
less bytes) blocks and Malloc the rest...naturally these now have
to be paired with __PHYSFS_smallFree() calls, so you can't be as
lazy as a basic alloca() would let you be. The benefit is both less
malloc pressure for those temporary allocations and better stack
overflow safety (so if some jerk tries to push a 78 megabyte string
through the library as a filename, we won't try to strcpy it to
the stack). Hopefully some internal interfaces can now get
refactored to stop generating heap pointers and let the caller use
smallAlloc to further reduce malloc pressure.
03212007 - Replaced LONGLONGLITERAL with __PHYSFS_UI64/__PHYSFS_SI64 ...
03202007 - Removed platform/skeleton.c (it was out of date), added
platform/macosx.c (To further Macify the code and get the #ifdefs
Expand Down
3 changes: 2 additions & 1 deletion archivers/lzma.c
Expand Up @@ -479,13 +479,14 @@ static void *LZMA_openArchive(const char *name, int forWriting)
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
{
char *newstr = alloca(ln + 1);
char *newstr = __PHYSFS_smallAlloc(ln + 1);
if (newstr == NULL)
return;

memcpy(newstr, str, ln);
newstr[ln] = '\0';
cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr);
} /* doEnumCallback */


Expand Down
3 changes: 2 additions & 1 deletion archivers/qpak.c
Expand Up @@ -378,13 +378,14 @@ static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
{
char *newstr = alloca(ln + 1);
char *newstr = __PHYSFS_smallAlloc(ln + 1);
if (newstr == NULL)
return;

memcpy(newstr, str, ln);
newstr[ln] = '\0';
cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr);
} /* doEnumCallback */


Expand Down
13 changes: 7 additions & 6 deletions archivers/zip.c
Expand Up @@ -682,15 +682,15 @@ static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
else /* symlink target path is compressed... */
{
z_stream stream;
PHYSFS_uint32 compsize = entry->compressed_size;
PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) allocator.Malloc(compsize);
PHYSFS_uint32 complen = entry->compressed_size;
PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
if (compressed != NULL)
{
if (__PHYSFS_platformRead(in, compressed, compsize, 1) == 1)
if (__PHYSFS_platformRead(in, compressed, complen, 1) == 1)
{
initializeZStream(&stream);
stream.next_in = compressed;
stream.avail_in = compsize;
stream.avail_in = complen;
stream.next_out = (unsigned char *) path;
stream.avail_out = size;
if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
Expand All @@ -702,7 +702,7 @@ static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
} /* if */
} /* if */
allocator.Free(compressed);
__PHYSFS_smallFree(compressed);
} /* if */
} /* else */

Expand Down Expand Up @@ -1177,13 +1177,14 @@ static PHYSFS_sint32 zip_find_start_of_dir(ZIPinfo *info, const char *path,
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
{
char *newstr = alloca(ln + 1);
char *newstr = __PHYSFS_smallAlloc(ln + 1);
if (newstr == NULL)
return;

memcpy(newstr, str, ln);
newstr[ln] = '\0';
cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr);
} /* doEnumCallback */


Expand Down

0 comments on commit 7386320

Please sign in to comment.