F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
BufferManagerComponentImpl.cpp
Go to the documentation of this file.
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>
17 #include <cstring>
18 #include <new>
19 
20 namespace Svc {
21 
22 // ----------------------------------------------------------------------
23 // Construction, initialization, and destruction
24 // ----------------------------------------------------------------------
25 
27  : BufferManagerComponentBase(compName),
28  m_setup(false),
29  m_cleaned(false),
30  m_mgrId(0),
31  m_buffers(nullptr),
32  m_allocator(nullptr),
33  m_memId(0),
34  m_numStructs(0),
35  m_highWater(0),
36  m_currBuffs(0),
37  m_noBuffs(0),
38  m_emptyBuffs(0) {}
39 
41  if (m_setup) {
42  this->cleanup();
43  }
44 }
45 
47  FW_ASSERT(this->m_buffers != nullptr);
48  FW_ASSERT(this->m_allocator != nullptr);
49 
50  if (not this->m_cleaned) {
51  // walk through Fw::Buffer instances and delete them
52  for (U16 entry = 0; entry < this->m_numStructs; entry++) {
53  this->m_buffers[entry].buff.~Buffer();
54  }
55  this->m_cleaned = true;
56  // release memory
57  this->m_allocator->deallocate(this->m_memId, this->m_buffers);
58  this->m_setup = false;
59  }
60 }
61 
62 // ----------------------------------------------------------------------
63 // Handler implementations for user-defined typed input ports
64 // ----------------------------------------------------------------------
65 
66 void BufferManagerComponentImpl ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
67  // make sure component has been set up
68  FW_ASSERT(this->m_setup);
69  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  if (fwBuffer.getData() == nullptr && fwBuffer.getSize() == 0) {
76  this->m_emptyBuffs++;
77  return;
78  }
79  // use the bufferID member field to find the original slot
80  U32 context = fwBuffer.getContext();
81  U32 id = context & 0xFFFF;
82  U32 mgrId = context >> 16;
83  // check some things
84  FW_ASSERT(id < this->m_numStructs, static_cast<FwAssertArgType>(id),
85  static_cast<FwAssertArgType>(this->m_numStructs));
86  FW_ASSERT(mgrId == this->m_mgrId, static_cast<FwAssertArgType>(mgrId), static_cast<FwAssertArgType>(id),
87  static_cast<FwAssertArgType>(this->m_mgrId));
88  FW_ASSERT(true == this->m_buffers[id].allocated, static_cast<FwAssertArgType>(id),
89  static_cast<FwAssertArgType>(this->m_mgrId));
90  FW_ASSERT(reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory, static_cast<FwAssertArgType>(id),
91  static_cast<FwAssertArgType>(this->m_mgrId));
92  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  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  this->m_buffers[id].allocated = false;
99  this->m_currBuffs--;
100 }
101 
102 Fw::Buffer BufferManagerComponentImpl ::bufferGetCallee_handler(const FwIndexType portNum, Fw::Buffer::SizeType size) {
103  // make sure component has been set up
104  FW_ASSERT(this->m_setup);
105  FW_ASSERT(m_buffers != nullptr);
106  // find smallest buffer based on size.
107  for (U16 buff = 0; buff < this->m_numStructs; buff++) {
108  if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) {
109  this->m_buffers[buff].allocated = true;
110  this->m_currBuffs++;
111  if (this->m_currBuffs > this->m_highWater) {
112  this->m_highWater = this->m_currBuffs;
113  }
114  Fw::Buffer copy = this->m_buffers[buff].buff;
115  // change size to match request
116  copy.setSize(size);
117  return copy;
118  }
119  }
120 
121  // if no buffers found, return empty buffer
123  this->m_noBuffs++;
124  return Fw::Buffer();
125 }
126 
128  FwEnumStoreType memId,
129  Fw::MemAllocator& allocator,
130  const BufferBins& bins
131 ) {
132  this->m_mgrId = mgrId;
133  this->m_memId = memId;
134  this->m_allocator = &allocator;
135  // clear bins
136  (void)memset(&this->m_bufferBins, 0, sizeof(this->m_bufferBins));
137 
138  this->m_bufferBins = bins;
139 
140  // compute the amount of memory needed
141  FwSizeType memorySize = 0; // track needed memory
142  this->m_numStructs = 0; // size the number of tracking structs
143  // walk through bins and add up the sizes
144  for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
145  if (this->m_bufferBins.bins[bin].numBuffers) {
146  const FwSizeType bufferSize = this->m_bufferBins.bins[bin].bufferSize;
147  const FwSizeType numBuffers = static_cast<FwSizeType>(this->m_bufferBins.bins[bin].numBuffers);
148  const FwSizeType perBuffer = bufferSize + static_cast<FwSizeType>(sizeof(AllocatedBuffer));
149 
150  FW_ASSERT(perBuffer >= bufferSize); // addition didn't wrap
151  FW_ASSERT(perBuffer <= std::numeric_limits<FwSizeType>::max() / numBuffers); // multiply safe
152  const FwSizeType binTotal = perBuffer * numBuffers;
153  FW_ASSERT(memorySize <= std::numeric_limits<FwSizeType>::max() - binTotal); // accumulate safe
154  memorySize += binTotal;
155 
156  // Total structures is bounded by U16 maximum value to fit in half of context (U32)
157  FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >=
158  this->m_bufferBins.bins[bin].numBuffers);
159  this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers);
160  }
161  }
162 
163  FwSizeType allocatedSize = memorySize;
164  bool recoverable = false;
165 
166  // allocate memory
167  void* memory = allocator.allocate(memId, allocatedSize, recoverable);
168  // make sure the memory returns was non-zero and the size requested
169  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  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  U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
178 
179  // walk through entries and initialize them
180  U16 currStruct = 0;
181  for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
182  if (this->m_bufferBins.bins[bin].numBuffers) {
183  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  U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct);
187  (void)new (&this->m_buffers[currStruct].buff)
188  Fw::Buffer(bufferMem, this->m_bufferBins.bins[bin].bufferSize, context);
189  this->m_buffers[currStruct].allocated = false;
190  this->m_buffers[currStruct].memory = bufferMem;
191  this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
192  bufferMem += this->m_bufferBins.bins[bin].bufferSize;
193  currStruct++;
194  }
195  }
196  }
197 
198  // check that the initiation pointer made it to the end of allocated space
199  U8* const CURR_PTR = bufferMem;
200  U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
201  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  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  this->m_setup = true;
210 }
211 
212 void BufferManagerComponentImpl ::schedIn_handler(const FwIndexType portNum, U32 context) {
213  // write telemetry values
214  this->tlmWrite_HiBuffs(this->m_highWater);
215  this->tlmWrite_CurrBuffs(this->m_currBuffs);
216  this->tlmWrite_TotalBuffs(this->m_numStructs);
217  this->tlmWrite_NoBuffs(this->m_noBuffs);
218  this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
219 }
220 
221 } // end namespace Svc
void log_WARNING_HI_NoBuffsAvailable(FwSizeType size)
virtual void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))=0
PlatformSizeType FwSizeType
BufferBin bins[BUFFERMGR_MAX_NUM_BINS]
set of bins to define buffers
I32 FwEnumStoreType
void setSize(FwSizeType size)
Definition: Buffer.cpp:131
U32 getContext() const
Definition: Buffer.cpp:102
U8 * getData() const
Definition: Buffer.cpp:82
Fw::Buffer::SizeType bufferSize
size of the buffers in this bin. Set to zero for unused bins.
static const U16 BUFFERMGR_MAX_NUM_BINS
BufferManagerComponentImpl(const char *const compName)
void tlmWrite_NoBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CurrBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_HiBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
void tlmWrite_TotalBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void setup(U16 mgrID, FwEnumStoreType memID, Fw::MemAllocator &allocator, const BufferBins &bins)
set up configuration
FwSizeType getSize() const
Definition: Buffer.cpp:90
Memory Allocation base class.
void tlmWrite_EmptyBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
PlatformIndexType FwIndexType
FwSizeType SizeType
The size type for a buffer - for backwards compatibility.
Definition: Buffer.hpp:67
RateGroupDivider component implementation.
virtual void deallocate(const FwEnumStoreType identifier, void *ptr)=0
Auto-generated base for BufferManager component.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U16 numBuffers
number of buffers in this bin. Set to zero for unused bins.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.