F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MmapAllocator.cpp
Go to the documentation of this file.
1 
13 #include <sys/mman.h>
14 #include <Fw/Types/Assert.hpp>
16 #include <cstdlib>
17 
18 namespace Fw {
19 
20 MmapAllocator::MmapAllocator() : m_length(0) {}
21 
23 
25  FwSizeType& size,
26  bool& recoverable,
27  FwSizeType alignment) {
28  void* addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
29  if (addr == MAP_FAILED) {
30  size = 0;
31  return nullptr;
32  }
33  this->m_length = size;
34 
35  // mmap memory is never recoverable
36  recoverable = false;
37 
38  return addr;
39 }
40 
41 void MmapAllocator::deallocate(const FwEnumStoreType identifier, void* ptr) {
42  if (this->m_length) {
43  int stat = munmap(ptr, this->m_length);
44  FW_ASSERT(stat == 0, stat);
45  }
46 }
47 
48 } /* namespace Fw */
void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t)) override
virtual ~MmapAllocator()
Destructor with no arguments.
PlatformSizeType FwSizeType
I32 FwEnumStoreType
A MemAllocator implementation class that uses mmap.
Implementation of malloc based allocator.
void deallocate(const FwEnumStoreType identifier, void *ptr) override
#define FW_ASSERT(...)
Definition: Assert.hpp:14