Skip to content

Commit b057b89

Browse files
committed
Create MemoryPool.h
1 parent b66c5cb commit b057b89

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

MemoryPool.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*-
2+
* Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef MEMORY_POOL_H
24+
#define MEMORY_POOL_H
25+
26+
#include <climits>
27+
#include <cstddef>
28+
29+
template <typename T, size_t BlockSize = 4096>
30+
class MemoryPool
31+
{
32+
public:
33+
/* Member types */
34+
typedef T value_type;
35+
typedef T* pointer;
36+
typedef T& reference;
37+
typedef const T* const_pointer;
38+
typedef const T& const_reference;
39+
typedef size_t size_type;
40+
typedef ptrdiff_t difference_type;
41+
typedef std::false_type propagate_on_container_copy_assignment;
42+
typedef std::true_type propagate_on_container_move_assignment;
43+
typedef std::true_type propagate_on_container_swap;
44+
45+
template <typename U> struct rebind {
46+
typedef MemoryPool<U> other;
47+
};
48+
49+
/* Member functions */
50+
MemoryPool() noexcept;
51+
MemoryPool(const MemoryPool& memoryPool) noexcept;
52+
MemoryPool(MemoryPool&& memoryPool) noexcept;
53+
template <class U> MemoryPool(const MemoryPool<U>& memoryPool) noexcept;
54+
55+
~MemoryPool() noexcept;
56+
57+
MemoryPool& operator=(const MemoryPool& memoryPool) = delete;
58+
MemoryPool& operator=(MemoryPool&& memoryPool) noexcept;
59+
60+
pointer address(reference x) const noexcept;
61+
const_pointer address(const_reference x) const noexcept;
62+
63+
// Can only allocate one object at a time. n and hint are ignored
64+
pointer allocate(size_type n = 1, const_pointer hint = 0);
65+
void deallocate(pointer p, size_type n = 1);
66+
67+
size_type max_size() const noexcept;
68+
69+
template <class U, class... Args> void construct(U* p, Args&&... args);
70+
template <class U> void destroy(U* p);
71+
72+
template <class... Args> pointer newElement(Args&&... args);
73+
void deleteElement(pointer p);
74+
75+
private:
76+
union Slot_ {
77+
value_type element;
78+
Slot_* next;
79+
};
80+
81+
typedef char* data_pointer_;
82+
typedef Slot_ slot_type_;
83+
typedef Slot_* slot_pointer_;
84+
85+
slot_pointer_ currentBlock_;
86+
slot_pointer_ currentSlot_;
87+
slot_pointer_ lastSlot_;
88+
slot_pointer_ freeSlots_;
89+
90+
size_type padPointer(data_pointer_ p, size_type align) const noexcept;
91+
void allocateBlock();
92+
93+
static_assert(BlockSize >= 2 * sizeof(slot_type_), "BlockSize too small.");
94+
};
95+
96+
#include "MemoryPool.tcc"
97+
98+
#endif // MEMORY_POOL_H

0 commit comments

Comments
 (0)