| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title ThinBufferManager.cpp | ||
| 3 | // \brief cpp file for ThinBufferManager component implementation class | ||
| 4 | // | ||
| 5 | // Ported from Svc::BufferManagerComponentImpl (lib/fprime/Svc/BufferManager), | ||
| 6 | // with the per-slot Fw::Buffer replaced by Samd21::ThinBuffer. See the header | ||
| 7 | // for rationale. | ||
| 8 | // | ||
| 9 | // ====================================================================== | ||
| 10 | |||
| 11 | #include "fprime-samd/Svc/ThinBufferManager/ThinBufferManager.hpp" | ||
| 12 | #include <Fw/Buffer/Buffer.hpp> | ||
| 13 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 14 | #include <Fw/Types/Assert.hpp> | ||
| 15 | #include <cstring> | ||
| 16 | |||
| 17 | namespace Samd21 { | ||
| 18 | |||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | // Construction, initialization, and destruction | ||
| 21 | // ---------------------------------------------------------------------- | ||
| 22 | |||
| 23 | 3 | ThinBufferManager ::ThinBufferManager(const char* const compName) | |
| 24 | : ThinBufferManagerComponentBase(compName), | ||
| 25 | 3 | m_setup(false), | |
| 26 | 3 | m_cleaned(false), | |
| 27 | 3 | m_mgrId(0), | |
| 28 | 3 | m_buffers(nullptr), | |
| 29 | 3 | m_allocator(nullptr), | |
| 30 | 3 | m_memId(0), | |
| 31 | 3 | m_numStructs(0), | |
| 32 | 3 | m_highWater(0), | |
| 33 | 3 | m_currBuffs(0), | |
| 34 | 3 | m_noBuffs(0), | |
| 35 | 6 | m_emptyBuffs(0) {} | |
| 36 | |||
| 37 | 6 | ThinBufferManager ::~ThinBufferManager() { | |
| 38 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
|
6 | if (m_setup) { |
| 39 | ✗ | this->cleanup(); | |
| 40 | } | ||
| 41 | 6 | } | |
| 42 | |||
| 43 | 3 | void ThinBufferManager ::cleanup() { | |
| 44 | 3 | FW_ASSERT(this->m_buffers != nullptr); | |
| 45 | 3 | FW_ASSERT(this->m_allocator != nullptr); | |
| 46 | |||
| 47 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | if (not this->m_cleaned) { |
| 48 | // ThinBuffer is trivially destructible; no per-entry destructor calls needed | ||
| 49 | // (unlike Fw::Buffer, which the fprime BufferManager destructs explicitly here). | ||
| 50 | 3 | this->m_cleaned = true; | |
| 51 | // release memory | ||
| 52 | 3 | this->m_allocator->deallocate(this->m_memId, this->m_buffers); | |
| 53 | 3 | this->m_setup = false; | |
| 54 | } | ||
| 55 | 3 | } | |
| 56 | |||
| 57 | // ---------------------------------------------------------------------- | ||
| 58 | // Handler implementations for user-defined typed input ports | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | 27 | void ThinBufferManager ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) { | |
| 62 | // make sure component has been set up | ||
| 63 | 27 | FW_ASSERT(this->m_setup); | |
| 64 | 27 | FW_ASSERT(m_buffers != nullptr); | |
| 65 | // check for null, empty buffers - this is a warning because this component returns | ||
| 66 | // null, empty buffers if it can't allocate one. | ||
| 67 | // however, empty non-null buffers could potentially be previously allocated | ||
| 68 | // buffers with their size reduced. the user is allowed to make buffers smaller. | ||
| 69 |
5/6✓ Branch 4 taken 4 times.
✓ Branch 5 taken 23 times.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 23 times.
|
27 | if (fwBuffer.getData() == nullptr && fwBuffer.getSize() == 0) { |
| 70 | 4 | this->log_WARNING_HI_NullEmptyBuffer(); | |
| 71 | 4 | this->m_emptyBuffs++; | |
| 72 | 4 | return; | |
| 73 | } | ||
| 74 | // use the bufferID member field to find the original slot | ||
| 75 | 23 | U32 context = fwBuffer.getContext(); | |
| 76 | 23 | U32 id = context & 0xFFFF; | |
| 77 | 23 | U32 mgrId = context >> 16; | |
| 78 | // check some things | ||
| 79 | 23 | FW_ASSERT(id < this->m_numStructs, static_cast<FwAssertArgType>(id), | |
| 80 | static_cast<FwAssertArgType>(this->m_numStructs)); | ||
| 81 | 23 | FW_ASSERT(mgrId == this->m_mgrId, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(id), | |
| 82 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 83 | 23 | FW_ASSERT(true == this->m_buffers[id].allocated, static_cast<FwAssertArgType>(id), | |
| 84 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 85 | 23 | FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory, static_cast<FwAssertArgType>(id), | |
| 86 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 87 | 23 | FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) < (this->m_buffers[id].memory + this->m_buffers[id].size), | |
| 88 | static_cast<FwAssertArgType>(id), static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 89 | // user can make smaller for their own purposes, but it shouldn't be bigger | ||
| 90 | 23 | FW_ASSERT(fwBuffer.getSize() <= this->m_buffers[id].size, static_cast<FwAssertArgType>(id), | |
| 91 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 92 | // clear the allocated flag | ||
| 93 | 23 | this->m_buffers[id].allocated = false; | |
| 94 | 23 | this->m_currBuffs--; | |
| 95 | } | ||
| 96 | |||
| 97 | 27 | Fw::Buffer ThinBufferManager ::bufferGetCallee_handler(const FwIndexType portNum, Fw::Buffer::SizeType size) { | |
| 98 | // make sure component has been set up | ||
| 99 | 27 | FW_ASSERT(this->m_setup); | |
| 100 | 27 | FW_ASSERT(m_buffers != nullptr); | |
| 101 | // find smallest buffer based on size. | ||
| 102 |
2/2✓ Branch 4 taken 152 times.
✓ Branch 5 taken 4 times.
|
156 | for (U16 buff = 0; buff < this->m_numStructs; buff++) { |
| 103 |
5/6✗ Branch 6 not taken.
✓ Branch 7 taken 152 times.
✓ Branch 8 taken 63 times.
✓ Branch 9 taken 89 times.
✓ Branch 17 taken 23 times.
✓ Branch 18 taken 40 times.
|
152 | if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) { |
| 104 | 23 | this->m_buffers[buff].allocated = true; | |
| 105 | 23 | this->m_currBuffs++; | |
| 106 |
2/2✓ Branch 8 taken 13 times.
✓ Branch 9 taken 10 times.
|
23 | if (this->m_currBuffs > this->m_highWater) { |
| 107 | 13 | this->m_highWater = this->m_currBuffs; | |
| 108 | } | ||
| 109 | // Build the caller-facing Fw::Buffer fresh from the stored ThinBuffer. This | ||
| 110 | // temporary lives on the caller's stack only -- the persistent slot never | ||
| 111 | // stores an Fw::Buffer. | ||
| 112 |
1/1✓ Branch 8 taken 23 times.
|
23 | Fw::Buffer copy = this->m_buffers[buff].buff.getBuffer(); |
| 113 | // change size to match request | ||
| 114 |
1/1✓ Branch 2 taken 23 times.
|
23 | copy.setSize(size); |
| 115 |
1/1✓ Branch 1 taken 23 times.
|
23 | return copy; |
| 116 | 23 | } | |
| 117 | } | ||
| 118 | |||
| 119 | // if no buffers found, return empty buffer | ||
| 120 | 4 | this->log_WARNING_HI_NoBuffsAvailable(size); | |
| 121 | 4 | this->m_noBuffs++; | |
| 122 | 4 | return Fw::Buffer(); | |
| 123 | } | ||
| 124 | |||
| 125 | 3 | void ThinBufferManager::setup(U16 mgrId, //!< manager ID | |
| 126 | FwEnumStoreType memId, //!< Memory segment identifier | ||
| 127 | Fw::MemAllocator& allocator, //!< memory allocator | ||
| 128 | const BufferBins& bins //!< Set of user bins | ||
| 129 | ) { | ||
| 130 | 3 | this->m_mgrId = mgrId; | |
| 131 | 3 | this->m_memId = memId; | |
| 132 | 3 | this->m_allocator = &allocator; | |
| 133 | // clear bins | ||
| 134 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | (void)memset(&this->m_bufferBins, 0, sizeof(this->m_bufferBins)); |
| 135 | |||
| 136 | 3 | this->m_bufferBins = bins; | |
| 137 | |||
| 138 | // compute the amount of memory needed | ||
| 139 | 3 | FwSizeType memorySize = 0; // track needed memory | |
| 140 | 3 | this->m_numStructs = 0; // size the number of tracking structs | |
| 141 | // walk through bins and add up the sizes | ||
| 142 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (U16 bin = 0; bin < ThinBufferManagerConfig::THINBUFFERMGR_MAX_NUM_BINS; bin++) { |
| 143 |
2/2✓ Branch 5 taken 7 times.
✓ Branch 6 taken 2 times.
|
9 | if (this->m_bufferBins.bins[bin].numBuffers) { |
| 144 | 7 | memorySize += (this->m_bufferBins.bins[bin].bufferSize * | |
| 145 | 7 | this->m_bufferBins.bins[bin].numBuffers) + // allocate each set of buffer memory | |
| 146 | (static_cast<FwSizeType>(sizeof(AllocatedBuffer)) * | ||
| 147 | this->m_bufferBins.bins[bin].numBuffers); // allocate the structs to track the buffers | ||
| 148 | // Total structures is bounded by U16 maximum value to fit in half of context (U32) | ||
| 149 | 7 | FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >= | |
| 150 | this->m_bufferBins.bins[bin].numBuffers); | ||
| 151 | 7 | this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers); | |
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | 3 | FwSizeType allocatedSize = memorySize; | |
| 156 | 3 | bool recoverable = false; //!< don't care if it is recoverable since they are a pool of user buffers | |
| 157 | |||
| 158 | // allocate memory | ||
| 159 |
1/1✓ Branch 6 taken 3 times.
|
3 | void* memory = allocator.allocate(memId, allocatedSize, recoverable); |
| 160 | // make sure the memory returns was non-zero and the size requested | ||
| 161 | 3 | FW_ASSERT(memory != nullptr && memorySize == allocatedSize, static_cast<FwAssertArgType>(mgrId), | |
| 162 | static_cast<FwAssertArgType>(memId), | ||
| 163 | static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(memory)), | ||
| 164 | static_cast<FwAssertArgType>(memorySize), static_cast<FwAssertArgType>(allocatedSize)); | ||
| 165 | // structs will be at beginning of memory | ||
| 166 | 3 | this->m_buffers = static_cast<AllocatedBuffer*>(memory); | |
| 167 | // memory buffers will be at end of structs in memory, so compute that memory as the beginning of the | ||
| 168 | // struct past the number of structs | ||
| 169 | 3 | U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]); | |
| 170 | |||
| 171 | // walk through entries and initialize them | ||
| 172 | 3 | U16 currStruct = 0; | |
| 173 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (U16 bin = 0; bin < ThinBufferManagerConfig::THINBUFFERMGR_MAX_NUM_BINS; bin++) { |
| 174 |
2/2✓ Branch 5 taken 7 times.
✓ Branch 6 taken 2 times.
|
9 | if (this->m_bufferBins.bins[bin].numBuffers) { |
| 175 |
2/2✓ Branch 5 taken 22 times.
✓ Branch 6 taken 7 times.
|
29 | for (U16 binEntry = 0; binEntry < this->m_bufferBins.bins[bin].numBuffers; binEntry++) { |
| 176 | 22 | U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct); | |
| 177 | // ThinBuffer is trivial, so a plain assignment (no placement new required) is | ||
| 178 | // sufficient -- unlike the fprime BufferManager, which must placement-new an | ||
| 179 | // Fw::Buffer here because it owns a non-trivial Fw::ExternalSerializeBuffer member. | ||
| 180 | // context must be stored per-slot: bufferSendIn_handler reads it back (via | ||
| 181 | // Fw::Buffer::getContext(), populated from ThinBuffer::getBuffer()) to identify | ||
| 182 | // which slot a returned buffer belongs to. | ||
| 183 | 22 | this->m_buffers[currStruct].buff = | |
| 184 |
1/1✓ Branch 7 taken 22 times.
|
44 | Samd21::ThinBuffer(bufferMem, static_cast<U32>(this->m_bufferBins.bins[bin].bufferSize), context); |
| 185 | 22 | this->m_buffers[currStruct].allocated = false; | |
| 186 | 22 | this->m_buffers[currStruct].memory = bufferMem; | |
| 187 | 22 | this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize; | |
| 188 | 22 | bufferMem += this->m_bufferBins.bins[bin].bufferSize; | |
| 189 | 22 | currStruct++; | |
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | // check that the initiation pointer made it to the end of allocated space | ||
| 195 | 3 | U8* const CURR_PTR = bufferMem; | |
| 196 | 3 | U8* const END_PTR = static_cast<U8*>(memory) + memorySize; | |
| 197 | 3 | FW_ASSERT(CURR_PTR == END_PTR, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(memId), | |
| 198 | static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(CURR_PTR)), | ||
| 199 | static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(END_PTR))); | ||
| 200 | // secondary init verification | ||
| 201 | 3 | FW_ASSERT(currStruct == this->m_numStructs, static_cast<FwAssertArgType>(mgrId), | |
| 202 | static_cast<FwAssertArgType>(memId), static_cast<FwAssertArgType>(currStruct), | ||
| 203 | static_cast<FwAssertArgType>(this->m_numStructs)); | ||
| 204 | // indicate setup is done | ||
| 205 | 3 | this->m_setup = true; | |
| 206 | 3 | } | |
| 207 | |||
| 208 | 8 | void ThinBufferManager ::schedIn_handler(const FwIndexType portNum, U32 context) { | |
| 209 | // write telemetry values | ||
| 210 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_HiBuffs(this->m_highWater); |
| 211 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_CurrBuffs(this->m_currBuffs); |
| 212 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_TotalBuffs(this->m_numStructs); |
| 213 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_NoBuffs(this->m_noBuffs); |
| 214 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_EmptyBuffs(this->m_emptyBuffs); |
| 215 | 8 | } | |
| 216 | |||
| 217 | } // end namespace Samd21 | ||
| 218 |