Created
May 4, 2025 19:49
-
-
Save kassane/38e7c1d92a7a27a968e44f986b14744b to your computer and use it in GitHub Desktop.
Build and run o1heap sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
License: CC0 | |
author: Matheus C. França | |
requires: https://github.com/pavel-kirienko/o1heap - credits: pavel-kirienko | |
*/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stddef.h> | |
#include <assert.h> | |
#include <o1heap.h> | |
// Raw arena size. The actual usable heap size will be slightly less | |
// because some space is used internally by the allocator (typically ~40–600 bytes). | |
#define RAW_BUFFER_SIZE 1024 | |
// Static arena buffer with guaranteed alignment | |
static uint8_t raw_buffer[RAW_BUFFER_SIZE + O1HEAP_ALIGNMENT] __attribute__((aligned(O1HEAP_ALIGNMENT))); | |
int main(void) { | |
// Align the base address of the arena manually | |
uintptr_t base_addr = ((uintptr_t)raw_buffer + (O1HEAP_ALIGNMENT - 1)) & ~(uintptr_t)(O1HEAP_ALIGNMENT - 1); | |
void* base = (void*)base_addr; | |
// Initialize the heap with the aligned arena | |
O1HeapInstance* heap = o1heapInit(base, RAW_BUFFER_SIZE); | |
if (!heap) { | |
fprintf(stderr, "Failed to initialize O1Heap.\n"); | |
return 1; | |
} | |
// Initial diagnostics | |
O1HeapDiagnostics diag = o1heapGetDiagnostics(heap); | |
printf("Initial heap capacity: %zu bytes\n", diag.capacity); | |
// Allocate two 64-byte blocks | |
void* p1 = o1heapAllocate(heap, 64); | |
assert(p1 && "First allocation of 64 bytes failed"); | |
printf("Allocated 64 bytes at %p\n", p1); | |
void* p2 = o1heapAllocate(heap, 64); | |
assert(p2 && "Second allocation of 64 bytes failed"); | |
printf("Allocated another 64 bytes at %p\n", p2); | |
// Free the first block | |
o1heapFree(heap, p1); | |
printf("Freed first block\n"); | |
// Allocate a third block — should reuse the freed space | |
void* p3 = o1heapAllocate(heap, 64); | |
assert(p3 && "Re-allocation of 64 bytes failed"); | |
printf("Reused 64 bytes at %p\n", p3); | |
// Print diagnostics | |
diag = o1heapGetDiagnostics(heap); | |
printf("Currently allocated: %zu bytes\n", diag.allocated); | |
printf("Peak allocated: %zu bytes\n", diag.peak_allocated); | |
printf("Out-of-memory events: %llu\n", (unsigned long long)diag.oom_count); | |
// Free remaining blocks | |
o1heapFree(heap, p2); | |
o1heapFree(heap, p3); | |
// Heap integrity check | |
assert(o1heapDoInvariantsHold(heap)); | |
printf("Heap integrity check passed.\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
Arch: x86-64 (zen3)
OS: Arch(Linux)
valgrind test