AWS IoT Device SDK C: Static Memory
Statically-allocated buffer pools
Return to main page ↑
IotStaticMemory_FindFree

Find a free buffer using the "in-use" flags.

int32_t IotStaticMemory_FindFree( uint32_t * pInUse,
size_t limit );

If a free buffer is found, this function marks the buffer in-use. This function is common to the static memory implementation.

Parameters
[in]pInUseThe "in-use" flags to search.
[in]limitHow many flags to check, i.e. the size of pInUse.
Returns
The index of a free buffer; -1 if no free buffers are available.

Example:

// To use this function, first declare two arrays. One provides the statically-allocated
// objects, the other provides flags to determine which objects are in-use.
#define NUMBER_OF_OBJECTS ...
#define OBJECT_SIZE ...
static uint32_t _pInUseObjects[ NUMBER_OF_OBJECTS ] = { 0 };
static uint8_t _pObjects[ NUMBER_OF_OBJECTS ][ OBJECT_SIZE ] = { { 0 } }; // Placeholder for objects.
// The function to statically allocate objects. Must have the same signature
// as malloc().
void * Iot_MallocObject( size_t size )
{
int32_t freeIndex = -1;
void * pNewObject = NULL;
// Check that sizes match.
if( size != OBJECT_SIZE )
{
// Get the index of a free object.
IOT_MESSAGE_BUFFERS );
if( freeIndex != -1 )
{
pNewBuffer = &( _pMessageBuffers[ freeIndex ][ 0 ] );
}
}
return pNewBuffer;
}