| 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 | 4 | BufferManagerComponentImpl ::BufferManagerComponentImpl(const char* const compName) | |
| 27 | : BufferManagerComponentBase(compName), | ||
| 28 | 4 | m_setup(false), | |
| 29 | 4 | m_cleaned(false), | |
| 30 | 4 | m_mgrId(0), | |
| 31 | 4 | m_buffers(nullptr), | |
| 32 | 4 | m_allocator(nullptr), | |
| 33 | 4 | m_memId(0), | |
| 34 | 4 | m_numStructs(0), | |
| 35 | 4 | m_highWater(0), | |
| 36 | 4 | m_currBuffs(0), | |
| 37 | 4 | m_noBuffs(0), | |
| 38 | 8 | m_emptyBuffs(0) {} | |
| 39 | |||
| 40 | 8 | BufferManagerComponentImpl ::~BufferManagerComponentImpl() { | |
| 41 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
|
8 | if (m_setup) { |
| 42 | ✗ | this->cleanup(); | |
| 43 | } | ||
| 44 | 8 | } | |
| 45 | |||
| 46 | 4 | void BufferManagerComponentImpl ::cleanup() { | |
| 47 | 4 | FW_ASSERT(this->m_buffers != nullptr); | |
| 48 | 4 | FW_ASSERT(this->m_allocator != nullptr); | |
| 49 | |||
| 50 |
2/4✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | if (not this->m_cleaned) { |
| 51 | // walk through Fw::Buffer instances and delete them | ||
| 52 |
2/2✓ Branch 4 taken 23 times.
✓ Branch 5 taken 4 times.
|
27 | for (U16 entry = 0; entry < this->m_numStructs; entry++) { |
| 53 | 23 | this->m_buffers[entry].buff.~Buffer(); | |
| 54 | } | ||
| 55 | 4 | this->m_cleaned = true; | |
| 56 | // release memory | ||
| 57 | 4 | this->m_allocator->deallocate(this->m_memId, this->m_buffers); | |
| 58 | 4 | this->m_setup = false; | |
| 59 | } | ||
| 60 | 4 | } | |
| 61 | |||
| 62 | // ---------------------------------------------------------------------- | ||
| 63 | // Handler implementations for user-defined typed input ports | ||
| 64 | // ---------------------------------------------------------------------- | ||
| 65 | |||
| 66 | 28 | void BufferManagerComponentImpl ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) { | |
| 67 | // make sure component has been set up | ||
| 68 | 28 | FW_ASSERT(this->m_setup); | |
| 69 | 28 | 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 |
5/6✓ Branch 4 taken 4 times.
✓ Branch 5 taken 24 times.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 24 times.
|
28 | if (fwBuffer.getData() == nullptr && fwBuffer.getSize() == 0) { |
| 75 | 4 | this->log_WARNING_HI_NullEmptyBuffer(); | |
| 76 | 4 | this->m_emptyBuffs++; | |
| 77 | 4 | return; | |
| 78 | } | ||
| 79 | // use the bufferID member field to find the original slot | ||
| 80 | 24 | U32 context = fwBuffer.getContext(); | |
| 81 | 24 | U32 id = context & 0xFFFF; | |
| 82 | 24 | U32 mgrId = context >> 16; | |
| 83 | // check some things | ||
| 84 | 24 | FW_ASSERT(id < this->m_numStructs, static_cast<FwAssertArgType>(id), | |
| 85 | static_cast<FwAssertArgType>(this->m_numStructs)); | ||
| 86 | 24 | FW_ASSERT(mgrId == this->m_mgrId, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(id), | |
| 87 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 88 | 24 | FW_ASSERT(true == this->m_buffers[id].allocated, static_cast<FwAssertArgType>(id), | |
| 89 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 90 | 24 | FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory, static_cast<FwAssertArgType>(id), | |
| 91 | static_cast<FwAssertArgType>(this->m_mgrId)); | ||
| 92 | 24 | 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 | 24 | 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 | 24 | this->m_buffers[id].allocated = false; | |
| 99 | 24 | this->m_currBuffs--; | |
| 100 | } | ||
| 101 | |||
| 102 | 28 | Fw::Buffer BufferManagerComponentImpl ::bufferGetCallee_handler(const FwIndexType portNum, Fw::Buffer::SizeType size) { | |
| 103 | // make sure component has been set up | ||
| 104 | 28 | FW_ASSERT(this->m_setup); | |
| 105 | 28 | FW_ASSERT(m_buffers != nullptr); | |
| 106 | // find smallest buffer based on size. | ||
| 107 |
2/2✓ Branch 4 taken 153 times.
✓ Branch 5 taken 4 times.
|
157 | for (U16 buff = 0; buff < this->m_numStructs; buff++) { |
| 108 |
5/6✗ Branch 6 not taken.
✓ Branch 7 taken 153 times.
✓ Branch 8 taken 64 times.
✓ Branch 9 taken 89 times.
✓ Branch 17 taken 24 times.
✓ Branch 18 taken 40 times.
|
153 | if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) { |
| 109 | 24 | this->m_buffers[buff].allocated = true; | |
| 110 | 24 | this->m_currBuffs++; | |
| 111 |
2/2✓ Branch 8 taken 14 times.
✓ Branch 9 taken 10 times.
|
24 | if (this->m_currBuffs > this->m_highWater) { |
| 112 | 14 | this->m_highWater = this->m_currBuffs; | |
| 113 | } | ||
| 114 |
1/1✓ Branch 9 taken 24 times.
|
24 | Fw::Buffer copy = this->m_buffers[buff].buff; |
| 115 | // change size to match request | ||
| 116 |
1/1✓ Branch 2 taken 24 times.
|
24 | copy.setSize(size); |
| 117 |
1/1✓ Branch 1 taken 24 times.
|
24 | return copy; |
| 118 | 24 | } | |
| 119 | } | ||
| 120 | |||
| 121 | // if no buffers found, return empty buffer | ||
| 122 | 4 | this->log_WARNING_HI_NoBuffsAvailable(size); | |
| 123 | 4 | this->m_noBuffs++; | |
| 124 | 4 | return Fw::Buffer(); | |
| 125 | } | ||
| 126 | |||
| 127 | 4 | 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 | 4 | this->m_mgrId = mgrId; | |
| 133 | 4 | this->m_memId = memId; | |
| 134 | 4 | this->m_allocator = &allocator; | |
| 135 | // clear bins | ||
| 136 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | (void)memset(&this->m_bufferBins, 0, sizeof(this->m_bufferBins)); |
| 137 | |||
| 138 | 4 | this->m_bufferBins = bins; | |
| 139 | |||
| 140 | // compute the amount of memory needed | ||
| 141 | 4 | FwSizeType memorySize = 0; // track needed memory | |
| 142 | 4 | 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 40 times.
✓ Branch 1 taken 4 times.
|
44 | for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) { |
| 145 |
2/2✓ Branch 5 taken 8 times.
✓ Branch 6 taken 32 times.
|
40 | if (this->m_bufferBins.bins[bin].numBuffers) { |
| 146 | 8 | memorySize += (this->m_bufferBins.bins[bin].bufferSize * | |
| 147 | 8 | 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 | 8 | FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >= | |
| 152 | this->m_bufferBins.bins[bin].numBuffers); | ||
| 153 | 8 | this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers); | |
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | 4 | FwSizeType allocatedSize = memorySize; | |
| 158 | 4 | 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 6 taken 4 times.
|
4 | void* memory = allocator.allocate(memId, allocatedSize, recoverable); |
| 162 | // make sure the memory returns was non-zero and the size requested | ||
| 163 | 4 | 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 | 4 | 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 | 4 | U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]); | |
| 172 | |||
| 173 | // walk through entries and initialize them | ||
| 174 | 4 | U16 currStruct = 0; | |
| 175 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 4 times.
|
44 | for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) { |
| 176 |
2/2✓ Branch 5 taken 8 times.
✓ Branch 6 taken 32 times.
|
40 | if (this->m_bufferBins.bins[bin].numBuffers) { |
| 177 |
2/2✓ Branch 5 taken 23 times.
✓ Branch 6 taken 8 times.
|
31 | 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 | 23 | U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct); | |
| 181 |
0/2✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
23 | (void)new (&this->m_buffers[currStruct].buff) |
| 182 |
1/1✓ Branch 8 taken 23 times.
|
23 | Fw::Buffer(bufferMem, this->m_bufferBins.bins[bin].bufferSize, context); |
| 183 | 23 | this->m_buffers[currStruct].allocated = false; | |
| 184 | 23 | this->m_buffers[currStruct].memory = bufferMem; | |
| 185 | 23 | this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize; | |
| 186 | 23 | bufferMem += this->m_bufferBins.bins[bin].bufferSize; | |
| 187 | 23 | currStruct++; | |
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | // check that the initiation pointer made it to the end of allocated space | ||
| 193 | 4 | U8* const CURR_PTR = bufferMem; | |
| 194 | 4 | U8* const END_PTR = static_cast<U8*>(memory) + memorySize; | |
| 195 | 4 | 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 | 4 | 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 | 4 | this->m_setup = true; | |
| 204 | 4 | } | |
| 205 | |||
| 206 | 8 | void BufferManagerComponentImpl ::schedIn_handler(const FwIndexType portNum, U32 context) { | |
| 207 | // write telemetry values | ||
| 208 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_HiBuffs(this->m_highWater); |
| 209 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_CurrBuffs(this->m_currBuffs); |
| 210 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_TotalBuffs(this->m_numStructs); |
| 211 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_NoBuffs(this->m_noBuffs); |
| 212 |
2/2✓ Branch 6 taken 8 times.
✓ Branch 13 taken 8 times.
|
8 | this->tlmWrite_EmptyBuffs(this->m_emptyBuffs); |
| 213 | 8 | } | |
| 214 | |||
| 215 | } // end namespace Svc | ||
| 216 |