Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
mem_blocks.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13#ifndef ZEPHYR_INCLUDE_SYS_MEM_BLOCKS_H_
14#define ZEPHYR_INCLUDE_SYS_MEM_BLOCKS_H_
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#include <stddef.h>
21#include <stdint.h>
22
23#include <zephyr/kernel.h>
24#include <zephyr/math/ilog2.h>
25#include <zephyr/sys/bitarray.h>
27
28#define MAX_MULTI_ALLOCATORS 8
29
39struct sys_mem_blocks;
40
44struct sys_multi_mem_blocks;
45
51typedef struct sys_mem_blocks sys_mem_blocks_t;
52
58typedef struct sys_multi_mem_blocks sys_multi_mem_blocks_t;
59
78typedef sys_mem_blocks_t *(*sys_multi_mem_blocks_choice_fn_t)
79 (struct sys_multi_mem_blocks *group, void *cfg);
80
85struct sys_mem_blocks_info {
86 uint32_t num_blocks; /* Total number of blocks */
87 uint8_t blk_sz_shift; /* Bit shift for block size */
88#ifdef CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS
89 uint32_t used_blocks; /* Current number of blocks in use */
90 uint32_t max_used_blocks; /* Maximum number of blocks in use */
91#endif
92};
93
94struct sys_mem_blocks {
95 struct sys_mem_blocks_info info;
96
97 /* Memory block buffer */
98 uint8_t *buffer;
99
100 /* Bitmap of allocated blocks */
101 sys_bitarray_t *bitmap;
102
103#ifdef CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS
104 /* Spinlock guarding access to memory block internals */
105 struct k_spinlock lock;
106#endif
107#ifdef CONFIG_OBJ_CORE_SYS_MEM_BLOCKS
108 struct k_obj_core obj_core;
109#endif
110};
111
112struct sys_multi_mem_blocks {
113 /* Number of allocators in this group */
114 int num_allocators;
117};
118
128#define _SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, blk_sz, num_blks, buf, mbmod) \
129 _SYS_BITARRAY_DEFINE(_sys_mem_blocks_bitmap_##name, \
130 num_blks, mbmod); \
131 mbmod struct sys_mem_blocks name = { \
132 .info = {num_blks, ilog2(blk_sz)}, \
133 .buffer = buf, \
134 .bitmap = &_sys_mem_blocks_bitmap_##name, \
135 }; \
136 STRUCT_SECTION_ITERABLE_ALTERNATE(sys_mem_blocks_ptr, \
137 sys_mem_blocks *, \
138 __##name##_ptr) = &name; \
139 LINKER_KEEP(__##name##_ptr);
140
150#define _SYS_MEM_BLOCKS_DEFINE(name, blk_sz, num_blks, balign, mbmod) \
151 mbmod uint8_t __noinit_named(sys_mem_blocks_buf_##name) \
152 __aligned(WB_UP(balign)) \
153 _sys_mem_blocks_buf_##name[num_blks * WB_UP(blk_sz)]; \
154 _SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, blk_sz, num_blks, \
155 _sys_mem_blocks_buf_##name, \
156 mbmod);
157
170#define SYS_MEM_BLOCKS_DEFINE(name, blk_sz, num_blks, buf_align) \
171 _SYS_MEM_BLOCKS_DEFINE(name, blk_sz, num_blks, buf_align,)
172
181#define SYS_MEM_BLOCKS_DEFINE_STATIC(name, blk_sz, num_blks, buf_align) \
182 _SYS_MEM_BLOCKS_DEFINE(name, blk_sz, num_blks, buf_align, static)
183
184
193#define SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, blk_sz, num_blks, buf) \
194 _SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, blk_sz, num_blks, buf,)
195
204#define SYS_MEM_BLOCKS_DEFINE_STATIC_WITH_EXT_BUF(name, blk_sz, num_blks, buf) \
205 _SYS_MEM_BLOCKS_DEFINE_WITH_EXT_BUF(name, blk_sz, num_blks, buf, static)
206
223int sys_mem_blocks_alloc(sys_mem_blocks_t *mem_block, size_t count,
224 void **out_blocks);
225
241 void **out_block);
242
259int sys_mem_blocks_get(sys_mem_blocks_t *mem_block, void *in_block, size_t count);
260
271int sys_mem_blocks_is_region_free(sys_mem_blocks_t *mem_block, void *in_block, size_t count);
272
287int sys_mem_blocks_free(sys_mem_blocks_t *mem_block, size_t count,
288 void **in_blocks);
289
303int sys_mem_blocks_free_contiguous(sys_mem_blocks_t *mem_block, void *block, size_t count);
304
305#ifdef CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS
317int sys_mem_blocks_runtime_stats_get(sys_mem_blocks_t *mem_block,
318 struct sys_memory_stats *stats);
319
330int sys_mem_blocks_runtime_stats_reset_max(sys_mem_blocks_t *mem_block);
331#endif
332
346
357 sys_mem_blocks_t *alloc);
358
381 void *cfg, size_t count,
382 void **out_blocks,
383 size_t *blk_size);
384
401 size_t count, void **in_blocks);
402
405#ifdef __cplusplus
406}
407#endif
408
409#endif /* ZEPHYR_INCLUDE_SYS_MEM_BLOCKS_H_ */
struct sys_bitarray sys_bitarray_t
Bitarray structure.
Definition: bitarray.h:48
void sys_multi_mem_blocks_add_allocator(sys_multi_mem_blocks_t *group, sys_mem_blocks_t *alloc)
Add an allocator to an allocator group.
int sys_mem_blocks_get(sys_mem_blocks_t *mem_block, void *in_block, size_t count)
Force allocation of a specified blocks in a memory block object.
sys_mem_blocks_t *(* sys_multi_mem_blocks_choice_fn_t)(struct sys_multi_mem_blocks *group, void *cfg)
Multi memory blocks allocator choice function.
Definition: mem_blocks.h:79
int sys_mem_blocks_free_contiguous(sys_mem_blocks_t *mem_block, void *block, size_t count)
Free contiguous multiple memory blocks.
int sys_mem_blocks_alloc(sys_mem_blocks_t *mem_block, size_t count, void **out_blocks)
Allocate multiple memory blocks.
int sys_mem_blocks_alloc_contiguous(sys_mem_blocks_t *mem_block, size_t count, void **out_block)
Allocate a contiguous set of memory blocks.
int sys_mem_blocks_is_region_free(sys_mem_blocks_t *mem_block, void *in_block, size_t count)
check if the region is free
int sys_multi_mem_blocks_free(sys_multi_mem_blocks_t *group, size_t count, void **in_blocks)
Free memory allocated from multi memory blocks allocator group.
struct sys_mem_blocks sys_mem_blocks_t
Memory Blocks Allocator.
Definition: mem_blocks.h:51
struct sys_multi_mem_blocks sys_multi_mem_blocks_t
Multi Memory Blocks Allocator.
Definition: mem_blocks.h:58
void sys_multi_mem_blocks_init(sys_multi_mem_blocks_t *group, sys_multi_mem_blocks_choice_fn_t choice_fn)
Initialize multi memory blocks allocator group.
int sys_mem_blocks_free(sys_mem_blocks_t *mem_block, size_t count, void **in_blocks)
Free multiple memory blocks.
int sys_multi_mem_blocks_alloc(sys_multi_mem_blocks_t *group, void *cfg, size_t count, void **out_blocks, size_t *blk_size)
Allocate memory from multi memory blocks allocator group.
Provide ilog2() function.
Public kernel APIs.
#define MAX_MULTI_ALLOCATORS
Definition: mem_blocks.h:28
Memory Statistics.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Object core structure.
Definition: obj_core.h:121
Kernel Spin Lock.
Definition: spinlock.h:45
Definition: mem_stats.h:24