Skip to content

Commit a4b36eb

Browse files
authored
Implement heap allocator with management features and unit tests (#22)
This pull request introduces a new boundary-tag free-list heap allocator for WebCC, replacing the previous bump allocator and improving memory management across the codebase. The allocator now supports splitting and coalescing of blocks, in-place growth, and is testable on both WASM and host builds. Several core containers and formatters are updated to take advantage of the new allocator's features, and placement new support is improved for both freestanding and hosted environments. ### Heap Allocator Redesign and Improvements * [`include/webcc/core/allocator.h`](diffhunk://#diff-bd10771928744a6c64d378f0bdbc45f48bc1ec2bcdd9cdc900b5854c2b50261cR5-R380): Replaces the old bump allocator with a boundary-tag free-list allocator, supporting block splitting on allocation, coalescing on free, and in-place growth of allocations. Introduces a platform-abstracted backend for both WASM and host builds, and provides new introspection helpers for testing. * [`examples/webcc_types/example.cc`](diffhunk://#diff-0378a58a312f61c17814815a4b0e3828b493e432b20a87dac94afc7c847a633bL40-R42): Updates heap visualization code to use the new allocator's backend and introspection helpers. ### Container and Formatter Enhancements * [`include/webcc/core/vector.h`](diffhunk://#diff-7f1d5cfb08de99c4f18ef7467e48b46078082132957e71ec986ca031fc6ccfefR21-R30): Updates `vector` to use the new `try_grow_inplace` API, allowing in-place capacity growth without unnecessary reallocations or element moves. * [`include/webcc/core/queue.h`](diffhunk://#diff-c07b9db23a442be44337e1aedad22a371b7f1354813616120140bb98ff81dc61R21-R33): Updates `queue` to use in-place growth when possible, reducing memory copying and improving performance in the common case. * [`include/webcc/core/format.h`](diffhunk://#diff-9ee51da0bcd04a394f769576442c08ebf423bf9a1e721cdaf571de25250600ceL185-R188): Refactors `dynamic_formatter` and `hybrid_formatter` to use `realloc` for buffer growth, leveraging in-place expansion and simplifying buffer management logic. [[1]](diffhunk://#diff-9ee51da0bcd04a394f769576442c08ebf423bf9a1e721cdaf571de25250600ceL185-R188) [[2]](diffhunk://#diff-9ee51da0bcd04a394f769576442c08ebf423bf9a1e721cdaf571de25250600ceR297-R314) ### Placement New Operator Support * [`include/webcc/core/new.h`](diffhunk://#diff-82261c7bb3b71b0b04f6d0134769cc4b22047ca257733c55140e76c9186a63d4L2-R3): Improves placement new/delete support for both freestanding WASM and hosted builds, conditionally including the appropriate headers or definitions. [[1]](diffhunk://#diff-82261c7bb3b71b0b04f6d0134769cc4b22047ca257733c55140e76c9186a63d4L2-R3) [[2]](diffhunk://#diff-82261c7bb3b71b0b04f6d0134769cc4b22047ca257733c55140e76c9186a63d4R16-R19) [[3]](diffhunk://#diff-82261c7bb3b71b0b04f6d0134769cc4b22047ca257733c55140e76c9186a63d4R29-R33) ### Test Suite Updates * [`tests/run.sh`](diffhunk://#diff-dd0c9cda3917fccebea6cea8d3fb46f46e4fbbeda2d6939864c9aaa9fc9c9571L30-R38): Adds compilation of new allocator and container tests, and ensures the correct include paths for the refactored code. Co-authored-by: io-eric <io-eric@users.noreply.github.com>
1 parent 925e0c5 commit a4b36eb

9 files changed

Lines changed: 743 additions & 93 deletions

File tree

examples/webcc_types/example.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ void log(const char* msg) {
3737
void draw_heap_viz() {
3838
if (!canvas_ctx.is_valid()) return;
3939

40-
uintptr_t base = (uintptr_t)&webcc::__heap_base;
41-
uintptr_t current = webcc::heap_ptr;
42-
size_t used = current - base;
43-
40+
uintptr_t base = webcc::detail::backend_base();
41+
uintptr_t current = webcc::detail::heap_ptr;
42+
size_t used = webcc::detail::heap_used();
43+
4444
// Get current memory size (in pages)
4545
size_t current_pages = __builtin_wasm_memory_size(0);
4646
size_t total_mem = current_pages * 64 * 1024;

0 commit comments

Comments
 (0)