diff --git a/samples/basic/sys_heap/CMakeLists.txt b/samples/basic/sys_heap/CMakeLists.txt new file mode 100644 index 0000000000..15d3406b63 --- /dev/null +++ b/samples/basic/sys_heap/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sys_heap) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/basic/sys_heap/README.rst b/samples/basic/sys_heap/README.rst new file mode 100644 index 0000000000..335084a901 --- /dev/null +++ b/samples/basic/sys_heap/README.rst @@ -0,0 +1,41 @@ +.. _system_heap: + +System heap +########### + +Overview +******** + +A simple sample that can be used with any :ref:`supported board ` and +prints system heap usage to the console. + +Building +******************** + +This application can be built on native_posix as follows: + +.. zephyr-app-commands:: + :zephyr-app: samples/basic/sys_heap + :host-os: unix + :board: native_posix + :goals: build + :compact: + +To build for another board, change "native_posix" above to that board's name. + +Running +******* + +Run build/zephyr/zephyr.exe + +Sample Output +************* + +.. code-block:: console + + System heap sample + + allocated 0, free 196, max allocated 0, heap size 256 + allocated 156, free 36, max allocated 156, heap size 256 + allocated 100, free 92, max allocated 156, heap size 256 + allocated 0, free 196, max allocated 156, heap size 256 diff --git a/samples/basic/sys_heap/prj.conf b/samples/basic/sys_heap/prj.conf new file mode 100644 index 0000000000..2ffddad1e9 --- /dev/null +++ b/samples/basic/sys_heap/prj.conf @@ -0,0 +1 @@ +CONFIG_SYS_HEAP_RUNTIME_STATS=y diff --git a/samples/basic/sys_heap/sample.yaml b/samples/basic/sys_heap/sample.yaml new file mode 100644 index 0000000000..b82e7a04a7 --- /dev/null +++ b/samples/basic/sys_heap/sample.yaml @@ -0,0 +1,17 @@ +sample: + description: System heap sample showing how to use sys_heap functions + name: Basic system heap sample +common: + integration_platforms: + - native_posix + harness: console + harness_config: + type: multi_line + ordered: true + regex: + - ".*allocated 15.,.*" + - ".*allocated 10.,.*" + - ".*allocated 0, free ..., max allocated ..., heap size 256.*" +tests: + sample.basic.sys_heap: + tags: heap statistics dynamic_memory diff --git a/samples/basic/sys_heap/src/main.c b/samples/basic/sys_heap/src/main.c new file mode 100644 index 0000000000..3a6bddf025 --- /dev/null +++ b/samples/basic/sys_heap/src/main.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Jeppe Odgaard + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define HEAP_SIZE 256 + +static char heap_mem[HEAP_SIZE]; +static struct sys_heap heap; + +void print_sys_memory_stats(void); + +void main(void) +{ + void *p; + + printk("System heap sample\n\n"); + + sys_heap_init(&heap, heap_mem, HEAP_SIZE); + print_sys_memory_stats(); + + p = sys_heap_alloc(&heap, 150); + print_sys_memory_stats(); + + p = sys_heap_realloc(&heap, p, 100); + print_sys_memory_stats(); + + sys_heap_free(&heap, p); + print_sys_memory_stats(); +} + +void print_sys_memory_stats(void) +{ + struct sys_memory_stats stats; + + sys_heap_runtime_stats_get(&heap, &stats); + + printk("allocated %zu, free %zu, max allocated %zu, heap size %u\n", + stats.allocated_bytes, stats.free_bytes, + stats.max_allocated_bytes, HEAP_SIZE); +}