ngscopeclient
0.1-dev+51fbda87c
lib
scopehal
WaveformPool.h
Go to the documentation of this file.
1
/***********************************************************************************************************************
2
* *
3
* libscopehal *
4
* *
5
* Copyright (c) 2012-2024 Andrew D. Zonenberg *
6
* All rights reserved. *
7
* *
8
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9
* following conditions are met: *
10
* *
11
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12
* following disclaimer. *
13
* *
14
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15
* following disclaimer in the documentation and/or other materials provided with the distribution. *
16
* *
17
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18
* derived from this software without specific prior written permission. *
19
* *
20
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26
* POSSIBILITY OF SUCH DAMAGE. *
27
* *
28
***********************************************************************************************************************/
29
36
#ifndef WaveformPool_h
37
#define WaveformPool_h
38
46
class
WaveformPool
47
{
48
public
:
49
55
WaveformPool
(
size_t
maxSize = 16)
56
:
m_maxSize
(maxSize)
57
{}
58
59
~WaveformPool
()
60
{
61
for
(
auto
w :
m_waveforms
)
62
delete
w;
63
m_waveforms
.clear();
64
}
65
73
void
Add
(
WaveformBase
* w)
74
{
75
std::lock_guard<std::mutex> lock(
m_mutex
);
76
w->
Rename
(
"WaveformPool.freelist"
);
77
78
if
(
m_waveforms
.size() <
m_maxSize
)
79
m_waveforms
.push_back(w);
80
else
81
delete
w;
82
}
83
89
WaveformBase
*
Get
()
90
{
91
std::lock_guard<std::mutex> lock(
m_mutex
);
92
93
if
(
m_waveforms
.empty())
94
return
nullptr
;
95
96
auto
ret = *
m_waveforms
.begin();
97
ret->m_revision ++;
98
m_waveforms
.pop_front();
99
100
ret->Rename(
"WaveformPool.allocated"
);
101
return
ret;
102
}
103
109
bool
clear
()
110
{
111
std::lock_guard<std::mutex> lock(
m_mutex
);
112
113
if
(
m_waveforms
.empty())
114
return
false
;
115
116
for
(
auto
w :
m_waveforms
)
117
delete
w;
118
m_waveforms
.clear();
119
120
return
true
;
121
}
122
123
protected
:
124
126
size_t
m_maxSize
;
127
129
std::mutex
m_mutex
;
130
132
std::list<WaveformBase*>
m_waveforms
;
133
};
134
135
#endif
WaveformBase
Base class for all Waveform specializations.
Definition:
Waveform.h:59
WaveformBase::Rename
virtual void Rename(const std::string &name="")=0
Assings a human readable name to the waveform for debug purposes.
WaveformPool
Thread safe memory pool for reusing Waveform objects.
Definition:
WaveformPool.h:47
WaveformPool::m_maxSize
size_t m_maxSize
Maximum number of waveforms to store in the pool.
Definition:
WaveformPool.h:126
WaveformPool::m_mutex
std::mutex m_mutex
Mutex for synchronizing access to m_waveforms across threads.
Definition:
WaveformPool.h:129
WaveformPool::WaveformPool
WaveformPool(size_t maxSize=16)
Creates a waveform pool.
Definition:
WaveformPool.h:55
WaveformPool::clear
bool clear()
Free all waveforms in the pool to reclaim memory.
Definition:
WaveformPool.h:109
WaveformPool::Get
WaveformBase * Get()
Attempts to get a waveform from the pool.
Definition:
WaveformPool.h:89
WaveformPool::m_waveforms
std::list< WaveformBase * > m_waveforms
The list of free waveforms.
Definition:
WaveformPool.h:132
WaveformPool::Add
void Add(WaveformBase *w)
Adds a new waveform to the pool if there's sufficient free slots in the pool.
Definition:
WaveformPool.h:73
Generated by
1.9.4