provide separate function for allocating zeroed memory for C client

This commit is contained in:
UnknownShadow200 2018-07-24 22:46:13 +10:00
parent 0eb88b846b
commit b44b169c71
7 changed files with 27 additions and 29 deletions

View File

@ -7,9 +7,7 @@
#include "Stream.h"
void Bitmap_Create(struct Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0) {
bmp->Width = width; bmp->Height = height;
bmp->Stride = width * BITMAP_SIZEOF_PIXEL;
bmp->Scan0 = scan0;
bmp->Width = width; bmp->Height = height; bmp->Scan0 = scan0;
}
void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, struct Bitmap* src, struct Bitmap* dst, Int32 size) {
@ -25,19 +23,17 @@ 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->Stride = width * BITMAP_SIZEOF_PIXEL;
bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL);
if (bmp->Scan0 == NULL) {
ErrorHandler_Fail("Bitmap - failed to allocate memory");
}
if (bmp->Scan0 == NULL) ErrorHandler_Fail("Bitmap - failed to allocate memory");
}
void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) {
width = Math_NextPowOf2(width);
width = Math_NextPowOf2(width);
height = Math_NextPowOf2(height);
Bitmap_Allocate(bmp, width, height);
Platform_MemSet(bmp->Scan0, 0, Bitmap_DataSize(width, height));
bmp->Width = width; bmp->Height = height;
bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL);
if (bmp->Scan0 == NULL) ErrorHandler_Fail("Bitmap - failed to allocate memory");
}
@ -337,7 +333,6 @@ 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->Stride = bmp->Width * BITMAP_SIZEOF_PIXEL;
bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL);
if (bmp->Scan0 == NULL) ErrorHandler_Fail("Failed to allocate memory for PNG bitmap");

View File

@ -5,17 +5,12 @@
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
struct Stream;
struct Bitmap {
UInt8* Scan0; /* Pointer to first scaneline. */
UInt32 Stride; /* Number of bytes in each scanline. TODO: Obsolete this completely and just use Width << 2 ? */
Int32 Width; /* Number of pixels horizontally. */
Int32 Height; /* Number of pixels vertically. */
};
struct Bitmap { UInt8* Scan0; Int32 Width, Height; };
#define PNG_MAX_DIMS 0x8000L
#define BITMAP_SIZEOF_PIXEL 4
#define BITMAP_SIZEOF_PIXEL 4 /* 32 bit ARGB */
#define Bitmap_DataSize(width, height) ((UInt32)(width) * (UInt32)(height) * (UInt32)BITMAP_SIZEOF_PIXEL)
#define Bitmap_GetRow(bmp, y) ((UInt32*)((bmp)->Scan0 + ((y) * (bmp)->Stride)))
#define Bitmap_GetRow(bmp, y) ((UInt32*)((bmp)->Scan0 + ((y) * ((bmp)->Width << 2))))
#define Bitmap_GetPixel(bmp, x, y) (Bitmap_GetRow(bmp, y)[x])
void Bitmap_Create(struct Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0);

View File

@ -94,14 +94,12 @@ static void ChunkUpdater_FreeAllocations(void) {
}
static void ChunkUpdater_PerformPartsAllocations(void) {
UInt32 partsCount = MapRenderer_ChunksCount * MapRenderer_1DUsedCount;
MapRenderer_PartsBuffer_Raw = Platform_MemAlloc(partsCount * 2, sizeof(struct ChunkPartInfo));
if (MapRenderer_PartsBuffer_Raw == NULL) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts buffer");
UInt32 count = MapRenderer_ChunksCount * MapRenderer_1DUsedCount;
MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo));
if (MapRenderer_PartsBuffer_Raw == NULL) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts");
UInt32 partsSize = partsCount * 2 * (UInt32)sizeof(struct ChunkPartInfo);
Platform_MemSet(MapRenderer_PartsBuffer_Raw, 0, partsSize);
MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw;
MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + partsCount;
MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + count;
}
static void ChunkUpdater_PerformAllocations(void) {

View File

@ -81,6 +81,10 @@ void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) {
return malloc(numElems * elemsSize);
}
void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) {
return calloc(numElems, elemsSize);
}
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) {
return realloc(mem, numElems * elemsSize);
}

View File

@ -404,7 +404,7 @@ static void Classic_LevelInit(struct Stream* stream) {
/* Fast map puts volume in header, doesn't bother with gzip */
if (cpe_fastMap) {
mapVolume = Stream_ReadI32_BE(stream);
mapVolume = Stream_ReadU32_BE(stream);
gzHeader.Done = true;
mapSizeIndex = sizeof(UInt32);
map = Platform_MemAlloc(mapVolume, sizeof(BlockID));
@ -436,7 +436,7 @@ static void Classic_LevelDataChunk(struct Stream* stream) {
if (mapSizeIndex == sizeof(UInt32)) {
if (map == NULL) {
mapVolume = (mapSize[0] << 24) | (mapSize[1] << 16) | (mapSize[2] << 8) | mapSize[3];
mapVolume = Stream_GetU32_BE(mapSize);
map = Platform_MemAlloc(mapVolume, sizeof(BlockID));
if (map == NULL) ErrorHandler_Fail("Failed to allocate memory for map");
}

View File

@ -33,6 +33,7 @@ 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_MemFree(void** mem);
void Platform_MemSet(void* dst, UInt8 value, UInt32 numBytes);

View File

@ -106,6 +106,11 @@ void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) {
return HeapAlloc(heap, 0, numBytes);
}
void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */
return HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes);
}
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */
return HeapReAlloc(heap, 0, mem, numBytes);
@ -456,7 +461,7 @@ struct Size2D Platform_TextDraw(struct DrawTextArgs* args, Int32 x, Int32 y, Pac
Int32 xx, yy;
struct Bitmap* bmp = platform_bmp;
for (yy = 0; yy < area.cy; yy++) {
UInt8* src = (UInt8*)platform_bits + (yy * bmp->Stride);
UInt8* src = (UInt8*)platform_bits + (yy * (bmp->Width << 2));
UInt8* dst = (UInt8*)Bitmap_GetRow(bmp, y + yy); dst += x * BITMAP_SIZEOF_PIXEL;
for (xx = 0; xx < area.cx; xx++) {