GCC Code Coverage Report


Directory: ./
File: Svc/BufferManager/BufferManagerComponentImpl.cpp
Date: 2026-09-23 22:11:34
Exec Total Coverage
Lines: 103 111 92.8%
Functions: 7 7 100.0%
Branches: 37 47 78.7%

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