This project implements a simple custom memory allocator in C, demonstrating concepts of memory management such as block allocation, freeing memory, and handling fragmentation.
This project implements a simple custom memory allocator in C, demonstrating concepts of memory management such as block allocation, freeing memory, and handling fragmentation.
init_arena
).arena_alloc
).arena_free
).#include "memory_allocator.h"
int main() {
// Initialize 1MB memory arena
memory_arena_t* arena = init_arena(1024 * 1024);
// Allocate memory
void* ptr = arena_alloc(arena, size);
// Free memory
arena_free(arena, ptr);
// Cleanup
destroy_arena(arena);
}
typedef struct block_header {
size_t size; // Block size including header
bool is_free; // Block status
struct block_header* next; // Next block in list
struct block_header* prev; // Previous block in list
uint32_t magic; // Validation number
uint32_t checksum; // Corruption detection
} block_header_t;
printf("Used Memory: %zu bytes\n", arena->used_memory);
printf("Peak Memory: %zu bytes\n", arena->peak_memory);
// Minimum allocation size
arena->min_alloc = custom_size;
MIT License