Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
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>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/* Simple, fast heap implementation.
18 *
19 * A more or less conventional segregated fit allocator with
20 * power-of-two buckets.
21 *
22 * Excellent space efficiency. Chunks can be split arbitrarily in 8
23 * byte units. Overhead is only four bytes per allocated chunk (eight
24 * bytes for heaps >256kb or on 64 bit systems), plus a log2-sized
25 * array of 2-word bucket headers. No coarse alignment restrictions
26 * on blocks, they can be split and merged (in units of 8 bytes)
27 * arbitrarily.
28 *
29 * Simple API. Initialize at runtime with any blob of memory and not
30 * a macro-generated, carefully aligned static array. Allocate and
31 * free by user pointer and not an opaque block handle.
32 *
33 * Good fragmentation resistance. Freed blocks are always immediately
34 * merged with adjacent free blocks. Allocations are attempted from a
35 * sample of the smallest bucket that might fit, falling back rapidly
36 * to the smallest block guaranteed to fit. Split memory remaining in
37 * the chunk is always returned immediately to the heap for other
38 * allocation.
39 *
40 * Excellent performance with firmly bounded runtime. All operations
41 * are constant time (though there is a search of the smallest bucket
42 * that has a compile-time-configurable upper bound, setting this to
43 * extreme values results in an effectively linear search of the
44 * list), objectively fast (~hundred instructions) and and amenable to
45 * locked operation.
46 */
47
48/* Note: the init_mem/bytes fields are for the static initializer to
49 * have somewhere to put the arguments. The actual heap metadata at
50 * runtime lives in the heap memory itself and this struct simply
51 * functions as an opaque pointer. Would be good to clean this up and
52 * put the two values somewhere else, though it would make
53 * SYS_HEAP_DEFINE a little hairy to write.
54 */
55struct sys_heap {
56 struct z_heap *heap;
57 void *init_mem;
58 size_t init_bytes;
59};
60
61struct z_heap_stress_result {
62 uint32_t total_allocs;
63 uint32_t successful_allocs;
64 uint32_t total_frees;
65 uint64_t accumulated_in_use_bytes;
66};
67
68#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
69
70struct sys_heap_runtime_stats {
71 size_t free_bytes;
72 size_t allocated_bytes;
73};
74
82int sys_heap_runtime_stats_get(struct sys_heap *heap,
83 struct sys_heap_runtime_stats *stats);
84
85#endif
86
95void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes);
96
114void *sys_heap_alloc(struct sys_heap *heap, size_t bytes);
115
129void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes);
130
144void sys_heap_free(struct sys_heap *heap, void *mem);
145
169void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
170 size_t align, size_t bytes);
171
172#define sys_heap_realloc(heap, ptr, bytes) \
173 sys_heap_aligned_realloc(heap, ptr, 0, bytes)
174
189size_t sys_heap_usable_size(struct sys_heap *heap, void *mem);
190
204bool sys_heap_validate(struct sys_heap *heap);
205
235void sys_heap_stress(void *(*alloc_fn)(void *arg, size_t bytes),
236 void (*free_fn)(void *arg, void *p),
237 void *arg, size_t total_bytes,
238 uint32_t op_count,
239 void *scratch_mem, size_t scratch_bytes,
240 int target_percent,
241 struct z_heap_stress_result *result);
242
251void sys_heap_print_info(struct sys_heap *heap, bool dump_chunks);
252
253
254#ifdef __cplusplus
255}
256#endif
257
258#endif /* ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_ */
struct k_pipe p
Definition: kobject.c:1316
void * ptr
Definition: printk.c:79
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
Definition: errno.c:36
Definition: sys_heap.h:55
size_t init_bytes
Definition: sys_heap.h:58
struct z_heap * heap
Definition: sys_heap.h:56
void * init_mem
Definition: sys_heap.h:57
void * sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr, size_t align, size_t bytes)
Expand the size of an existing allocation.
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.
void * sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
Allocate aligned memory from a sys_heap.
bool sys_heap_validate(struct sys_heap *heap)
Validate heap integrity.
void sys_heap_free(struct sys_heap *heap, void *mem)
Free memory into a sys_heap.
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.