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 <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 
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 (NATIVE_UINT_TYPE 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 NATIVE_INT_TYPE 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 NATIVE_INT_TYPE portNum,
133  U32 size
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 (NATIVE_UINT_TYPE 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  NATIVE_UINT_TYPE mgrId,
163  NATIVE_UINT_TYPE 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  NATIVE_UINT_TYPE 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 (NATIVE_UINT_TYPE 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<NATIVE_UINT_TYPE>(sizeof(AllocatedBuffer)) * this->m_bufferBins.bins[bin].numBuffers); // allocate the structs to track the buffers
185  this->m_numStructs += this->m_bufferBins.bins[bin].numBuffers;
186  }
187  }
188 
189  NATIVE_UINT_TYPE allocatedSize = memorySize;
190  bool recoverable = false;
191 
192  // allocate memory
193  void *memory = allocator.allocate(memId,allocatedSize,recoverable);
194  // make sure the memory returns was non-zero and the size requested
195  FW_ASSERT(memory);
196  FW_ASSERT(
197  memorySize == allocatedSize,
198  static_cast<FwAssertArgType>(memorySize),
199  static_cast<FwAssertArgType>(allocatedSize));
200  // structs will be at beginning of memory
201  this->m_buffers = static_cast<AllocatedBuffer*>(memory);
202  // memory buffers will be at end of structs in memory, so compute that memory as the beginning of the
203  // struct past the number of structs
204  U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
205 
206  // walk through entries and initialize them
207  NATIVE_UINT_TYPE currStruct = 0;
208  for (NATIVE_UINT_TYPE bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
209  if (this->m_bufferBins.bins[bin].numBuffers) {
210  for (NATIVE_UINT_TYPE binEntry = 0; binEntry < this->m_bufferBins.bins[bin].numBuffers; binEntry++) {
211  // placement new for Fw::Buffer instance. We don't need the new() return value,
212  // because we know where the Fw::Buffer instance is
213  U32 context = (this->m_mgrId << 16) | currStruct;
214  (void) new(&this->m_buffers[currStruct].buff) Fw::Buffer(bufferMem,this->m_bufferBins.bins[bin].bufferSize,context);
215  this->m_buffers[currStruct].allocated = false;
216  this->m_buffers[currStruct].memory = bufferMem;
217  this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
218  bufferMem += this->m_bufferBins.bins[bin].bufferSize;
219  currStruct++;
220  }
221  }
222  }
223 
224  // check that the initiation pointer made it to the end of allocated space
225  U8* const CURR_PTR = bufferMem;
226  U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
227  FW_ASSERT(CURR_PTR == END_PTR,
228  static_cast<FwAssertArgType>(reinterpret_cast<POINTER_CAST>(CURR_PTR)),
229  static_cast<FwAssertArgType>(reinterpret_cast<POINTER_CAST>(END_PTR)));
230  // secondary init verification
231  FW_ASSERT(
232  currStruct == this->m_numStructs,
233  static_cast<FwAssertArgType>(currStruct),
234  static_cast<FwAssertArgType>(this->m_numStructs));
235  // indicate setup is done
236  this->m_setup = true;
237  }
238 
239  void BufferManagerComponentImpl ::
240  schedIn_handler(
241  const NATIVE_INT_TYPE portNum,
242  U32 context
243  )
244  {
245  // write telemetry values
246  this->tlmWrite_HiBuffs(this->m_highWater);
247  this->tlmWrite_CurrBuffs(this->m_currBuffs);
248  this->tlmWrite_TotalBuffs(this->m_numStructs);
249  this->tlmWrite_NoBuffs(this->m_noBuffs);
250  this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
251  }
252 
253 } // end namespace Svc
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
void setup(NATIVE_UINT_TYPE mgrID, NATIVE_UINT_TYPE memID, Fw::MemAllocator &allocator, const BufferBins &bins)
set up configuration
BufferBin bins[BUFFERMGR_MAX_NUM_BINS]
set of bins to define buffers
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
virtual void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)=0
Allocate memory.
U32 getContext() const
Definition: Buffer.cpp:76
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
BufferManagerComponentImpl(const char *const compName)
void setSize(U32 size)
Definition: Buffer.cpp:87
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.
void tlmWrite_NoBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CurrBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
NATIVE_UINT_TYPE numBuffers
number of buffers in this bin. Set to zero for unused bins.
void tlmWrite_HiBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
void tlmWrite_TotalBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_EmptyBuffs(U32 arg, Fw::Time _tlmTime=Fw::Time())
Auto-generated base for BufferManager component.
NATIVE_UINT_TYPE bufferSize
size of the buffers in this bin. Set to zero for unused bins.
static const NATIVE_UINT_TYPE BUFFERMGR_MAX_NUM_BINS
#define FW_ASSERT(...)
Definition: Assert.hpp:14