Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
Heap APIs

Data Structures

struct  k_heap
 

Macros

#define K_HEAP_DEFINE(name, bytes)
 Define a static k_heap. More...
 
#define K_HEAP_DEFINE_NOCACHE(name, bytes)    Z_HEAP_DEFINE_IN_SECT(name, bytes, __nocache)
 Define a static k_heap in uncached memory. More...
 

Functions

void k_heap_init (struct k_heap *h, void *mem, size_t bytes)
 Initialize a k_heap. More...
 
voidk_heap_aligned_alloc (struct k_heap *h, size_t align, size_t bytes, k_timeout_t timeout)
 Allocate aligned memory from a k_heap. More...
 
voidk_heap_alloc (struct k_heap *h, size_t bytes, k_timeout_t timeout)
 Allocate memory from a k_heap. More...
 
void k_heap_free (struct k_heap *h, void *mem)
 Free memory allocated by k_heap_alloc() More...
 
voidk_aligned_alloc (size_t align, size_t size)
 Allocate memory from the heap with a specified alignment. More...
 
voidk_malloc (size_t size)
 Allocate memory from the heap. More...
 
void k_free (void *ptr)
 Free memory allocated from heap. More...
 
voidk_calloc (size_t nmemb, size_t size)
 Allocate memory from heap, array style. More...
 

Detailed Description

Macro Definition Documentation

◆ K_HEAP_DEFINE

#define K_HEAP_DEFINE (   name,
  bytes 
)

#include <include/kernel.h>

Value:
Z_HEAP_DEFINE_IN_SECT(name, bytes, \
__noinit_named(kheap_buf_##name))

Define a static k_heap.

This macro defines and initializes a static memory region and k_heap of the requested size. After kernel start, &name can be used as if k_heap_init() had been called.

Note that this macro enforces a minimum size on the memory region to accommodate metadata requirements. Very small heaps will be padded to fit.

Parameters
nameSymbol name for the struct k_heap object
bytesSize of memory region, in bytes

◆ K_HEAP_DEFINE_NOCACHE

#define K_HEAP_DEFINE_NOCACHE (   name,
  bytes 
)     Z_HEAP_DEFINE_IN_SECT(name, bytes, __nocache)

#include <include/kernel.h>

Define a static k_heap in uncached memory.

This macro defines and initializes a static memory region and k_heap of the requested size in uncache memory. After kernel start, &name can be used as if k_heap_init() had been called.

Note that this macro enforces a minimum size on the memory region to accommodate metadata requirements. Very small heaps will be padded to fit.

Parameters
nameSymbol name for the struct k_heap object
bytesSize of memory region, in bytes

Function Documentation

◆ k_aligned_alloc()

void * k_aligned_alloc ( size_t  align,
size_t  size 
)

#include <include/kernel.h>

Allocate memory from the heap with a specified alignment.

This routine provides semantics similar to aligned_alloc(); memory is allocated from the heap with a specified alignment. However, one minor difference is that k_aligned_alloc() accepts any non-zero size, wherase aligned_alloc() only accepts a size that is an integral multiple of align.

Above, aligned_alloc() refers to: C11 standard (ISO/IEC 9899:2011): 7.22.3.1 The aligned_alloc function (p: 347-348)

Parameters
alignAlignment of memory requested (in bytes).
sizeAmount of memory requested (in bytes).
Returns
Address of the allocated memory if successful; otherwise NULL.

◆ k_calloc()

void * k_calloc ( size_t  nmemb,
size_t  size 
)

#include <include/kernel.h>

Allocate memory from heap, array style.

This routine provides traditional calloc() semantics. Memory is allocated from the heap memory pool and zeroed.

Parameters
nmembNumber of elements in the requested array
sizeSize of each array element (in bytes).
Returns
Address of the allocated memory if successful; otherwise NULL.

◆ k_free()

void k_free ( void ptr)

#include <include/kernel.h>

Free memory allocated from heap.

This routine provides traditional free() semantics. The memory being returned must have been allocated from the heap memory pool or k_mem_pool_malloc().

If ptr is NULL, no operation is performed.

Parameters
ptrPointer to previously allocated memory.
Returns
N/A

◆ k_heap_aligned_alloc()

void * k_heap_aligned_alloc ( struct k_heap h,
size_t  align,
size_t  bytes,
k_timeout_t  timeout 
)

#include <include/kernel.h>

Allocate aligned memory from a k_heap.

Behaves in all ways like k_heap_alloc(), except that the returned memory (if available) will have a starting address in memory which is a multiple of the specified power-of-two alignment value in bytes. The resulting memory can be returned to the heap using k_heap_free().

Note
timeout must be set to K_NO_WAIT if called from ISR.
When CONFIG_MULTITHREADING=n any timeout is treated as K_NO_WAIT.
Function properties (list may not be complete)
isr-ok
Parameters
hHeap from which to allocate
alignAlignment in bytes, must be a power of two
bytesNumber of bytes requested
timeoutHow long to wait, or K_NO_WAIT
Returns
Pointer to memory the caller can now use

◆ k_heap_alloc()

void * k_heap_alloc ( struct k_heap h,
size_t  bytes,
k_timeout_t  timeout 
)

#include <include/kernel.h>

Allocate memory from a k_heap.

Allocates and returns a memory buffer from the memory region owned by the heap. If no memory is available immediately, the call will block for the specified timeout (constructed via the standard timeout API, or K_NO_WAIT or K_FOREVER) waiting for memory to be freed. If the allocation cannot be performed by the expiration of the timeout, NULL will be returned.

Note
timeout must be set to K_NO_WAIT if called from ISR.
When CONFIG_MULTITHREADING=n any timeout is treated as K_NO_WAIT.
Function properties (list may not be complete)
isr-ok
Parameters
hHeap from which to allocate
bytesDesired size of block to allocate
timeoutHow long to wait, or K_NO_WAIT
Returns
A pointer to valid heap memory, or NULL

◆ k_heap_free()

void k_heap_free ( struct k_heap h,
void mem 
)

#include <include/kernel.h>

Free memory allocated by k_heap_alloc()

Returns the specified memory block, which must have been returned from k_heap_alloc(), to the heap for use by other callers. Passing a NULL block is legal, and has no effect.

Parameters
hHeap to which to return the memory
memA valid memory block, or NULL

◆ k_heap_init()

void k_heap_init ( struct k_heap h,
void mem,
size_t  bytes 
)

#include <include/kernel.h>

Initialize a k_heap.

This constructs a synchronized k_heap object over a memory region specified by the user. Note that while any alignment and size can be passed as valid parameters, internal alignment restrictions inside the inner sys_heap mean that not all bytes may be usable as allocated memory.

Parameters
hHeap struct to initialize
memPointer to memory.
bytesSize of memory region, in bytes

◆ k_malloc()

void * k_malloc ( size_t  size)

#include <include/kernel.h>

Allocate memory from the heap.

This routine provides traditional malloc() semantics. Memory is allocated from the heap memory pool.

Parameters
sizeAmount of memory requested (in bytes).
Returns
Address of the allocated memory if successful; otherwise NULL.