From c4786a62e6906022db264312ff5569efde0e564f Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Sat, 7 Dec 2024 19:35:40 +0000 Subject: [PATCH] Fix bug in allocations (thanks @skmp) --- GL/alloc/alloc.c | 17 +++++++++++++---- samples/nehe08/main.c | 4 ++-- samples/nehe20/main.c | 5 +++-- samples/primitive_modes/main.c | 1 + 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/GL/alloc/alloc.c b/GL/alloc/alloc.c index 0058baf..3dfce8e 100644 --- a/GL/alloc/alloc.c +++ b/GL/alloc/alloc.c @@ -296,7 +296,7 @@ static inline void block_and_offset_from_subblock(size_t sb, size_t* b, uint8_t* *off = (sb % 8); } -void* alloc_malloc(void* pool, size_t size) { +static void* alloc_malloc_internal(void* pool, size_t size, bool for_defrag) { DBG_MSG("Allocating: %d\n", size); size_t start_subblock, required_subblocks; @@ -339,6 +339,11 @@ void* alloc_malloc(void* pool, size_t size) { pool_header.block_usage[block++] |= mask; } + // defrag allocations don't create new entries, they reuse old ones + if(for_defrag) { + return ret; + } + /* Insert allocations in the list by size descending so that when we * defrag we can move the larger blocks before the smaller ones without * much effort */ @@ -380,6 +385,10 @@ void* alloc_malloc(void* pool, size_t size) { return ret; } +void* alloc_malloc(void* pool, size_t size) { + return alloc_malloc_internal(pool, size, false); +} + static void alloc_release_blocks(struct AllocEntry* it) { size_t used_subblocks = size_to_subblock_count(it->size); size_t subblock = subblock_from_pointer(it->pointer); @@ -437,14 +446,14 @@ void alloc_free(void* pool, void* p) { DBG_MSG("Freed: size: %d, us: %d, sb: %d, off: %d\n", it->size, used_subblocks, block, offset); free(it); - break; + return; } last = it; it = it->next; } - DBG_MSG("Free done\n"); + assert("Freed pointer not found, heap corruption?" && 0); } void alloc_run_defrag(void* pool, defrag_address_move callback, int max_iterations, void* user_data) { @@ -461,7 +470,7 @@ void alloc_run_defrag(void* pool, defrag_address_move callback, int max_iteratio while(it) { void* potential_dest = alloc_next_available(pool, it->size); if(potential_dest && potential_dest < it->pointer) { - potential_dest = alloc_malloc(pool, it->size); + potential_dest = alloc_malloc_internal(pool, it->size, true); memcpy(potential_dest, it->pointer, it->size); /* Mark this block as now free, but don't fiddle with the diff --git a/samples/nehe08/main.c b/samples/nehe08/main.c index 8d6814d..4048b59 100644 --- a/samples/nehe08/main.c +++ b/samples/nehe08/main.c @@ -11,10 +11,10 @@ #ifdef __DREAMCAST__ #include #endif - #include -#include #include +#include +#include /* Simple OpenGL example to demonstrate blending and lighting. diff --git a/samples/nehe20/main.c b/samples/nehe20/main.c index 54c7825..bce9182 100644 --- a/samples/nehe20/main.c +++ b/samples/nehe20/main.c @@ -63,8 +63,9 @@ KOS_INIT_ROMDISK(romdisk); #include // Header File For The GLu32 Library #include #else -#include // Header File For The OpenGL32 Library -#include // Header File For The GLu32 Library +#include // Header File For The OpenGL32 Library +#include +#include // Header File For The GLu32 Library #endif #define BOOL int diff --git a/samples/primitive_modes/main.c b/samples/primitive_modes/main.c index 09e9ab3..5f7acab 100644 --- a/samples/primitive_modes/main.c +++ b/samples/primitive_modes/main.c @@ -1,4 +1,5 @@ #include +#include #include #include