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:
parent
f4e1611692
commit
e919bb2d16
3 changed files with 9 additions and 7 deletions
|
@ -343,7 +343,7 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
|
||||||
if (count) {
|
if (count) {
|
||||||
printk("%9d %12d %12d %12zd %12zd\n",
|
printk("%9d %12d %12d %12zd %12zd\n",
|
||||||
i, (1 << i) - 1 + min_chunk_size(h), count,
|
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) {
|
if (c == 0 || c == h->end_chunk) {
|
||||||
/* those are always allocated for internal purposes */
|
/* those are always allocated for internal purposes */
|
||||||
} else if (chunk_used(h, c)) {
|
} else if (chunk_used(h, c)) {
|
||||||
allocated_bytes += chunk_size(h, c) * CHUNK_UNIT
|
allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
|
||||||
- chunk_header_bytes(h);
|
|
||||||
} else if (!solo_free_header(h, c)) {
|
} else if (!solo_free_header(h, c)) {
|
||||||
free_bytes += chunk_size(h, c) * CHUNK_UNIT
|
free_bytes += chunksz_to_bytes(h, chunk_size(h, c));
|
||||||
- chunk_header_bytes(h);
|
|
||||||
}
|
}
|
||||||
if (dump_chunks) {
|
if (dump_chunks) {
|
||||||
printk("chunk %4zd: [%c] size=%-4zd left=%-4zd right=%zd\n",
|
printk("chunk %4zd: [%c] size=%-4zd left=%-4zd right=%zd\n",
|
||||||
|
|
|
@ -367,8 +367,7 @@ void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
|
||||||
void *ptr2 = sys_heap_aligned_alloc(heap, align, bytes);
|
void *ptr2 = sys_heap_aligned_alloc(heap, align, bytes);
|
||||||
|
|
||||||
if (ptr2 != NULL) {
|
if (ptr2 != NULL) {
|
||||||
size_t prev_size = chunk_size(h, c) * CHUNK_UNIT
|
size_t prev_size = chunksz_to_bytes(h, chunk_size(h, c)) - align_gap;
|
||||||
- chunk_header_bytes(h) - align_gap;
|
|
||||||
|
|
||||||
memcpy(ptr2, ptr, MIN(prev_size, bytes));
|
memcpy(ptr2, ptr, MIN(prev_size, bytes));
|
||||||
sys_heap_free(heap, ptr);
|
sys_heap_free(heap, ptr);
|
||||||
|
|
|
@ -228,6 +228,11 @@ static inline int min_chunk_size(struct z_heap *h)
|
||||||
return bytes_to_chunksz(h, 1);
|
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)
|
static inline int bucket_idx(struct z_heap *h, size_t sz)
|
||||||
{
|
{
|
||||||
size_t usable_sz = sz - min_chunk_size(h) + 1;
|
size_t usable_sz = sz - min_chunk_size(h) + 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue