ok, I realized we need to realloc buffers allocated by FCEU_malloc (why didn't I guess that..) which makes the fact that they're aligned be horrible. so I added FCEU_amalloc and FCEU_afree instead to do aligned allocs and frees.

This commit is contained in:
zeromus 2022-08-28 04:31:34 -04:00
parent 1798fca76a
commit 9d831d8b8b
3 changed files with 61 additions and 37 deletions

View File

@ -28,18 +28,34 @@
#include "../fceu.h"
#include "memory.h"
void *FCEU_amalloc(size_t size, size_t alignment)
{
size = (size + alignment - 1) & ~(alignment-1);
#ifdef _MSC_VER
void *ret = _aligned_malloc(size,alignment);
#else
void *ret = aligned_alloc(alignment,size);
#endif
if(!ret)
FCEU_abort("Error allocating memory!");
return ret;
}
void FCEU_afree(void* ptr)
{
#ifdef _MSC_VER
_aligned_free(ptr);
#else
free(ptr);
#endif
}
static void *_FCEU_malloc(uint32 size)
{
//do not add an aligned allocation function. if a larger alignment is needed, change this constant to use it for all allocations.
static const int alignment = 32;
size = (size + alignment - 1) & ~(alignment-1);
#ifdef _MSC_VER
void *ret = _aligned_malloc(size,alignment);
#else
void *ret = aligned_alloc(alignment,size);
#endif
void* ret = malloc(size);
if(!ret)
FCEU_abort("Error allocating memory!");
@ -49,22 +65,17 @@ static void *_FCEU_malloc(uint32 size)
static void _FCEU_free(void* ptr)
{
#ifdef _MSC_VER
_aligned_free(ptr);
#else
free(ptr);
#endif
}
///allocates the specified number of bytes. exits process if this fails
void *FCEU_gmalloc(uint32 size)
{
void *ret = _FCEU_malloc(size);
void *ret = _FCEU_malloc(size);
// initialize according to RAMInitOption, default zero
FCEU_MemoryRand((uint8*)ret,size,true);
// initialize according to RAMInitOption, default zero
FCEU_MemoryRand((uint8*)ret,size,true);
return ret;
return ret;
}
void *FCEU_malloc(uint32 size)
@ -74,13 +85,11 @@ void *FCEU_malloc(uint32 size)
return ret;
}
//frees memory allocated with FCEU_gmalloc
void FCEU_gfree(void *ptr)
{
_FCEU_free(ptr);
}
//frees memory allocated with FCEU_malloc
void FCEU_free(void *ptr)
{
_FCEU_free(ptr);
@ -96,8 +105,13 @@ void FCEU_dfree(void *ptr)
return FCEU_free(ptr);
}
void FCEU_abort(const char* message)
{
void* FCEU_realloc(void* ptr, size_t size)
{
return realloc(ptr,size);
}
void FCEU_abort(const char* message)
{
if(message) FCEU_PrintError(message);
abort();
}
}

View File

@ -24,19 +24,29 @@
#define FCEU_dwmemset(d,c,n) {int _x; for(_x=n-4;_x>=0;_x-=4) *(uint32 *)&(d)[_x]=c;}
//returns a 32-aligned buffer, initialized to 0
void *FCEU_malloc(uint32 size);
//returns a buffer initialized to 0
void *FCEU_malloc(uint32 size);
//returns a 32-aligned buffer, with jumbled initial contents
//returns a buffer, with jumbled initial contents
//used by boards for WRAM etc, initialized to 0 (default) or other via RAMInitOption
void *FCEU_gmalloc(uint32 size);
void *FCEU_gmalloc(uint32 size);
//free memory allocated with FCEU_gmalloc
void FCEU_gfree(void *ptr);
//free memory allocated with
//returns an aligned buffer, initialized to 0
//the alignment will default to the largest thing you could ever sensibly want for massively aligned cache friendly buffers
void *FCEU_amalloc(size_t size, size_t alignment = 256);
//frees memory allocated with FCEU_amalloc
void FCEU_afree(void* ptr);
//free memory allocated with FCEU_malloc
void FCEU_free(void *ptr);
//reallocate memory allocated with FCEU_malloc
void* FCEU_realloc(void* ptr, size_t size);
//don't use these. change them if you find them.
void *FCEU_dmalloc(uint32 size);

View File

@ -91,19 +91,19 @@ void FCEU_KillVirtualVideo(void)
{
if ( XBuf )
{
FCEU_free(XBuf); XBuf = NULL;
FCEU_afree(XBuf); XBuf = NULL;
}
if ( XBackBuf )
{
FCEU_free(XBackBuf); XBackBuf = NULL;
FCEU_afree(XBackBuf); XBackBuf = NULL;
}
if ( XDBuf )
{
FCEU_free(XDBuf); XDBuf = NULL;
FCEU_afree(XDBuf); XDBuf = NULL;
}
if ( XDBackBuf )
{
FCEU_free(XDBackBuf); XDBackBuf = NULL;
FCEU_afree(XDBackBuf); XDBackBuf = NULL;
}
//printf("Video Core Cleanup\n");
}
@ -120,10 +120,10 @@ int FCEU_InitVirtualVideo(void)
if(XBuf)
return 1;
XBuf = (u8*)FCEU_malloc(256 * 256);
XBackBuf = (u8*)FCEU_malloc(256 * 256);
XDBuf = (u8*)FCEU_malloc(256 * 256);
XDBackBuf = (u8*)FCEU_malloc(256 * 256);
XBuf = (u8*)FCEU_amalloc(256 * 256);
XBackBuf = (u8*)FCEU_amalloc(256 * 256);
XDBuf = (u8*)FCEU_amalloc(256 * 256);
XDBackBuf = (u8*)FCEU_amalloc(256 * 256);
xbsave = XBuf;