From 734f9d2c6b0d5c7a99a37785b9cf0d3fd7cb1862 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 20 May 2025 08:00:55 +1000 Subject: [PATCH] Make temp mem allocation more reliable --- src/Bitmap.c | 3 ++- src/Builder.c | 5 +++-- src/Core.h | 4 ---- src/Generator.c | 4 +++- src/Platform.h | 1 + src/_PlatformBase.h | 8 +++++++- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Bitmap.c b/src/Bitmap.c index 96d6b7989..36d341264 100644 --- a/src/Bitmap.c +++ b/src/Bitmap.c @@ -359,7 +359,8 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) { /* idat decompressor */ #if CC_BUILD_MAXSTACK <= (50 * 1024) - struct InflateState* inflate = (struct InflateState*)temp_mem; + void* mem = TempMem_Alloc(sizeof(struct InflateState)); + struct InflateState* inflate = (struct InflateState*)mem; #else struct InflateState _inflate; struct InflateState* inflate = &_inflate; diff --git a/src/Builder.c b/src/Builder.c index 17421e949..41e457710 100644 --- a/src/Builder.c +++ b/src/Builder.c @@ -361,8 +361,9 @@ static void OutputChunkPartsMeta(int x, int y, int z, struct ChunkInfo* info) { void Builder_MakeChunk(struct ChunkInfo* info) { #if CC_BUILD_MAXSTACK <= (32 * 1024) - BlockID* chunk = (cc_uint8*)temp_mem; - cc_uint8* counts = (cc_uint8*)temp_mem + EXTCHUNK_SIZE_3; + void* mem = TempMem_Alloc((EXTCHUNK_SIZE_3 * sizeof(BlockID)) + (CHUNK_SIZE_3 * FACE_COUNT)); + BlockID* chunk = (BlockID*)mem; + cc_uint8* counts = (cc_uint8*)(chunk + EXTCHUNK_SIZE_3); #else BlockID chunk[EXTCHUNK_SIZE_3]; cc_uint8 counts[CHUNK_SIZE_3 * FACE_COUNT]; diff --git a/src/Core.h b/src/Core.h index 9c5fe5123..2a55fd903 100644 --- a/src/Core.h +++ b/src/Core.h @@ -638,9 +638,5 @@ struct Texture { #define CC_END_HEADER #endif -#if CC_BUILD_MAXSTACK < (64 * 1024) -extern char temp_mem[45000]; -#endif - #endif diff --git a/src/Generator.c b/src/Generator.c index 83ce08d53..fed6f3e86 100644 --- a/src/Generator.c +++ b/src/Generator.c @@ -316,7 +316,9 @@ static void NotchyGen_CreateHeightmap(void) { struct CombinedNoise n1, n2; struct OctaveNoise n3; }; - struct NoiseBuffer* buf = (struct NoiseBuffer*)temp_mem; + void* mem = TempMem_Alloc(sizeof(struct NoiseBuffer)); + + struct NoiseBuffer* buf = (struct NoiseBuffer*)mem; struct CombinedNoise* n1 = &buf->n1; struct CombinedNoise* n2 = &buf->n2; struct OctaveNoise* n3 = &buf->n3; diff --git a/src/Platform.h b/src/Platform.h index f31dcdb9e..372b9c673 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -18,6 +18,7 @@ Copyright 2014-2025 ClassiCube | Licensed under BSD-3 /* Suffix added to app name sent to the server */ extern const char* Platform_AppNameSuffix; +void* TempMem_Alloc(int size); #if defined CC_BUILD_WIN typedef struct cc_winstring_ { diff --git a/src/_PlatformBase.h b/src/_PlatformBase.h index f8f525b2c..0800484ad 100644 --- a/src/_PlatformBase.h +++ b/src/_PlatformBase.h @@ -6,7 +6,13 @@ #include "Funcs.h" #if CC_BUILD_MAXSTACK < (64 * 1024) -CC_BIG_VAR char temp_mem[45000]; +static CC_BIG_VAR int temp_mem[45000 / 4]; + +void* TempMem_Alloc(int size) { + if (size > sizeof(temp_mem)) Process_Abort("TempMem overflow"); + + return (void*)temp_mem; +} #endif /*########################################################################################################################*