Skip to content

Commit 5aeb809

Browse files
Nicolas Pitrenashif
authored andcommitted
lib/os/heap-validate: code cleanup
Remove code duplication, use common code idiom, etc. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent f13387d commit 5aeb809

File tree

1 file changed

+21
-46
lines changed

1 file changed

+21
-46
lines changed

lib/os/heap-validate.c

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,27 @@ static inline void check_nexts(struct z_heap *h, int bidx)
6767
}
6868
}
6969

70-
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
71-
72-
static inline void sys_heap_get_info(struct sys_heap *heap,
73-
struct sys_heap_runtime_stats *stats)
70+
static void get_alloc_info(struct z_heap *h, size_t *alloc_bytes,
71+
size_t *free_bytes)
7472
{
75-
chunkid_t c = 0;
76-
struct z_heap *h = heap->heap;
73+
chunkid_t c;
7774

78-
stats->allocated_bytes = 0;
79-
stats->free_bytes = 0;
75+
*alloc_bytes = 0;
76+
*free_bytes = 0;
8077

81-
do {
78+
for (c = right_chunk(h, 0); c < h->end_chunk; c = right_chunk(h, c)) {
8279
if (chunk_used(h, c)) {
83-
if ((c != 0) && (c != h->end_chunk)) {
84-
stats->allocated_bytes +=
85-
chunksz_to_bytes(h, chunk_size(h, c));
86-
}
87-
} else {
88-
if (!solo_free_header(h, c)) {
89-
stats->free_bytes +=
90-
chunksz_to_bytes(h, chunk_size(h, c));
91-
}
80+
*alloc_bytes += chunksz_to_bytes(h, chunk_size(h, c));
81+
} else if (!solo_free_header(h, c)) {
82+
*free_bytes += chunksz_to_bytes(h, chunk_size(h, c));
9283
}
93-
c = right_chunk(h, c);
94-
} while (c != h->end_chunk);
84+
}
9585
}
9686

97-
#endif
98-
9987
bool sys_heap_validate(struct sys_heap *heap)
10088
{
10189
struct z_heap *h = heap->heap;
10290
chunkid_t c;
103-
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
104-
struct sys_heap_runtime_stats stat1, stat2;
105-
#endif
10691

10792
/*
10893
* Walk through the chunks linearly, verifying sizes and end pointer.
@@ -123,11 +108,13 @@ bool sys_heap_validate(struct sys_heap *heap)
123108
* free bytes, then compare with the results of
124109
* sys_heap_runtime_stats_get function.
125110
*/
126-
sys_heap_runtime_stats_get(heap, &stat1);
127-
sys_heap_get_info(heap, &stat2);
111+
size_t allocated_bytes, free_bytes;
112+
struct sys_heap_runtime_stats stat;
128113

129-
if ((stat1.allocated_bytes != stat2.allocated_bytes) ||
130-
(stat1.free_bytes != stat2.free_bytes)) {
114+
get_alloc_info(h, &allocated_bytes, &free_bytes);
115+
sys_heap_runtime_stats_get(heap, &stat);
116+
if ((stat.allocated_bytes != allocated_bytes) ||
117+
(stat.free_bytes != free_bytes)) {
131118
return false;
132119
}
133120
#endif
@@ -393,20 +380,7 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
393380

394381
if (dump_chunks) {
395382
printk("\nChunk dump:\n");
396-
}
397-
free_bytes = allocated_bytes = 0;
398-
for (chunkid_t c = 0; ; c = right_chunk(h, c)) {
399-
if (chunk_used(h, c)) {
400-
if ((c != 0) && (c != h->end_chunk)) {
401-
/* 1st and last are always allocated for internal purposes */
402-
allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
403-
}
404-
} else {
405-
if (!solo_free_header(h, c)) {
406-
free_bytes += chunksz_to_bytes(h, chunk_size(h, c));
407-
}
408-
}
409-
if (dump_chunks) {
383+
for (chunkid_t c = 0; ; c = right_chunk(h, c)) {
410384
printk("chunk %4d: [%c] size=%-4d left=%-4d right=%d\n",
411385
c,
412386
chunk_used(h, c) ? '*'
@@ -415,12 +389,13 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
415389
chunk_size(h, c),
416390
left_chunk(h, c),
417391
right_chunk(h, c));
418-
}
419-
if (c == h->end_chunk) {
420-
break;
392+
if (c == h->end_chunk) {
393+
break;
394+
}
421395
}
422396
}
423397

398+
get_alloc_info(h, &allocated_bytes, &free_bytes);
424399
/* The end marker chunk has a header. It is part of the overhead. */
425400
total = h->end_chunk * CHUNK_UNIT + chunk_header_bytes(h);
426401
overhead = total - free_bytes - allocated_bytes;

0 commit comments

Comments
 (0)