Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
malloc-impl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the gobelijn software.
3  * Gobelijn is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or any later
6  * version. Gobelijn is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.
9  * See the GNU General Public License for details. You should have received
10  * a copy of the GNU General Public License along with the software. If not,
11  * see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2016, Jan Broeckhove.
14  */
20 #include "MemoryPool.h"
21 #include <cstdlib>
22 #include <vector>
23 
24 namespace Pimpl {
28 struct MemoryPool::MemoryPoolImpl
29 {
30  MemoryPoolImpl() : pointers(std::vector<void*>()) {}
31 
32  // Remembers all pointers that have been allocated, so we can
33  // deallocate them when the MemoryPool is destroyed.
34  std::vector<void*> pointers;
35 };
36 
40 MemoryPool::MemoryPool(size_t) : m_impl(new MemoryPoolImpl()) {}
41 
45 MemoryPool::~MemoryPool()
46 {
47  // Deallocate all pointers.
48  for (auto ptr : m_impl->pointers)
49  std::free(ptr);
50 }
51 
56 char* MemoryPool::AllocateMemory(size_t size)
57 {
58  void* ptr = std::malloc(size);
59  m_impl->pointers.push_back(ptr);
60  return reinterpret_cast<char*>(ptr);
61 }
62 } // namespace Pimpl
Pimpl demo with memory pool.