Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sys_heap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_
7#define ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_
8
9#include <stddef.h>
10#include <stdbool.h>
11#include <zephyr/types.h>
13#include <zephyr/toolchain.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/* Simple, fast heap implementation.
20 *
21 * A more or less conventional segregated fit allocator with
22 * power-of-two buckets.
23 *
24 * Excellent space efficiency. Chunks can be split arbitrarily in 8
25 * byte units. Overhead is only four bytes per allocated chunk (eight
26 * bytes for heaps >256kb or on 64 bit systems), plus a log2-sized
27 * array of 2-word bucket headers. No coarse alignment restrictions
28 * on blocks, they can be split and merged (in units of 8 bytes)
29 * arbitrarily.
30 *
31 * Simple API. Initialize at runtime with any blob of memory and not
32 * a macro-generated, carefully aligned static array. Allocate and
33 * free by user pointer and not an opaque block handle.
34 *
35 * Good fragmentation resistance. Freed blocks are always immediately
36 * merged with adjacent free blocks. Allocations are attempted from a
37 * sample of the smallest bucket that might fit, falling back rapidly
38 * to the smallest block guaranteed to fit. Split memory remaining in
39 * the chunk is always returned immediately to the heap for other
40 * allocation.
41 *
42 * Excellent performance with firmly bounded runtime. All operations
43 * are constant time (though there is a search of the smallest bucket
44 * that has a compile-time-configurable upper bound, setting this to
45 * extreme values results in an effectively linear search of the
46 * list), objectively fast (~hundred instructions) and amenable to
47 * locked operation.
48 */
49
50/* Note: the init_mem/bytes fields are for the static initializer to
51 * have somewhere to put the arguments. The actual heap metadata at
52 * runtime lives in the heap memory itself and this struct simply
53 * functions as an opaque pointer. Would be good to clean this up and
54 * put the two values somewhere else, though it would make
55 * SYS_HEAP_DEFINE a little hairy to write.
56 */
57struct sys_heap {
58 struct z_heap *heap;
59 void *init_mem;
60 size_t init_bytes;
61};
62
63struct z_heap_stress_result {
64 uint32_t total_allocs;
65 uint32_t successful_allocs;
66 uint32_t total_frees;
67 uint64_t accumulated_in_use_bytes;
68};
69
84 struct sys_memory_stats *stats);
85
96
105void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes);
106
124void *sys_heap_alloc(struct sys_heap *heap, size_t bytes);
125
139void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes);
140
151void *sys_heap_noalign_alloc(struct sys_heap *heap, size_t align, size_t bytes);
152
166void sys_heap_free(struct sys_heap *heap, void *mem);
167
185void *sys_heap_realloc(struct sys_heap *heap, void *ptr, size_t bytes);
186
202void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
203 size_t align, size_t bytes);
204
219size_t sys_heap_usable_size(struct sys_heap *heap, void *mem);
220
234#ifdef CONFIG_SYS_HEAP_VALIDATE
235bool sys_heap_validate(struct sys_heap *heap);
236#else
237static inline bool sys_heap_validate(struct sys_heap *heap)
238{
239 ARG_UNUSED(heap);
240 return true;
241}
242#endif
243
273void sys_heap_stress(void *(*alloc_fn)(void *arg, size_t bytes),
274 void (*free_fn)(void *arg, void *p),
275 void *arg, size_t total_bytes,
276 uint32_t op_count,
277 void *scratch_mem, size_t scratch_bytes,
278 int target_percent,
279 struct z_heap_stress_result *result);
280
289void sys_heap_print_info(struct sys_heap *heap, bool dump_chunks);
290
299
307int sys_heap_array_get(struct sys_heap ***heap);
308
313#ifdef __cplusplus
314}
315#endif
316
317#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:237
void * sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
Allocate aligned memory from 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:57
size_t init_bytes
Definition sys_heap.h:60
struct z_heap * heap
Definition sys_heap.h:58
void * init_mem
Definition sys_heap.h:59
Definition mem_stats.h:24
Macros to abstract toolchain specific capabilities.