GCC Code Coverage Report


Directory: /home/runner/work/fprime/fprime/Svc/BufferManager/
File: BufferManagerComponentImpl.cpp
Date: 2026-09-03 22:13:18
Exec Total Coverage
Lines: 97 105 92.4%
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 1431 void BufferManagerComponentImpl ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
67 // make sure component has been set up
68 1431 FW_ASSERT(this->m_setup);
69 1431 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 1431 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1431 times.
1431 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 1431 U32 context = fwBuffer.getContext();
81 1431 U32 id = context & 0xFFFF;
82 1431 U32 mgrId = context >> 16;
83 // check some things
84 1431 FW_ASSERT(id < this->m_numStructs, static_cast<FwAssertArgType>(id),
85 static_cast<FwAssertArgType>(this->m_numStructs));
86 1431 FW_ASSERT(mgrId == this->m_mgrId, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(id),
87 static_cast<FwAssertArgType>(this->m_mgrId));
88 1431 FW_ASSERT(true == this->m_buffers[id].allocated, static_cast<FwAssertArgType>(id),
89 static_cast<FwAssertArgType>(this->m_mgrId));
90 1431 FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory, static_cast<FwAssertArgType>(id),
91 static_cast<FwAssertArgType>(this->m_mgrId));
92 1431 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 1431 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 1431 this->m_buffers[id].allocated = false;
99 1431 this->m_currBuffs--;
100 }
101
102 1431 Fw::Buffer BufferManagerComponentImpl ::bufferGetCallee_handler(const FwIndexType portNum, Fw::Buffer::SizeType size) {
103 // make sure component has been set up
104 1431 FW_ASSERT(this->m_setup);
105 1431 FW_ASSERT(m_buffers != nullptr);
106 // find smallest buffer based on size.
107
1/2
✓ Branch 0 taken 2241 times.
✗ Branch 1 not taken.
2241 for (U16 buff = 0; buff < this->m_numStructs; buff++) {
108
3/4
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 810 times.
✓ Branch 2 taken 1431 times.
✗ Branch 3 not taken.
2241 if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) {
109 1431 this->m_buffers[buff].allocated = true;
110 1431 this->m_currBuffs++;
111
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1427 times.
1431 if (this->m_currBuffs > this->m_highWater) {
112 4 this->m_highWater = this->m_currBuffs;
113 }
114
1/1
✓ Branch 1 taken 1431 times.
1431 Fw::Buffer copy = this->m_buffers[buff].buff;
115 // change size to match request
116
1/1
✓ Branch 1 taken 1431 times.
1431 copy.setSize(size);
117
1/1
✓ Branch 1 taken 1431 times.
1431 return copy;
118 1431 }
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 memorySize += (this->m_bufferBins.bins[bin].bufferSize *
147 3 this->m_bufferBins.bins[bin].numBuffers) + // allocate each set of buffer memory
148 (static_cast<FwSizeType>(sizeof(AllocatedBuffer)) *
149 this->m_bufferBins.bins[bin].numBuffers); // allocate the structs to track the buffers
150 // Total structures is bounded by U16 maximum value to fit in half of context (U32)
151 3 FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >=
152 this->m_bufferBins.bins[bin].numBuffers);
153 3 this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers);
154 }
155 }
156
157 2 FwSizeType allocatedSize = memorySize;
158 2 bool recoverable = false; //!< don't care if it is recoverable since they are a pool of user buffers
159
160 // allocate memory
161
1/1
✓ Branch 1 taken 2 times.
2 void* memory = allocator.allocate(memId, allocatedSize, recoverable);
162 // make sure the memory returns was non-zero and the size requested
163 2 FW_ASSERT(memory != nullptr && memorySize == allocatedSize, static_cast<FwAssertArgType>(mgrId),
164 static_cast<FwAssertArgType>(memId),
165 static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(memory)),
166 static_cast<FwAssertArgType>(memorySize), static_cast<FwAssertArgType>(allocatedSize));
167 // structs will be at beginning of memory
168 2 this->m_buffers = static_cast<AllocatedBuffer*>(memory);
169 // memory buffers will be at end of structs in memory, so compute that memory as the beginning of the
170 // struct past the number of structs
171 2 U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
172
173 // walk through entries and initialize them
174 2 U16 currStruct = 0;
175
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
22 for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
176
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17 times.
20 if (this->m_bufferBins.bins[bin].numBuffers) {
177
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++) {
178 // placement new for Fw::Buffer instance. We don't need the new() return value,
179 // because we know where the Fw::Buffer instance is
180 60 U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct);
181 (void)new (&this->m_buffers[currStruct].buff)
182
1/1
✓ Branch 2 taken 60 times.
60 Fw::Buffer(bufferMem, this->m_bufferBins.bins[bin].bufferSize, context);
183 60 this->m_buffers[currStruct].allocated = false;
184 60 this->m_buffers[currStruct].memory = bufferMem;
185 60 this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
186 60 bufferMem += this->m_bufferBins.bins[bin].bufferSize;
187 60 currStruct++;
188 }
189 }
190 }
191
192 // check that the initiation pointer made it to the end of allocated space
193 2 U8* const CURR_PTR = bufferMem;
194 2 U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
195 2 FW_ASSERT(CURR_PTR == END_PTR, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(memId),
196 static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(CURR_PTR)),
197 static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(END_PTR)));
198 // secondary init verification
199 2 FW_ASSERT(currStruct == this->m_numStructs, static_cast<FwAssertArgType>(mgrId),
200 static_cast<FwAssertArgType>(memId), static_cast<FwAssertArgType>(currStruct),
201 static_cast<FwAssertArgType>(this->m_numStructs));
202 // indicate setup is done
203 2 this->m_setup = true;
204 2 }
205
206 118 void BufferManagerComponentImpl ::schedIn_handler(const FwIndexType portNum, U32 context) {
207 // write telemetry values
208
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 this->tlmWrite_HiBuffs(this->m_highWater);
209
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 this->tlmWrite_CurrBuffs(this->m_currBuffs);
210
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 this->tlmWrite_TotalBuffs(this->m_numStructs);
211
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 this->tlmWrite_NoBuffs(this->m_noBuffs);
212
2/2
✓ Branch 1 taken 118 times.
✓ Branch 4 taken 118 times.
118 this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
213 118 }
214
215 } // end namespace Svc
216