tests/lib/heap: Add coverage for sys_heap_usable_size()

Call and validate the new function.  Note that this is actually
whiteboxing the heap implementation and duplicating the internal logic
needed to compute the header size, so as to validate that the correct
number is being returned.  I had to write that test code anyway, might
as well commit it (the heap header is awfully well optimized now and
very unlikely to change).

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
Andy Ross 2021-10-06 06:28:20 -07:00 committed by Anas Nashif
parent cf0c5e2a1c
commit caf197262a

View file

@ -106,6 +106,23 @@ void *testalloc(void *arg, size_t bytes)
{
void *ret = sys_heap_alloc(arg, bytes);
if (ret != NULL) {
/* White box: the heap internals will allocate memory
* in 8 chunk units, no more than needed, but with a
* header prepended that is 4 or 8 bytes. Use this to
* validate the block_size predicate.
*/
size_t blksz = sys_heap_usable_size(arg, ret);
size_t addr = (size_t) ret;
size_t chunk = ROUND_DOWN(addr - 1, 8);
size_t hdr = addr - chunk;
size_t expect = ROUND_UP(bytes + hdr, 8) - hdr;
zassert_equal(blksz, expect,
"wrong size block returned bytes = %ld ret = %ld",
bytes, blksz);
}
fill_block(ret, bytes);
sys_heap_validate(arg);
return ret;