Mbed OS Reference
|
CacheAlignedBuffer is used by Mbed in locations where we need a cache-aligned buffer. More...
#include <CacheAlignedBuffer.h>
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... | |
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.
For code using static arrays, like this:
Use a StaticCacheAlignedBuffer:
For code using dynamic allocation to handle unknown buffer sizes, like:
use a DynamicCacheAlignedBuffer:
DataT | Type 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.