VR-Vantage 3.1 API Documentation
Home
Modules
Namespaces
Classes
Files
Libraries
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
sensorFx
include
ssInstancePool.h
Go to the documentation of this file.
1
2
// Copyright JRM Enterprises, Inc. 2004 - 2015
3
// All rights reserved.
4
//
5
// This code is the intellectual property of JRM Enterprises, Inc.
6
// It may not be used or released as source or compiled binary form
7
// without the prior written consent of JRM Enterprises, Inc.
8
//
9
// Original Author: Steve Butrimas Date: Jan 2015
11
12
#ifndef SS_INSTANCE_POOL_H
13
#define SS_INSTANCE_POOL_H
14
15
#include <memory>
16
#include <vector>
17
#include <algorithm>
18
19
template
<
class
T>
20
struct
ssInstancePoolTrait
21
{
22
23
// Constructs an object or array of type T. The memory is constructed
24
// via the placement new operator but has the option of using recycled
25
// memory (if ptr is not NULL) or allocating a new block.
26
27
static
T*
construct
(
int
count
,
void
*
pointer
)
28
{
29
if
(pointer)
30
return
new
(
pointer
) T();
31
32
else
33
return
new
(malloc( count *
sizeof
(T) )) T();
34
}
35
36
37
// Destroys an object or array of type T. Calling the object's
38
// destructor will be made to. If destroy is true, the memory block
39
// will be deleted with free().
40
41
static
void
destruct
(T *
pointer
,
bool
Free)
42
{
43
if
(!pointer)
44
return
;
45
46
if
(Free)
47
free(pointer);
48
else
49
pointer->~T();
50
}
51
52
};
53
54
template
<
class
T,
class
Trait>
55
struct
DeleteAll
56
{
57
void
operator()
(T *instance)
const
58
{
59
Trait::destruct(instance,
true
);
60
}
61
};
62
63
64
65
66
// ssInstancePool is a template class that implements a memory pool design.
67
// The premise behind the pool is reuse of memory to avoid unnecessary
68
// allocation and deallocation of memory. This class will hold onto memory blocks
69
// for subsequent reuse and avoid the allocation overhead.
70
//
71
// TODO: Consider/Implement multithread safety on the memory pool access
72
73
template
<
class
T,
class
Trait = ssInstancePoolTrait<T> >
74
class
ssInstancePool
75
{
76
public
:
77
typedef
std::vector<T *>
pool_container
;
78
typedef
typename
pool_container::iterator
iterator
;
79
80
ssInstancePool
(
int
num
= 1 ) :
n_number
(
num
)
81
{}
82
83
~ssInstancePool
()
84
{
85
clear
();
86
}
87
88
// This will retrieve the memory from the pool utilizing
89
// the trait property to dictate how the memory is obtained
90
T *
obtain
()
91
{
92
T *ptr = NULL;
93
94
if
( !
m_InstancePool
.empty()) {
95
96
ptr =
m_InstancePool
.back();
97
98
m_InstancePool
.pop_back();
99
}
100
101
ptr = Trait::construct(
n_number
,ptr);
102
103
return
ptr;
104
}
105
106
// Return the pointer to the pool dictated by the trait behavior
107
void
recycle
(T *ptr)
108
{
109
if
(!ptr)
return
;
110
111
Trait::destruct(ptr,
false
);
112
113
m_InstancePool
.push_back(ptr);
114
}
115
116
size_t
size
()
117
{
118
return
m_InstancePool
.size();
119
}
120
121
void
clear
()
122
{
123
// Must use the same trait for deletion functor to avoid undefined behavior
124
for_each(
m_InstancePool
.begin(),
m_InstancePool
.end(),
DeleteAll<T, Trait>
());
125
126
m_InstancePool
.clear();
127
128
// Apply swap trick to clear out vector
129
pool_container
(
m_InstancePool
).swap(
m_InstancePool
);
130
}
131
132
private
:
133
134
pool_container
m_InstancePool
;
135
int
n_number
;
136
137
// Prevent unwanted compiler generated default assignment and copy operators
138
ssInstancePool
(
const
ssInstancePool
&);
139
ssInstancePool
&
operator=
(
const
ssInstancePool
&);
140
};
141
142
#endif
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (
www.mak.com
)