-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPointerStorage.cxx
61 lines (52 loc) · 1.63 KB
/
PointerStorage.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "sys.h"
#include "PointerStorage.h"
#include <algorithm>
namespace threadsafe {
void VoidPointerStorage::increase_size(uint32_t initial_size)
{
m_rwlock.rd2wrlock(); // This might throw if another thread is already trying to convert its read lock into a write lock.
index_type size = m_size;
m_size = std::max(static_cast<index_type>(initial_size), static_cast<index_type>(memory_grow_factor * size));
if (AI_UNLIKELY(m_size == size))
m_size += 1;
m_storage.reserve(m_size);
m_storage.resize(m_size);
std::vector<index_type> tmp;
m_free_indices.consume_all([&tmp](index_type index){
tmp.push_back(index);
});
m_free_indices.reserve(m_size);
ASSERT(m_free_indices.empty());
index_type index = m_size;
// Dout(dc::notice|continued_cf, "Pushing:");
while (index > size)
{
// Dout(dc::continued, ' ' << (index - 1));
m_free_indices.bounded_push(--index);
}
for (auto iter = tmp.rbegin(); iter != tmp.rend(); ++iter)
{
// Dout(dc::continued, ' ' << *iter);
m_free_indices.bounded_push(*iter);
}
// Dout(dc::finish, '.');
m_rwlock.wr2rdlock();
}
#ifdef CWDEBUG
bool VoidPointerStorage::debug_empty() const
{
// The storage contains no pointers when all indices are free.
// The only way we can check this is by emptying the stack :/.
m_rwlock.wrlock();
std::vector<index_type> tmp;
m_free_indices.consume_all([&tmp](index_type index){
tmp.push_back(index);
});
bool empty = tmp.size() == m_size;
for (auto iter = tmp.rbegin(); iter != tmp.rend(); ++iter)
m_free_indices.bounded_push(*iter);
m_rwlock.wrunlock();
return empty;
}
#endif
} // namespace threadsafe