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 
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include <Fw/Types/Assert.hpp>
17 #include <Fw/Buffer/Buffer.hpp>
18 #include <new>
19 
20 namespace Svc {
21 
22  // ----------------------------------------------------------------------
23  // Construction, initialization, and destruction
24  // ----------------------------------------------------------------------
25 
28  const char *const compName
29  ) : BufferManagerComponentBase(compName)
30  ,m_setup(false)
31  ,m_cleaned(false)
32  ,m_mgrId(0)
33  ,m_buffers(nullptr)
34  ,m_allocator(nullptr)
35  ,m_memId(0)
36  ,m_numStructs(0)
37  ,m_highWater(0)
38  ,m_currBuffs(0)
39  ,m_noBuffs(0)
40  ,m_emptyBuffs(0)
41  {
42 
43  }
44 
47  {
48  if (m_setup) {
49  this->cleanup();
50  }
51  }
52 
55  {
56  FW_ASSERT(this->m_buffers);
57  FW_ASSERT(this->m_allocator);
58 
59  if (not this->m_cleaned) {
60  // walk through Fw::Buffer instances and delete them
61  for (U16 entry = 0; entry < this->m_numStructs; entry++) {
62  this->m_buffers[entry].buff.~Buffer();
63  }
64  this->m_cleaned = true;
65  // release memory
66  this->m_allocator->deallocate(this->m_memId,this->m_buffers);
67  this->m_setup = false;
68  }
69  }
70 
71 
72  // ----------------------------------------------------------------------
73  // Handler implementations for user-defined typed input ports
74  // ----------------------------------------------------------------------
75 
76  void BufferManagerComponentImpl ::
77  bufferSendIn_handler(
78  const FwIndexType portNum,
79  Fw::Buffer &fwBuffer
80  )
81  {
82  // make sure component has been set up
83  FW_ASSERT(this->m_setup);
84  FW_ASSERT(m_buffers);
85  // check for null, empty buffers - this is a warning because this component returns
86  // null, empty buffers if it can't allocate one.
87  // however, empty non-null buffers could potentially be previously allocated
88  // buffers with their size reduced. the user is allowed to make buffers smaller.
89  if (fwBuffer.getData() == nullptr && fwBuffer.getSize() == 0) {
91  this->m_emptyBuffs++;
92  return;
93  }
94  // use the bufferID member field to find the original slot
95  U32 context = fwBuffer.getContext();
96  U32 id = context & 0xFFFF;
97  U32 mgrId = context >> 16;
98  // check some things
99  FW_ASSERT(
100  id < this->m_numStructs,
101  static_cast<FwAssertArgType>(id),
102  static_cast<FwAssertArgType>(this->m_numStructs));
103  FW_ASSERT(
104  mgrId == this->m_mgrId,
105  static_cast<FwAssertArgType>(mgrId),
106  static_cast<FwAssertArgType>(id),
107  static_cast<FwAssertArgType>(this->m_mgrId));
108  FW_ASSERT(
109  true == this->m_buffers[id].allocated,
110  static_cast<FwAssertArgType>(id),
111  static_cast<FwAssertArgType>(this->m_mgrId));
112  FW_ASSERT(
113  reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory,
114  static_cast<FwAssertArgType>(id),
115  static_cast<FwAssertArgType>(this->m_mgrId));
116  FW_ASSERT(
117  reinterpret_cast<U8*>(fwBuffer.getData()) < (this->m_buffers[id].memory + this->m_buffers[id].size),
118  static_cast<FwAssertArgType>(id),
119  static_cast<FwAssertArgType>(this->m_mgrId));
120  // user can make smaller for their own purposes, but it shouldn't be bigger
121  FW_ASSERT(
122  fwBuffer.getSize() <= this->m_buffers[id].size,
123  static_cast<FwAssertArgType>(id),
124  static_cast<FwAssertArgType>(this->m_mgrId));
125  // clear the allocated flag
126  this->m_buffers[id].allocated = false;
127  this->m_currBuffs--;
128  }
129 
130  Fw::Buffer BufferManagerComponentImpl ::
131  bufferGetCallee_handler(
132  const FwIndexType portNum,
134  )
135  {
136  // make sure component has been set up
137  FW_ASSERT(this->m_setup);
138  FW_ASSERT(m_buffers);
139  // find smallest buffer based on size.
140  for (U16 buff = 0; buff < this->m_numStructs; buff++) {
141  if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) {
142  this->m_buffers[buff].allocated = true;
143  this->m_currBuffs++;
144  if (this->m_currBuffs > this->m_highWater) {
145  this->m_highWater = this->m_currBuffs;
146  }
147  Fw::Buffer copy = this->m_buffers[buff].buff;
148  // change size to match request
149  copy.setSize(size);
150  return copy;
151  }
152  }
153 
154  // if no buffers found, return empty buffer
156  this->m_noBuffs++;
157  return Fw::Buffer();
158 
159  }
160 
162  U16 mgrId,
163  FwEnumStoreType memId,
164  Fw::MemAllocator& allocator,
165  const BufferBins& bins
166  ) {
167 
168  this->m_mgrId = mgrId;
169  this->m_memId = memId;
170  this->m_allocator = &allocator;
171  // clear bins
172  memset(&this->m_bufferBins,0,sizeof(this->m_bufferBins));
173 
174  this->m_bufferBins = bins;
175 
176  // compute the amount of memory needed
177  FwSizeType memorySize = 0; // track needed memory
178  this->m_numStructs = 0; // size the number of tracking structs
179  // walk through bins and add up the sizes
180  for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
181  if (this->m_bufferBins.bins[bin].numBuffers) {
182  memorySize +=
183  (this->m_bufferBins.bins[bin].bufferSize * this->m_bufferBins.bins[bin].numBuffers) + // allocate each set of buffer memory
184  (static_cast<FwSizeType>(sizeof(AllocatedBuffer)) * this->m_bufferBins.bins[bin].numBuffers); // allocate the structs to track the buffers
185  // Total structures is bounded by U16 maximum value to fit in half of context (U32)
186  FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >= this->m_bufferBins.bins[bin].numBuffers);
187  this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers);
188  }
189  }
190 
191  FwSizeType allocatedSize = memorySize;
192  bool recoverable = false;
193 
194  // allocate memory
195  void *memory = allocator.allocate(memId,allocatedSize,recoverable);
196  // make sure the memory returns was non-zero and the size requested
197  FW_ASSERT(memory != nullptr && memorySize == allocatedSize,
198  static_cast<FwAssertArgType>(mgrId),
199  static_cast<FwAssertArgType>(memId),
200  static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(memory)),
201  static_cast<FwAssertArgType>(memorySize),
202  static_cast<FwAssertArgType>(allocatedSize));
203  // structs will be at beginning of memory
204  this->m_buffers = static_cast<AllocatedBuffer*>(memory);
205  // memory buffers will be at end of structs in memory, so compute that memory as the beginning of the
206  // struct past the number of structs
207  U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
208 
209  // walk through entries and initialize them
210  U16 currStruct = 0;
211  for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
212  if (this->m_bufferBins.bins[bin].numBuffers) {
213  for (U16 binEntry = 0; binEntry < this->m_bufferBins.bins[bin].numBuffers; binEntry++) {
214  // placement new for Fw::Buffer instance. We don't need the new() return value,
215  // because we know where the Fw::Buffer instance is
216  U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct);
217  (void) new(&this->m_buffers[currStruct].buff) Fw::Buffer(bufferMem,this->m_bufferBins.bins[bin].bufferSize,context);
218  this->m_buffers[currStruct].allocated = false;
219  this->m_buffers[currStruct].memory = bufferMem;
220  this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
221  bufferMem += this->m_bufferBins.bins[bin].bufferSize;
222  currStruct++;
223  }
224  }
225  }
226 
227  // check that the initiation pointer made it to the end of allocated space
228  U8* const CURR_PTR = bufferMem;
229  U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
230  FW_ASSERT(CURR_PTR == END_PTR,
231  static_cast<FwAssertArgType>(mgrId),
232  static_cast<FwAssertArgType>(memId),
233  static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(CURR_PTR)),
234  static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(END_PTR)));
235  // secondary init verification
236  FW_ASSERT(
237  currStruct == this->m_numStructs,
238  static_cast<FwAssertArgType>(mgrId),
239  static_cast<FwAssertArgType>(memId),
240  static_cast<FwAssertArgType>(currStruct),
241  static_cast<FwAssertArgType>(this->m_numStructs));
242  // indicate setup is done
243  this->m_setup = true;
244  }
245 
246  void BufferManagerComponentImpl ::
247  schedIn_handler(
248  const FwIndexType portNum,
249  U32 context
250  )
251  {
252  // write telemetry values
253  this->tlmWrite_HiBuffs(this->m_highWater);
254  this->tlmWrite_CurrBuffs(this->m_currBuffs);
255  this->tlmWrite_TotalBuffs(this->m_numStructs);
256  this->tlmWrite_NoBuffs(this->m_noBuffs);
257  this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
258  }
259 
260 } // end namespace Svc
PlatformSizeType FwSizeType
BufferBin bins[BUFFERMGR_MAX_NUM_BINS]
set of bins to define buffers
I32 FwEnumStoreType
virtual void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable)=0
Allocate memory.
void setSize(SizeType size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
U8 * getData() const
Definition: Buffer.cpp:68
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:56
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
void tlmWrite_EmptyBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
PlatformIndexType FwIndexType
RateGroupDivider component implementation.
virtual void deallocate(const FwEnumStoreType identifier, void *ptr)=0
Deallocate memory.
Auto-generated base for BufferManager component.
SizeType getSize() const
Definition: Buffer.cpp:72
U32 SizeType
The size type for a buffer.
Definition: Buffer.hpp:47
#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.