ngscopeclient v0.2.1
Loading...
Searching...
No Matches
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
47{
48public:
49
55 WaveformPool(size_t maxSize = 16)
57 {}
58
60 {
61 for(auto w : m_waveforms)
62 delete w;
63 m_waveforms.clear();
64 }
65
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
87 size_t size()
88 {
89 std::lock_guard<std::mutex> lock(m_mutex);
90 return m_waveforms.size();
91 }
92
99 {
100 std::lock_guard<std::mutex> lock(m_mutex);
101
102 if(m_waveforms.empty())
103 return nullptr;
104
105 auto ret = *m_waveforms.begin();
106 ret->m_revision ++;
107 m_waveforms.pop_front();
108
109 ret->Rename("WaveformPool.allocated");
110 return ret;
111 }
112
118 bool clear()
119 {
120 std::lock_guard<std::mutex> lock(m_mutex);
121
122 if(m_waveforms.empty())
123 return false;
124
125 for(auto w : m_waveforms)
126 delete w;
127 m_waveforms.clear();
128
129 return true;
130 }
131
132protected:
133
135 size_t m_maxSize;
136
138 std::mutex m_mutex;
139
141 std::list<WaveformBase*> m_waveforms;
142};
143
144#endif
Definition AcceleratorBuffer.h:204
Base class for all Waveform specializations.
Definition Waveform.h:59
virtual void Rename(const std::string &name="")=0
Assigns a human readable name to the waveform for debug purposes.
Thread safe memory pool for reusing Waveform objects.
Definition WaveformPool.h:47
size_t m_maxSize
Maximum number of waveforms to store in the pool.
Definition WaveformPool.h:135
std::mutex m_mutex
Mutex for synchronizing access to m_waveforms across threads.
Definition WaveformPool.h:138
WaveformPool(size_t maxSize=16)
Creates a waveform pool.
Definition WaveformPool.h:55
bool clear()
Free all waveforms in the pool to reclaim memory.
Definition WaveformPool.h:118
size_t size()
Returns the number of waveforms available in the pool.
Definition WaveformPool.h:87
WaveformBase * Get()
Attempts to get a waveform from the pool.
Definition WaveformPool.h:98
std::list< WaveformBase * > m_waveforms
The list of free waveforms.
Definition WaveformPool.h:141
void Add(WaveformBase *w)
Adds a new waveform to the pool if there's sufficient free slots in the pool.
Definition WaveformPool.h:73