13 #ifndef TYPES_MMAPALLOCATOR_HPP_ 14 #define TYPES_MMAPALLOCATOR_HPP_ 32 template <FwSizeType C>
37 explicit MmapAllocator(
int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS);
50 FwSizeType alignment =
alignof(std::max_align_t))
override;
62 const int m_mmap_flags;
65 template <FwSizeType C>
68 template <FwSizeType C>
71 template <FwSizeType C>
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));
88 if (m_size_map.getCapacity() == m_size_map.getSize()) {
89 Fw::Logger::log(
"Unable to allocate. MmapAllocator out of size_map slots\n");
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);
101 Fw::Success ok = m_size_map.insert(reinterpret_cast<intptr_t>(addr), size);
107 template <FwSizeType C>
109 size_t alloc_size = 0;
110 Fw::Success ok = m_size_map.remove(reinterpret_cast<intptr_t>(ptr), alloc_size);
113 int stat = munmap(ptr, alloc_size);
PlatformSizeType FwSizeType
static void log(const char *format,...)
log a formated string with supplied arguments
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.