Mbed OS Reference
Loading...
Searching...
No Matches
CacheAlignedBuffer< DataT > Class Template Reference

CacheAlignedBuffer is used by Mbed in locations where we need a cache-aligned buffer. More...

#include <CacheAlignedBuffer.h>

Inheritance diagram for CacheAlignedBuffer< DataT >:
StaticCacheAlignedBuffer< uint8_t, _block_size > DynamicCacheAlignedBuffer< DataT > StaticCacheAlignedBuffer< DataT, BufferSize >

Public Member Functions

DataT * data ()
 Get a pointer to the aligned data array inside the buffer. More...
 
DataT const * data () const
 Get a pointer to the aligned data array inside the buffer (const version) More...
 
DataT & operator[] (size_t index)
 Element access. More...
 
DataT operator[] (size_t index) const
 Element access (const) More...
 
iterator begin ()
 Get iterator for start of buffer. More...
 
const_iterator begin () const
 Get iterator for start of buffer. More...
 
iterator end ()
 Get iterator for end of buffer. More...
 
const_iterator end () const
 Get iterator for end of buffer. More...
 
constexpr size_t capacity ()
 

Static Protected Member Functions

static DataT * findCacheLineStart (uint8_t *buffer)
 Find and return the first location in the given buffer that starts on a cache line. More...
 

Protected Attributes

DataT * _alignedBufferPtr
 Pointer to the aligned buffer. Must be set in each constructor of each subclass. More...
 
size_t _alignedBufferCapacity
 Capacity of the aligned buffer, in terms of number of DataT elements. More...
 

Detailed Description

template<typename DataT>
class mbed::CacheAlignedBuffer< DataT >

CacheAlignedBuffer is used by Mbed in locations where we need a cache-aligned buffer.

Cache alignment is desirable in several different situations in embedded programming – one common use is when working with DMA or other peripherals which write their results back to main memory. After these peripherals do their work, the data will be correct in main memory, but the CPU cache might also contain a value cached from that memory which is now incorrect.

In order to read those results from memory without risk of getting old data from the CPU cache, one needs to align the buffer so it takes up an integer number of cache lines, then invalidate the cache lines so that the data gets reread from RAM.

CacheAlignedBuffer provides an easy way to allocate the correct amount of space so that a buffer of any size can be made cache-aligned. To instantiate a CacheAlignedBuffer, create one of its subtypes, StaticCacheAlignedBuffer or DynamicCacheAlignedBuffer.

Converting Code to use CacheAlignedBuffer

For code using static arrays, like this:

uint8_t rxBuffer[16];
spi.transfer_and_wait(nullptr, 0, rxBuffer, 16);

Use a StaticCacheAlignedBuffer:

spi.transfer_and_wait(nullptr, 0, rxBuffer, 16);
CacheAlignedBuffer type designed for static allocation.

For code using dynamic allocation to handle unknown buffer sizes, like:

uint16_t * rxBuffer = new uint16_t[bufferSize];
spi.transfer_and_wait(nullptr, 0, rxBuffer, bufferSize);
...
delete[] rxBuffer;

use a DynamicCacheAlignedBuffer:

spi.transfer_and_wait(nullptr, 0, rxBuffer, bufferSize);
CacheAlignedBuffer type which allocates its backing buffer on the heap.
Template Parameters
DataTType of the data to store in the buffer. Note: CacheAlignedBuffer is not designed for using class types as DataT, and will not call constructors.

Definition at line 108 of file CacheAlignedBuffer.h.