-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRing.cpp
More file actions
169 lines (132 loc) · 3.3 KB
/
Ring.cpp
File metadata and controls
169 lines (132 loc) · 3.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*-----------------------------------------------------------------------------
* This file is part of the Colony.Core Project. The Colony.Core Project is an
* open source project with a BSD type of licensing agreement. See the license
* agreement (license.txt) in the top/ directory or on the Internet at
* http://integerfox.com/colony.core/license.txt
*
* Copyright (c) 2023 Michael N. Moran
*
* Redistributions of the source code must retain the above copyright notice.
*
* This implementation is an adaptation/simplification of the OSCL
* Oscl::Buffer::Ring ring buffer code.
* This adaptation was done by myself (Michael N. Moran)
* and donated to the Colony.Core Project.
*----------------------------------------------------------------------------*/
#include "Ring.h"
///
using namespace Cpl::Container::Ring;
TxDesc::TxDesc(
Header& header
) noexcept:
_allocationOffset(0),
_header(header)
{
}
bool TxDesc::alloc(uint32_t& index) noexcept
{
uint32_t
nElements = _header._nElements;
uint32_t
writeIndex = _header._writeIndex + _allocationOffset;
writeIndex %= nElements;
uint32_t
nextIndex = (writeIndex + 1) % nElements;
if( nextIndex == _header._readIndex )
{
return false;
}
else
{
index = writeIndex;
++_allocationOffset;
return true;
}
}
bool TxDesc::isFull() const noexcept
{
uint32_t
nElements = _header._nElements;
uint32_t
index = _header._writeIndex + _allocationOffset;
index %= nElements;
uint32_t
nextIndex = (index + 1) % nElements;
return nextIndex == _header._readIndex;
}
void TxDesc::commit() noexcept
{
if( !_allocationOffset )
{
/* This is a client error where no
slot has been allocated before
calling commit().
*/
return;
}
uint32_t
index = _header._writeIndex;
uint32_t
nextIndex = (index + 1) % _header._nElements;
_header._writeIndex = nextIndex;
--_allocationOffset;
}
void TxDesc::reset() noexcept
{
_allocationOffset = 0;
}
RxDesc::RxDesc(
Header& header
) noexcept:
_allocationOffset(0),
_header(header)
{
}
bool RxDesc::alloc(uint32_t& index) noexcept
{
uint32_t
readIndex = _header._readIndex + _allocationOffset;
uint32_t
nElements = _header._nElements;
readIndex %= nElements;
uint32_t
writeIndex = _header._writeIndex;
if( readIndex == writeIndex )
{
return false;
}
else
{
++_allocationOffset;
index = readIndex;
return true;
}
}
void RxDesc::resetAllocationOffset() noexcept
{
_allocationOffset = 0;
}
void RxDesc::commit() noexcept
{
uint32_t
nElements = _header._nElements;
uint32_t
index = _header._readIndex + 1;
index %= nElements;
_header._readIndex = index;
--_allocationOffset;
}
void RxDesc::commitAll() noexcept
{
uint32_t
nElements = _header._nElements;
uint32_t
index = _header._readIndex + _allocationOffset;
index %= nElements;
_header._readIndex = index;
_allocationOffset = 0;
}
void RxDesc::reset() noexcept
{
_allocationOffset = 0;
}