| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Buffer.cpp | ||
| 3 | // \author mstarch | ||
| 4 | // \brief cpp file for Fw::Buffer implementation | ||
| 5 | // | ||
| 6 | // \copyright | ||
| 7 | // Copyright 2009-2020, by the California Institute of Technology. | ||
| 8 | // ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 9 | // acknowledged. | ||
| 10 | // | ||
| 11 | // ====================================================================== | ||
| 12 | #include <Fw/Buffer/Buffer.hpp> | ||
| 13 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 14 | #include <Fw/Types/Assert.hpp> | ||
| 15 | |||
| 16 | #if FW_SERIALIZABLE_TO_STRING | ||
| 17 | #include <Fw/Types/String.hpp> | ||
| 18 | #endif | ||
| 19 | #include <cstring> | ||
| 20 | |||
| 21 | namespace Fw { | ||
| 22 | |||
| 23 | 1498862 | Buffer::Buffer() | |
| 24 | : Serializable(), | ||
| 25 |
1/1✓ Branch 4 taken 1498862 times.
|
1498862 | m_serialize_repr(), |
| 26 | 1498862 | m_bufferData(nullptr), | |
| 27 | 1498862 | m_offset(0), | |
| 28 | 1498862 | m_size(0), | |
| 29 | 1498862 | m_capacity(0), | |
| 30 | 2997724 | m_context(0xFFFFFFFF) {} | |
| 31 | |||
| 32 | 40181 | Buffer::Buffer(const Buffer& src) | |
| 33 | : Serializable(), | ||
| 34 |
1/1✓ Branch 4 taken 40181 times.
|
40181 | m_serialize_repr(), |
| 35 | 40181 | m_bufferData(src.m_bufferData), | |
| 36 | 40181 | m_offset(src.m_offset), | |
| 37 | 40181 | m_size(src.m_size), | |
| 38 | 40181 | m_capacity(src.m_capacity), | |
| 39 | 80362 | m_context(src.m_context) { | |
| 40 |
2/2✓ Branch 4 taken 40179 times.
✓ Branch 5 taken 2 times.
|
40181 | if (src.m_bufferData != nullptr) { |
| 41 |
1/1✓ Branch 19 taken 40179 times.
|
40179 | this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size); |
| 42 | } | ||
| 43 | 40181 | } | |
| 44 | |||
| 45 | 593555 | Buffer::Buffer(U8* data, FwSizeType size, U32 context) | |
| 46 | : Serializable(), | ||
| 47 |
1/1✓ Branch 4 taken 593555 times.
|
593555 | m_serialize_repr(), |
| 48 | 593555 | m_bufferData(data), | |
| 49 | 593555 | m_offset(0), | |
| 50 | 593555 | m_size(size), | |
| 51 | 593555 | m_capacity(size), | |
| 52 | 1187110 | m_context(context) { | |
| 53 |
2/2✓ Branch 4 taken 593553 times.
✓ Branch 5 taken 2 times.
|
593555 | if (m_bufferData != nullptr) { |
| 54 |
1/1✓ Branch 14 taken 593553 times.
|
593553 | this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size); |
| 55 | } | ||
| 56 | 593555 | } | |
| 57 | |||
| 58 | 1271695 | Buffer& Buffer::operator=(const Buffer& src) { | |
| 59 | // Ward against self-assignment | ||
| 60 |
2/2✓ Branch 0 taken 1271693 times.
✓ Branch 1 taken 2 times.
|
1271695 | if (this != &src) { |
| 61 | 1271693 | this->m_bufferData = src.m_bufferData; | |
| 62 | 1271693 | this->m_offset = src.m_offset; | |
| 63 | 1271693 | this->m_size = src.m_size; | |
| 64 | 1271693 | this->m_capacity = src.m_capacity; | |
| 65 | 1271693 | this->m_context = src.m_context; | |
| 66 |
2/2✓ Branch 4 taken 1264753 times.
✓ Branch 5 taken 6940 times.
|
1271693 | if (this->m_bufferData != nullptr) { |
| 67 | 1264753 | this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size); | |
| 68 | } | ||
| 69 | } | ||
| 70 | 1271695 | return *this; | |
| 71 | } | ||
| 72 | |||
| 73 | 39077 | bool Buffer::operator==(const Buffer& src) const { | |
| 74 |
1/2✓ Branch 16 taken 39077 times.
✗ Branch 17 not taken.
|
78154 | return (this->m_bufferData == src.m_bufferData) && (this->m_offset == src.m_offset) && |
| 75 |
4/8✓ Branch 0 taken 39077 times.
✗ Branch 1 not taken.
✓ Branch 10 taken 39077 times.
✗ Branch 11 not taken.
✓ Branch 20 taken 39077 times.
✗ Branch 21 not taken.
✓ Branch 30 taken 39077 times.
✗ Branch 31 not taken.
|
78154 | (this->m_size == src.m_size) && (this->m_capacity == src.m_capacity) && (this->m_context == src.m_context); |
| 76 | } | ||
| 77 | |||
| 78 | 241241 | bool Buffer::isValid() const { | |
| 79 |
4/4✓ Branch 4 taken 230184 times.
✓ Branch 5 taken 11057 times.
✓ Branch 10 taken 230183 times.
✓ Branch 11 taken 1 times.
|
241241 | return (this->m_bufferData != nullptr) && (this->m_size > 0); |
| 80 | } | ||
| 81 | |||
| 82 | 32178942 | U8* Buffer::getData() const { | |
| 83 |
2/2✓ Branch 4 taken 32168843 times.
✓ Branch 5 taken 10099 times.
|
32178942 | return (this->m_bufferData == nullptr) ? nullptr : (this->m_bufferData + this->m_offset); |
| 84 | } | ||
| 85 | |||
| 86 | 6 | U8* Buffer::getOriginalData() const { | |
| 87 | 6 | return this->m_bufferData; | |
| 88 | } | ||
| 89 | |||
| 90 | 15168120 | FwSizeType Buffer::getSize() const { | |
| 91 | 15168120 | return this->m_size; | |
| 92 | } | ||
| 93 | |||
| 94 | 4 | FwSizeType Buffer::getCapacity() const { | |
| 95 | 4 | return this->m_capacity; | |
| 96 | } | ||
| 97 | |||
| 98 | 6 | FwSizeType Buffer::getOffset() const { | |
| 99 | 6 | return this->m_offset; | |
| 100 | } | ||
| 101 | |||
| 102 | 12926 | U32 Buffer::getContext() const { | |
| 103 | 12926 | return this->m_context; | |
| 104 | } | ||
| 105 | |||
| 106 | 4254 | void Buffer::advance(const FwSignedSizeType amount) { | |
| 107 | 4254 | FW_ASSERT(this->m_bufferData != nullptr); | |
| 108 | // New offset must remain within [0, capacity] | ||
| 109 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4253 times.
|
4254 | if (amount < 0) { |
| 110 | 1 | FW_ASSERT(this->m_offset >= static_cast<FwSizeType>(-amount), static_cast<FwAssertArgType>(this->m_offset), | |
| 111 | static_cast<FwAssertArgType>(amount)); | ||
| 112 | } else { | ||
| 113 | // Advancing must not move past the end of the represented data | ||
| 114 | 4253 | FW_ASSERT(static_cast<FwSizeType>(amount) <= this->m_size, static_cast<FwAssertArgType>(this->m_size), | |
| 115 | static_cast<FwAssertArgType>(amount)); | ||
| 116 | } | ||
| 117 | 4254 | this->m_offset = static_cast<FwSizeType>(static_cast<FwSignedSizeType>(this->m_offset) + amount); | |
| 118 | // Keep the end of the represented data fixed | ||
| 119 | 4254 | this->m_size = static_cast<FwSizeType>(static_cast<FwSignedSizeType>(this->m_size) - amount); | |
| 120 | 4254 | FW_ASSERT(this->m_offset + this->m_size <= this->m_capacity, static_cast<FwAssertArgType>(this->m_offset), | |
| 121 | static_cast<FwAssertArgType>(this->m_size), static_cast<FwAssertArgType>(this->m_capacity)); | ||
| 122 | 4254 | this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size); | |
| 123 | 4254 | } | |
| 124 | |||
| 125 | 2522 | void Buffer::setData(U8* const data) { | |
| 126 | 2522 | FW_ASSERT(this->m_bufferData != nullptr); | |
| 127 | 2522 | FW_ASSERT(data >= this->m_bufferData && data <= &this->m_bufferData[this->m_capacity]); | |
| 128 | 2522 | this->advance(static_cast<FwSignedSizeType>(data - (this->m_bufferData + this->m_offset))); | |
| 129 | 2522 | } | |
| 130 | |||
| 131 | 405870 | void Buffer::setSize(const FwSizeType size) { | |
| 132 | 405870 | FW_ASSERT(this->m_offset + size <= this->m_capacity, static_cast<FwAssertArgType>(this->m_offset), | |
| 133 | static_cast<FwAssertArgType>(size), static_cast<FwAssertArgType>(this->m_capacity)); | ||
| 134 | 405870 | this->m_size = size; | |
| 135 |
1/2✓ Branch 4 taken 405870 times.
✗ Branch 5 not taken.
|
405870 | if (m_bufferData != nullptr) { |
| 136 | 405870 | this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size); | |
| 137 | } | ||
| 138 | 405870 | } | |
| 139 | |||
| 140 | 5064 | void Buffer::setContext(const U32 context) { | |
| 141 | 5064 | this->m_context = context; | |
| 142 | 5064 | } | |
| 143 | |||
| 144 | 31259 | void Buffer::set(U8* const data, const FwSizeType size, const U32 context) { | |
| 145 | 31259 | this->m_bufferData = data; | |
| 146 | 31259 | this->m_offset = 0; | |
| 147 | 31259 | this->m_size = size; | |
| 148 | 31259 | this->m_capacity = size; | |
| 149 |
2/2✓ Branch 4 taken 21181 times.
✓ Branch 5 taken 10078 times.
|
31259 | if (m_bufferData != nullptr) { |
| 150 | 21181 | this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size); | |
| 151 | } | ||
| 152 | 31259 | this->m_context = context; | |
| 153 | 31259 | } | |
| 154 | |||
| 155 | 59760 | Fw::ExternalSerializeBufferWithMemberCopy Buffer::getSerializer() { | |
| 156 |
2/2✓ Branch 4 taken 59759 times.
✓ Branch 5 taken 1 times.
|
59760 | if (this->isValid()) { |
| 157 |
1/1✓ Branch 15 taken 59759 times.
|
59759 | Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData + this->m_offset, this->m_size); |
| 158 |
1/1✓ Branch 2 taken 59759 times.
|
59759 | esb.resetSer(); |
| 159 |
1/1✓ Branch 1 taken 59759 times.
|
59759 | return esb; |
| 160 | 59759 | } else { | |
| 161 | 1 | return ExternalSerializeBufferWithMemberCopy(); | |
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | 75635 | Fw::ExternalSerializeBufferWithMemberCopy Buffer::getDeserializer() { | |
| 166 |
2/2✓ Branch 4 taken 75634 times.
✓ Branch 5 taken 1 times.
|
75635 | if (this->isValid()) { |
| 167 |
1/1✓ Branch 15 taken 75634 times.
|
75634 | Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData + this->m_offset, this->m_size); |
| 168 |
1/1✓ Branch 6 taken 75634 times.
|
75634 | Fw::SerializeStatus stat = esb.setBuffLen(this->m_size); |
| 169 | 75634 | FW_ASSERT(stat == Fw::FW_SERIALIZE_OK); | |
| 170 |
1/1✓ Branch 1 taken 75634 times.
|
75634 | return esb; |
| 171 | 75634 | } else { | |
| 172 | 1 | return ExternalSerializeBufferWithMemberCopy(); | |
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | 13866 | Fw::SerializeStatus Buffer::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const { | |
| 177 | Fw::SerializeStatus stat; | ||
| 178 | 13866 | stat = buffer.serializeFrom(reinterpret_cast<PlatformPointerCastType>(this->m_bufferData), mode); | |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13866 times.
|
13866 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 180 | ✗ | return stat; | |
| 181 | } | ||
| 182 | 13866 | stat = buffer.serializeFrom(this->m_size, mode); | |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13866 times.
|
13866 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 184 | ✗ | return stat; | |
| 185 | } | ||
| 186 | 13866 | stat = buffer.serializeFrom(this->m_context, mode); | |
| 187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13866 times.
|
13866 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 188 | ✗ | return stat; | |
| 189 | } | ||
| 190 | 13866 | stat = buffer.serializeFrom(this->m_offset, mode); | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13866 times.
|
13866 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 192 | ✗ | return stat; | |
| 193 | } | ||
| 194 | 13866 | stat = buffer.serializeFrom(this->m_capacity, mode); | |
| 195 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13865 times.
|
13866 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 196 | 1 | return stat; | |
| 197 | } | ||
| 198 | 13865 | return stat; | |
| 199 | } | ||
| 200 | |||
| 201 | 13846 | Fw::SerializeStatus Buffer::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) { | |
| 202 | Fw::SerializeStatus stat; | ||
| 203 | 13846 | PlatformPointerCastType pointer; | |
| 204 |
1/1✓ Branch 7 taken 13846 times.
|
13846 | stat = buffer.deserializeTo(pointer, mode); |
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13846 times.
|
13846 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 206 | ✗ | return stat; | |
| 207 | } | ||
| 208 | 13846 | this->m_bufferData = reinterpret_cast<U8*>(pointer); | |
| 209 | |||
| 210 |
1/1✓ Branch 10 taken 13846 times.
|
13846 | stat = buffer.deserializeTo(this->m_size, mode); |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13846 times.
|
13846 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 212 | ✗ | return stat; | |
| 213 | } | ||
| 214 |
1/1✓ Branch 10 taken 13846 times.
|
13846 | stat = buffer.deserializeTo(this->m_context, mode); |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13846 times.
|
13846 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 216 | ✗ | return stat; | |
| 217 | } | ||
| 218 |
1/1✓ Branch 10 taken 13846 times.
|
13846 | stat = buffer.deserializeTo(this->m_offset, mode); |
| 219 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13846 times.
|
13846 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 220 | ✗ | return stat; | |
| 221 | } | ||
| 222 |
1/1✓ Branch 10 taken 13846 times.
|
13846 | stat = buffer.deserializeTo(this->m_capacity, mode); |
| 223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13846 times.
|
13846 | if (stat != Fw::FW_SERIALIZE_OK) { |
| 224 | ✗ | return stat; | |
| 225 | } | ||
| 226 | |||
| 227 |
2/2✓ Branch 4 taken 12853 times.
✓ Branch 5 taken 993 times.
|
13846 | if (this->m_bufferData != nullptr) { |
| 228 |
1/1✓ Branch 19 taken 12853 times.
|
12853 | this->m_serialize_repr.setExtBuffer(this->m_bufferData + this->m_offset, this->m_size); |
| 229 | } | ||
| 230 | 13846 | return stat; | |
| 231 | } | ||
| 232 | |||
| 233 | #if FW_SERIALIZABLE_TO_STRING | ||
| 234 | ✗ | void Buffer::toString(Fw::StringBase& text) const { | |
| 235 | static const char* formatString = "(data = %p, size = %u, context = %u, offset = %u, capacity = %u)"; | ||
| 236 | ✗ | (void)text.format(formatString, this->m_bufferData, this->m_size, this->m_context, this->m_offset, | |
| 237 | ✗ | this->m_capacity); // display string may safely truncate | |
| 238 | ✗ | } | |
| 239 | #endif | ||
| 240 | |||
| 241 | #ifdef BUILD_UT | ||
| 242 | ✗ | std::ostream& operator<<(std::ostream& os, const Buffer& obj) { | |
| 243 | ✗ | Fw::String str; | |
| 244 | ✗ | obj.toString(str); | |
| 245 | ✗ | os << str.toChar(); | |
| 246 | ✗ | return os; | |
| 247 | ✗ | } | |
| 248 | #endif | ||
| 249 | |||
| 250 | } // end namespace Fw | ||
| 251 |