zephyr/samples/basic/sys_heap/src/main.c
Keith Packard 0b90fd5adf samples, tests, boards: Switch main return type from void to int
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.

Most of these changes were automated using coccinelle with the following
script:

@@
@@
- void
+ int
main(...) {
	...
-	return;
+	return 0;
	...
}

Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 07:49:41 +09:00

47 lines
869 B
C

/*
* Copyright (c) 2022 Jeppe Odgaard
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/sys_heap.h>
#define HEAP_SIZE 256
static char heap_mem[HEAP_SIZE];
static struct sys_heap heap;
void print_sys_memory_stats(void);
int 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();
return 0;
}
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);
}