comparison base/Scavenger.h @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents 48e9f538e6e9
children ad5f892c0c4d
comparison
equal deleted inserted replaced
1324:d4a28d1479a8 1527:710e6250a401
104 104
105 template <typename T> 105 template <typename T>
106 Scavenger<T>::~Scavenger() 106 Scavenger<T>::~Scavenger()
107 { 107 {
108 if (m_scavenged < m_claimed) { 108 if (m_scavenged < m_claimed) {
109 for (size_t i = 0; i < m_objects.size(); ++i) { 109 for (size_t i = 0; i < m_objects.size(); ++i) {
110 ObjectTimePair &pair = m_objects[i]; 110 ObjectTimePair &pair = m_objects[i];
111 if (pair.first != 0) { 111 if (pair.first != 0) {
112 T *ot = pair.first; 112 T *ot = pair.first;
113 pair.first = 0; 113 pair.first = 0;
114 delete ot; 114 delete ot;
115 ++m_scavenged; 115 ++m_scavenged;
116 } 116 }
117 } 117 }
118 } 118 }
119 119
120 clearExcess(0); 120 clearExcess(0);
121 } 121 }
122 122
129 struct timeval tv; 129 struct timeval tv;
130 (void)gettimeofday(&tv, 0); 130 (void)gettimeofday(&tv, 0);
131 time_t sec = tv.tv_sec; 131 time_t sec = tv.tv_sec;
132 132
133 for (size_t i = 0; i < m_objects.size(); ++i) { 133 for (size_t i = 0; i < m_objects.size(); ++i) {
134 ObjectTimePair &pair = m_objects[i]; 134 ObjectTimePair &pair = m_objects[i];
135 if (pair.first == 0) { 135 if (pair.first == 0) {
136 pair.second = sec; 136 pair.second = sec;
137 pair.first = t; 137 pair.first = t;
138 ++m_claimed; 138 ++m_claimed;
139 return; 139 return;
140 } 140 }
141 } 141 }
142 142
143 std::cerr << "WARNING: Scavenger::claim(" << t << "): run out of slots, " 143 std::cerr << "WARNING: Scavenger::claim(" << t << "): run out of slots, "
144 << "using non-RT-safe method" << std::endl; 144 << "using non-RT-safe method" << std::endl;
145 pushExcess(t); 145 pushExcess(t);
146 } 146 }
147 147
148 template <typename T> 148 template <typename T>
149 void 149 void
156 struct timeval tv; 156 struct timeval tv;
157 (void)gettimeofday(&tv, 0); 157 (void)gettimeofday(&tv, 0);
158 time_t sec = tv.tv_sec; 158 time_t sec = tv.tv_sec;
159 159
160 for (size_t i = 0; i < m_objects.size(); ++i) { 160 for (size_t i = 0; i < m_objects.size(); ++i) {
161 ObjectTimePair &pair = m_objects[i]; 161 ObjectTimePair &pair = m_objects[i];
162 if (clearNow || 162 if (clearNow ||
163 (pair.first != 0 && pair.second + m_sec < sec)) { 163 (pair.first != 0 && pair.second + m_sec < sec)) {
164 T *ot = pair.first; 164 T *ot = pair.first;
165 pair.first = 0; 165 pair.first = 0;
166 delete ot; 166 delete ot;
167 ++m_scavenged; 167 ++m_scavenged;
168 } 168 }
169 } 169 }
170 170
171 if (sec > m_lastExcess + m_sec) { 171 if (sec > m_lastExcess + m_sec) {
172 clearExcess(sec); 172 clearExcess(sec);
173 } 173 }
189 void 189 void
190 Scavenger<T>::clearExcess(time_t sec) 190 Scavenger<T>::clearExcess(time_t sec)
191 { 191 {
192 m_excessMutex.lock(); 192 m_excessMutex.lock();
193 for (typename ObjectList::iterator i = m_excess.begin(); 193 for (typename ObjectList::iterator i = m_excess.begin();
194 i != m_excess.end(); ++i) { 194 i != m_excess.end(); ++i) {
195 delete *i; 195 delete *i;
196 } 196 }
197 m_excess.clear(); 197 m_excess.clear();
198 m_lastExcess = sec; 198 m_lastExcess = sec;
199 m_excessMutex.unlock(); 199 m_excessMutex.unlock();
200 } 200 }