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);
48  FW_ASSERT(this->m_allocator);
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);
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);
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  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  memorySize += (this->m_bufferBins.bins[bin].bufferSize *
147  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  FW_ASSERT((std::numeric_limits<U16>::max() - this->m_numStructs) >=
152  this->m_bufferBins.bins[bin].numBuffers);
153  this->m_numStructs = static_cast<U16>(this->m_numStructs + this->m_bufferBins.bins[bin].numBuffers);
154  }
155  }
156 
157  FwSizeType allocatedSize = memorySize;
158  bool recoverable = false;
159 
160  // allocate memory
161  void* memory = allocator.allocate(memId, allocatedSize, recoverable);
162  // make sure the memory returns was non-zero and the size requested
163  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  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  U8* bufferMem = reinterpret_cast<U8*>(&this->m_buffers[this->m_numStructs]);
172 
173  // walk through entries and initialize them
174  U16 currStruct = 0;
175  for (U16 bin = 0; bin < BUFFERMGR_MAX_NUM_BINS; bin++) {
176  if (this->m_bufferBins.bins[bin].numBuffers) {
177  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  U32 context = (static_cast<U32>(this->m_mgrId) << 16) | static_cast<U32>(currStruct);
181  (void)new (&this->m_buffers[currStruct].buff)
182  Fw::Buffer(bufferMem, this->m_bufferBins.bins[bin].bufferSize, context);
183  this->m_buffers[currStruct].allocated = false;
184  this->m_buffers[currStruct].memory = bufferMem;
185  this->m_buffers[currStruct].size = this->m_bufferBins.bins[bin].bufferSize;
186  bufferMem += this->m_bufferBins.bins[bin].bufferSize;
187  currStruct++;
188  }
189  }
190  }
191 
192  // check that the initiation pointer made it to the end of allocated space
193  U8* const CURR_PTR = bufferMem;
194  U8* const END_PTR = static_cast<U8*>(memory) + memorySize;
195  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  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  this->m_setup = true;
204 }
205 
206 void BufferManagerComponentImpl ::schedIn_handler(const FwIndexType portNum, U32 context) {
207  // write telemetry values
208  this->tlmWrite_HiBuffs(this->m_highWater);
209  this->tlmWrite_CurrBuffs(this->m_currBuffs);
210  this->tlmWrite_TotalBuffs(this->m_numStructs);
211  this->tlmWrite_NoBuffs(this->m_noBuffs);
212  this->tlmWrite_EmptyBuffs(this->m_emptyBuffs);
213 }
214 
215 } // 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:75
U32 getContext() const
Definition: Buffer.cpp:64
U8 * getData() const
Definition: Buffer.cpp:56
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:53
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:60
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:61
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.