F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MmapAllocator.hpp
Go to the documentation of this file.
1 
13 #ifndef TYPES_MMAPALLOCATOR_HPP_
14 #define TYPES_MMAPALLOCATOR_HPP_
15 
16 #include <sys/mman.h>
17 #include <unistd.h>
19 #include <Fw/Logger/Logger.hpp>
21 #include <cerrno>
22 
23 namespace Fw {
24 
32 template <FwSizeType C>
33 class MmapAllocator : public MemAllocator {
34  public:
37  explicit MmapAllocator(int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS);
39  virtual ~MmapAllocator();
40 
47  void* allocate(const FwEnumStoreType identifier,
48  FwSizeType& size,
49  bool& recoverable,
50  FwSizeType alignment = alignof(std::max_align_t)) override;
51 
55  void deallocate(const FwEnumStoreType identifier, void* ptr) override;
56 
57  private:
60 
62  const int m_mmap_flags;
63 };
64 
65 template <FwSizeType C>
66 MmapAllocator<C>::MmapAllocator(int mmap_flags) : m_size_map(), m_mmap_flags(mmap_flags) {}
67 
68 template <FwSizeType C>
70 
71 template <FwSizeType C>
73  FwSizeType& size,
74  bool& recoverable,
75  FwSizeType alignment) {
76  // mmap memory is never recoverable
77  recoverable = false;
78 
79  // mmap allocates pages at a virtual boundary. Alignment is guaranteed
80  // up to the page size
81  if (alignment > static_cast<FwSizeType>(sysconf(_SC_PAGESIZE))) {
82  Fw::Logger::log("Unable to allocate. Alignment request (%ld) > PAGE_SIZE (%ld)\n", alignment,
83  sysconf(_SC_PAGESIZE));
84  size = 0;
85  return nullptr;
86  }
87 
88  if (m_size_map.getCapacity() == m_size_map.getSize()) {
89  Fw::Logger::log("Unable to allocate. MmapAllocator out of size_map slots\n");
90  size = 0;
91  return nullptr;
92  }
93 
94  void* addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, m_mmap_flags, -1, 0);
95  if (addr == MAP_FAILED) {
96  Fw::Logger::log("Unable to allocate. mmap syscall failed. Errno (%ld)\n", errno);
97  size = 0;
98  return nullptr;
99  }
100 
101  Fw::Success ok = m_size_map.insert(reinterpret_cast<intptr_t>(addr), size);
102  FW_ASSERT(ok == Fw::Success::SUCCESS, ok);
103 
104  return addr;
105 }
106 
107 template <FwSizeType C>
108 void MmapAllocator<C>::deallocate(const FwEnumStoreType identifier, void* ptr) {
109  size_t alloc_size = 0;
110  Fw::Success ok = m_size_map.remove(reinterpret_cast<intptr_t>(ptr), alloc_size);
111  FW_ASSERT(ok == Fw::Success::SUCCESS, ok);
112 
113  int stat = munmap(ptr, alloc_size);
114  FW_ASSERT(stat == 0, stat);
115 }
116 
117 } /* namespace Fw */
118 
119 #endif /* TYPES_MMAPALLOCATOR_HPP_ */
Representing success.
PlatformSizeType FwSizeType
I32 FwEnumStoreType
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t)) override
void deallocate(const FwEnumStoreType identifier, void *ptr) override
MmapAllocator(int mmap_flags=MAP_PRIVATE|MAP_ANONYMOUS)
virtual ~MmapAllocator()
Destructor with no arguments.
Memory Allocation base class.
Defines a base class for a memory allocator for classes.
Implementation of malloc based allocator.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.