Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
MemoryPool.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This file is part of the gobelijn software.
4  * Gobelijn is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or any later
7  * version. Gobelijn is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for details. You should have received
11  * a copy of the GNU General Public License along with the software. If not,
12  * see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2016, Jan Broeckhove.
15  */
21 #include <cstddef>
22 #include <memory>
23 
24 namespace Pimpl {
31 {
32 public:
33  // Delete the usual constructors/assignment operators.
34  MemoryPool() = delete;
35  MemoryPool(const MemoryPool&) = delete;
36  MemoryPool(MemoryPool&&) = delete;
37  MemoryPool& operator=(const MemoryPool&) = delete;
38  MemoryPool& operator=(MemoryPool&&) = delete;
39 
42  MemoryPool(size_t capacity);
43 
45  ~MemoryPool();
46 
49  template <typename T>
50  T* Allocate(size_t size = 1)
51  {
52  return reinterpret_cast<T*>(AllocateMemory(size * sizeof(T)));
53  }
54 
55 private:
57  char* AllocateMemory(size_t size);
58 
60  struct MemoryPoolImpl;
61 
64  std::unique_ptr<MemoryPoolImpl> m_impl;
65 };
66 
67 } // namespace Pimpl
~MemoryPool()
Frees this memory pool's resources, if any.
Definition: array-impl.cpp:41
T * Allocate(size_t size=1)
Allocates a region of memory that is 'size * sizeof(T)' of bytes in size.
Definition: MemoryPool.h:50
A memory pool that uses the pointer-to-implementation idiom under the hood.
Definition: MemoryPool.h:30
std::unique_ptr< MemoryPoolImpl > m_impl
Fortunately, we don't need a definition to create a pointer to MemoryPoolImpl, so that's exactly what...
Definition: MemoryPool.h:60
MemoryPool & operator=(const MemoryPool &)=delete
char * AllocateMemory(size_t size)
Allocates a region of memory that is the given number of bytes in size.
Definition: array-impl.cpp:51