cpukit/libcsupport: Implement malloc_usable_size

Implement malloc_usable_size to get the underlying heap block size of a
memory address on the heap.

Closes #4503
This commit is contained in:
Jeremy Lorelli
2025-07-16 21:18:57 -07:00
committed by Amar Takhar
parent b7f35f9db9
commit 20baae458d

View File

@@ -40,6 +40,7 @@
#ifdef RTEMS_NEWLIB
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
#include "malloc_p.h"
@@ -62,4 +63,38 @@ void *malloc(
return return_this;
}
size_t malloc_usable_size(
void *area
)
{
Heap_Block* block = NULL;
size_t size = 0;
bool needs_lock;
if ( area == NULL ) {
return 0;
}
needs_lock = _Malloc_System_state() == MALLOC_SYSTEM_STATE_NORMAL;
if ( needs_lock ) {
_RTEMS_Lock_allocator();
}
block = _Heap_Block_of_alloc_area(
(uintptr_t)area,
RTEMS_Malloc_Heap->page_size
);
if (block) {
size = _Heap_Block_size(block);
}
if ( needs_lock ) {
_RTEMS_Unlock_allocator();
}
return size;
}
#endif