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