diff options
author | zeromus <zeromus@users.noreply.github.com> | 2022-08-28 11:31:34 +0300 |
---|---|---|
committer | zeromus <zeromus@users.noreply.github.com> | 2022-08-28 11:31:34 +0300 |
commit | 9d831d8b8b647f0e7a84cd0ebb4baea4ba22f596 (patch) | |
tree | c35be41b3e30c6dd67eb2b34ce68c7119cd8d126 | |
parent | 1798fca76a3ffc8d22318e445d10fbb7b63a762b (diff) |
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.
-rw-r--r-- | src/utils/memory.cpp | 62 | ||||
-rw-r--r-- | src/utils/memory.h | 20 | ||||
-rw-r--r-- | src/video.cpp | 16 |
3 files changed, 61 insertions, 37 deletions
diff --git a/src/utils/memory.cpp b/src/utils/memory.cpp index 93bc3af9..fa5d65b4 100644 --- a/src/utils/memory.cpp +++ b/src/utils/memory.cpp @@ -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(); -}
+} diff --git a/src/utils/memory.h b/src/utils/memory.h index b9e6f145..4fd97d9d 100644 --- a/src/utils/memory.h +++ b/src/utils/memory.h @@ -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); diff --git a/src/video.cpp b/src/video.cpp index cbad58fc..20a48e76 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -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;
|