Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
sys_heap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 * Copyright (c) 2026 Qualcomm Technologies, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_
8#define ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_
9
10#include <stddef.h>
11#include <stdbool.h>
12#include <zephyr/types.h>
14#include <zephyr/toolchain.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20/* Simple, fast heap implementation.
21 *
22 * A more or less conventional segregated fit allocator with
23 * power-of-two buckets.
24 *
25 * Excellent space efficiency. Chunks can be split arbitrarily in 8
26 * byte units. Overhead is only four bytes per allocated chunk (eight
27 * bytes for heaps >256kb or on 64 bit systems), plus a log2-sized
28 * array of 2-word bucket headers. No coarse alignment restrictions
29 * on blocks, they can be split and merged (in units of 8 bytes)
30 * arbitrarily.
31 *
32 * Simple API. Initialize at runtime with any blob of memory and not
33 * a macro-generated, carefully aligned static array. Allocate and
34 * free by user pointer and not an opaque block handle.
35 *
36 * Good fragmentation resistance. Freed blocks are always immediately
37 * merged with adjacent free blocks. Allocations are attempted from a
38 * sample of the smallest bucket that might fit, falling back rapidly
39 * to the smallest block guaranteed to fit. Split memory remaining in
40 * the chunk is always returned immediately to the heap for other
41 * allocation.
42 *
43 * Excellent performance with firmly bounded runtime. All operations
44 * are constant time (though there is a search of the smallest bucket
45 * that has a compile-time-configurable upper bound, setting this to
46 * extreme values results in an effectively linear search of the
47 * list), objectively fast (~hundred instructions) and amenable to
48 * locked operation.
49 */
50
51/* Note: the init_mem/bytes fields are for the static initializer to
52 * have somewhere to put the arguments. The actual heap metadata at
53 * runtime lives in the heap memory itself and this struct simply
54 * functions as an opaque pointer. Would be good to clean this up and
55 * put the two values somewhere else, though it would make
56 * SYS_HEAP_DEFINE a little hairy to write.
57 */
58/* Forward declaration; full definition in <zephyr/sys/bitarray.h>. */
59struct sys_bitarray;
60
61struct sys_heap {
62 struct z_heap *heap;
63 void *init_mem;
64 size_t init_bytes;
65#ifdef CONFIG_SYS_HEAP_KASAN
66 struct sys_bitarray *kasan_ba;
67#endif
68};
69
70struct z_heap_stress_result {
71 uint32_t total_allocs;
72 uint32_t successful_allocs;
73 uint32_t total_frees;
74 uint64_t accumulated_in_use_bytes;
75};
76
84
93 struct sys_memory_stats *stats);
94
105
117int sys_heap_get_largest_free_block(struct sys_heap *heap, size_t *bytes);
118
127void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes);
128
146void *sys_heap_alloc(struct sys_heap *heap, size_t bytes);
147
161void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes);
162
173void *sys_heap_noalign_alloc(struct sys_heap *heap, size_t align, size_t bytes);
174
188void sys_heap_free(struct sys_heap *heap, void *mem);
189
207void *sys_heap_realloc(struct sys_heap *heap, void *ptr, size_t bytes);
208
224void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
225 size_t align, size_t bytes);
226
241size_t sys_heap_usable_size(struct sys_heap *heap, void *mem);
242
256#ifdef CONFIG_SYS_HEAP_VALIDATE
257bool sys_heap_validate(struct sys_heap *heap);
258#else
259static inline bool sys_heap_validate(struct sys_heap *heap)
260{
261 ARG_UNUSED(heap);
262 return true;
263}
264#endif
265
295void sys_heap_stress(void *(*alloc_fn)(void *arg, size_t bytes),
296 void (*free_fn)(void *arg, void *p),
297 void *arg, size_t total_bytes,
298 uint32_t op_count,
299 void *scratch_mem, size_t scratch_bytes,
300 int target_percent,
301 struct z_heap_stress_result *result);
302
311void sys_heap_print_info(struct sys_heap *heap, bool dump_chunks);
312
321
329int sys_heap_array_get(struct sys_heap ***heap);
330
343#ifdef CONFIG_SYS_HEAP_THREAD_STATS
344void sys_heap_stats_log(struct sys_heap *heap);
345#endif
346
364#ifdef CONFIG_SYS_HEAP_CALLER_POINTER
365void *sys_heap_get_caller(struct sys_heap *heap, void *mem);
366#endif
367
371
372#ifdef __cplusplus
373}
374#endif
375
377
390void sys_heap_release_hook(void *mem, size_t bytes);
391
393
394#endif /* ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_ */
int sys_heap_runtime_stats_get(struct sys_heap *heap, struct sys_memory_stats *stats)
Get the runtime statistics of a sys_heap.
void * sys_heap_noalign_alloc(struct sys_heap *heap, size_t align, size_t bytes)
Allocate memory from a sys_heap.
void * sys_heap_realloc(struct sys_heap *heap, void *ptr, size_t bytes)
Expand the size of an existing allocation.
void * sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr, size_t align, size_t bytes)
Expand the size of an existing allocation.
int sys_heap_runtime_stats_reset_max(struct sys_heap *heap)
Reset the maximum heap usage.
int sys_heap_array_get(struct sys_heap ***heap)
Get the array of saved heap pointers.
void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes)
Initialize sys_heap.
void * sys_heap_alloc(struct sys_heap *heap, size_t bytes)
Allocate memory from a sys_heap.
static bool sys_heap_validate(struct sys_heap *heap)
Validate heap integrity.
Definition sys_heap.h:259
void * sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
Allocate aligned memory from a sys_heap.
int sys_heap_get_largest_free_block(struct sys_heap *heap, size_t *bytes)
Get the size of the largest free block in a sys_heap.
void sys_heap_free(struct sys_heap *heap, void *mem)
Free memory into a sys_heap.
int sys_heap_array_save(struct sys_heap *heap)
Save the heap pointer.
void sys_heap_stress(void *(*alloc_fn)(void *arg, size_t bytes), void(*free_fn)(void *arg, void *p), void *arg, size_t total_bytes, uint32_t op_count, void *scratch_mem, size_t scratch_bytes, int target_percent, struct z_heap_stress_result *result)
sys_heap stress test rig
void sys_heap_print_info(struct sys_heap *heap, bool dump_chunks)
Print heap internal structure information to the console.
size_t sys_heap_usable_size(struct sys_heap *heap, void *mem)
Return allocated memory size.
Memory Statistics.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
Definition sys_heap.h:61
size_t init_bytes
Definition sys_heap.h:64
struct z_heap * heap
Definition sys_heap.h:62
void * init_mem
Definition sys_heap.h:63
Definition mem_stats.h:24
Macros to abstract toolchain specific capabilities.