SYS/BIOS  7.00
Data Structures | Macros | Typedefs | Functions
HeapMultiBuf.h File Reference

Detailed Description

Multiple fixed size buffer heap manager.

The HeapMultiBuf manager provides functions to allocate and free storage from a heap of type HeapMultiBuf which inherits from IHeap. HeapMultiBuf manages multiple fixed-size memory buffers. Each buffer contains a fixed number of allocable memory 'blocks' of the same size. Simply put, a HeapMultiBuf instance manages a collection of HeapBuf instances. HeapMultiBuf is intended as a fast and deterministic memory manager which can service requests for blocks of arbitrary size.

An example HeapMultiBuf instance might have sixteen 32-byte blocks in one buffer, and four 128-byte blocks in another buffer. A request for memory will be serviced by the smallest possible block, so a request for 100 bytes would receive a 128-byte block in our example.

Allocating from HeapMultiBuf will try to return a block from the first buffer which has:

  1. A block size that is >= to the requested size

AND

  1. An alignment that is >= to the requested alignment

If the first matching buffer is empty, HeapMultiBuf will only continue searching for a block if 'block borrowing' is enabled (see Block Borrowing).

HeapMultiBuf and HeapBuf

The HeapMultiBuf module is built on top of the HeapBuf module. Each buffer in a HeapMultiBuf is in fact managed by a HeapBuf instance. Configuration of a HeapMultiBuf is done by providing an array of configured HeapBuf parameter structures. Refer to the HeapBuf documentation for information on the buffer parameters. All of the documentation and parameters for HeapBuf apply equally to HeapMultiBuf. Another consequence of this is that configuration checking is left to the HeapBuf module. If a buffer in a HeapMultiBuf has been incorrectly configured (with blockSize = 0, for example), HeapBuf, not HeapMultiBuf, will raise an Assert. Since HeapMultiBuf is built on HeapBuf, it simply performs the logic to determine which HeapBuf to allocate a block from or which HeapBuf to free a block to.

Configuration Example

The following configuration code creates a HeapMultiBuf instance which manages 3 pools of 10 blocks each, with block sizes of 64, 128 and 256.

var HeapMultiBuf = xdc.useModule('ti.sysbios.heaps.HeapMultiBuf');
var HeapBuf = xdc.useModule('ti.sysbios.heaps.HeapBuf');
// Create parameter structure for HeapMultiBuf instance.
var hmbParams = new HeapMultiBuf.Params();
hmbParams.numBufs = 3;
// Create the parameter structures for each of the three
// HeapBufs to be managed by the HeapMultiBuf instance.
hmbParams.bufParams.$add(new HeapBuf.Params());
hmbParams.bufParams[0].blockSize = 64;
hmbParams.bufParams[0].numBlocks = 10;
hmbParams.bufParams.$add(new HeapBuf.Params());
hmbParams.bufParams[1].blockSize = 128;
hmbParams.bufParams[1].numBlocks = 10;
hmbParams.bufParams.$add(new HeapBuf.Params());
hmbParams.bufParams[2].blockSize = 256;
hmbParams.bufParams[2].numBlocks = 10;
// Create the HeapMultiBuf instance, and assign the global handle
// 'multiBufHeap' to it. Add '#include <xdc/cfg/global.h>' to your
// .c file to reference the instance by this handle.
Program.global.multiBufHeap = HeapMultiBuf.create(hmbParams);

Block Borrowing

HeapMultiBuf can support "block borrowing". With this feature turned on, if a request is made for a 32-byte block and none are available, HeapMultiBuf will continue looking for an available block in other buffers. When a borrowed block is freed, it will be returned back to its original buffer. Enabling Block Borrowing changes the determinism of alloc, since it may have to check any number of buffers to find an available block. Block borrowing may also occur, even if it is disabled, if a block of a particular size is requested with an alignment that is greater than the configured alignment for that block size. For example, a HeapMultiBuf is configured with a buffer of 32-byte blocks with an alignment of 8, and a buffer of 64-byte blocks with an alignment of 16. If a request is made for a 32-byte block with an alignment of 16, it will be serviced by the buffer of 64-byte blocks.

Static vs. Dynamic Creation

As with HeapBuf, a statically created HeapMultiBuf instance will ignore the bufSize and buf parameters. Dynamic creates require all of the parameters. It should be noted that static creates are ideal if code space is a concern; dynamically creating a HeapMultiBuf requires a relatively large amount of initialization code to be pulled in to the executable.

Block Sizes and Alignment

Real-Time Concerns

Allocation from and freeing to a HeapMultiBuf instance is non-blocking. HeapMultiBuf is deterministic:

Restrictions

Unconstrained Functions

All functions

Calling Context

Function Hwi Swi Task Main Startup
Params_init Y Y Y Y Y
alloc Y Y Y Y N
construct N* N* Y Y N
create N* N* Y Y N
delete N* N* Y Y N
destruct N* N* Y Y N
free Y Y Y Y N
getStats Y Y Y Y N
isBlocking Y Y Y Y N
Definitions:
  • Hwi: API is callable from a Hwi thread.
  • Swi: API is callable from a Swi thread.
  • Task: API is callable from a Task thread.
  • Main: API is callable during any of these phases:
    • In your module startup after this module is started (e.g. HeapMultiBuf_Module_startupDone() returns true).
    • During xdc.runtime.Startup.lastFxns.
    • During main().
    • During BIOS.startupFxns.
  • Startup: API is callable during any of these phases:
    • During xdc.runtime.Startup.firstFxns.
    • In your module startup before this module is started (e.g. HeapMultiBuf_Module_startupDone() returns false).
  • *: Assuming blocking Heap is used for creation.
  • **: Assuming GateMutex is used as HeapMem's Gate.
  • + : Cannot use HeapMem object while it is being restored.

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti/sysbios/heaps/HeapBuf.h>
#include <ti/sysbios/runtime/Error.h>
#include <ti/sysbios/runtime/IHeap.h>
#include <ti/sysbios/runtime/Memory.h>
Include dependency graph for HeapMultiBuf.h:

Go to the source code of this file.

Data Structures

struct  HeapMultiBuf_Params
 
struct  HeapMultiBuf_Struct
 

Macros

#define HeapMultiBuf_A_blockNotFreed   "Invalid block address on the free. Failed to free block back to heap."
 Invalid block pointer. More...
 
#define HeapMultiBuf_E_size   "requested size is too big: handle=0x%x, size=%u"
 Raised when requested size exceeds all blockSizes. More...
 
#define ti_sysbios_heaps_HeapMultiBuf_Handle_upCast(handle)   ((IHeap_Handle)(handle))
 cast handle to an IHeap_Handle for use by Memory_alloc, etc. More...
 

Typedefs

typedef struct HeapMultiBuf_StructHeapMultiBuf_Handle
 
typedef HeapMultiBuf_Struct HeapMultiBuf_Object
 

Functions

void * HeapMultiBuf_alloc (HeapMultiBuf_Handle handle, size_t size, size_t align, Error_Block *eb)
 HeapMultiBuf will return a block that is >= 'size' with an alignment that is >= 'align'. The HeapMultiBuf will attempt to service a request for any size; the specified size does not need to match the configured block sizes of the buffers. More...
 
void HeapMultiBuf_free (HeapMultiBuf_Handle handle, void *buf, size_t size)
 HeapMultiBuf ignores the 'size' parameter to free. It determines the correct buffer to free the block to by comparing addresses. More...
 
bool HeapMultiBuf_isBlocking (HeapMultiBuf_Handle handle)
 This function always returns false since the alloc/free never block on a resource. More...
 
void HeapMultiBuf_getStats (HeapMultiBuf_Handle handle, Memory_Stats *statBuf)
 get memory stats for a HeapMultiBuf object More...
 
HeapMultiBuf_Handle HeapMultiBuf_create (const HeapMultiBuf_Params *params, Error_Block *eb)
 Create a HeapMultiBuf heap. More...
 
HeapMultiBuf_Handle HeapMultiBuf_construct (HeapMultiBuf_Struct *obj, const HeapMultiBuf_Params *params, Error_Block *eb)
 Construct a HeapMultiBuf heap. More...
 
void HeapMultiBuf_delete (HeapMultiBuf_Handle *handle)
 Delete a HeapMultiBuf heap. More...
 
void HeapMultiBuf_destruct (HeapMultiBuf_Struct *obj)
 Destruct a HeapMultiBuf heap. More...
 
void HeapMultiBuf_Params_init (HeapMultiBuf_Params *prms)
 Initialize the HeapMultiBuf_Params structure with default values. More...
 
HeapMultiBuf_Handle HeapMultiBuf_Object_first (void)
 return handle of the first HeapMultiBuf on HeapMultiBuf list More...
 
HeapMultiBuf_Handle HeapMultiBuf_Object_next (HeapMultiBuf_Handle heap)
 return handle of the next HeapMultiBuf on HeapMultiBuf list More...
 

Macro Definition Documentation

§ HeapMultiBuf_A_blockNotFreed

#define HeapMultiBuf_A_blockNotFreed   "Invalid block address on the free. Failed to free block back to heap."

Invalid block pointer.

This Assert is raised if a call to free does not successfully free the block back to any of the buffers. Indicates that the block pointer is invalid, or that the HeapMultiBuf state has been corrupted.

§ HeapMultiBuf_E_size

#define HeapMultiBuf_E_size   "requested size is too big: handle=0x%x, size=%u"

Raised when requested size exceeds all blockSizes.

§ ti_sysbios_heaps_HeapMultiBuf_Handle_upCast

#define ti_sysbios_heaps_HeapMultiBuf_Handle_upCast (   handle)    ((IHeap_Handle)(handle))

cast handle to an IHeap_Handle for use by Memory_alloc, etc.

Parameters
handleheap handle
Return values
IHeap_Handle

Typedef Documentation

§ HeapMultiBuf_Handle

§ HeapMultiBuf_Object

Function Documentation

§ HeapMultiBuf_alloc()

void* HeapMultiBuf_alloc ( HeapMultiBuf_Handle  handle,
size_t  size,
size_t  align,
Error_Block eb 
)

HeapMultiBuf will return a block that is >= 'size' with an alignment that is >= 'align'. The HeapMultiBuf will attempt to service a request for any size; the specified size does not need to match the configured block sizes of the buffers.

§ HeapMultiBuf_free()

void HeapMultiBuf_free ( HeapMultiBuf_Handle  handle,
void *  buf,
size_t  size 
)

HeapMultiBuf ignores the 'size' parameter to free. It determines the correct buffer to free the block to by comparing addresses.

§ HeapMultiBuf_isBlocking()

bool HeapMultiBuf_isBlocking ( HeapMultiBuf_Handle  handle)

This function always returns false since the alloc/free never block on a resource.

§ HeapMultiBuf_getStats()

void HeapMultiBuf_getStats ( HeapMultiBuf_Handle  handle,
Memory_Stats statBuf 
)

get memory stats for a HeapMultiBuf object

Parameters
handleheap handle
statBufpointer to a Memory_Stats object

§ HeapMultiBuf_create()

HeapMultiBuf_Handle HeapMultiBuf_create ( const HeapMultiBuf_Params params,
Error_Block eb 
)

Create a HeapMultiBuf heap.

Parameters
paramsoptional parameters
eberror block
Return values
HeapMultiBufhandle (NULL on failure)

§ HeapMultiBuf_construct()

HeapMultiBuf_Handle HeapMultiBuf_construct ( HeapMultiBuf_Struct obj,
const HeapMultiBuf_Params params,
Error_Block eb 
)

Construct a HeapMultiBuf heap.

HeapMultiBuf_construct is equivalent to HeapMultiBuf_create except that the HeapMultiBuf_Struct is pre-allocated.

Parameters
objpointer to a HeapMultiBuf object
paramsoptional parameters
eberror block
Return values
HeapMultiBufhandle (NULL on failure)

§ HeapMultiBuf_delete()

void HeapMultiBuf_delete ( HeapMultiBuf_Handle handle)

Delete a HeapMultiBuf heap.

Note that HeapMultiBuf_delete takes a pointer to a HeapMultiBuf_Handle which enables HeapMultiBuf_delete to set the HeapMultiBuf handle to NULL.

Parameters
handlepointer to a HeapMultiBuf handle

§ HeapMultiBuf_destruct()

void HeapMultiBuf_destruct ( HeapMultiBuf_Struct obj)

Destruct a HeapMultiBuf heap.

Parameters
objpointer to a HeapMultiBuf objects

§ HeapMultiBuf_Params_init()

void HeapMultiBuf_Params_init ( HeapMultiBuf_Params prms)

Initialize the HeapMultiBuf_Params structure with default values.

HeapMultiBuf_Params_init initializes the HeapMultiBuf_Params structure with default values. HeapMultiBuf_Params_init should always be called before setting individual parameter fields. This allows new fields to be added in the future with compatible defaults – existing source code does not need to change when new fields are added.

Parameters
prmspointer to uninitialized params structure

§ HeapMultiBuf_Object_first()

HeapMultiBuf_Handle HeapMultiBuf_Object_first ( void  )

return handle of the first HeapMultiBuf on HeapMultiBuf list

Return the handle of the first HeapMultiBuf on the create/construct list. NULL if no HeapMultiBufs have been created or constructed.

Return values
HeapMultiBufhandle

§ HeapMultiBuf_Object_next()

HeapMultiBuf_Handle HeapMultiBuf_Object_next ( HeapMultiBuf_Handle  heap)

return handle of the next HeapMultiBuf on HeapMultiBuf list

Return the handle of the next HeapMultiBuf on the create/construct list. NULL if no more HeapMultiBufs are on the list.

Parameters
heapHeapMultiBuf handle
Return values
HeapMultiBufhandle
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale