6.1 Main RAM Allocation

For general-purpose allocations: - Use KOS’s malloc/free as foundation - Add debug headers in debug builds - Track statistics for all allocations

void *d3tui_malloc(size_t size) {
    #ifdef D3TUI_DEBUG
        // Add debug header
        size_t total_size = size + sizeof(d3tui_alloc_header_t);
        d3tui_alloc_header_t *header = (d3tui_alloc_header_t *)malloc(total_size);
        if (!header) return NULL;
        
        header->size = size;
        header->magic = D3TUI_ALLOC_MAGIC;
        // Fill other fields...
        
        // Update statistics
        memory_manager.stats.current_usage += total_size;
        memory_manager.stats.peak_usage = max(memory_manager.stats.peak_usage, 
                                              memory_manager.stats.current_usage);
        
        return (void *)(header + 1);
    #else
        return malloc(size);
    #endif
}