Skip to content

Commit

Permalink
Catch all memory allocation failures
Browse files Browse the repository at this point in the history
  • Loading branch information
andwn committed Mar 28, 2017
1 parent f42ce0c commit 811a976
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Binary file added res/sprite/redsplode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void DMA_init(uint16_t size, uint16_t capacity)
if (dmaQueues) MEM_free(dmaQueues);
// allocate DMA queue
dmaQueues = MEM_alloc(queueSize * sizeof(DMAOpInfo));

if (!dmaQueues) SYS_die("Out of memory creating DMA queue!");

// clear queue
DMA_clearQueue();
}
Expand Down
7 changes: 2 additions & 5 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,9 @@ Entity *entity_create(int32_t x, int32_t y, uint16_t type, uint16_t flags) {
// Allocate memory and start applying values
uint8_t sprite_count = npc_info[type].sprite_count;
Entity *e = MEM_alloc(sizeof(Entity) + sizeof(VDPSprite) * sprite_count);
if(!e) {
// If the allocation failed, stop with a meaningful message
// instead of leaving the game crash later
SYS_die("Not enough memory to create new entity!");
}
if(!e) SYS_die("Out of memory creating new entity!");
memset(e, 0, sizeof(Entity) + sizeof(VDPSprite) * sprite_count);

e->x = x;
e->y = y;
e->sprite_count = sprite_count;
Expand Down
4 changes: 3 additions & 1 deletion src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void stage_load(uint16_t id) {
VDP_setEnable(FALSE);

// Prevents an issue where a column of the previous map would get drawn over the new one
DMA_flushQueue();
DMA_clearQueue();

input_update(); // Prevent menu from opening after loading save
stageID = id;
Expand Down Expand Up @@ -279,9 +279,11 @@ void stage_load_blocks() {
stageHeight = PXM[6] + (PXM[7] << 8);
PXM += 8;
stageBlocks = MEM_alloc(stageWidth * stageHeight);
if(!stageBlocks) SYS_die("Out of memory loading stage layout!");
memcpy(stageBlocks, PXM, stageWidth * stageHeight);
// Multiplication table for stage rows
stageTable = MEM_alloc(stageHeight * 2);
if(!stageTable) SYS_die("Out of memory creating stage table!");
uint16_t blockTotal = 0;
for(uint16_t y = 0; y < stageHeight; y++) {
stageTable[y] = blockTotal;
Expand Down

0 comments on commit 811a976

Please sign in to comment.