Skip to content

Commit e919bb2

Browse files
Nicolas Pitrenashif
authored andcommitted
lib/os/heap: abstract conversion from chunk size to usable bytes
This is the reverse of bytes_to_chunksz(). Signed-off-by: Nicolas Pitre <[email protected]>
1 parent f4e1611 commit e919bb2

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

lib/os/heap-validate.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
343343
if (count) {
344344
printk("%9d %12d %12d %12zd %12zd\n",
345345
i, (1 << i) - 1 + min_chunk_size(h), count,
346-
largest, largest * CHUNK_UNIT - chunk_header_bytes(h));
346+
largest, chunksz_to_bytes(h, largest));
347347
}
348348
}
349349

@@ -355,11 +355,9 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
355355
if (c == 0 || c == h->end_chunk) {
356356
/* those are always allocated for internal purposes */
357357
} else if (chunk_used(h, c)) {
358-
allocated_bytes += chunk_size(h, c) * CHUNK_UNIT
359-
- chunk_header_bytes(h);
358+
allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
360359
} else if (!solo_free_header(h, c)) {
361-
free_bytes += chunk_size(h, c) * CHUNK_UNIT
362-
- chunk_header_bytes(h);
360+
free_bytes += chunksz_to_bytes(h, chunk_size(h, c));
363361
}
364362
if (dump_chunks) {
365363
printk("chunk %4zd: [%c] size=%-4zd left=%-4zd right=%zd\n",

lib/os/heap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
367367
void *ptr2 = sys_heap_aligned_alloc(heap, align, bytes);
368368

369369
if (ptr2 != NULL) {
370-
size_t prev_size = chunk_size(h, c) * CHUNK_UNIT
371-
- chunk_header_bytes(h) - align_gap;
370+
size_t prev_size = chunksz_to_bytes(h, chunk_size(h, c)) - align_gap;
372371

373372
memcpy(ptr2, ptr, MIN(prev_size, bytes));
374373
sys_heap_free(heap, ptr);

lib/os/heap.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ static inline int min_chunk_size(struct z_heap *h)
228228
return bytes_to_chunksz(h, 1);
229229
}
230230

231+
static inline size_t chunksz_to_bytes(struct z_heap *h, size_t chunksz)
232+
{
233+
return chunksz * CHUNK_UNIT - chunk_header_bytes(h);
234+
}
235+
231236
static inline int bucket_idx(struct z_heap *h, size_t sz)
232237
{
233238
size_t usable_sz = sz - min_chunk_size(h) + 1;

0 commit comments

Comments
 (0)