Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
All memory management now goes through allocation hooks instead of di…
…rectly to

 C runtime malloc/realloc/free...
  • Loading branch information
icculus committed Mar 14, 2005
1 parent 4787894 commit d840403
Show file tree
Hide file tree
Showing 19 changed files with 419 additions and 260 deletions.
22 changes: 11 additions & 11 deletions archivers/dir.c
Expand Up @@ -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 */
Expand All @@ -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 */

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

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

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

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

Expand All @@ -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 */
Expand Down Expand Up @@ -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 */

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


Expand Down
24 changes: 12 additions & 12 deletions archivers/grp.c
Expand Up @@ -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 */


Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -284,10 +284,10 @@ static void *GRP_openArchive(const char *name, int forWriting)
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);
Expand Down Expand Up @@ -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 */

Expand Down
24 changes: 12 additions & 12 deletions archivers/hog.c
Expand Up @@ -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 */


Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -323,10 +323,10 @@ static void *HOG_openArchive(const char *name, int forWriting)
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);
Expand Down Expand Up @@ -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 */

Expand Down
22 changes: 11 additions & 11 deletions archivers/mix.c
Expand Up @@ -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 */


Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -274,10 +274,10 @@ static void *MIX_openArchive(const char *name, int forWriting)
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)
Expand Down Expand Up @@ -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 */

Expand Down
24 changes: 12 additions & 12 deletions archivers/mvl.c
Expand Up @@ -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 */


Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -280,10 +280,10 @@ static void *MVL_openArchive(const char *name, int forWriting)
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);
Expand Down Expand Up @@ -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 */

Expand Down

0 comments on commit d840403

Please sign in to comment.