diff options
author | quou <quou@disroot.org> | 2024-12-19 19:09:59 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-12-19 19:09:59 +1100 |
commit | d75880e8a6d4af817c464118ce9a43abca8cf3be (patch) | |
tree | 04939caca2267a566859cc73d0fc702b88f6f32e /qstd | |
parent | 09ee83e34b3280d8876dd920aae877c3798a4a97 (diff) |
heap_block_size
Diffstat (limited to 'qstd')
-rw-r--r-- | qstd/memory.c | 11 | ||||
-rw-r--r-- | qstd/memory.h | 1 |
2 files changed, 12 insertions, 0 deletions
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; diff --git a/qstd/memory.h b/qstd/memory.h index 26c0382..842cb49 100644 --- a/qstd/memory.h +++ b/qstd/memory.h @@ -53,4 +53,5 @@ void heap_free( void* p ); void heap_defrag(Heap* h); +int heap_block_size(void* ptr); #endif |