-
Notifications
You must be signed in to change notification settings - Fork 371
/
used_chunk_list.inl
154 lines (129 loc) · 4.33 KB
/
used_chunk_list.inl
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_POSH_POPO_USED_CHUNK_LIST_INL
#define IOX_POSH_POPO_USED_CHUNK_LIST_INL
namespace iox
{
namespace popo
{
template <uint32_t Capacity>
constexpr typename UsedChunkList<Capacity>::DataElement_t UsedChunkList<Capacity>::DATA_ELEMENT_LOGICAL_NULLPTR;
template <uint32_t Capacity>
UsedChunkList<Capacity>::UsedChunkList() noexcept
{
static_assert(sizeof(DataElement_t) <= 8U, "The size of the data element type must not exceed 64 bit!");
static_assert(std::is_trivially_copyable<DataElement_t>::value,
"The data element type must be trivially copyable!");
init();
}
template <uint32_t Capacity>
bool UsedChunkList<Capacity>::insert(mepoo::SharedChunk chunk) noexcept
{
auto hasFreeSpace = m_freeListHead != INVALID_INDEX;
if (hasFreeSpace)
{
// get next free entry after freelistHead
auto nextFree = m_listIndices[m_freeListHead];
// freeListHead is getting new usedListHead, next of this entry is updated to next in usedList
m_listIndices[m_freeListHead] = m_usedListHead;
m_usedListHead = m_freeListHead;
m_listData[m_usedListHead] = DataElement_t(chunk);
// set freeListHead to the next free entry
m_freeListHead = nextFree;
m_synchronizer.clear(std::memory_order_release);
return true;
}
else
{
return false;
}
}
template <uint32_t Capacity>
bool UsedChunkList<Capacity>::remove(const mepoo::ChunkHeader* chunkHeader, mepoo::SharedChunk& chunk) noexcept
{
auto previous = INVALID_INDEX;
// go through usedList with stored chunks
for (auto current = m_usedListHead; current != INVALID_INDEX; current = m_listIndices[current])
{
if (!m_listData[current].isLogicalNullptr())
{
// does the entry match the one we want to remove?
if (m_listData[current].getChunkHeader() == chunkHeader)
{
chunk = m_listData[current].releaseToSharedChunk();
// remove index from used list
if (current == m_usedListHead)
{
m_usedListHead = m_listIndices[current];
}
else
{
m_listIndices[previous] = m_listIndices[current];
}
// insert index to free list
m_listIndices[current] = m_freeListHead;
m_freeListHead = current;
m_synchronizer.clear(std::memory_order_release);
return true;
}
}
previous = current;
}
return false;
}
template <uint32_t Capacity>
void UsedChunkList<Capacity>::cleanup() noexcept
{
m_synchronizer.test_and_set(std::memory_order_acquire);
for (auto& data : m_listData)
{
if (!data.isLogicalNullptr())
{
// release ownership by creating a SharedChunk
data.releaseToSharedChunk();
}
}
init(); // just to save us from the future self
}
template <uint32_t Capacity>
void UsedChunkList<Capacity>::init() noexcept
{
// build list
for (uint32_t i = 0U; i < Capacity; ++i)
{
m_listIndices[i] = i + 1u;
}
if (Capacity > 0U)
{
m_listIndices[Capacity - 1U] = INVALID_INDEX; // just to save us from the future self
}
else
{
m_listIndices[0U] = INVALID_INDEX;
}
m_usedListHead = INVALID_INDEX;
m_freeListHead = 0U;
// clear data
for (auto& data : m_listData)
{
data.releaseToSharedChunk();
}
m_synchronizer.clear(std::memory_order_release);
}
} // namespace popo
} // namespace iox
#endif // IOX_POSH_POPO_USED_CHUNK_LIST_INL