-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.hpp
More file actions
87 lines (65 loc) · 2.33 KB
/
Copy pathObjectPool.hpp
File metadata and controls
87 lines (65 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
module: ObjectPool
author: digimokan
date: 19 NOV 2018 (created)
purpose: pool of objects for new/delete heap allocations
*******************************************************************************/
#ifndef OBJECT_POOL_HPP
#define OBJECT_POOL_HPP 1
/*******************************************************************************
* SYSTEM INCLUDES
*******************************************************************************/
#include <cstddef>
#include <mutex>
#include <type_traits>
/*******************************************************************************
* Types
*******************************************************************************/
template<typename T, size_t init_num_obj>
union StoredObj {
T obj;
StoredObj* next;
};
/*******************************************************************************
* INTERFACE
*******************************************************************************/
template <typename T, size_t init_num_obj>
class ObjectPool {
public:
// constructors
ObjectPool ();
// destructor
~ObjectPool ();
// operators
ObjectPool (const ObjectPool& in) = default;
ObjectPool& operator= (const ObjectPool& rh) = default;
ObjectPool (ObjectPool&& in) = default;
ObjectPool& operator= (ObjectPool&& rh) = default;
// specialized
void* allocate ();
void deallocate (void* ptr);
size_t get_peak_size () const;
private:
// types
using Storage = typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type;
// fields
Storage* pool_blocks;
StoredObj<T, init_num_obj>* free_list_head;
std::mutex mtx;
size_t size;
size_t peak_size;
// helper methods
void init_blocks ();
StoredObj<T, init_num_obj>* get_block_addr (size_t index);
void set_block_next (StoredObj<T, init_num_obj>* block, StoredObj<T, init_num_obj>* next_link);
void inc_sizes ();
void dec_sizes ();
};
/*******************************************************************************
* IMPLEMENTATION
*******************************************************************************/
#include "ObjectPool.hxx"
/*******************************************************************************
* END
*******************************************************************************/
#endif // OBJECT_POOL_HPP