F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MemAllocator.cpp
Go to the documentation of this file.
1 
7 #include <Fw/Types/Assert.hpp>
9 
10 namespace Fw {
11 
12 MemAllocatorRegistry* MemAllocatorRegistry::s_registry = nullptr;
13 
15 
17 
18 void* MemAllocator::allocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) {
19  bool unused = false;
20  return this->allocate(identifier, size, unused, alignment);
21 }
22 
24  FwSizeType& size,
25  bool& recoverable,
26  FwSizeType alignment) {
27  FwSizeType requestedSize = size;
28  void* memory = this->allocate(identifier, size, recoverable, alignment);
29  FW_ASSERT(memory != nullptr && size >= requestedSize, static_cast<FwAssertArgType>(identifier),
30  static_cast<FwAssertArgType>(requestedSize), static_cast<FwAssertArgType>(size));
31  return memory;
32 }
33 
34 void* MemAllocator ::checkedAllocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) {
35  bool unused = false;
36  return this->checkedAllocate(identifier, size, unused, alignment);
37 }
38 
40  // Register self as the singleton
41  MemAllocatorRegistry::s_registry = this;
42 }
43 
45  MemAllocator& allocator) {
46  this->m_allocators[type] = &allocator;
47 }
48 
50  FW_ASSERT(s_registry != nullptr);
51  return *s_registry;
52 }
53 
55  FW_ASSERT(this->m_allocators[type] != nullptr, static_cast<FwAssertArgType>(type));
56  return *this->m_allocators[type];
57 }
58 
60  // If the allocator is not registered, return the SYSTEM allocator
61  if (this->m_allocators[type] == nullptr) {
63  }
64  return *this->m_allocators[type];
65 }
66 } /* namespace Fw */
virtual void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))=0
PlatformSizeType FwSizeType
I32 FwEnumStoreType
virtual ~MemAllocator()
REQUIRED: required for allocation for memory using a standard system allocator (i.e. the default)
static MemAllocatorRegistry & getInstance()
get the singleton registry
MemAllocator & getAnAllocator(const MemoryAllocation::MemoryAllocatorType type)
void * checkedAllocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))
Memory Allocation base class.
MemAllocator & getAllocator(const MemoryAllocation::MemoryAllocatorType type)
Defines a base class for a memory allocator for classes.
Implementation of malloc based allocator.
void registerAllocator(const MemoryAllocation::MemoryAllocatorType type, MemAllocator &allocator)
register an allocator for the given type
#define FW_ASSERT(...)
Definition: Assert.hpp:14