AWS IoT Device SDK C: Static Memory
Statically-allocated buffer pools
Return to main page ↑
iot_static_memory_common.c File Reference

Implementation of common static memory functions in iot_static_memory.h. More...

#include "iot_config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "platform/iot_threads.h"
#include "iot_static_memory.h"
#include "iot_atomic.h"

Functions

int32_t IotStaticMemory_FindFree (uint32_t *pInUse, size_t limit)
 Find a free buffer using the "in-use" flags. More...
 
void IotStaticMemory_ReturnInUse (void *ptr, void *pPool, uint32_t *pInUse, size_t limit, size_t elementSize)
 Return an "in-use" buffer. More...
 
size_t Iot_MessageBufferSize (void)
 Get the fixed size of a message buffer. More...
 
void * Iot_MallocMessageBuffer (size_t size)
 Get an empty message buffer. More...
 
void Iot_FreeMessageBuffer (void *ptr)
 Free an in-use message buffer. More...
 

Variables

static uint32_t _pInUseMessageBuffers [IOT_MESSAGE_BUFFERS] = { 0U }
 Message buffer in-use flags.
 
static char _pMessageBuffers [IOT_MESSAGE_BUFFERS][IOT_MESSAGE_BUFFER_SIZE] = { { 0 } }
 Message buffers.
 

Detailed Description

Implementation of common static memory functions in iot_static_memory.h.

Function Documentation

◆ IotStaticMemory_FindFree()

int32_t IotStaticMemory_FindFree ( uint32_t *  pInUse,
size_t  limit 
)

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

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;
}

◆ IotStaticMemory_ReturnInUse()

void IotStaticMemory_ReturnInUse ( void *  ptr,
void *  pPool,
uint32_t *  pInUse,
size_t  limit,
size_t  elementSize 
)

Return an "in-use" buffer.

This function is common to the static memory implementation.

Parameters
[in]ptrPointer to the buffer to return.
[in]pPoolThe pool of buffers that the in-use buffer was allocated from.
[in]pInUseThe "in-use" flags for pPool.
[in]limitHow many buffers (and flags) to check while searching for ptr.
[in]elementSizeThe size of a single element in pPool.

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 free statically-allocated objects. Must have the same signature
// as free().
void Iot_FreeObject( void * ptr )
{
_pObjects,
_pInUseObjects,
NUMBER_OF_OBJECTS,
OBJECT_SIZE );
}

◆ Iot_MessageBufferSize()

size_t Iot_MessageBufferSize ( void  )

Get the fixed size of a message buffer.

The size of the message buffers are known at compile time, but it is a constant that may not be visible to all source files. This function allows other source files to know the size of a message buffer.

Returns
The size, in bytes, of a single message buffer.

◆ Iot_MallocMessageBuffer()

void* Iot_MallocMessageBuffer ( size_t  size)

Get an empty message buffer.

This function is the analog of malloc for message buffers.

Parameters
[in]sizeRequested size for a message buffer.
Returns
Pointer to the start of a message buffer. If the size argument is larger than the fixed size of a message buffer or no message buffers are available, NULL is returned.

◆ Iot_FreeMessageBuffer()

void Iot_FreeMessageBuffer ( void *  ptr)

Free an in-use message buffer.

This function is the analog of free for message buffers.

Parameters
[in]ptrPointer to the message buffer to free.