Skip to content
Permalink
Browse files
Replaced some Malloc and all the alloca() calls with __PHYSFS_smallAl…
…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 73863208112e1b280b5e967b761df1ef324dffb8
Showing with 414 additions and 210 deletions.
  1. +12 −0 CHANGELOG.txt
  2. +2 −1 archivers/lzma.c
  3. +2 −1 archivers/qpak.c
  4. +7 −6 archivers/zip.c
  5. +332 −183 physfs.c
  6. +32 −0 physfs_internal.h
  7. +21 −5 platform/pocketpc.c
  8. +2 −5 platform/posix.c
  9. +4 −9 platform/windows.c
@@ -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
@@ -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 */


@@ -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 */


@@ -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)
@@ -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 */

@@ -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 */


0 comments on commit 7386320

Please sign in to comment.