Skip to content

Commit 4bd7e0b

Browse files
JordanYateskartben
authored andcommitted
samples: sys_heap: extend with k_heap_array_get
Extend the sample with a demonstration of `k_heap_array_get`. Signed-off-by: Jordan Yates <[email protected]>
1 parent f11bddb commit 4bd7e0b

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

samples/basic/sys_heap/README.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ Sample Output
3333

3434
.. code-block:: console
3535
36-
System heap sample
37-
38-
allocated 0, free 196, max allocated 0, heap size 256
39-
allocated 156, free 36, max allocated 156, heap size 256
40-
allocated 100, free 92, max allocated 156, heap size 256
41-
allocated 0, free 196, max allocated 156, heap size 256
36+
System heap sample
37+
38+
allocated 0, free 196, max allocated 0, heap size 256
39+
allocated 156, free 36, max allocated 156, heap size 256
40+
allocated 100, free 92, max allocated 156, heap size 256
41+
allocated 0, free 196, max allocated 156, heap size 256
42+
1 static heap(s) allocated:
43+
0 - address 0x80552a8 allocated 12, free 180, max allocated 12, heap size 256
44+
2 heap(s) allocated (including static):
45+
0 - address 0x80552a8 allocated 12, free 180, max allocated 12, heap size 256
46+
1 - address 0x805530c allocated 0, free 196, max allocated 156, heap size 256

samples/basic/sys_heap/src/main.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static char heap_mem[HEAP_SIZE];
1515
static struct sys_heap heap;
1616

1717
static void print_sys_memory_stats(struct sys_heap *);
18+
static void print_static_heaps(void);
1819
static void print_all_heaps(void);
1920

2021
int main(void)
@@ -35,7 +36,17 @@ int main(void)
3536
sys_heap_free(&heap, p);
3637
print_sys_memory_stats(&heap);
3738

39+
/* Allocate from our custom k_heap to make the output
40+
* more interesting and prevent compiler optimisations from
41+
* discarding it.
42+
*/
43+
p = k_heap_alloc(&my_kernel_heap, 10, K_FOREVER);
44+
45+
print_static_heaps();
3846
print_all_heaps();
47+
48+
/* Cleanup memory */
49+
k_heap_free(&my_kernel_heap, p);
3950
return 0;
4051
}
4152

@@ -50,16 +61,30 @@ static void print_sys_memory_stats(struct sys_heap *hp)
5061
stats.max_allocated_bytes, HEAP_SIZE);
5162
}
5263

64+
static void print_static_heaps(void)
65+
{
66+
struct k_heap *ha;
67+
int n;
68+
69+
n = k_heap_array_get(&ha);
70+
printk("%d static heap(s) allocated:\n", n);
71+
72+
for (int i = 0; i < n; i++) {
73+
printk("\t%d - address %p ", i, &ha[i]);
74+
print_sys_memory_stats(&ha[i].heap);
75+
}
76+
}
77+
5378
static void print_all_heaps(void)
5479
{
5580
struct sys_heap **ha;
56-
size_t i, n;
81+
int n;
5782

5883
n = sys_heap_array_get(&ha);
59-
printk("There are %zu heaps allocated:\n", n);
84+
printk("%d heap(s) allocated (including static):\n", n);
6085

61-
for (i = 0; i < n; i++) {
62-
printk("\t%zu - address %p ", i, ha[i]);
86+
for (int i = 0; i < n; i++) {
87+
printk("\t%d - address %p ", i, ha[i]);
6388
print_sys_memory_stats(ha[i]);
6489
}
6590
}

0 commit comments

Comments
 (0)