Skip to content

Commit 7d27309

Browse files
Jeppe Odgaardstephanosio
authored andcommitted
samples: Basic system heap Demo
A demo to showcase some of the sys_heap functions Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent 010730a commit 7d27309

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

samples/basic/sys_heap/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(sys_heap)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/basic/sys_heap/README.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. _system_heap:
2+
3+
System heap
4+
###########
5+
6+
Overview
7+
********
8+
9+
A simple sample that can be used with any :ref:`supported board <boards>` and
10+
prints system heap usage to the console.
11+
12+
Building
13+
********************
14+
15+
This application can be built on native_posix as follows:
16+
17+
.. zephyr-app-commands::
18+
:zephyr-app: samples/basic/sys_heap
19+
:host-os: unix
20+
:board: native_posix
21+
:goals: build
22+
:compact:
23+
24+
To build for another board, change "native_posix" above to that board's name.
25+
26+
Running
27+
*******
28+
29+
Run build/zephyr/zephyr.exe
30+
31+
Sample Output
32+
*************
33+
34+
.. code-block:: console
35+
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

samples/basic/sys_heap/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SYS_HEAP_RUNTIME_STATS=y

samples/basic/sys_heap/sample.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sample:
2+
description: System heap sample showing how to use sys_heap functions
3+
name: Basic system heap sample
4+
common:
5+
integration_platforms:
6+
- native_posix
7+
harness: console
8+
harness_config:
9+
type: multi_line
10+
ordered: true
11+
regex:
12+
- ".*allocated 15.,.*"
13+
- ".*allocated 10.,.*"
14+
- ".*allocated 0, free ..., max allocated ..., heap size 256.*"
15+
tests:
16+
sample.basic.sys_heap:
17+
tags: heap statistics dynamic_memory

samples/basic/sys_heap/src/main.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022 Jeppe Odgaard
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/sys_heap.h>
9+
10+
#define HEAP_SIZE 256
11+
12+
static char heap_mem[HEAP_SIZE];
13+
static struct sys_heap heap;
14+
15+
void print_sys_memory_stats(void);
16+
17+
void main(void)
18+
{
19+
void *p;
20+
21+
printk("System heap sample\n\n");
22+
23+
sys_heap_init(&heap, heap_mem, HEAP_SIZE);
24+
print_sys_memory_stats();
25+
26+
p = sys_heap_alloc(&heap, 150);
27+
print_sys_memory_stats();
28+
29+
p = sys_heap_realloc(&heap, p, 100);
30+
print_sys_memory_stats();
31+
32+
sys_heap_free(&heap, p);
33+
print_sys_memory_stats();
34+
}
35+
36+
void print_sys_memory_stats(void)
37+
{
38+
struct sys_memory_stats stats;
39+
40+
sys_heap_runtime_stats_get(&heap, &stats);
41+
42+
printk("allocated %zu, free %zu, max allocated %zu, heap size %u\n",
43+
stats.allocated_bytes, stats.free_bytes,
44+
stats.max_allocated_bytes, HEAP_SIZE);
45+
}

0 commit comments

Comments
 (0)