From a51d3c0f7408d3976e9469586096108adf1e8a1f Mon Sep 17 00:00:00 2001 From: Krzysztof Sychla Date: Fri, 27 Jun 2025 14:49:41 +0200 Subject: [PATCH] kernel: heap: Add allocation metadata to allocated_bytes The allocated_bytes were missing the allocation metadata. Because of that the sum of free_bytes and allocated_bytes doesn't remain constant after each allocation. This convention doesn't match glibc's behavior. This commit changes the chunksz_to_bytes function to include the metadata in the calculation. The analysis of the mallinfo2 function from glibc has been done in #92392 Pull Request. Signed-off-by: Krzysztof Sychla --- lib/heap/heap.h | 2 +- samples/basic/sys_heap/sample.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/heap/heap.h b/lib/heap/heap.h index 6f402dbd588..8a3b893a658 100644 --- a/lib/heap/heap.h +++ b/lib/heap/heap.h @@ -255,7 +255,7 @@ static inline chunksz_t min_chunk_size(struct z_heap *h) static inline size_t chunksz_to_bytes(struct z_heap *h, chunksz_t chunksz_in) { - return chunksz_in * CHUNK_UNIT - chunk_header_bytes(h); + return chunksz_in * CHUNK_UNIT; } static inline int bucket_idx(struct z_heap *h, chunksz_t sz) diff --git a/samples/basic/sys_heap/sample.yaml b/samples/basic/sys_heap/sample.yaml index d6179247991..8d92f9b50fb 100644 --- a/samples/basic/sys_heap/sample.yaml +++ b/samples/basic/sys_heap/sample.yaml @@ -9,8 +9,8 @@ common: type: multi_line ordered: true regex: - - ".*allocated 15.,.*" - - ".*allocated 10.,.*" + - ".*allocated 16.,.*" + - ".*allocated 1..,.*" - ".*allocated 0, free ..., max allocated ..., heap size 256.*" tests: sample.basic.sys_heap: