Minor changes here and there.
[aesalon.git] / src / storage / Mempool.cpp
blob25973e66d710df5a40c7be1bd1d1d5365425c8d2
1 /** Aesalon, a tool to visualize program behaviour in real time.
2 Copyright (C) 2009-2011, Aesalon development team.
4 Aesalon is distributed under the terms of the GNU GPLv3. See
5 the included file LICENSE for more information.
7 @file src/storage/Mempool.cpp
8 */
10 #include <sys/mman.h>
11 #include <errno.h>
12 #include <cstring>
14 #include "storage/Mempool.h"
15 #include "util/MessageSystem.h"
17 namespace Storage {
19 Mempool::Mempool() {
23 Mempool::~Mempool() {
27 void *Mempool::request(uint32_t size) {
28 int pools = m_mapVector.size();
29 int pool;
30 for(pool = 0; pool < pools; pool ++) {
31 if(m_useVector[pool] + size <= m_mempoolSize) break;
34 if(pool == pools) createNew();
36 m_useVector[pool] += size;
38 return (m_mapVector[pool] + m_useVector[pool] - size);
41 void Mempool::createNew() {
42 void *memory = mmap(NULL, m_mempoolSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
43 if(memory == MAP_FAILED) {
44 Message(Fatal, "Could not create memory mapping: " << std::strerror(errno));
46 m_mapVector.push_back(static_cast<uint8_t *>(memory));
47 m_useVector.push_back(0);
50 } // namespace Storage