GCC Code Coverage Report


Directory: lib/fprime-samd/fprime-samd/
File: Svc/ThinBufferManager/ThinBufferManager.cpp
Date: 2026-09-26 00:05:09
Exec Total Coverage
Lines: 0 103 0.0%
Functions: 0 7 0.0%
Branches: 0 106 0.0%

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 ✗ ThinBufferManager ::ThinBufferManager(const char* const compName)
24 : ThinBufferManagerComponentBase(compName),
25 ✗ m_setup(false),
26 ✗ m_cleaned(false),
27 ✗ m_mgrId(0),
28 ✗ m_buffers(nullptr),
29 ✗ m_allocator(nullptr),
30 ✗ m_memId(0),
31 ✗ m_numStructs(0),
32 ✗ m_highWater(0),
33 ✗ m_currBuffs(0),
34 ✗ m_noBuffs(0),
35 ✗ m_emptyBuffs(0) {}
36
37 ✗ ThinBufferManager ::~ThinBufferManager() {
38 ✗ if (m_setup) {
39 ✗ this->cleanup();
40 }
41 ✗ }
42
43 ✗ void ThinBufferManager ::cleanup() {
44 ✗ FW_ASSERT(this->m_buffers != nullptr);
45 ✗ FW_ASSERT(this->m_allocator != nullptr);
46
47 ✗ 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 ✗ this->m_cleaned = true;
51 // release memory
52 ✗ this->m_allocator->deallocate(this->m_memId, this->m_buffers);
53 ✗ this->m_setup = false;
54 }
55 ✗ }
56
57 // ----------------------------------------------------------------------
58 // Handler implementations for user-defined typed input ports
59 // ----------------------------------------------------------------------
60
61 ✗ void ThinBufferManager ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
62 // make sure component has been set up
63 ✗ FW_ASSERT(this->m_setup);
64 ✗ 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 ✗ if (fwBuffer.getData() == nullptr && fwBuffer.getSize() == 0) {
70 ✗ this->log_WARNING_HI_NullEmptyBuffer();
71 ✗ this->m_emptyBuffs++;
72 ✗ return;
73 }
74 // use the bufferID member field to find the original slot
75 ✗ U32 context = fwBuffer.getContext();
76 ✗ U32 id = context & 0xFFFF;
77 ✗ U32 mgrId = context >> 16;
78 // check some things
79 ✗ FW_ASSERT(id < this->m_numStructs, static_cast<FwAssertArgType>(id),
80 static_cast<FwAssertArgType>(this->m_numStructs));
81 ✗ FW_ASSERT(mgrId == this->m_mgrId, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(id),
82 static_cast<FwAssertArgType>(this->m_mgrId));
83 ✗ FW_ASSERT(true == this->m_buffers[id].allocated, static_cast<FwAssertArgType>(id),
84 static_cast<FwAssertArgType>(this->m_mgrId));
85 ✗ FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory, static_cast<FwAssertArgType>(id),
86 static_cast<FwAssertArgType>(this->m_mgrId));
87 ✗ 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 ✗ 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 ✗ this->m_buffers[id].allocated = false;
94 ✗ this->m_currBuffs--;
95 }
96
97 ✗ Fw::Buffer ThinBufferManager ::bufferGetCallee_handler(const FwIndexType portNum, Fw::Buffer::SizeType size) {
98 // make sure component has been set up
99 ✗ FW_ASSERT(this->m_setup);
100 ✗ FW_ASSERT(m_buffers != nullptr);
101 // find smallest buffer based on size.
102 ✗ for (U16 buff = 0; buff < this->m_numStructs; buff++) {
103 ✗ if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) {
104 ✗ this->m_buffers[buff].allocated = true;
105 ✗ this->m_currBuffs++;
106 ✗ if (this->m_currBuffs > this->m_highWater) {
107 ✗ 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 ✗ Fw::Buffer copy = this->m_buffers[buff].buff.getBuffer();
113 // change size to match request
114 ✗ copy.setSize(size);
115 ✗ return copy;
116 ✗ }
117 }
118
119 // if no buffers found, return empty buffer
120 ✗ this->log_WARNING_HI_NoBuffsAvailable(size);
121 ✗ this->m_noBuffs++;
122 ✗ return Fw::Buffer();
123 }
124
125 ✗ 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 ✗ this->m_mgrId = mgrId;
131 ✗ this->m_memId = memId;
132 ✗ this->m_allocator = &allocator;
133 // clear bins
134 ✗ (void)memset(&this->m_bufferBins, 0, sizeof(this->m_bufferBins));
135
136 ✗ this->m_bufferBins = bins;
137
138 // compute the amount of memory needed
139 ✗ FwSizeType memorySize = 0; // track needed memory
140 ✗ this->m_numStructs = 0; // size the number of tracking structs
141 // walk through bins and add up the sizes
142 ✗ for (U16 bin = 0; bin < ThinBufferManagerConfig::THINBUFFERMGR_MAX_NUM_BINS; bin++) {
143 ✗ if (this->m_bufferBins.bins[bin].numBuffers) {
144 ✗ memorySize += (this->m_bufferBins.bins[bin].bufferSize *
145 ✗ 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 ✗ FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >=
150 this->m_bufferBins.bins[bin].numBuffers);
151 ✗ this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers);
152 }
153 }
154
155 ✗ FwSizeType allocatedSize = memorySize;
156 ✗ bool recoverable = false; //!< don't care if it is recoverable since they are a pool of user buffers
157
158 // allocate memory
159 ✗ void* memory = allocator.allocate(memId, allocatedSize, recoverable);
160 // make sure the memory returns was non-zero and the size requested
161 ✗ 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 ✗ 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 ✗ U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
170
171 // walk through entries and initialize them
172 ✗ U16 currStruct = 0;
173 ✗ for (U16 bin = 0; bin < ThinBufferManagerConfig::THINBUFFERMGR_MAX_NUM_BINS; bin++) {
174 ✗ if (this->m_bufferBins.bins[bin].numBuffers) {
175 ✗ for (U16 binEntry = 0; binEntry < this->m_bufferBins.bins[bin].numBuffers; binEntry++) {
176 ✗ 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 ✗ this->m_buffers[currStruct].buff =
184 ✗ Samd21::ThinBuffer(bufferMem, static_cast<U32>(this->m_bufferBins.bins[bin].bufferSize), context);
185 ✗ this->m_buffers[currStruct].allocated = false;
186 ✗ this->m_buffers[currStruct].memory = bufferMem;
187 ✗ this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
188 ✗ bufferMem += this->m_bufferBins.bins[bin].bufferSize;
189 ✗ currStruct++;
190 }
191 }
192 }
193
194 // check that the initiation pointer made it to the end of allocated space
195 ✗ U8* const CURR_PTR = bufferMem;
196 ✗ U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
197 ✗ 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 ✗ 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 ✗ this->m_setup = true;
206 ✗ }
207
208 ✗ void ThinBufferManager ::schedIn_handler(const FwIndexType portNum, U32 context) {
209 // write telemetry values
210 ✗ this->tlmWrite_HiBuffs(this->m_highWater);
211 ✗ this->tlmWrite_CurrBuffs(this->m_currBuffs);
212 ✗ this->tlmWrite_TotalBuffs(this->m_numStructs);
213 ✗ this->tlmWrite_NoBuffs(this->m_noBuffs);
214 ✗ this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
215 ✗ }
216
217 } // end namespace Samd21
218