From d75880e8a6d4af817c464118ce9a43abca8cf3be Mon Sep 17 00:00:00 2001 From: quou Date: Thu, 19 Dec 2024 19:09:59 +1100 Subject: heap_block_size --- qstd/memory.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'qstd/memory.c') diff --git a/qstd/memory.c b/qstd/memory.c index f245289..6ea43f7 100644 --- a/qstd/memory.c +++ b/qstd/memory.c @@ -209,6 +209,17 @@ void heap_free( heap_free_aligned(h, p); } +int heap_block_size(void* ptr) { + unsigned char* a; + int f = ~((unsigned)-1 >> 1); + ptrdiff_t shift; + a = ptr; + shift = a[-1]; + shift += 256 * shift == 0; + a -= shift; + return ((int*)a)[-1] & ~f; +} + /* void print_blocks(Heap* h) { int i, o; -- cgit v1.2.3-54-g00ecf