a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit ce0179e8dfb970f8592506e76e223fdfc18cda11
parent b86b9495c14cf140c15fbf4de74e34b6e4f4d292
Author: amin <dev@aminmesbah.com>
Date:   Sat, 13 Jul 2019 04:05:29 +0000

Add asserts catching a nasty allocator bug

FossilOrigin-Name: 41d538c0a3628c339668e3cc795aa0056786c795757f2307431a8a88f0adad1e
Diffstat:
Msrc/game.c | 4++--
Msrc/image.c | 1+
Msrc/memory.c | 10+++++++++-
3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -90,7 +90,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->player.move_mode = MOVE_MODE_FALLING; game_state->player.facing = DIR_RIGHT; - size_t temp_memory_size = MEBIBYTES(1); + size_t temp_memory_size = MEBIBYTES(2); size_t world_memory_size = game_memory->buffer_size - (sizeof(struct GameState) + temp_memory_size); mem_st_init( &game_state->temp_allocator, @@ -195,7 +195,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) // game_load_images { size_t file_len = 0; - u8 *t = platform.platform_read_entire_file("assets/tile0.tga", &file_len); + u8 *t = platform.platform_read_entire_file("assets/tileset0.tga", &file_len); assert(t); i32 img_w = 0; diff --git a/src/image.c b/src/image.c @@ -100,6 +100,7 @@ internal u8 *img_load_from_memory(u8 *buffer, size_t len, i32 *out_width, i32 *o img_bytes_read += run_length_in_bytes; } } + assert(img_bytes_read == img_data_buf_len); } // NOTE(amin): we convert BGRA to RGBA because OpenGL ES and WebGL don't diff --git a/src/memory.c b/src/memory.c @@ -19,14 +19,22 @@ internal void mem_st_init(struct StackAllocator *allocator, void *backing_buffer allocator->used = 0; } -internal void* mem_st_alloc(struct StackAllocator *allocator, size_t bytes, size_t alignment) +internal void *mem_st_alloc(struct StackAllocator *allocator, size_t bytes, size_t alignment) { size_t worst_case_bytes = bytes + alignment - 1; assert(allocator->used + worst_case_bytes <= allocator->size); allocator->used += worst_case_bytes; uintptr_t current_head = (uintptr_t)allocator->base + allocator->used; + void *address = (void *)mem_align_address(current_head, alignment); + { + void *address_after_allocated_buffer = address + bytes; + void *address_after_used_allocator_memory = allocator->base + allocator->used; + void *address_after_backing_buffer = allocator->base + allocator->size; + assert(address_after_allocated_buffer <= address_after_used_allocator_memory); + assert(address_after_allocated_buffer <= address_after_backing_buffer); + } return address; }