ChibiOS/RT Architecture - Reference Manual - Guides |
Heap Allocator related APIs.
The heap allocator implements a first-fit strategy and its APIs are functionally equivalent to the usual malloc()
and free()
library functions. The main difference is that the OS heap APIs are guaranteed to be thread safe.
By enabling the CH_USE_MALLOC_HEAP
option the heap manager will use the runtime-provided malloc()
and free()
as backend for the heap APIs instead of the system provided allocator.
In order to use the heap APIs the CH_USE_HEAP
option must be enabled in chconf.h
.
Data Structures | |
union | heap_header |
Memory heap block header. More... | |
struct | memory_heap |
Structure describing a memory heap. More... | |
Functions | |
void | heap_init (void) |
Initializes the default heap. | |
void | chHeapInit (MemoryHeap *heapp, void *buf, size_t size) |
Initializes a memory heap from a static memory area. | |
void * | chHeapAlloc (MemoryHeap *heapp, size_t size) |
Allocates a block of memory from the heap by using the first-fit algorithm. | |
void | chHeapFree (void *p) |
Frees a previously allocated memory block. | |
size_t | chHeapStatus (MemoryHeap *heapp, size_t *sizep) |
Reports the heap status. |
void heap_init | ( | void | ) |
Initializes the default heap.
Definition at line 73 of file chheap.c.
References chMtxInit(), chSemInit(), memory_heap::h_free, memory_heap::h_mtx, memory_heap::h_provider, heap_header::size, and heap_header::u.
Referenced by chSysInit().
void chHeapInit | ( | MemoryHeap * | heapp, | |
void * | buf, | |||
size_t | size | |||
) |
Initializes a memory heap from a static memory area.
align_t
type size.[out] | heapp | pointer to the memory heap descriptor to be initialized |
[in] | buf | heap buffer base |
[in] | size | heap size |
Definition at line 93 of file chheap.c.
References chDbgCheck, chMtxInit(), chSemInit(), memory_heap::h_free, memory_heap::h_mtx, memory_heap::h_provider, MEM_IS_ALIGNED, heap_header::size, and heap_header::u.
void * chHeapAlloc | ( | MemoryHeap * | heapp, | |
size_t | size | |||
) |
Allocates a block of memory from the heap by using the first-fit algorithm.
The allocated block is guaranteed to be properly aligned for a pointer data type (align_t
).
[in] | heapp | pointer to a heap descriptor or NULL in order to access the default heap. |
[in] | size | the size of the block to be allocated. Note that the allocated block may be a bit bigger than the requested size for alignment and fragmentation reasons. |
NULL | if the block cannot be allocated. |
Definition at line 124 of file chheap.c.
References memory_heap::h_free, memory_heap::h_provider, MEM_ALIGN_SIZE, heap_header::size, and heap_header::u.
Referenced by chThdCreateFromHeap().
void chHeapFree | ( | void * | p | ) |
Frees a previously allocated memory block.
[in] | p | pointer to the memory block to be freed |
Definition at line 184 of file chheap.c.
References chDbgAssert, chDbgCheck, memory_heap::h_free, heap_header::size, and heap_header::u.
Referenced by chThdRelease().
size_t chHeapStatus | ( | MemoryHeap * | heapp, | |
size_t * | sizep | |||
) |
Reports the heap status.
CH_USE_MALLOC_HEAP
configuration option is used (it always returns zero).[in] | heapp | pointer to a heap descriptor or NULL in order to access the default heap. |
[in] | sizep | pointer to a variable that will receive the total fragmented free space |
Definition at line 238 of file chheap.c.
References memory_heap::h_free, and heap_header::u.