F´ Flight Software - C/C++ Documentation  devel
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 
24 void* MmapAllocator::allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE& size, bool& recoverable) {
25  void* addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
26  if (addr == MAP_FAILED) {
27  size = 0;
28  return nullptr;
29  }
30  this->m_length = size;
31 
32  // mmap memory is never recoverable
33  recoverable = false;
34 
35  return addr;
36 }
37 
38 void MmapAllocator::deallocate(const NATIVE_UINT_TYPE identifier, void* ptr) {
39  if (this->m_length) {
40  int stat = munmap(ptr, this->m_length);
41  FW_ASSERT(stat == 0, stat);
42  }
43 }
44 
45 } /* namespace Fw */
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
A MemAllocator implementation class that uses mmap.
void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)
virtual ~MmapAllocator()
Destructor with no arguments.
void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)