F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.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 
46  init(
47  const NATIVE_INT_TYPE instance
48  )
49  {
51  }
52 
55  {
56  if (m_setup) {
57  this->cleanup();
58  }
59  }
60 
62  cleanup()
63  {
64  FW_ASSERT(this->m_buffers);
65  FW_ASSERT(this->m_allocator);
66 
67  if (not this->m_cleaned) {
68  // walk through Fw::Buffer instances and delete them
69  for (NATIVE_UINT_TYPE entry = 0; entry < this->m_numStructs; entry++) {
70  this->m_buffers[entry].buff.~Buffer();
71  }
72  this->m_cleaned = true;
73  // release memory
74  this->m_allocator->deallocate(this->m_memId,this->m_buffers);
75  this->m_setup = false;
76  }
77  }
78 
79 
80  // ----------------------------------------------------------------------
81  // Handler implementations for user-defined typed input ports
82  // ----------------------------------------------------------------------
83 
84  void BufferManagerComponentImpl ::
85  bufferSendIn_handler(
86  const NATIVE_INT_TYPE portNum,
87  Fw::Buffer &fwBuffer
88  )
89  {
90  // make sure component has been set up
91  FW_ASSERT(this->m_setup);
92  FW_ASSERT(m_buffers);
93  // check for empty buffers - this is just a warning since this component returns
94  // empty buffers if it can't allocate one.
95  if (fwBuffer.getSize() == 0) {
97  this->m_emptyBuffs++;
98  return;
99  }
100  // use the bufferID member field to find the original slot
101  U32 context = fwBuffer.getContext();
102  U32 id = context & 0xFFFF;
103  U32 mgrId = context >> 16;
104  // check some things
105  FW_ASSERT(
106  id < this->m_numStructs,
107  static_cast<FwAssertArgType>(id),
108  static_cast<FwAssertArgType>(this->m_numStructs));
109  FW_ASSERT(
110  mgrId == this->m_mgrId,
111  static_cast<FwAssertArgType>(mgrId),
112  static_cast<FwAssertArgType>(id),
113  static_cast<FwAssertArgType>(this->m_mgrId));
114  FW_ASSERT(
115  true == this->m_buffers[id].allocated,
116  static_cast<FwAssertArgType>(id),
117  static_cast<FwAssertArgType>(this->m_mgrId));
118  FW_ASSERT(
119  reinterpret_cast<U8*>(fwBuffer.getData()) >= this->m_buffers[id].memory,
120  static_cast<FwAssertArgType>(id),
121  static_cast<FwAssertArgType>(this->m_mgrId));
122  FW_ASSERT(
123  reinterpret_cast<U8*>(fwBuffer.getData()) < (this->m_buffers[id].memory + this->m_buffers[id].size),
124  static_cast<FwAssertArgType>(id),
125  static_cast<FwAssertArgType>(this->m_mgrId));
126  // user can make smaller for their own purposes, but it shouldn't be bigger
127  FW_ASSERT(
128  fwBuffer.getSize() <= this->m_buffers[id].size,
129  static_cast<FwAssertArgType>(id),
130  static_cast<FwAssertArgType>(this->m_mgrId));
131  // clear the allocated flag
132  this->m_buffers[id].allocated = false;
133  this->m_currBuffs--;
134  }
135 
136  Fw::Buffer BufferManagerComponentImpl ::
137  bufferGetCallee_handler(
138  const NATIVE_INT_TYPE portNum,
139  U32 size
140  )
141  {
142  // make sure component has been set up
143  FW_ASSERT(this->m_setup);
144  FW_ASSERT(m_buffers);
145  // find smallest buffer based on size.
146  for (NATIVE_UINT_TYPE buff = 0; buff < this->m_numStructs; buff++) {
147  if ((not this->m_buffers[buff].allocated) and (size <= this->m_buffers[buff].size)) {
148  this->m_buffers[buff].allocated = true;
149  this->m_currBuffs++;
150  if (this->m_currBuffs > this->m_highWater) {
151  this->m_highWater = this->m_currBuffs;
152  }
153  Fw::Buffer copy = this->m_buffers[buff].buff;
154  // change size to match request
155  copy.setSize(size);
156  return copy;
157  }
158  }
159 
160  // if no buffers found, return empty buffer
162  this->m_noBuffs++;
163  return Fw::Buffer();
164 
165  }
166 
168  NATIVE_UINT_TYPE mgrId,
169  NATIVE_UINT_TYPE memId,
170  Fw::MemAllocator& allocator,
171  const BufferBins& bins
172  ) {
173 
174  this->m_mgrId = mgrId;
175  this->m_memId = memId;
176  this->m_allocator = &allocator;
177  // clear bins
178  memset(&this->m_bufferBins,0,sizeof(this->m_bufferBins));
179 
180  this->m_bufferBins = bins;
181 
182  // compute the amount of memory needed
183  NATIVE_UINT_TYPE memorySize = 0; // track needed memory
184  this->m_numStructs = 0; // size the number of tracking structs
185  // walk through bins and add up the sizes
186  for (NATIVE_UINT_TYPE bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
187  if (this->m_bufferBins.bins[bin].numBuffers) {
188  memorySize +=
189  (this->m_bufferBins.bins[bin].bufferSize * this->m_bufferBins.bins[bin].numBuffers) + // allocate each set of buffer memory
190  (static_cast<NATIVE_UINT_TYPE>(sizeof(AllocatedBuffer)) * this->m_bufferBins.bins[bin].numBuffers); // allocate the structs to track the buffers
191  this->m_numStructs += this->m_bufferBins.bins[bin].numBuffers;
192  }
193  }
194 
195  NATIVE_UINT_TYPE allocatedSize = memorySize;
196  bool recoverable = false;
197 
198  // allocate memory
199  void *memory = allocator.allocate(memId,allocatedSize,recoverable);
200  // make sure the memory returns was non-zero and the size requested
201  FW_ASSERT(memory);
202  FW_ASSERT(
203  memorySize == allocatedSize,
204  static_cast<FwAssertArgType>(memorySize),
205  static_cast<FwAssertArgType>(allocatedSize));
206  // structs will be at beginning of memory
207  this->m_buffers = static_cast<AllocatedBuffer*>(memory);
208  // memory buffers will be at end of structs in memory, so compute that memory as the beginning of the
209  // struct past the number of structs
210  U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
211 
212  // walk through entries and initialize them
213  NATIVE_UINT_TYPE currStruct = 0;
214  for (NATIVE_UINT_TYPE bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
215  if (this->m_bufferBins.bins[bin].numBuffers) {
216  for (NATIVE_UINT_TYPE binEntry = 0; binEntry < this->m_bufferBins.bins[bin].numBuffers; binEntry++) {
217  // placement new for Fw::Buffer instance. We don't need the new() return value,
218  // because we know where the Fw::Buffer instance is
219  U32 context = (this->m_mgrId << 16) | currStruct;
220  (void) new(&this->m_buffers[currStruct].buff) Fw::Buffer(bufferMem,this->m_bufferBins.bins[bin].bufferSize,context);
221  this->m_buffers[currStruct].allocated = false;
222  this->m_buffers[currStruct].memory = bufferMem;
223  this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
224  bufferMem += this->m_bufferBins.bins[bin].bufferSize;
225  currStruct++;
226  }
227  }
228  }
229 
230  // check that the initiation pointer made it to the end of allocated space
231  U8* const CURR_PTR = bufferMem;
232  U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
233  FW_ASSERT(CURR_PTR == END_PTR,
234  static_cast<FwAssertArgType>(reinterpret_cast<POINTER_CAST>(CURR_PTR)),
235  static_cast<FwAssertArgType>(reinterpret_cast<POINTER_CAST>(END_PTR)));
236  // secondary init verification
237  FW_ASSERT(
238  currStruct == this->m_numStructs,
239  static_cast<FwAssertArgType>(currStruct),
240  static_cast<FwAssertArgType>(this->m_numStructs));
241  // indicate setup is done
242  this->m_setup = true;
243  }
244 
245  void BufferManagerComponentImpl ::
246  schedIn_handler(
247  const NATIVE_INT_TYPE portNum,
248  U32 context
249  )
250  {
251  // write telemetry values
252  this->tlmWrite_HiBuffs(this->m_highWater);
253  this->tlmWrite_CurrBuffs(this->m_currBuffs);
254  this->tlmWrite_TotalBuffs(this->m_numStructs);
255  this->tlmWrite_NoBuffs(this->m_noBuffs);
256  this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
257  }
258 
259 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
C++-compatible configuration header for fprime configuration.
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
void setSize(U32 size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.
virtual void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)=0
Allocate memory.
void init()
Object initializer.
Definition: ObjBase.cpp:27
Auto-generated base for BufferManager component.
void tlmWrite_HiBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_EmptyBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_NoBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_TotalBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CurrBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void setup(NATIVE_UINT_TYPE mgrID, NATIVE_UINT_TYPE memID, Fw::MemAllocator &allocator, const BufferBins &bins)
set up configuration
BufferManagerComponentImpl(const char *const compName)
static const NATIVE_UINT_TYPE BUFFERMGR_MAX_NUM_BINS
NATIVE_UINT_TYPE numBuffers
number of buffers in this bin. Set to zero for unused bins.
NATIVE_UINT_TYPE bufferSize
size of the buffers in this bin. Set to zero for unused bins.
BufferBin bins[BUFFERMGR_MAX_NUM_BINS]
set of bins to define buffers