All memory management now goes through allocation hooks instead of directly to

C runtime malloc/realloc/free...
This commit is contained in:
Ryan C. Gordon 2005-03-14 11:49:30 +00:00
parent 4787894ffa
commit d840403c1c
19 changed files with 419 additions and 260 deletions

View File

@ -91,7 +91,7 @@ static void *DIR_openArchive(const char *name, int forWriting)
BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
ERR_UNSUPPORTED_ARCHIVE, 0);
retval = malloc(namelen + seplen + 1);
retval = allocator.Malloc(namelen + seplen + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/* make sure there's a dir separator at the end of the string */
@ -111,7 +111,7 @@ static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
if (d != NULL)
{
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb, callbackdata);
free(d);
allocator.Free(d);
} /* if */
} /* DIR_enumerateFiles */
@ -123,7 +123,7 @@ static int DIR_exists(dvoid *opaque, const char *name)
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformExists(f);
free(f);
allocator.Free(f);
return(retval);
} /* DIR_exists */
@ -137,7 +137,7 @@ static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
free(d);
allocator.Free(d);
return(retval);
} /* DIR_isDirectory */
@ -151,7 +151,7 @@ static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
free(f);
allocator.Free(f);
return(retval);
} /* DIR_isSymLink */
@ -167,7 +167,7 @@ static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformGetLastModTime(d);
free(d);
allocator.Free(d);
return(retval);
} /* DIR_getLastModTime */
@ -186,13 +186,13 @@ static fvoid *doOpen(dvoid *opaque, const char *name,
*fileExists = __PHYSFS_platformExists(f);
if (!(*fileExists))
{
free(f);
allocator.Free(f);
return(NULL);
} /* if */
} /* if */
rc = openFunc(f);
free(f);
allocator.Free(f);
return((fvoid *) rc);
} /* doOpen */
@ -223,7 +223,7 @@ static int DIR_remove(dvoid *opaque, const char *name)
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformDelete(f);
free(f);
allocator.Free(f);
return(retval);
} /* DIR_remove */
@ -235,14 +235,14 @@ static int DIR_mkdir(dvoid *opaque, const char *name)
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformMkDir(f);
free(f);
allocator.Free(f);
return(retval);
} /* DIR_mkdir */
static void DIR_dirClose(dvoid *opaque)
{
free(opaque);
allocator.Free(opaque);
} /* DIR_dirClose */

View File

@ -64,9 +64,9 @@ typedef struct
static void GRP_dirClose(dvoid *opaque)
{
GRPinfo *info = ((GRPinfo *) opaque);
free(info->filename);
free(info->entries);
free(info);
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
} /* GRP_dirClose */
@ -138,7 +138,7 @@ static int GRP_fileClose(fvoid *opaque)
{
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
allocator.Free(finfo);
return(1);
} /* GRP_fileClose */
@ -221,7 +221,7 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
@ -264,12 +264,12 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
static void *GRP_openArchive(const char *name, int forWriting)
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
GRPinfo *info = malloc(sizeof (GRPinfo));
GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
memset(info, '\0', sizeof (GRPinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
if (!grp_load_entries(name, forWriting, info))
@ -284,10 +284,10 @@ GRP_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
allocator.Free(info->entries);
allocator.Free(info);
} /* if */
return(NULL);
@ -390,14 +390,14 @@ static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -78,9 +78,9 @@ typedef struct
static void HOG_dirClose(dvoid *opaque)
{
HOGinfo *info = ((HOGinfo *) opaque);
free(info->filename);
free(info->entries);
free(info);
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
} /* HOG_dirClose */
@ -152,7 +152,7 @@ static int HOG_fileClose(fvoid *opaque)
{
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
allocator.Free(finfo);
return(1);
} /* HOG_fileClose */
@ -256,7 +256,7 @@ static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
info->entries = (HOGentry *) malloc(sizeof (HOGentry) * fileCount);
info->entries = (HOGentry *) allocator.Malloc(sizeof(HOGentry)*fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
@ -304,11 +304,11 @@ static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
static void *HOG_openArchive(const char *name, int forWriting)
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
HOGinfo *info = malloc(sizeof (HOGinfo));
HOGinfo *info = (HOGinfo *) allocator.Malloc(sizeof (HOGinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
memset(info, '\0', sizeof (HOGinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, HOG_openArchive_failed);
if (!hog_load_entries(name, forWriting, info))
@ -323,10 +323,10 @@ HOG_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
allocator.Free(info->entries);
allocator.Free(info);
} /* if */
return(NULL);
@ -429,14 +429,14 @@ static fvoid *HOG_openRead(dvoid *opaque, const char *fnm, int *fileExists)
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
finfo = (HOGfileinfo *) malloc(sizeof (HOGfileinfo));
finfo = (HOGfileinfo *) allocator.Malloc(sizeof (HOGfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -116,8 +116,8 @@ static PHYSFS_uint32 MIX_hash(const char *name)
static void MIX_dirClose(dvoid *opaque)
{
MIXinfo *info = ((MIXinfo *) opaque);
free(info->entry);
free(info->filename);
allocator.Free(info->entry);
allocator.Free(info->filename);
} /* MIX_dirClose */
@ -186,7 +186,7 @@ static int MIX_fileClose(fvoid *opaque)
{
MIXfileinfo *finfo = (MIXfileinfo *) opaque;
__PHYSFS_platformClose(finfo->handle);
free(finfo);
allocator.Free(finfo);
return(1);
} /* MIX_fileClose */
@ -231,11 +231,11 @@ static void *MIX_openArchive(const char *name, int forWriting)
MIXinfo *info = NULL;
void *handle = NULL;
info = (MIXinfo *) malloc(sizeof (MIXinfo));
info = (MIXinfo *) allocator.Malloc(sizeof (MIXinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
memset(info, '\0', sizeof (MIXinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, MIX_openArchive_failed);
/* store filename */
@ -254,7 +254,7 @@ static void *MIX_openArchive(const char *name, int forWriting)
info->delta = 6 + (info->header.num_files * 12);
/* allocate space for the entries and read the entries */
info->entry = malloc(sizeof (MIXentry) * info->header.num_files);
info->entry = allocator.Malloc(sizeof (MIXentry) * info->header.num_files);
GOTO_IF_MACRO(!info->entry, ERR_OUT_OF_MEMORY, MIX_openArchive_failed);
/* read the directory list */
@ -274,10 +274,10 @@ MIX_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entry != NULL)
free(info->entry);
free(info);
allocator.Free(info->entry);
allocator.Free(info);
} /* if */
if (handle != NULL)
@ -367,14 +367,14 @@ static fvoid *MIX_openRead(dvoid *opaque, const char *fnm, int *fileExists)
BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
/* allocate a MIX handle */
finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
finfo = (MIXfileinfo *) allocator.Malloc(sizeof (MIXfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
/* open the archive */
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if(!finfo->handle)
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -67,9 +67,9 @@ typedef struct
static void MVL_dirClose(dvoid *opaque)
{
MVLinfo *info = ((MVLinfo *) opaque);
free(info->filename);
free(info->entries);
free(info);
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
} /* MVL_dirClose */
@ -141,7 +141,7 @@ static int MVL_fileClose(fvoid *opaque)
{
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
allocator.Free(finfo);
return(1);
} /* MVL_fileClose */
@ -223,7 +223,7 @@ static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
BAIL_IF_MACRO(!mvl_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
info->entries = (MVLentry *) malloc(sizeof (MVLentry) * fileCount);
info->entries = (MVLentry *) allocator.Malloc(sizeof(MVLentry)*fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
@ -262,12 +262,12 @@ static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
static void *MVL_openArchive(const char *name, int forWriting)
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
MVLinfo *info = malloc(sizeof (MVLinfo));
MVLinfo *info = (MVLinfo *) allocator.Malloc(sizeof (MVLinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
memset(info, '\0', sizeof (MVLinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, MVL_openArchive_failed);
if (!mvl_load_entries(name, forWriting, info))
goto MVL_openArchive_failed;
@ -280,10 +280,10 @@ MVL_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
allocator.Free(info->entries);
allocator.Free(info);
} /* if */
return(NULL);
@ -386,14 +386,14 @@ static fvoid *MVL_openRead(dvoid *opaque, const char *fnm, int *fileExists)
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
finfo = (MVLfileinfo *) malloc(sizeof (MVLfileinfo));
finfo = (MVLfileinfo *) allocator.Malloc(sizeof (MVLfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -81,9 +81,9 @@ typedef struct
static void QPAK_dirClose(dvoid *opaque)
{
QPAKinfo *info = ((QPAKinfo *) opaque);
free(info->filename);
free(info->entries);
free(info);
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
} /* QPAK_dirClose */
@ -155,7 +155,7 @@ static int QPAK_fileClose(fvoid *opaque)
{
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
allocator.Free(finfo);
return(1);
} /* QPAK_fileClose */
@ -245,7 +245,7 @@ static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
info->entries = (QPAKentry *) malloc(sizeof (QPAKentry) * fileCount);
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
@ -288,13 +288,13 @@ static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
static void *QPAK_openArchive(const char *name, int forWriting)
{
QPAKinfo *info = malloc(sizeof (QPAKinfo));
QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
memset(info, '\0', sizeof (QPAKinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
if (info->filename == NULL)
{
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
@ -312,10 +312,10 @@ QPAK_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
allocator.Free(info->entries);
allocator.Free(info);
} /* if */
return(NULL);
@ -544,14 +544,14 @@ static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
finfo = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -83,9 +83,9 @@ typedef struct
static void WAD_dirClose(dvoid *opaque)
{
WADinfo *info = ((WADinfo *) opaque);
free(info->filename);
free(info->entries);
free(info);
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
} /* WAD_dirClose */
@ -157,7 +157,7 @@ static int WAD_fileClose(fvoid *opaque)
{
WADfileinfo *finfo = (WADfileinfo *) opaque;
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
allocator.Free(finfo);
return(1);
} /* WAD_fileClose */
@ -247,7 +247,7 @@ static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
info->entryCount = fileCount;
info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
info->entries = (WADentry *) allocator.Malloc(sizeof(WADentry)*fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
@ -292,12 +292,12 @@ static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
static void *WAD_openArchive(const char *name, int forWriting)
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
WADinfo *info = malloc(sizeof (WADinfo));
WADinfo *info = (WADinfo *) allocator.Malloc(sizeof (WADinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
memset(info, '\0', sizeof (WADinfo));
info->filename = (char *) malloc(strlen(name) + 1);
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, WAD_openArchive_failed);
if (!wad_load_entries(name, forWriting, info))
@ -311,10 +311,10 @@ WAD_openArchive_failed:
if (info != NULL)
{
if (info->filename != NULL)
free(info->filename);
allocator.Free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
allocator.Free(info->entries);
allocator.Free(info);
} /* if */
return(NULL);
@ -449,14 +449,14 @@ static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists)
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo));
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
allocator.Free(finfo);
return(NULL);
} /* if */

View File

@ -27,8 +27,8 @@
#include "physfs_internal.h"
/*
* A buffer of ZIP_READBUFSIZE is malloc() for each compressed file opened,
* and is free()'d when you close the file; compressed data is read into
* A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened,
* and is freed when you close the file; compressed data is read into
* this buffer, and then is decompressed into the buffer passed to
* PHYSFS_read().
*
@ -121,7 +121,7 @@ typedef struct
*/
static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
{
return(((PHYSFS_Allocator *) opaque)->malloc(items * size));
return(((PHYSFS_Allocator *) opaque)->Malloc(items * size));
} /* zlibPhysfsAlloc */
/*
@ -129,7 +129,7 @@ static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
*/
static void zlibPhysfsFree(voidpf opaque, voidpf address)
{
((PHYSFS_Allocator *) opaque)->free(address);
((PHYSFS_Allocator *) opaque)->Free(address);
} /* zlibPhysfsFree */
@ -141,7 +141,7 @@ static void initializeZStream(z_stream *pstr)
memset(pstr, '\0', sizeof (z_stream));
pstr->zalloc = zlibPhysfsAlloc;
pstr->zfree = zlibPhysfsFree;
pstr->opaque = __PHYSFS_getAllocator();
pstr->opaque = &allocator;
} /* initializeZStream */
@ -369,9 +369,9 @@ static int ZIP_fileClose(fvoid *opaque)
inflateEnd(&finfo->stream);
if (finfo->buffer != NULL)
free(finfo->buffer);
allocator.Free(finfo->buffer);
free(finfo);
allocator.Free(finfo);
return(1);
} /* ZIP_fileClose */
@ -388,7 +388,7 @@ static PHYSFS_sint64 zip_find_end_of_central_dir(void *in, PHYSFS_sint64 *len)
PHYSFS_uint32 extra = 0;
filelen = __PHYSFS_platformFileLength(in);
BAIL_IF_MACRO(filelen == -1, NULL, 0);
BAIL_IF_MACRO(filelen == -1, NULL, 0); /* !!! FIXME: unlocalized string */
BAIL_IF_MACRO(filelen > 0xFFFFFFFF, "ZIP bigger than 2 gigs?!", 0);
/*
@ -501,10 +501,10 @@ static void zip_free_entries(ZIPentry *entries, PHYSFS_uint32 max)
{
ZIPentry *entry = &entries[i];
if (entry->name != NULL)
free(entry->name);
allocator.Free(entry->name);
} /* for */
free(entries);
allocator.Free(entries);
} /* zip_free_entries */
@ -658,7 +658,7 @@ static ZIPentry *zip_follow_symlink(void *in, ZIPinfo *info, char *path)
} /* else */
} /* if */
free(path);
allocator.Free(path);
return(entry);
} /* zip_follow_symlink */
@ -677,7 +677,7 @@ static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
path = (char *) malloc(size + 1);
path = (char *) allocator.Malloc(size + 1);
BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, 0);
if (entry->compression_method == COMPMETH_NONE)
@ -687,7 +687,7 @@ static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
{
z_stream stream;
PHYSFS_uint32 compsize = entry->compressed_size;
PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) malloc(compsize);
PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) allocator.Malloc(compsize);
if (compressed != NULL)
{
if (__PHYSFS_platformRead(in, compressed, compsize, 1) == 1)
@ -706,12 +706,12 @@ static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
} /* if */
} /* if */
free(compressed);
allocator.Free(compressed);
} /* if */
} /* else */
if (!rc)
free(path);
allocator.Free(path);
else
{
path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
@ -928,7 +928,7 @@ static int zip_load_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ?
ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
entry->name = (char *) malloc(fnamelen + 1);
entry->name = (char *) allocator.Malloc(fnamelen + 1);
BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
if (__PHYSFS_platformRead(in, entry->name, fnamelen, 1) != 1)
goto zip_load_entry_puked;
@ -947,7 +947,7 @@ static int zip_load_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
return(1); /* success. */
zip_load_entry_puked:
free(entry->name);
allocator.Free(entry->name);
return(0); /* failure. */
} /* zip_load_entry */
@ -978,7 +978,7 @@ static int zip_load_entries(void *in, ZIPinfo *info,
BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, central_ofs), NULL, 0);
info->entries = (ZIPentry *) malloc(sizeof (ZIPentry) * max);
info->entries = (ZIPentry *) allocator.Malloc(sizeof (ZIPentry) * max);
BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
for (i = 0; i < max; i++)
@ -1065,14 +1065,14 @@ static int zip_parse_end_of_central_dir(void *in, ZIPinfo *info,
static ZIPinfo *zip_create_zipinfo(const char *name)
{
char *ptr;
ZIPinfo *info = (ZIPinfo *) malloc(sizeof (ZIPinfo));
ZIPinfo *info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
memset(info, '\0', sizeof (ZIPinfo));
ptr = (char *) malloc(strlen(name) + 1);
ptr = (char *) allocator.Malloc(strlen(name) + 1);
if (ptr == NULL)
{
free(info);
allocator.Free(info);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
@ -1110,8 +1110,8 @@ zip_openarchive_failed:
if (info != NULL)
{
if (info->archiveName != NULL)
free(info->archiveName);
free(info);
allocator.Free(info->archiveName);
allocator.Free(info);
} /* if */
if (in != NULL)
@ -1340,7 +1340,7 @@ static fvoid *ZIP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
in = zip_get_file_handle(info->archiveName, info, entry);
BAIL_IF_MACRO(in == NULL, NULL, NULL);
finfo = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo));
finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
if (finfo == NULL)
{
__PHYSFS_platformClose(in);
@ -1359,7 +1359,7 @@ static fvoid *ZIP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
return(NULL);
} /* if */
finfo->buffer = (PHYSFS_uint8 *) malloc(ZIP_READBUFSIZE);
finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
if (finfo->buffer == NULL)
{
ZIP_fileClose(finfo);
@ -1387,8 +1387,8 @@ static void ZIP_dirClose(dvoid *opaque)
{
ZIPinfo *zi = (ZIPinfo *) (opaque);
zip_free_entries(zi->entries, zi->entryCount);
free(zi->archiveName);
free(zi);
allocator.Free(zi->archiveName);
allocator.Free(zi);
} /* ZIP_dirClose */

120
physfs.c
View File

@ -180,7 +180,7 @@ static void *stateLock = NULL; /* protects other PhysFS static state. */
/* allocator ... */
static int externalAllocator = 0;
static PHYSFS_Allocator allocator;
PHYSFS_Allocator allocator;
/* functions ... */
@ -201,8 +201,8 @@ static void enumStringListCallback(void *data, const char *str)
if (pecd->errorstr)
return;
ptr = realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = malloc(strlen(str) + 1);
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = (char *) allocator.Malloc(strlen(str) + 1);
if (ptr != NULL)
pecd->list = (char **) ptr;
@ -224,7 +224,7 @@ static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
{
EnumStringListCallbackData ecd;
memset(&ecd, '\0', sizeof (ecd));
ecd.list = (char **) malloc(sizeof (char *));
ecd.list = (char **) allocator.Malloc(sizeof (char *));
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
func(enumStringListCallback, &ecd);
BAIL_IF_MACRO(ecd.errorstr != NULL, ecd.errorstr, NULL);
@ -345,7 +345,7 @@ void __PHYSFS_setError(const char *str)
if (err == NULL)
{
err = (ErrMsg *) malloc(sizeof (ErrMsg));
err = (ErrMsg *) allocator.Malloc(sizeof (ErrMsg));
if (err == NULL)
return; /* uhh...? */
@ -389,7 +389,7 @@ static void freeErrorMessages(void)
for (i = errorMessages; i != NULL; i = next)
{
next = i->next;
free(i);
allocator.Free(i);
} /* for */
errorMessages = NULL;
@ -435,7 +435,7 @@ static DirHandle *tryOpenDir(const PHYSFS_Archiver *funcs,
void *opaque = funcs->openArchive(d, forWriting);
if (opaque != NULL)
{
retval = (DirHandle *) allocator.malloc(sizeof (DirHandle));
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
if (retval == NULL)
funcs->dirClose(opaque);
else
@ -592,13 +592,13 @@ static DirHandle *createDirHandle(const char *newDir,
dirHandle = openDirectory(newDir, forWriting);
GOTO_IF_MACRO(!dirHandle, NULL, badDirHandle);
dirHandle->dirName = (char *) malloc(strlen(newDir) + 1);
dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
GOTO_IF_MACRO(!dirHandle->dirName, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->dirName, newDir);
if ((mountPoint != NULL) && (*mountPoint != '\0'))
{
dirHandle->mountPoint = (char *) malloc(strlen(mountPoint) + 2);
dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
GOTO_IF_MACRO(!dirHandle->mountPoint, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->mountPoint, mountPoint);
strcat(dirHandle->mountPoint, "/");
@ -610,9 +610,9 @@ badDirHandle:
if (dirHandle != NULL)
{
dirHandle->funcs->dirClose(dirHandle->opaque);
free(dirHandle->dirName);
free(dirHandle->mountPoint);
free(dirHandle);
allocator.Free(dirHandle->dirName);
allocator.Free(dirHandle->mountPoint);
allocator.Free(dirHandle);
} /* if */
return(NULL);
@ -631,9 +631,9 @@ static int freeDirHandle(DirHandle *dh, FileHandle *openList)
BAIL_IF_MACRO(i->dirHandle == dh, ERR_FILES_STILL_OPEN, 0);
dh->funcs->dirClose(dh->opaque);
free(dh->dirName);
free(dh->mountPoint);
free(dh);
allocator.Free(dh->dirName);
allocator.Free(dh->mountPoint);
allocator.Free(dh);
return(1);
} /* freeDirHandle */
@ -652,8 +652,8 @@ static char *calculateUserDir(void)
const char *uname = __PHYSFS_platformGetUserName();
str = (uname != NULL) ? uname : "default";
retval = (char *) malloc(strlen(baseDir) + strlen(str) +
strlen(dirsep) + 6);
retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
strlen(dirsep) + 6);
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
@ -661,7 +661,7 @@ static char *calculateUserDir(void)
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
if (uname != NULL)
free((void *) uname);
allocator.Free((void *) uname);
} /* else */
return(retval);
@ -676,10 +676,10 @@ static int appendDirSep(char **dir)
if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
return(1);
ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
if (!ptr)
{
free(*dir);
allocator.Free(*dir);
return(0);
} /* if */
@ -717,7 +717,7 @@ static char *calculateBaseDir(const char *argv0)
} /* while */
size = (size_t) (ptr - argv0);
retval = (char *) malloc(size + 1);
retval = (char *) allocator.Malloc(size + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
memcpy(retval, argv0, size);
retval[size] = '\0';
@ -736,7 +736,7 @@ static char *calculateBaseDir(const char *argv0)
* Not a good alternative, but it only happens if the current
* directory was deleted from under the program.
*/
retval = (char *) malloc(strlen(dirsep) + 1);
retval = (char *) allocator.Malloc(strlen(dirsep) + 1);
strcpy(retval, dirsep);
return(retval);
} /* calculateBaseDir */
@ -777,7 +777,7 @@ int PHYSFS_init(const char *argv0)
if (!externalAllocator)
setDefaultAllocator();
BAIL_IF_MACRO(!allocator.init(), NULL, 0);
BAIL_IF_MACRO(!allocator.Init(), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
@ -787,7 +787,7 @@ int PHYSFS_init(const char *argv0)
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
ptr = __PHYSFS_platformRealPath(baseDir);
free(baseDir);
allocator.Free(baseDir);
BAIL_IF_MACRO(ptr == NULL, NULL, 0);
baseDir = ptr;
@ -797,13 +797,13 @@ int PHYSFS_init(const char *argv0)
if (userDir != NULL)
{
ptr = __PHYSFS_platformRealPath(userDir);
free(userDir);
allocator.Free(userDir);
userDir = ptr;
} /* if */
if ((userDir == NULL) || (!appendDirSep(&userDir)))
{
free(baseDir);
allocator.Free(baseDir);
baseDir = NULL;
return(0);
} /* if */
@ -832,7 +832,7 @@ static int closeFileHandleList(FileHandle **list)
return(0);
} /* if */
free(i);
allocator.Free(i);
} /* for */
*list = NULL;
@ -873,13 +873,13 @@ int PHYSFS_deinit(void)
if (baseDir != NULL)
{
free(baseDir);
allocator.Free(baseDir);
baseDir = NULL;
} /* if */
if (userDir != NULL)
{
free(userDir);
allocator.Free(userDir);
userDir = NULL;
} /* if */
@ -889,7 +889,7 @@ int PHYSFS_deinit(void)
__PHYSFS_platformDestroyMutex(errorLock);
__PHYSFS_platformDestroyMutex(stateLock);
allocator.deinit();
allocator.Deinit();
errorLock = stateLock = NULL;
return(1);
@ -906,9 +906,9 @@ void PHYSFS_freeList(void *list)
{
void **i;
for (i = (void **) list; *i != NULL; i++)
free(*i);
allocator.Free(*i);
free(list);
allocator.Free(list);
} /* PHYSFS_freeList */
@ -1106,8 +1106,10 @@ int PHYSFS_setSaneConfig(const char *organization, const char *appName,
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
/* set write dir... */
str = malloc(strlen(userdir) + (strlen(organization) * 2) +
(strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
str = (char *) allocator.Malloc(
strlen(userdir) + (strlen(organization) * 2) +
(strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
@ -1130,14 +1132,14 @@ int PHYSFS_setSaneConfig(const char *organization, const char *appName,
if (no_write)
{
PHYSFS_setWriteDir(NULL); /* just in case. */
free(str);
allocator.Free(str);
BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
} /* if */
} /* if */
/* Put write dir first in search path... */
PHYSFS_addToSearchPath(str, 0);
free(str);
allocator.Free(str);
/* Put base path on search path... */
PHYSFS_addToSearchPath(basedir, 1);
@ -1170,12 +1172,13 @@ int PHYSFS_setSaneConfig(const char *organization, const char *appName,
if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
{
const char *d = PHYSFS_getRealDir(*i);
str = malloc(strlen(d) + strlen(dirsep) + l + 1);
size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
str = (char *) allocator.Malloc(allocsize);
if (str != NULL)
{
sprintf(str, "%s%s%s", d, dirsep, *i);
PHYSFS_addToSearchPath(str, archivesFirst == 0);
free(str);
allocator.Free(str);
} /* if */
} /* if */
} /* if */
@ -1195,9 +1198,9 @@ void PHYSFS_permitSymbolicLinks(int allow)
/* string manipulation in C makes my ass itch. */
char * __PHYSFS_convertToDependent(const char *prepend,
const char *dirName,
const char *append)
char *__PHYSFS_convertToDependent(const char *prepend,
const char *dirName,
const char *append)
{
const char *dirsep = __PHYSFS_platformDirSeparator;
size_t sepsize = strlen(dirsep);
@ -1206,7 +1209,7 @@ char * __PHYSFS_convertToDependent(const char *prepend,
char *i2;
size_t allocSize;
while (*dirName == '/')
while (*dirName == '/') /* !!! FIXME: pass through sanitize function. */
dirName++;
allocSize = strlen(dirName) + 1;
@ -1230,7 +1233,7 @@ char * __PHYSFS_convertToDependent(const char *prepend,
} while (str != NULL);
} /* if */
str = (char *) malloc(allocSize);
str = (char *) allocator.Malloc(allocSize);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend == NULL)
@ -1504,8 +1507,8 @@ static void enumFilesCallback(void *data, const char *str)
return; /* already in the list. */
} /* if */
ptr = realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = malloc(strlen(str) + 1);
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = (char *) allocator.Malloc(strlen(str) + 1);
if (ptr != NULL)
pecd->list = (char **) ptr;
@ -1529,7 +1532,7 @@ char **PHYSFS_enumerateFiles(const char *path)
{
EnumStringListCallbackData ecd;
memset(&ecd, '\0', sizeof (ecd));
ecd.list = (char **) malloc(sizeof (char *));
ecd.list = (char **) allocator.Malloc(sizeof (char *));
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
ecd.list[ecd.size] = NULL;
@ -1690,7 +1693,7 @@ static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
fh = (FileHandle *) malloc(sizeof (FileHandle));
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
if (fh == NULL)
{
f->fileClose(opaque);
@ -1736,7 +1739,7 @@ PHYSFS_File *PHYSFS_openRead(const char *_fname)
__PHYSFS_platformGrabMutex(stateLock);
BAIL_IF_MACRO_MUTEX(!searchPath, ERR_PATH_NOT_FOUND, stateLock, NULL);
BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NO_SUCH_PATH, stateLock, NULL);
/* !!! FIXME: Why aren't we using a for loop here? */
i = searchPath;
@ -1755,7 +1758,7 @@ PHYSFS_File *PHYSFS_openRead(const char *_fname)
BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
fh = (FileHandle *) malloc(sizeof (FileHandle));
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
if (fh == NULL)
{
i->funcs->fileClose(opaque);
@ -1793,14 +1796,14 @@ static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
return(-1);
if (tmp != NULL) /* free any associated buffer. */
free(tmp);
allocator.Free(tmp);
if (prev == NULL)
*list = handle->next;
else
prev->next = handle->next;
free(handle);
allocator.Free(handle);
return(1);
} /* if */
prev = i;
@ -2005,14 +2008,15 @@ int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
{
if (fh->buffer != NULL)
{
free(fh->buffer);
allocator.Free(fh->buffer);
fh->buffer = NULL;
} /* if */
} /* if */
else
{
PHYSFS_uint8 *newbuf = realloc(fh->buffer, bufsize);
PHYSFS_uint8 *newbuf;
newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
fh->buffer = newbuf;
} /* else */
@ -2054,11 +2058,11 @@ int PHYSFS_setAllocator(PHYSFS_Allocator *a)
static void setDefaultAllocator(void)
{
assert(!externalAllocator);
allocator.init = __PHYSFS_platformAllocatorInit;
allocator.deinit = __PHYSFS_platformAllocatorDeinit;
allocator.malloc = __PHYSFS_platformAllocatorMalloc;
allocator.realloc = __PHYSFS_platformAllocatorRealloc;
allocator.free = __PHYSFS_platformAllocatorFree;
allocator.Init = __PHYSFS_platformAllocatorInit;
allocator.Deinit = __PHYSFS_platformAllocatorDeinit;
allocator.Malloc = __PHYSFS_platformAllocatorMalloc;
allocator.Realloc = __PHYSFS_platformAllocatorRealloc;
allocator.Free = __PHYSFS_platformAllocatorFree;
} /* setDefaultAllocator */

View File

@ -1861,11 +1861,11 @@ __EXPORT__ int PHYSFS_writeUBE64(PHYSFS_File *file, PHYSFS_uint64 val);
*/
typedef struct
{
int (*init)(void);
void (*deinit)(void);
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
int (*Init)(void);
void (*Deinit)(void);
void *(*Malloc)(size_t);
void *(*Realloc)(void *, size_t);
void (*Free)(void *);
} PHYSFS_Allocator;
@ -1876,6 +1876,8 @@ typedef struct
* (This is for limited, hardcore use. If you don't immediately see a need
* for it, you can probably ignore this forever.)
*
* !!! FIXME: don't promise C runtime malloc.
*
* By default, PhysicsFS will use ANSI C malloc/realloc/calloc/free calls
* to manage dynamic memory, but in some uncommon cases, the app might want
* more control over the library's memory management. This lets you redirect

View File

@ -26,6 +26,11 @@
extern "C" {
#endif
/* Use the allocation hooks. */
#define malloc(x) Do not use malloc() directly.
#define realloc(x, y) Do not use realloc() directly.
#define free(x) Do not use free() directly.
/* The LANG section. */
/* please send questions/translations to Ryan: icculus@clutteredmind.org. */
@ -1241,10 +1246,12 @@ void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
/*
* Get the current allocator. Not valid before PHYSFS_init is called!
* The current allocator. Not valid before PHYSFS_init is called!
*/
PHYSFS_Allocator *__PHYSFS_getAllocator(void);
extern PHYSFS_Allocator __PHYSFS_AllocatorHooks;
/* convenience macro to make this less cumbersome internally... */
#define allocator __PHYSFS_AllocatorHooks
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

View File

@ -19,6 +19,7 @@ EXTRA_DIST = \
skeleton.c \
macclassic.c \
win32.c \
pocketpc.c \
unix.c \
os2.c \
beos.cpp

View File

@ -241,7 +241,7 @@ static char *convFSSpecToPath(FSSpec *spec, int includeFile)
if (oserr(PBGetCatInfoSync(&infoPB)) != noErr)
{
if (retval != NULL)
free(retval);
allocator.Free(retval);
return(NULL);
} /* if */
@ -249,11 +249,11 @@ static char *convFSSpecToPath(FSSpec *spec, int includeFile)
/* allocate more space for the retval... */
retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
ptr = (char *) malloc(retLength);
ptr = (char *) allocator.Malloc(retLength);
if (ptr == NULL)
{
if (retval != NULL)
free(retval);
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
@ -264,7 +264,7 @@ static char *convFSSpecToPath(FSSpec *spec, int includeFile)
{
strcat(ptr, ":");
strcat(ptr, retval);
free(retval);
allocator.Free(retval);
} /* if */
retval = ptr;
} while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
@ -297,7 +297,7 @@ char *__PHYSFS_platformGetUserName(void)
BAIL_IF_MACRO(strHandle == NULL, NULL, NULL);
HLock((Handle) strHandle);
retval = (char *) malloc((*strHandle)[0] + 1);
retval = (char *) allocator.Malloc((*strHandle)[0] + 1);
if (retval == NULL)
{
HUnlock((Handle) strHandle);
@ -533,7 +533,7 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend,
strlen(dirName) + 1;
const char *src;
char *dst;
char *retval = malloc(len);
char *retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend != NULL)
@ -691,7 +691,7 @@ static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
created = 1;
} /* if */
retval = (SInt16 *) malloc(sizeof (SInt16));
retval = (SInt16 *) allocator.Malloc(sizeof (SInt16));
if (retval == NULL)
{
if (created)
@ -702,7 +702,7 @@ static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
err = HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval);
if (oserr(err) != noErr)
{
free(retval);
allocator.Free(retval);
if (created)
HDelete(spec.vRefNum, spec.parID, spec.name);
return(NULL);
@ -866,7 +866,7 @@ int __PHYSFS_platformClose(void *opaque)
} /* if */
BAIL_IF_MACRO(oserr(FSClose(ref)) != noErr, NULL, 0);
free(opaque);
allocator.Free(opaque);
if (flushVol)
FlushVol(volName, vRefNum); /* update catalog info, etc. */
@ -936,5 +936,39 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
return((PHYSFS_sint64) modDate);
} /* __PHYSFS_platformGetLastModTime */
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
/* end of macclassic.c ... */

View File

@ -199,7 +199,7 @@ int __PHYSFS_platformInit(void)
/* The string is capitalized! Figure out the REAL case... */
cvt_path_to_correct_case(buf);
baseDir = (char *) malloc(len + 1);
baseDir = (char *) allocator.Malloc(len + 1);
BAIL_IF_MACRO(baseDir == NULL, ERR_OUT_OF_MEMORY, 0);
strcpy(baseDir, buf);
return(1); /* success. */
@ -209,7 +209,7 @@ int __PHYSFS_platformInit(void)
int __PHYSFS_platformDeinit(void)
{
assert(baseDir != NULL);
free(baseDir);
allocator.Free(baseDir);
baseDir = NULL;
return(1); /* success. */
} /* __PHYSFS_platformDeinit */
@ -279,7 +279,7 @@ void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
char *retval = (char *) malloc(strlen(baseDir) + 1);
char *retval = (char *) allocator.Malloc(strlen(baseDir) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, baseDir); /* calculated at init time. */
return(retval);
@ -365,6 +365,7 @@ int __PHYSFS_platformIsDirectory(const char *fname)
} /* __PHYSFS_platformIsDirectory */
/* !!! FIXME: can we lose the malloc here? */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
@ -372,7 +373,7 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend,
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = malloc(len);
char *retval = allocator.Malloc(len);
char *p;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -441,14 +442,14 @@ char *__PHYSFS_platformCurrentDir(void)
/* The first call just tells us how much space we need for the string. */
rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
pathSize++; /* Add space for null terminator. */
retval = (char *) malloc(pathSize + 3); /* plus "x:\\" */
retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/* Actually get the string this time. */
rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
if (os2err(rc) != NO_ERROR)
{
free(retval);
allocator.Free(retval);
return(NULL);
} /* if */
@ -465,7 +466,7 @@ char *__PHYSFS_platformRealPath(const char *path)
char *retval;
APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
retval = (char *) malloc(strlen(buf) + 1);
retval = (char *) allocator.Malloc(strlen(buf) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, buf);
return(retval);

View File

@ -72,11 +72,11 @@ static char *UnicodeToAsc(const wchar_t *w_str)
if (w_str != NULL)
{
int len = wcslen(w_str) + 1;
str = (char *) malloc(len);
str = (char *) allocator.Malloc(len);
if (WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL) == 0)
{ /*Conversion failed */
free(str);
allocator.Free(str);
return(NULL);
} /* if */
else
@ -98,10 +98,10 @@ static wchar_t *AscToUnicode(const char *str)
if (str != NULL)
{
int len = strlen(str) + 1;
w_str = (wchar_t *) malloc(sizeof (wchar_t) * len);
w_str = (wchar_t *) allocator.Malloc(sizeof (wchar_t) * len);
if (MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len) == 0)
{
free(w_str);
allocator.Free(w_str);
return(NULL);
} /* if */
else
@ -121,7 +121,7 @@ static char *getExePath()
DWORD buflen;
int success = 0;
TCHAR *ptr = NULL;
TCHAR *retval = (TCHAR *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
TCHAR *retval = (TCHAR*) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
char *charretval;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -145,17 +145,17 @@ static char *getExePath()
if (!success)
{
free(retval);
allocator.Free(retval);
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
ptr = (TCHAR *) realloc(retval, sizeof(TCHAR) * _tcslen(retval) + 1);
ptr = (TCHAR *) allocator.Realloc(retval, sizeof(TCHAR)*_tcslen(retval)+1);
if (ptr != NULL)
retval = ptr;
charretval = UnicodeToAsc(retval);
free(retval);
allocator.Free(retval);
if(charretval == NULL) {
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
}
@ -173,7 +173,7 @@ int __PHYSFS_platformInit(void)
int __PHYSFS_platformDeinit(void)
{
free(userDir);
allocator.Free(userDir);
return(1); /* always succeed. */
} /* __PHYSFS_platformDeinit */
@ -230,7 +230,7 @@ int __PHYSFS_platformExists(const char *fname)
if(w_fname!=NULL)
{
retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
free(w_fname);
allocator.Free(w_fname);
}
return(retval);
@ -252,7 +252,7 @@ int __PHYSFS_platformIsDirectory(const char *fname)
if(w_fname!=NULL)
{
retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
free(w_fname);
allocator.Free(w_fname);
}
return(retval);
@ -266,7 +266,7 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend,
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = malloc(len);
char *retval = (char *) allocator.Malloc(len);
char *p;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -325,8 +325,8 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
w_SearchPath=AscToUnicode(SearchPath);
dir = FindFirstFile(w_SearchPath, &ent);
free(w_SearchPath);
free(SearchPath);
allocator.Free(w_SearchPath);
allocator.Free(SearchPath);
if (dir == INVALID_HANDLE_VALUE)
return;
@ -347,7 +347,7 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
break;
callback(callbackdata, str);
free(str);
allocator.Free(str);
} while (FindNextFile(dir, &ent) != 0);
FindClose(dir);
@ -362,10 +362,8 @@ char *__PHYSFS_platformCurrentDir(void)
char *__PHYSFS_platformRealPath(const char *path)
{
char *retval=(char *)malloc(strlen(path)+1);
char *retval = (char *) allocator.Malloc(strlen(path) + 1);
strcpy(retval,path);
return(retval);
} /* __PHYSFS_platformRealPath */
@ -376,7 +374,7 @@ int __PHYSFS_platformMkDir(const char *path)
if(w_path!=NULL)
{
DWORD rc = CreateDirectory(w_path, NULL);
free(w_path);
allocator.Free(w_path);
if(rc==0)
{
return(0);
@ -399,7 +397,7 @@ static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
creation, FILE_ATTRIBUTE_NORMAL, NULL);
free(w_fname);
allocator.Free(w_fname);
if(fileHandle==INVALID_HANDLE_VALUE)
{
@ -408,7 +406,7 @@ static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
retval = malloc(sizeof (winCEfile));
retval = (winCEfile *) allocator.Malloc(sizeof (winCEfile));
if (retval == NULL)
{
CloseHandle(fileHandle);
@ -443,7 +441,7 @@ void *__PHYSFS_platformOpenAppend(const char *filename)
{
const char *err = win32strerror();
CloseHandle(h);
free(retval);
allocator.Free(retval);
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
@ -604,7 +602,7 @@ int __PHYSFS_platformClose(void *opaque)
{
HANDLE Handle = ((winCEfile *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
free(opaque);
allocator.Free(opaque);
return(1);
} /* __PHYSFS_platformClose */
@ -617,13 +615,13 @@ int __PHYSFS_platformDelete(const char *path)
if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
{
int retval=!RemoveDirectory(w_path);
free(w_path);
allocator.Free(w_path);
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* if */
else
{
int retval=!DeleteFile(w_path);
free(w_path);
allocator.Free(w_path);
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* else */
@ -660,5 +658,39 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
} /* __PHYSFS_platformGetLastModTime */
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
/* end of pocketpc.c ... */

View File

@ -59,7 +59,7 @@ char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
if (envr != NULL)
{
retval = (char *) malloc(strlen(envr) + 1);
retval = (char *) allocator.Malloc(strlen(envr) + 1);
if (retval != NULL)
strcpy(retval, envr);
} /* if */
@ -77,7 +77,7 @@ static char *getUserNameByUID(void)
pw = getpwuid(uid);
if ((pw != NULL) && (pw->pw_name != NULL))
{
retval = (char *) malloc(strlen(pw->pw_name) + 1);
retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
if (retval != NULL)
strcpy(retval, pw->pw_name);
} /* if */
@ -95,7 +95,7 @@ static char *getUserDirByUID(void)
pw = getpwuid(uid);
if ((pw != NULL) && (pw->pw_dir != NULL))
{
retval = (char *) malloc(strlen(pw->pw_dir) + 1);
retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
if (retval != NULL)
strcpy(retval, pw->pw_dir);
} /* if */
@ -204,7 +204,7 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend,
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = (char *) malloc(len);
char *retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -240,7 +240,7 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
{
dlen = strlen(dirname);
bufsize = dlen + 256;
buf = (char *) malloc(bufsize);
buf = (char *) allocator.Malloc(bufsize);
if (buf == NULL)
return;
strcpy(buf, dirname);
@ -256,7 +256,7 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
if (dir == NULL)
{
if (buf != NULL)
free(buf);
allocator.Free(buf);
return;
} /* if */
@ -274,7 +274,7 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
int len = strlen(ent->d_name) + dlen + 1;
if (len > bufsize)
{
p = realloc(buf, len);
p = (char *) allocator.Realloc(buf, len);
if (p == NULL)
continue;
buf = p;
@ -290,7 +290,7 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
} /* while */
if (buf != NULL)
free(buf);
allocator.Free(buf);
closedir(dir);
} /* __PHYSFS_platformEnumerateFiles */
@ -305,11 +305,11 @@ char *__PHYSFS_platformCurrentDir(void)
do
{
allocSize += 100;
ptr = (char *) realloc(retval, allocSize);
ptr = (char *) allocator.Realloc(retval, allocSize);
if (ptr == NULL)
{
if (retval != NULL)
free(retval);
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
@ -324,7 +324,7 @@ char *__PHYSFS_platformCurrentDir(void)
* directory not existing.
*/
if (retval != NULL)
free(retval);
allocator.Free(retval);
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* if */
@ -352,7 +352,7 @@ static void *doOpen(const char *filename, int mode)
fd = open(filename, mode, S_IRUSR | S_IWUSR);
BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
retval = (int *) malloc(sizeof (int));
retval = (int *) allocator.Malloc(sizeof (int));
if (retval == NULL)
{
close(fd);
@ -482,7 +482,7 @@ int __PHYSFS_platformClose(void *opaque)
{
int fd = *((int *) opaque);
BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
free(opaque);
allocator.Free(opaque);
return(1);
} /* __PHYSFS_platformClose */
@ -516,18 +516,21 @@ void __PHYSFS_platformAllocatorDeinit(void)
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */

View File

@ -233,5 +233,38 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
} /* __PHYSFS_platformGetLastModTime */
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
/* end of skeleton.c ... */

View File

@ -57,6 +57,10 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
/* Seems to get defined in some system header... */
#ifdef Free
#undef Free
#endif
const char *__PHYSFS_platformDirSeparator = "/";
@ -250,7 +254,7 @@ extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
* if it doesn't exist or there were other problems. PHYSFS_SetError() is
* called if we have a problem.
*
* (envr) will be scribbled over, and you are expected to free() the
* (envr) will be scribbled over, and you are expected to allocator.Free() the
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
@ -273,11 +277,11 @@ static char *findBinaryInPath(const char *bin, char *envr)
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
{
char *x = (char *) realloc(exe, size);
char *x = (char *) allocator.Realloc(exe, size);
if (x == NULL)
{
if (exe != NULL)
free(exe);
allocator.Free(exe);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
@ -301,7 +305,7 @@ static char *findBinaryInPath(const char *bin, char *envr)
} while (ptr != NULL);
if (exe != NULL)
free(exe);
allocator.Free(exe);
return(NULL); /* doesn't exist in path. */
} /* findBinaryInPath */
@ -320,7 +324,7 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0)
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
BAIL_IF_MACRO(!envr, NULL, NULL);
retval = findBinaryInPath(argv0, envr);
free(envr);
allocator.Free(envr);
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
@ -377,11 +381,14 @@ static void stripAppleBundle(char *path)
char *sub_str = "/contents/macos";
char *found_ptr = NULL;
char *tempbuf = NULL;
size_t len = strlen(path) + 1;
int i;
/* Calloc will place the \0 character in the proper place for us */
/* !!! FIXME: Can we stack-allocate this? --ryan. */
tempbuf = (char*)calloc( (strlen(path)+1), sizeof(char) );
tempbuf = (char *) allocator.Malloc(len);
if (!tempbuf) return;
memset(tempbuf, '\0', len);
/* Unlike other Unix filesystems, HFS is case insensitive
* It wouldn be nice to use strcasestr, but it doesn't seem
* to be available in the OSX gcc library right now.
@ -399,7 +406,7 @@ static void stripAppleBundle(char *path)
{
/* It doesn't look like a bundle so we can keep the
* original path. Just return */
free(tempbuf);
allocator.Free(tempbuf);
return;
}
/* We have a bundle, so let's backstep character by character
@ -424,7 +431,7 @@ static void stripAppleBundle(char *path)
/* Back up one more to remove trailing '/' and set the '\0' */
path[i] = '\0';
}
free(tempbuf);
allocator.Free(tempbuf);
return;
}
#endif /* defined __MACH__ && defined __APPLE__ */
@ -437,7 +444,7 @@ char *__PHYSFS_platformRealPath(const char *path)
errno = 0;
BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
retval = (char *) malloc(strlen(resolved_path) + 1);
retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, resolved_path);
@ -483,12 +490,13 @@ PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
void *__PHYSFS_platformCreateMutex(void)
{
int rc;
pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
pthread_mutex_t *m;
m = (pthread_mutex_t *) allocator.Malloc(sizeof (pthread_mutex_t));
BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
rc = pthread_mutex_init(m, NULL);
if (rc != 0)
{
free(m);
allocator.Free(m);
BAIL_MACRO(strerror(rc), NULL);
} /* if */
@ -499,7 +507,7 @@ void *__PHYSFS_platformCreateMutex(void)
void __PHYSFS_platformDestroyMutex(void *mutex)
{
pthread_mutex_destroy((pthread_mutex_t *) mutex);
free(mutex);
allocator.Free(mutex);
} /* __PHYSFS_platformDestroyMutex */

View File

@ -111,7 +111,7 @@ static char *getExePath(const char *argv0)
DWORD buflen;
int success = 0;
char *ptr = NULL;
char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
char *retval = (char *) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -159,12 +159,12 @@ static char *getExePath(const char *argv0)
if (!success)
{
free(retval);
allocator.Free(retval);
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
ptr = (char *) realloc(retval, strlen(retval) + 1);
ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr;
@ -221,12 +221,12 @@ static int determineUserDir(void)
assert(!rc); /* success?! */
/* Allocate memory for the profile directory */
userDir = (char *) malloc(psize);
userDir = (char *) allocator.Malloc(psize);
if (userDir != NULL)
{
if (!GetUserProfileDirectory(accessToken, userDir, &psize))
{
free(userDir);
allocator.Free(userDir);
userDir = NULL;
} /* if */
} /* else */
@ -297,12 +297,12 @@ char *__PHYSFS_platformGetUserName(void)
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
retval = (LPTSTR) malloc(bufsize);
retval = (LPTSTR) allocator.Malloc(bufsize);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
__PHYSFS_setError(win32strerror());
free(retval);
allocator.Free(retval);
retval = NULL;
} /* if */
} /* if */
@ -313,7 +313,7 @@ char *__PHYSFS_platformGetUserName(void)
char *__PHYSFS_platformGetUserDir(void)
{
char *retval = (char *) malloc(strlen(userDir) + 1);
char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, userDir); /* calculated at init time. */
return(retval);
@ -409,7 +409,7 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend,
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = malloc(len);
char *retval = (char *) allocator.Malloc(len);
char *p;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
@ -491,7 +491,7 @@ char *__PHYSFS_platformCurrentDir(void)
DWORD buflen = 0;
buflen = GetCurrentDirectory(buflen, NULL);
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
retval = (LPTSTR) allocator.Malloc(sizeof (TCHAR) * (buflen + 2));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
GetCurrentDirectory(buflen, retval);
@ -511,7 +511,7 @@ char *__PHYSFS_platformRealPath(const char *path)
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
retval = (char *) malloc(MAX_PATH);
retval = (char *) allocator.Malloc(MAX_PATH);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/*
@ -526,7 +526,7 @@ char *__PHYSFS_platformRealPath(const char *path)
char *currentDir = __PHYSFS_platformCurrentDir();
if (currentDir == NULL)
{
free(retval);
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
@ -572,7 +572,7 @@ char *__PHYSFS_platformRealPath(const char *path)
} /* else */
} /* else */
free(currentDir);
allocator.Free(currentDir);
} /* else */
/* (whew.) Ok, now take out "." and ".." path entries... */
@ -615,7 +615,7 @@ char *__PHYSFS_platformRealPath(const char *path)
} /* while */
/* shrink the retval's memory block if possible... */
p = (char *) realloc(retval, strlen(retval) + 1);
p = (char *) allocator.Realloc(retval, strlen(retval) + 1);
if (p != NULL)
retval = p;
@ -708,7 +708,7 @@ int __PHYSFS_platformDeinit(void)
{
if (userDir != NULL)
{
free(userDir);
allocator.Free(userDir);
userDir = NULL;
} /* if */
@ -736,7 +736,7 @@ static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
win32strerror(), NULL
);
retval = malloc(sizeof (win32file));
retval = (win32file *) allocator.Malloc(sizeof (win32file));
if (retval == NULL)
{
CloseHandle(fileHandle);
@ -772,7 +772,7 @@ void *__PHYSFS_platformOpenAppend(const char *filename)
{
const char *err = win32strerror();
CloseHandle(h);
free(retval);
allocator.Free(retval);
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
@ -946,7 +946,7 @@ int __PHYSFS_platformClose(void *opaque)
{
HANDLE Handle = ((win32file *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
free(opaque);
allocator.Free(opaque);
return(1);
} /* __PHYSFS_platformClose */
@ -1100,7 +1100,7 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
rc = GetFileTime(f->handle, NULL, NULL, &ft);
err = win32strerror();
CloseHandle(f->handle);
free(f);
allocator.Free(f);
BAIL_IF_MACRO(!rc, err, -1);
retval = FileTimeToPhysfsTime(&ft);
} /* if */
@ -1108,6 +1108,40 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
return(retval);
} /* __PHYSFS_platformGetLastModTime */
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
#endif
/* end of win32.c ... */