ngscopeclient 0.1-dev+51fbda87c
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)
56 : m_maxSize(maxSize)
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
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
123protected:
124
126 size_t m_maxSize;
127
129 std::mutex m_mutex;
130
132 std::list<WaveformBase*> m_waveforms;
133};
134
135#endif
Base class for all Waveform specializations.
Definition: Waveform.h:59
virtual void Rename(const std::string &name="")=0
Assings 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:126
std::mutex m_mutex
Mutex for synchronizing access to m_waveforms across threads.
Definition: WaveformPool.h:129
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:109
WaveformBase * Get()
Attempts to get a waveform from the pool.
Definition: WaveformPool.h:89
std::list< WaveformBase * > m_waveforms
The list of free waveforms.
Definition: WaveformPool.h:132
void Add(WaveformBase *w)
Adds a new waveform to the pool if there's sufficient free slots in the pool.
Definition: WaveformPool.h:73