lib/os/heap: abstract conversion from chunk size to usable bytes

This is the reverse of bytes_to_chunksz().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2021-03-17 22:21:14 -04:00 committed by Anas Nashif
commit e919bb2d16
3 changed files with 9 additions and 7 deletions

View file

@ -343,7 +343,7 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
if (count) {
printk("%9d %12d %12d %12zd %12zd\n",
i, (1 << i) - 1 + min_chunk_size(h), count,
largest, largest * CHUNK_UNIT - chunk_header_bytes(h));
largest, chunksz_to_bytes(h, largest));
}
}
@ -355,11 +355,9 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
if (c == 0 || c == h->end_chunk) {
/* those are always allocated for internal purposes */
} else if (chunk_used(h, c)) {
allocated_bytes += chunk_size(h, c) * CHUNK_UNIT
- chunk_header_bytes(h);
allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
} else if (!solo_free_header(h, c)) {
free_bytes += chunk_size(h, c) * CHUNK_UNIT
- chunk_header_bytes(h);
free_bytes += chunksz_to_bytes(h, chunk_size(h, c));
}
if (dump_chunks) {
printk("chunk %4zd: [%c] size=%-4zd left=%-4zd right=%zd\n",

View file

@ -367,8 +367,7 @@ void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
void *ptr2 = sys_heap_aligned_alloc(heap, align, bytes);
if (ptr2 != NULL) {
size_t prev_size = chunk_size(h, c) * CHUNK_UNIT
- chunk_header_bytes(h) - align_gap;
size_t prev_size = chunksz_to_bytes(h, chunk_size(h, c)) - align_gap;
memcpy(ptr2, ptr, MIN(prev_size, bytes));
sys_heap_free(heap, ptr);

View file

@ -228,6 +228,11 @@ static inline int min_chunk_size(struct z_heap *h)
return bytes_to_chunksz(h, 1);
}
static inline size_t chunksz_to_bytes(struct z_heap *h, size_t chunksz)
{
return chunksz * CHUNK_UNIT - chunk_header_bytes(h);
}
static inline int bucket_idx(struct z_heap *h, size_t sz)
{
size_t usable_sz = sz - min_chunk_size(h) + 1;