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 #include <config/MemoryAllocation.hpp>
10 #include <type_traits>
11 namespace Fw {
12 
14 
16 
17 void* MemAllocator::allocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) {
18  bool unused = false;
19  return this->allocate(identifier, size, unused, alignment);
20 }
21 
23  FwSizeType& size,
24  bool& recoverable,
25  FwSizeType alignment) {
26  FwSizeType requestedSize = size;
27  void* memory = this->allocate(identifier, size, recoverable, alignment);
28  FW_ASSERT(memory != nullptr && size >= requestedSize, static_cast<FwAssertArgType>(identifier),
29  static_cast<FwAssertArgType>(requestedSize), static_cast<FwAssertArgType>(size));
30  return memory;
31 }
32 
33 void* MemAllocator ::checkedAllocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) {
34  bool unused = false;
35  return this->checkedAllocate(identifier, size, unused, alignment);
36 }
37 
38 MemAllocatorRegistry::MemAllocatorRegistry() : m_defaultAllocator(MemAllocatorRegistry::getDefaultAllocator()) {
39  this->registerAllocator(MemoryAllocation::MemoryAllocatorType::SYSTEM, m_defaultAllocator);
40 }
41 
43  MemAllocator& allocator) {
44  this->m_allocators[type] = &allocator;
45 }
46 
48  static MemAllocatorRegistry registry;
49  return registry;
50 }
51 
53  FW_ASSERT(this->m_allocators[type] != nullptr, static_cast<FwAssertArgType>(type));
54  return *this->m_allocators[type];
55 }
56 
58  // If the allocator is not registered, return the SYSTEM allocator
59  if (this->m_allocators[type] == nullptr) {
61  }
62  return *this->m_allocators[type];
63 }
64 
65 MemAllocator& MemAllocatorRegistry::getDefaultAllocator() {
66  static_assert(std::is_constructible<MemoryAllocation::DefaultMemoryAllocatorType>::value,
67  "DefaultMemoryAllocatorType must be constructible without arguments");
68  static MemoryAllocation::DefaultMemoryAllocatorType defaultAllocator;
69  return defaultAllocator;
70 }
71 
72 } /* 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))
malloc based memory allocator
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