Since all the platform layers were using the same cut-and-paste of the
malloc/realloc/free allocator, I moved it into physfs.c as a default, which is used if the platform layer doesn't offer a platform-specific default allocator, which none do at this point, but will soon.
This commit is contained in:
parent
bb1d757244
commit
857d9bc84d
36
physfs.c
36
physfs.c
|
@ -2029,14 +2029,40 @@ int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
|
||||||
} /* PHYSFS_setAllocator */
|
} /* PHYSFS_setAllocator */
|
||||||
|
|
||||||
|
|
||||||
|
static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
|
||||||
|
{
|
||||||
|
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
||||||
|
#undef malloc
|
||||||
|
return(malloc((size_t) s));
|
||||||
|
} /* mallocAllocatorMalloc */
|
||||||
|
|
||||||
|
|
||||||
|
static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
||||||
|
{
|
||||||
|
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
||||||
|
#undef realloc
|
||||||
|
return(realloc(ptr, (size_t) s));
|
||||||
|
} /* mallocAllocatorRealloc */
|
||||||
|
|
||||||
|
|
||||||
|
static void mallocAllocatorFree(void *ptr)
|
||||||
|
{
|
||||||
|
#undef free
|
||||||
|
free(ptr);
|
||||||
|
} /* mallocAllocatorFree */
|
||||||
|
|
||||||
|
|
||||||
static void setDefaultAllocator(void)
|
static void setDefaultAllocator(void)
|
||||||
{
|
{
|
||||||
assert(!externalAllocator);
|
assert(!externalAllocator);
|
||||||
allocator.Init = __PHYSFS_platformAllocatorInit;
|
if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
|
||||||
allocator.Deinit = __PHYSFS_platformAllocatorDeinit;
|
{
|
||||||
allocator.Malloc = __PHYSFS_platformAllocatorMalloc;
|
allocator.Init = NULL;
|
||||||
allocator.Realloc = __PHYSFS_platformAllocatorRealloc;
|
allocator.Deinit = NULL;
|
||||||
allocator.Free = __PHYSFS_platformAllocatorFree;
|
allocator.Malloc = mallocAllocatorMalloc;
|
||||||
|
allocator.Realloc = mallocAllocatorRealloc;
|
||||||
|
allocator.Free = mallocAllocatorFree;
|
||||||
|
} /* if */
|
||||||
} /* setDefaultAllocator */
|
} /* setDefaultAllocator */
|
||||||
|
|
||||||
/* end of physfs.c ... */
|
/* end of physfs.c ... */
|
||||||
|
|
|
@ -1719,39 +1719,18 @@ int __PHYSFS_platformGrabMutex(void *mutex);
|
||||||
void __PHYSFS_platformReleaseMutex(void *mutex);
|
void __PHYSFS_platformReleaseMutex(void *mutex);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Called during PHYSFS_init() to initialize the allocator, if the user
|
* Called at the start of PHYSFS_init() to prepare the allocator, if the user
|
||||||
* hasn't selected their own allocator via PHYSFS_setAllocator().
|
* hasn't selected their own allocator via PHYSFS_setAllocator().
|
||||||
* Return zero on initialization error (which will make PHYSFS_init() fail,
|
* If the platform has a custom allocator, it should fill in the fields of
|
||||||
* too), non-zero on success.
|
* (a) with the proper function pointers and return non-zero.
|
||||||
|
* If the platform just wants to use malloc()/free()/etc, return zero
|
||||||
|
* immediately and the higher level will handle it. The Init and Deinit
|
||||||
|
* fields of (a) are optional...set them to NULL if you don't need them.
|
||||||
|
* Everything else must be implemented. All rules follow those for
|
||||||
|
* PHYSFS_setAllocator(). If Init isn't NULL, it will be called shortly
|
||||||
|
* after this function returns non-zero.
|
||||||
*/
|
*/
|
||||||
int __PHYSFS_platformAllocatorInit(void);
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a);
|
||||||
|
|
||||||
/*
|
|
||||||
* Called during PHYSFS_deinit() to deinitialize the allocator, if the user
|
|
||||||
* hasn't selected their own allocator via PHYSFS_setAllocator().
|
|
||||||
*/
|
|
||||||
void __PHYSFS_platformAllocatorDeinit(void);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Implement malloc. It's safe to just pass through from the C runtime.
|
|
||||||
* This is used for allocation if the user hasn't selected their own
|
|
||||||
* allocator via PHYSFS_setAllocator().
|
|
||||||
*/
|
|
||||||
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Implement realloc. It's safe to just pass through from the C runtime.
|
|
||||||
* This is used for allocation if the user hasn't selected their own
|
|
||||||
* allocator via PHYSFS_setAllocator().
|
|
||||||
*/
|
|
||||||
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Implement free. It's safe to just pass through from the C runtime.
|
|
||||||
* This is used for deallocation if the user hasn't selected their own
|
|
||||||
* allocator via PHYSFS_setAllocator().
|
|
||||||
*/
|
|
||||||
void __PHYSFS_platformAllocatorFree(void *ptr);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,6 +240,12 @@ void __PHYSFS_platformReleaseMutex(void *mutex)
|
||||||
release_sem(*((sem_id *) mutex));
|
release_sem(*((sem_id *) mutex));
|
||||||
} /* __PHYSFS_platformReleaseMutex */
|
} /* __PHYSFS_platformReleaseMutex */
|
||||||
|
|
||||||
|
|
||||||
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
||||||
|
{
|
||||||
|
return(0); /* just use malloc() and friends. */
|
||||||
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
||||||
|
|
||||||
#endif /* PHYSFS_PLATFORM_BEOS */
|
#endif /* PHYSFS_PLATFORM_BEOS */
|
||||||
|
|
||||||
/* end of beos.cpp ... */
|
/* end of beos.cpp ... */
|
||||||
|
|
|
@ -690,39 +690,11 @@ void __PHYSFS_platformReleaseMutex(void *mutex)
|
||||||
} /* __PHYSFS_platformReleaseMutex */
|
} /* __PHYSFS_platformReleaseMutex */
|
||||||
|
|
||||||
|
|
||||||
int __PHYSFS_platformAllocatorInit(void)
|
/* !!! FIXME: Don't use C runtime for allocators? */
|
||||||
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
||||||
{
|
{
|
||||||
return(1); /* always succeeds. */
|
return(0); /* just use malloc() and friends. */
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorDeinit(void)
|
|
||||||
{
|
|
||||||
/* no-op */
|
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef malloc
|
|
||||||
return(malloc((size_t) s));
|
|
||||||
} /* __PHYSFS_platformMalloc */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef realloc
|
|
||||||
return(realloc(ptr, (size_t) s));
|
|
||||||
} /* __PHYSFS_platformRealloc */
|
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorFree(void *ptr)
|
|
||||||
{
|
|
||||||
#undef free
|
|
||||||
free(ptr);
|
|
||||||
} /* __PHYSFS_platformAllocatorFree */
|
|
||||||
|
|
||||||
#endif /* PHYSFS_PLATFORM_OS2 */
|
#endif /* PHYSFS_PLATFORM_OS2 */
|
||||||
|
|
||||||
|
|
|
@ -576,39 +576,10 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
|
||||||
|
|
||||||
|
|
||||||
/* !!! FIXME: Don't use C runtime for allocators? */
|
/* !!! FIXME: Don't use C runtime for allocators? */
|
||||||
int __PHYSFS_platformAllocatorInit(void)
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
||||||
{
|
{
|
||||||
return(1); /* always succeeds. */
|
return(0); /* just use malloc() and friends. */
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorDeinit(void)
|
|
||||||
{
|
|
||||||
/* no-op */
|
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef malloc
|
|
||||||
return(malloc((size_t) s));
|
|
||||||
} /* __PHYSFS_platformMalloc */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef realloc
|
|
||||||
return(realloc(ptr, (size_t) s));
|
|
||||||
} /* __PHYSFS_platformRealloc */
|
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorFree(void *ptr)
|
|
||||||
{
|
|
||||||
#undef free
|
|
||||||
free(ptr);
|
|
||||||
} /* __PHYSFS_platformAllocatorFree */
|
|
||||||
|
|
||||||
#endif /* PHYSFS_PLATFORM_POCKETPC */
|
#endif /* PHYSFS_PLATFORM_POCKETPC */
|
||||||
|
|
||||||
|
|
|
@ -429,41 +429,6 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
|
||||||
return statbuf.st_mtime;
|
return statbuf.st_mtime;
|
||||||
} /* __PHYSFS_platformGetLastModTime */
|
} /* __PHYSFS_platformGetLastModTime */
|
||||||
|
|
||||||
|
|
||||||
int __PHYSFS_platformAllocatorInit(void)
|
|
||||||
{
|
|
||||||
return(1); /* always succeeds. */
|
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorDeinit(void)
|
|
||||||
{
|
|
||||||
/* no-op */
|
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef malloc
|
|
||||||
return(malloc((size_t) s));
|
|
||||||
} /* __PHYSFS_platformMalloc */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef realloc
|
|
||||||
return(realloc(ptr, (size_t) s));
|
|
||||||
} /* __PHYSFS_platformRealloc */
|
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorFree(void *ptr)
|
|
||||||
{
|
|
||||||
#undef free
|
|
||||||
free(ptr);
|
|
||||||
} /* __PHYSFS_platformAllocatorFree */
|
|
||||||
|
|
||||||
#endif /* PHYSFS_PLATFORM_POSIX */
|
#endif /* PHYSFS_PLATFORM_POSIX */
|
||||||
|
|
||||||
/* end of posix.c ... */
|
/* end of posix.c ... */
|
||||||
|
|
|
@ -471,6 +471,12 @@ char *__PHYSFS_platformRealPath(const char *path)
|
||||||
} /* __PHYSFS_platformRealPath */
|
} /* __PHYSFS_platformRealPath */
|
||||||
|
|
||||||
|
|
||||||
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
||||||
|
{
|
||||||
|
return(0); /* just use malloc() and friends. */
|
||||||
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
||||||
|
|
||||||
|
|
||||||
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
|
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
|
||||||
|
|
||||||
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
|
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
|
||||||
|
|
|
@ -1051,39 +1051,10 @@ PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
|
||||||
|
|
||||||
|
|
||||||
/* !!! FIXME: Don't use C runtime for allocators? */
|
/* !!! FIXME: Don't use C runtime for allocators? */
|
||||||
int __PHYSFS_platformAllocatorInit(void)
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
||||||
{
|
{
|
||||||
return(1); /* always succeeds. */
|
return(0); /* just use malloc() and friends. */
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorDeinit(void)
|
|
||||||
{
|
|
||||||
/* no-op */
|
|
||||||
} /* __PHYSFS_platformAllocatorInit */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef malloc
|
|
||||||
return(malloc((size_t) s));
|
|
||||||
} /* __PHYSFS_platformMalloc */
|
|
||||||
|
|
||||||
|
|
||||||
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
|
||||||
{
|
|
||||||
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
#undef realloc
|
|
||||||
return(realloc(ptr, (size_t) s));
|
|
||||||
} /* __PHYSFS_platformRealloc */
|
|
||||||
|
|
||||||
|
|
||||||
void __PHYSFS_platformAllocatorFree(void *ptr)
|
|
||||||
{
|
|
||||||
#undef free
|
|
||||||
free(ptr);
|
|
||||||
} /* __PHYSFS_platformAllocatorFree */
|
|
||||||
|
|
||||||
#endif /* PHYSFS_PLATFORM_WINDOWS */
|
#endif /* PHYSFS_PLATFORM_WINDOWS */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue