diff --git a/src/Bitmap.c b/src/Bitmap.c index b37a731d7..3d445021e 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 189303003..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 = (BlockID*)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 566ebe02a..9c612f0cb 100644 --- a/src/Core.h +++ b/src/Core.h @@ -659,9 +659,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 241abac27..88ef2081c 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; @@ -567,7 +569,7 @@ static void NotchyGen_CreateSurfaceLayer(void) { struct NoiseBuffer { struct OctaveNoise n1, n2; }; - struct NoiseBuffer* buf = (struct NoiseBuffer*)temp_mem; + struct NoiseBuffer* buf = TempMem_Alloc(sizeof(struct NoiseBuffer)); struct OctaveNoise* n1 = &buf->n1; struct OctaveNoise* n2 = &buf->n2; #else 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 2dbf388e4..2249bff83 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 /*########################################################################################################################*