F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Fw::MemAllocator Class Referenceabstract

Memory Allocation base class. More...

#include <Fw/Types/MemAllocator.hpp>

Inheritance diagram for Fw::MemAllocator:
Fw::MallocAllocator Fw::MmapAllocator

Public Member Functions

virtual void * allocate (const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))=0
 
virtual void deallocate (const FwEnumStoreType identifier, void *ptr)=0
 
void * allocate (const FwEnumStoreType identifier, FwSizeType &size, FwSizeType alignment=alignof(std::max_align_t))
 
void * checkedAllocate (const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))
 
void * checkedAllocate (const FwEnumStoreType identifier, FwSizeType &size, FwSizeType alignment=alignof(std::max_align_t))
 

Protected Member Functions

 MemAllocator ()
 
virtual ~MemAllocator ()
 

Detailed Description

Memory Allocation base class.

This class is a pure virtual base class for memory allocators in F Prime. The intent is to provide derived classes that allocate memory from different sources. The base class can be passed to classes so the allocator can be selected at the system level, and different allocators can be used by different components as appropriate.

The identifier can be used to look up a pre-allocated buffer by ID in an embedded system. There is no guarantee that an identifier is unique across multiple calls to allocate(). It is intended to be unique to a given entity.

Warning
implementors providing derived classes that tie an identifier to a specific memory segment should use bump pointer or other implementation to handle multiple calls to allocate() with the same identifier.
alignment must be respected in allocation calls, but may be larger than requested.

The size is the requested size of the memory. If the allocator cannot return the requested amount, it should return the actual amount and users should check.

The recoverable flag is intended to be used in embedded environments where memory can survive a processor reset and data can be recovered. The component using the allocator can then use the data. Any integrity checks are up to the user of the memory.

Definition at line 46 of file MemAllocator.hpp.

Constructor & Destructor Documentation

◆ MemAllocator()

Fw::MemAllocator::MemAllocator ( )
protected

Definition at line 14 of file MemAllocator.cpp.

◆ ~MemAllocator()

Fw::MemAllocator::~MemAllocator ( )
protectedvirtual

Definition at line 16 of file MemAllocator.cpp.

Member Function Documentation

◆ allocate() [1/2]

virtual void* Fw::MemAllocator::allocate ( const FwEnumStoreType  identifier,
FwSizeType size,
bool &  recoverable,
FwSizeType  alignment = alignof(std::max_align_t) 
)
pure virtual

Allocate memory

Allows allocation of memory of a given size and alignment. The actual returned memory size may be smaller than the requested size. The alignment of the memory is guaranteed to be at least as large as the requested alignment but may be larger. The recoverable flag indicates if the memory is recoverable (i.e. non-volatile) and thus may be valid without initialization.

identifier is a unique identifier for the allocating entity. This entity (e.g. a component) may call allocate multiple times with the same id, but no other entity in the system shall call allocate with that id.

Parameters
identifierthe memory segment identifier, each identifier is to be used in once single allocation
sizethe requested size - changed to actual if different
recoverable- flag to indicate the memory could be recoverable
alignment- alignment requirement for the allocation. Default: maximum alignment defined by C++.
Returns
the pointer to memory. Zero if unable to allocate

Implemented in Fw::MallocAllocator, and Fw::MmapAllocator.

◆ allocate() [2/2]

void * Fw::MemAllocator::allocate ( const FwEnumStoreType  identifier,
FwSizeType size,
FwSizeType  alignment = alignof(std::max_align_t) 
)

Allocate memory without recoverable flag

This is a convenience method that calls allocate() without the recoverable flag. The recoverable flag is filled by the underlying allocator but is not returned to the caller. This is for cases when the caller does not care about recoverability of memory.

Parameters
identifierthe memory segment identifier, each identifier is to be used in once single allocation
sizethe requested size - changed to actual if different
alignment- alignment requirement for the allocation. Default: maximum alignment defined by C++.
Returns
the pointer to memory. Zero if unable to allocate

Definition at line 18 of file MemAllocator.cpp.

◆ checkedAllocate() [1/2]

void * Fw::MemAllocator::checkedAllocate ( const FwEnumStoreType  identifier,
FwSizeType size,
bool &  recoverable,
FwSizeType  alignment = alignof(std::max_align_t) 
)

Allocate memory checking that the allocation was successful

This is a convenience method that calls allocate() and checks that the returned pointer is not null and that size is at least as large as the requested size.

Allocations are checked using FW_ASSERT implying that an allocation failure results in a tripped assertion.

Parameters
identifierthe memory segment identifier, each identifier is to be used in once single allocation
sizethe requested size, actual allocation will be at least this size
recoverable- flag to indicate the memory could be recoverable
alignment- alignment requirement for the allocation. Default: maximum alignment defined by C++.
Returns
the pointer to memory. Zero if unable to allocate

Definition at line 23 of file MemAllocator.cpp.

◆ checkedAllocate() [2/2]

void * Fw::MemAllocator::checkedAllocate ( const FwEnumStoreType  identifier,
FwSizeType size,
FwSizeType  alignment = alignof(std::max_align_t) 
)

Allocate memory checking that the allocation was successful without recoverable flag

This is a convenience method that calls allocate() and checks that the returned pointer is not null and that size is at least as large as the requested size. The recoverable flag is filled by the underlying allocator but is not returned to the caller. This is for cases when the caller does not care about recoverability of memory.

Allocations are checked using FW_ASSERT implying that an allocation failure results in a tripped assertion.

Parameters
identifierthe memory segment identifier, each identifier is to be used in once single allocation
sizethe requested size, actual allocation will be at least this size
alignment- alignment requirement for the allocation. Default: maximum alignment defined by C++.
Returns
the pointer to memory. Zero if unable to allocate

Definition at line 34 of file MemAllocator.cpp.

◆ deallocate()

virtual void Fw::MemAllocator::deallocate ( const FwEnumStoreType  identifier,
void *  ptr 
)
pure virtual

Deallocate memory

Deallocate memory previously allocated by allocate(). The pointer must be one returned by allocate() and the identifier must match the one used in the original allocate() call.

Parameters
identifierthe memory segment identifier, each identifier is to be used in once single allocation
ptrthe pointer to memory returned by allocate()

Implemented in Fw::MallocAllocator, and Fw::MmapAllocator.


The documentation for this class was generated from the following files: