2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
31 #include "allocator.hpp"
33 Allocator::Allocator(size_t size, size_t threshold)
34 : m_size(size), m_threshold(threshold), m_alloc_offset(0)
37 int fd = open("/mnt/huge/hp", O_CREAT | O_RDWR, 0755);
39 cerr << "Allocator failed to open huge page file descriptor: " << strerror(errno) << endl;
42 m_mem = (uint8_t *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43 if (m_mem == MAP_FAILED) {
46 cerr << "Allocator mmap failed: " << strerror(errno) << endl;
50 m_mem = new uint8_t[size];
54 Allocator::~Allocator()
57 munmap((void *)m_mem, m_size);
63 void *Allocator::alloc(size_t size)
65 void *ret = &m_mem[m_alloc_offset];
67 m_alloc_offset += size;
71 void Allocator::reset()
76 size_t Allocator::getFreeSize() const
78 return m_size - m_alloc_offset;
81 bool Allocator::lowThresholdReached() const
83 return (m_size - m_alloc_offset) < m_threshold;