From 098136160ea7adb5c9647f2fdbed203e3ba723a0 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 28 Jul 2018 06:08:39 +1000 Subject: [PATCH] now get up to setup header before dying --- .gitignore | 1 + src/Client/Animations.c | 3 +-- src/Client/Audio.c | 9 +++++++- src/Client/Audio.h | 1 + src/Client/Bitmap.c | 10 +++------ src/Client/BlockPhysics.c | 7 ++----- src/Client/Builder.c | 4 +--- src/Client/ChunkUpdater.c | 22 ++++++-------------- src/Client/D3D9Api.c | 3 +-- src/Client/EnvRenderer.c | 41 ++++++++++++++++--------------------- src/Client/Formats.c | 14 ++++--------- src/Client/Lighting.c | 3 +-- src/Client/MapGenerator.c | 10 ++------- src/Client/NixPlatform.c | 26 +++++++++++++++++------ src/Client/OpenGLApi.c | 3 +-- src/Client/PacketHandlers.c | 6 ++---- src/Client/Physics.c | 6 +----- src/Client/Platform.h | 6 +++--- src/Client/Program.c | 14 +++++++++++++ src/Client/String.c | 6 ++---- src/Client/WinPlatform.c | 30 +++++++++++++++++++-------- 21 files changed, 113 insertions(+), 112 deletions(-) diff --git a/.gitignore b/.gitignore index a1547d034..d4bc1a7d6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ bld/ [Oo]bj/ [Oo]utput/ [Pp]rofilingSessions/ +[sS]rc/Client/audio [sS]rc/Client/texpacks [sS]rc/Client/maps [sS]rc/Client/texturecache diff --git a/src/Client/Animations.c b/src/Client/Animations.c index 899a39df5..34a4ae4dc 100644 --- a/src/Client/Animations.c +++ b/src/Client/Animations.c @@ -202,8 +202,7 @@ static void Animations_Draw(struct AnimationData* data, TextureLoc texLoc, Int32 UInt8* ptr = buffer; if (size > ANIMS_FAST_SIZE) { /* cannot allocate memory on the stack for very big animation.png frames */ - ptr = Platform_MemAlloc(size * size, BITMAP_SIZEOF_PIXEL); - if (!ptr) ErrorHandler_Fail("Failed to allocate memory for anim frame"); + ptr = Platform_MemAlloc(size * size, BITMAP_SIZEOF_PIXEL, "anim frame"); } Int32 index_1D = Atlas1D_Index(texLoc); diff --git a/src/Client/Audio.c b/src/Client/Audio.c index c649f5aac..34dec90d1 100644 --- a/src/Client/Audio.c +++ b/src/Client/Audio.c @@ -85,6 +85,7 @@ void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source) /* Ensures there are 'bitsCount' bits */ #define Vorbis_EnsureBits(state, bitsCount) while (state->NumBits < bitsCount) { Vorbis_GetByte(state); } /* Peeks then consumes given bits */ +/* TODO: COMPLETELY BROKEN */ #define Vorbis_ReadBits(state, bitsCount) Vorbis_PeekBits(state, bitsCount); Vorbis_ConsumeBits(state, bitsCount); #define VORBIS_MAX_CHANS 8 @@ -130,7 +131,7 @@ UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { } UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { - UInt32 i, j; + UInt32 i; /* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */ /* TODO: verify this */ for (i = 1; ; i++) { @@ -388,6 +389,7 @@ static ReturnCode Vorbis_DecodeHeader(struct VorbisState* state, UInt8 type) { static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* state) { UInt8 header[23]; + Stream_Read(state->Source, header, sizeof(header)); UInt32 version = Stream_GetU32_LE(&header[0]); if (version != 0) return VORBIS_ERR_VERSION; @@ -424,6 +426,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { ReturnCode result; count = Vorbis_ReadBits(state, 8); count++; + state->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks"); for (i = 0; i < count; i++) { result = Codebook_DecodeSetup(state, &state->Codebooks[i]); if (result) return result; @@ -436,6 +439,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { } count = Vorbis_ReadBits(state, 6); count++; + state->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors"); for (i = 0; i < count; i++) { UInt16 floor = Vorbis_ReadBits(state, 16); if (floor != 1) return VORBIS_ERR_FLOOR_TYPE; @@ -444,6 +448,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { } count = Vorbis_ReadBits(state, 6); count++; + state->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues"); for (i = 0; i < count; i++) { UInt16 residue = Vorbis_ReadBits(state, 16); if (residue > 2) return VORBIS_ERR_FLOOR_TYPE; @@ -452,6 +457,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { } count = Vorbis_ReadBits(state, 6); count++; + state->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings"); for (i = 0; i < count; i++) { UInt16 mapping = Vorbis_ReadBits(state, 16); if (mapping != 0) return VORBIS_ERR_FLOOR_TYPE; @@ -460,6 +466,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { } count = Vorbis_ReadBits(state, 6); count++; + state->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes"); for (i = 0; i < count; i++) { result = Mode_DecodeSetup(state, &state->Modes[i]); if (result) return result; diff --git a/src/Client/Audio.h b/src/Client/Audio.h index 85217ac5a..25386f144 100644 --- a/src/Client/Audio.h +++ b/src/Client/Audio.h @@ -43,4 +43,5 @@ struct VorbisState { struct Mode* Modes; }; void Vorbis_Init(struct VorbisState* state, struct Stream* source); +ReturnCode Vorbis_DecodeHeaders(struct VorbisState* state); #endif diff --git a/src/Client/Bitmap.c b/src/Client/Bitmap.c index 54ddccf92..8188c2501 100644 --- a/src/Client/Bitmap.c +++ b/src/Client/Bitmap.c @@ -23,8 +23,7 @@ void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, struct Bit void Bitmap_Allocate(struct Bitmap* bmp, Int32 width, Int32 height) { bmp->Width = width; bmp->Height = height; - bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); - if (!bmp->Scan0) ErrorHandler_Fail("Bitmap - failed to allocate memory"); + bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "bitmap data"); } void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) { @@ -32,8 +31,7 @@ void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) { height = Math_NextPowOf2(height); bmp->Width = width; bmp->Height = height; - bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL); - if (!bmp->Scan0) ErrorHandler_Fail("Bitmap - failed to allocate memory"); + bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL, "bitmap data"); } @@ -369,9 +367,7 @@ ReturnCode Bitmap_DecodePng(struct Bitmap* bmp, struct Stream* stream) { if (bmp->Width < 0 || bmp->Width > PNG_MAX_DIMS) return PNG_ERR_TOO_WIDE; if (bmp->Height < 0 || bmp->Height > PNG_MAX_DIMS) return PNG_ERR_TOO_TALL; - bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL); - if (!bmp->Scan0) ErrorHandler_Fail("Failed to allocate memory for PNG bitmap"); - + bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL, "PNG bitmap data"); bitsPerSample = buffer[8]; col = buffer[9]; rowExpander = Png_GetExpander(col, bitsPerSample); if (rowExpander == NULL) return PNG_ERR_INVALID_COL_BPP; diff --git a/src/Client/BlockPhysics.c b/src/Client/BlockPhysics.c index 50814206b..d37c5743d 100644 --- a/src/Client/BlockPhysics.c +++ b/src/Client/BlockPhysics.c @@ -42,13 +42,10 @@ static void TickQueue_Resize(struct TickQueue* queue) { if (queue->BufferSize >= (Int32_MaxValue / 4)) { ErrorHandler_Fail("TickQueue - too large to resize."); } + UInt32 capacity = queue->BufferSize * 2; if (capacity < 32) capacity = 32; - - UInt32* newBuffer = Platform_MemAlloc(capacity, sizeof(UInt32)); - if (!newBuffer) { - ErrorHandler_Fail("TickQueue - failed to allocate memory"); - } + UInt32* newBuffer = Platform_MemAlloc(capacity, sizeof(UInt32), "physics tick queue"); UInt32 i, idx; for (i = 0; i < queue->Size; i++) { diff --git a/src/Client/Builder.c b/src/Client/Builder.c index 076c5d8b7..9b477da53 100644 --- a/src/Client/Builder.c +++ b/src/Client/Builder.c @@ -368,10 +368,8 @@ static void Builder_DefaultPostStretchTiles(Int32 x1, Int32 y1, Int32 z1) { if (vertsCount > Builder_VerticesElems) { Platform_MemFree(&Builder_Vertices); /* ensure buffer can be accessed with 64 bytes alignment by putting 2 extra vertices at end. */ - Builder_Vertices = Platform_MemAlloc(vertsCount + 2, sizeof(VertexP3fT2fC4b)); + Builder_Vertices = Platform_MemAlloc(vertsCount + 2, sizeof(VertexP3fT2fC4b), "chunk vertices"); Builder_VerticesElems = vertsCount; - - if (!Builder_Vertices) ErrorHandler_Fail("Builder1DPart_Prepare - failed to allocate memory"); } vertsCount = 0; diff --git a/src/Client/ChunkUpdater.c b/src/Client/ChunkUpdater.c index d84c51168..50ee7e37e 100644 --- a/src/Client/ChunkUpdater.c +++ b/src/Client/ChunkUpdater.c @@ -95,26 +95,16 @@ static void ChunkUpdater_FreeAllocations(void) { static void ChunkUpdater_PerformPartsAllocations(void) { UInt32 count = MapRenderer_ChunksCount * MapRenderer_1DUsedCount; - MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo)); - if (!MapRenderer_PartsBuffer_Raw) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts"); - - MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw; + MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo), "chunk parts"); + MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw; MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + count; } static void ChunkUpdater_PerformAllocations(void) { - MapRenderer_Chunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo)); - if (!MapRenderer_Chunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk info"); - - MapRenderer_SortedChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*)); - if (!MapRenderer_Chunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate sorted chunk info"); - - MapRenderer_RenderChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*)); - if (!MapRenderer_RenderChunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate render chunk info"); - - ChunkUpdater_Distances = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(Int32)); - if (!ChunkUpdater_Distances) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk distances"); - + MapRenderer_Chunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo), "chunk info"); + MapRenderer_SortedChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*), "sorted chunk info"); + MapRenderer_RenderChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*), "render chunk info"); + ChunkUpdater_Distances = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(Int32), "chunk distances"); ChunkUpdater_PerformPartsAllocations(); } diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index d1ca37a52..41d70a07a 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -202,8 +202,7 @@ static void D3D9_DoMipmaps(IDirect3DTexture9* texture, Int32 x, Int32 y, struct if (width > 1) width /= 2; if (height > 1) height /= 2; - UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); - if (!cur) ErrorHandler_Fail("Allocating memory for mipmaps"); + UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "mipmaps"); GfxCommon_GenMipmaps(width, height, cur, prev); struct Bitmap mipmap; diff --git a/src/Client/EnvRenderer.c b/src/Client/EnvRenderer.c index ea24579f7..1bbe798f3 100644 --- a/src/Client/EnvRenderer.c +++ b/src/Client/EnvRenderer.c @@ -19,6 +19,7 @@ #include "Camera.h" #include "Particle.h" +#define ENV_SMALL_VERTICES 4096 static Real32 EnvRenderer_BlendFactor(Real32 x) { /* return -0.05 + 0.22 * (Math_Log(x) * 0.25f); */ Real64 blend = -0.13 + 0.28 * (Math_Log(x) * 0.25); @@ -176,17 +177,16 @@ static void EnvRenderer_UpdateClouds(void) { Int32 z1 = -extent, z2 = World_Length + extent; clouds_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); - VertexP3fT2fC4b v[4096]; + VertexP3fT2fC4b v[ENV_SMALL_VERTICES]; VertexP3fT2fC4b* ptr = v; - if (clouds_vertices > 4096) { - ptr = Platform_MemAlloc(clouds_vertices, sizeof(VertexP3fT2fC4b)); - if (!ptr) ErrorHandler_Fail("EnvRenderer_Clouds - failed to allocate memory"); + if (clouds_vertices > ENV_SMALL_VERTICES) { + ptr = Platform_MemAlloc(clouds_vertices, sizeof(VertexP3fT2fC4b), "temp clouds vertices"); } EnvRenderer_DrawCloudsY(x1, z1, x2, z2, WorldEnv_CloudsHeight, ptr); clouds_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, clouds_vertices); - if (clouds_vertices > 4096) Platform_MemFree(&ptr); + if (clouds_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr); } @@ -249,18 +249,17 @@ static void EnvRenderer_UpdateSky(void) { Int32 z1 = -extent, z2 = World_Length + extent; sky_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); - VertexP3fC4b v[4096]; + VertexP3fC4b v[ENV_SMALL_VERTICES]; VertexP3fC4b* ptr = v; - if (sky_vertices > 4096) { - ptr = Platform_MemAlloc(sky_vertices, sizeof(VertexP3fC4b)); - if (!ptr) ErrorHandler_Fail("EnvRenderer_Sky - failed to allocate memory"); + if (sky_vertices > ENV_SMALL_VERTICES) { + ptr = Platform_MemAlloc(sky_vertices, sizeof(VertexP3fC4b), "temp sky vertices"); } Int32 height = max((World_Height + 2) + 6, WorldEnv_CloudsHeight + 6); EnvRenderer_DrawSkyY(x1, z1, x2, z2, height, ptr); sky_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FC4B, sky_vertices); - if (sky_vertices > 4096) Platform_MemFree(&ptr); + if (sky_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr); } /*########################################################################################################################* @@ -342,9 +341,7 @@ Real64 weather_accumulator; Vector3I weather_lastPos; static void EnvRenderer_InitWeatherHeightmap(void) { - Weather_Heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16)); - if (!Weather_Heightmap) ErrorHandler_Fail("WeatherRenderer - Failed to allocate heightmap"); - + Weather_Heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16), "weather heightmap"); Int32 i; for (i = 0; i < World_Width * World_Length; i++) { Weather_Heightmap[i] = Int16_MaxValue; @@ -646,11 +643,10 @@ static void EnvRenderer_UpdateMapSides(void) { sides_vertices += 2 * EnvRenderer_Vertices(World_Width, Math_AbsI(y)); /* ZQuads */ sides_vertices += 2 * EnvRenderer_Vertices(World_Length, Math_AbsI(y)); /* XQuads */ - VertexP3fT2fC4b v[4096]; + VertexP3fT2fC4b v[ENV_SMALL_VERTICES]; VertexP3fT2fC4b* ptr = v; - if (sides_vertices > 4096) { - ptr = Platform_MemAlloc(sides_vertices, sizeof(VertexP3fT2fC4b)); - if (!ptr) ErrorHandler_Fail("BordersRenderer_Sides - failed to allocate memory"); + if (sides_vertices > ENV_SMALL_VERTICES) { + ptr = Platform_MemAlloc(sides_vertices, sizeof(VertexP3fT2fC4b), "temp sides vertices"); } VertexP3fT2fC4b* temp = ptr; @@ -675,7 +671,7 @@ static void EnvRenderer_UpdateMapSides(void) { EnvRenderer_DrawBorderX(World_Width, 0, World_Length, y1, y2, col, &temp); sides_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, sides_vertices); - if (sides_vertices > 4096) Platform_MemFree(&ptr); + if (sides_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr); } static void EnvRenderer_UpdateMapEdges(void) { @@ -693,11 +689,10 @@ static void EnvRenderer_UpdateMapEdges(void) { edges_vertices += EnvRenderer_Vertices(r.Width, r.Height); /* YPlanes outside */ } - VertexP3fT2fC4b v[4096]; + VertexP3fT2fC4b v[ENV_SMALL_VERTICES]; VertexP3fT2fC4b* ptr = v; - if (edges_vertices > 4096) { - ptr = Platform_MemAlloc(edges_vertices, sizeof(VertexP3fT2fC4b)); - if (!ptr) ErrorHandler_Fail("BordersRenderer_Edges - failed to allocate memory"); + if (edges_vertices > ENV_SMALL_VERTICES) { + ptr = Platform_MemAlloc(edges_vertices, sizeof(VertexP3fT2fC4b), "temp edge vertices"); } VertexP3fT2fC4b* temp = ptr; @@ -714,7 +709,7 @@ static void EnvRenderer_UpdateMapEdges(void) { } edges_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, edges_vertices); - if (edges_vertices > 4096) Platform_MemFree(&ptr); + if (edges_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr); } diff --git a/src/Client/Formats.c b/src/Client/Formats.c index 5b250f658..50083ebc4 100644 --- a/src/Client/Formats.c +++ b/src/Client/Formats.c @@ -13,10 +13,7 @@ static void Map_ReadBlocks(struct Stream* stream) { World_BlocksSize = World_Width * World_Length * World_Height; - World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(BlockID)); - if (!World_Blocks) { - ErrorHandler_Fail("Failed to allocate memory for reading blocks array from file"); - } + World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(BlockID), "map blocks for load"); Stream_Read(stream, World_Blocks, World_BlocksSize); } @@ -294,8 +291,7 @@ static void Nbt_ReadTag(UInt8 typeId, bool readTagName, struct Stream* stream, s if (count < NBT_SMALL_SIZE) { Stream_Read(stream, tag.DataSmall, count); } else { - tag.DataBig = Platform_MemAlloc(count, sizeof(UInt8)); - if (!tag.DataBig) ErrorHandler_Fail("Nbt_ReadTag - allocating memory"); + tag.DataBig = Platform_MemAlloc(count, sizeof(UInt8), "NBT tag data"); Stream_Read(stream, tag.DataBig, count); } break; @@ -379,8 +375,7 @@ static bool Cw_Callback_1(struct NbtTag* tag) { if (IsTag(tag, "BlockArray")) { World_BlocksSize = tag->DataSize; if (tag->DataSize < NBT_SMALL_SIZE) { - World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(UInt8)); - if (!World_Blocks) ErrorHandler_Fail("Failed to allocate memory for map"); + World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(UInt8), ".cw map blocks"); Platform_MemCpy(World_Blocks, tag->DataSmall, tag->DataSize); } else { World_Blocks = tag->DataBig; @@ -706,8 +701,7 @@ static void Dat_ReadFieldData(struct Stream* stream, struct JFieldDesc* field) { if (arrayClassDesc.ClassName[1] != JFIELD_INT8) ErrorHandler_Fail("Only byte array fields supported"); UInt32 size = Stream_ReadU32_BE(stream); - field->Value_Ptr = Platform_MemAlloc(size, sizeof(UInt8)); - if (!field->Value_Ptr) ErrorHandler_Fail("Failed to allocate memory for map"); + field->Value_Ptr = Platform_MemAlloc(size, sizeof(UInt8), ".dat map blocks"); Stream_Read(stream, field->Value_Ptr, size); field->Value_Size = size; diff --git a/src/Client/Lighting.c b/src/Client/Lighting.c index 6cefc0faa..18fa8b4f5 100644 --- a/src/Client/Lighting.c +++ b/src/Client/Lighting.c @@ -332,8 +332,7 @@ static void Lighting_OnNewMap(void) { } static void Lighting_OnNewMapLoaded(void) { - Lighting_heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16)); - if (!Lighting_heightmap) ErrorHandler_Fail("WorldLighting - failed to allocate heightmap"); + Lighting_heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16), "lighting heightmap"); Lighting_Refresh(); } diff --git a/src/Client/MapGenerator.c b/src/Client/MapGenerator.c index bf13b17c3..0c879c482 100644 --- a/src/Client/MapGenerator.c +++ b/src/Client/MapGenerator.c @@ -15,10 +15,7 @@ static void Gen_Init(void) { Gen_CurrentProgress = 0.0f; Gen_CurrentState = ""; - Gen_Blocks = Platform_MemAlloc(Gen_Width * Gen_Height * Gen_Length, sizeof(BlockID)); - if (!Gen_Blocks) { - ErrorHandler_Fail("MapGen - failed to allocate Blocks array"); - } + Gen_Blocks = Platform_MemAlloc(Gen_Width * Gen_Height * Gen_Length, sizeof(BlockID), "map blocks for gen"); Gen_Done = false; } @@ -470,10 +467,7 @@ static void NotchyGen_PlantTrees(void) { void NotchyGen_Generate(void) { Gen_Init(); - Heightmap = Platform_MemAlloc(Gen_Width * Gen_Length, sizeof(Int16)); - if (!Heightmap) { - ErrorHandler_Fail("NotchyGen - Failed to allocate Heightmap array"); - } + Heightmap = Platform_MemAlloc(Gen_Width * Gen_Length, sizeof(Int16), "gen heightmap"); Random_Init(&rnd, Gen_Seed); oneY = Gen_Width * Gen_Length; diff --git a/src/Client/NixPlatform.c b/src/Client/NixPlatform.c index 2f91b999e..7c6a93bd4 100644 --- a/src/Client/NixPlatform.c +++ b/src/Client/NixPlatform.c @@ -76,16 +76,30 @@ STRING_PURE String Platform_GetCommandLineArgs(void) { return String_MakeNull(); } -void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { - return malloc(numElems * elemsSize); +/* TODO: Not duplicate with windows*/ +static void Platform_AllocFailed(const UChar* place) { + UChar logBuffer[String_BufferSize(STRING_SIZE + 20)]; + String log = String_InitAndClearArray(logBuffer); + String_Format1(&log, "Failed allocating memory for: %c", place); + ErrorHandler_Fail(&log.buffer); } -void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { - return calloc(numElems, elemsSize); +void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place) { + void* ptr = malloc(numElems * elemsSize); /* TODO: avoid overflow here */ + if (!ptr) Platform_AllocFailed(place); + return ptr; } -void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { - return realloc(mem, numElems * elemsSize); +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place) { + void* ptr = calloc(numElems, elemsSize); /* TODO: avoid overflow here */ + if (!ptr) Platform_AllocFailed(place); + return ptr; +} + +void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place) { + void* ptr = realloc(mem, numElems * elemsSize); /* TODO: avoid overflow here */ + if (!ptr) Platform_AllocFailed(place); + return ptr; } void Platform_MemFree(void** mem) { diff --git a/src/Client/OpenGLApi.c b/src/Client/OpenGLApi.c index cebc6fbbb..8a933cb7e 100644 --- a/src/Client/OpenGLApi.c +++ b/src/Client/OpenGLApi.c @@ -122,8 +122,7 @@ static void GL_DoMipmaps(GfxResourceID texId, Int32 x, Int32 y, struct Bitmap* b if (width > 1) width /= 2; if (height > 1) height /= 2; - UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); - if (!cur) ErrorHandler_Fail("Allocating memory for mipmaps"); + UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "mipmaps"); GfxCommon_GenMipmaps(width, height, cur, prev); if (partial) { diff --git a/src/Client/PacketHandlers.c b/src/Client/PacketHandlers.c index d9f2236d5..7ceade667 100644 --- a/src/Client/PacketHandlers.c +++ b/src/Client/PacketHandlers.c @@ -407,8 +407,7 @@ static void Classic_LevelInit(struct Stream* stream) { mapVolume = Stream_ReadU32_BE(stream); gzHeader.Done = true; mapSizeIndex = sizeof(UInt32); - map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); - if (!map) ErrorHandler_Fail("Failed to allocate memory for map"); + map = Platform_MemAlloc(mapVolume, sizeof(BlockID), "map blocks"); } } @@ -437,8 +436,7 @@ static void Classic_LevelDataChunk(struct Stream* stream) { if (mapSizeIndex == sizeof(UInt32)) { if (!map) { mapVolume = Stream_GetU32_BE(mapSize); - map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); - if (!map) ErrorHandler_Fail("Failed to allocate memory for map"); + map = Platform_MemAlloc(mapVolume, sizeof(BlockID), "map blocks"); } UInt8* src = map + mapIndex; diff --git a/src/Client/Physics.c b/src/Client/Physics.c index e56f78417..8f886c456 100644 --- a/src/Client/Physics.c +++ b/src/Client/Physics.c @@ -169,11 +169,7 @@ Int32 Searcher_FindReachableBlocks(struct Entity* entity, struct AABB* entityBB, Platform_MemFree(&Searcher_States); } Searcher_StatesCount = elements; - - Searcher_States = Platform_MemAlloc(elements, sizeof(struct SearcherState)); - if (!Searcher_States) { - ErrorHandler_Fail("Failed to allocate memory for Searcher_FindReachableBlocks"); - } + Searcher_States = Platform_MemAlloc(elements, sizeof(struct SearcherState), "collision search states"); } /* Order loops so that we minimise cache misses */ diff --git a/src/Client/Platform.h b/src/Client/Platform.h index 168e724d0..2b2f103fe 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -33,9 +33,9 @@ void Platform_Free(void); void Platform_Exit(ReturnCode code); STRING_PURE String Platform_GetCommandLineArgs(void); -void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize); -void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize); -void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize); +void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place); +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place); +void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place); void Platform_MemFree(void** mem); void Platform_MemSet(void* dst, UInt8 value, UInt32 numBytes); void Platform_MemCpy(void* dst, void* src, UInt32 numBytes); diff --git a/src/Client/Program.c b/src/Client/Program.c index e6e9f3549..aeaecab29 100644 --- a/src/Client/Program.c +++ b/src/Client/Program.c @@ -11,11 +11,25 @@ #include "Game.h" #include "Funcs.h" #include "AsyncDownloader.h" +#include "Audio.h" int main(void) { ErrorHandler_Init("client.log"); Platform_Init(); + void* file; + String oggPath = String_FromConst("audio/calm1.ogg"); + Platform_FileOpen(&file, &oggPath); + struct Stream oggBase; + Stream_FromFile(&oggBase, file, &oggPath); + struct Stream ogg; + UInt8 oggBuffer[OGG_BUFFER_SIZE]; + Ogg_MakeStream(&ogg, oggBuffer, &oggBase); + + struct VorbisState state; + Vorbis_Init(&state, &ogg); + Vorbis_DecodeHeaders(&state); + /*Platform_HttpInit(); AsyncRequest req = { 0 }; String url = String_FromEmptyArray(req.URL); diff --git a/src/Client/String.c b/src/Client/String.c index 2a1bf4aee..61e9bce3f 100644 --- a/src/Client/String.c +++ b/src/Client/String.c @@ -721,12 +721,10 @@ void StringsBuffer_Resize(void** buffer, UInt32* elems, UInt32 elemSize, UInt32 UInt32 curElems = *elems; if (curElems <= defElems) { - dst = Platform_MemAlloc(curElems + expandElems, elemSize); - if (!dst) ErrorHandler_Fail("Failed allocating memory for StringsBuffer"); + dst = Platform_MemAlloc(curElems + expandElems, elemSize, "StringsBuffer"); Platform_MemCpy(dst, cur, curElems * elemSize); } else { - dst = Platform_MemRealloc(cur, curElems + expandElems, elemSize); - if (!dst) ErrorHandler_Fail("Failed allocating memory for resizing StringsBuffer"); + dst = Platform_MemRealloc(cur, curElems + expandElems, elemSize, "resizing StringsBuffer"); } *buffer = dst; diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index 2cb012add..9831adf3a 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -102,19 +102,32 @@ STRING_PURE String Platform_GetCommandLineArgs(void) { return args; } -void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { - UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ - return HeapAlloc(heap, 0, numBytes); +static void Platform_AllocFailed(const UChar* place) { + UChar logBuffer[String_BufferSize(STRING_SIZE + 20)]; + String log = String_InitAndClearArray(logBuffer); + String_Format1(&log, "Failed allocating memory for: %c", place); + ErrorHandler_Fail(log.buffer); } -void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { +void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place) { UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ - return HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes); + void* ptr = HeapAlloc(heap, 0, numBytes); + if (!ptr) Platform_AllocFailed(place); + return ptr; } -void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place) { UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ - return HeapReAlloc(heap, 0, mem, numBytes); + void* ptr = HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes); + if (!ptr) Platform_AllocFailed(place); + return ptr; +} + +void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place) { + UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ + void* ptr = HeapReAlloc(heap, 0, mem, numBytes); + if (!ptr) Platform_AllocFailed(place); + return ptr; } void Platform_MemFree(void** mem) { @@ -552,8 +565,7 @@ ReturnCode Platform_HttpGetRequestHeaders(struct AsyncRequest* request, void* ha ReturnCode Platform_HttpGetRequestData(struct AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress) { if (size == 0) return ERROR_NOT_SUPPORTED; - *data = Platform_MemAlloc(size, 1); - if (*data == NULL) ErrorHandler_Fail("Failed to allocate memory for http get data"); + *data = Platform_MemAlloc(size, sizeof(UInt8), "http get data"); *progress = 0; UInt8* buffer = *data;