F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BufferManagerComponentImpl.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title BufferManagerComponentImpl.hpp
3 // \author tcanham
4 // \brief hpp 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 #ifndef BufferManager_HPP
14 #define BufferManager_HPP
15 
16 #include "Svc/BufferManager/BufferManagerComponentAc.hpp"
19 
20 namespace Svc
21 {
22 
23  // To use the class, instantiate an instance of the BufferBins struct below. This
24  // table specifies N buffers of M size per bin. Up to MAX_NUM_BINS bins can be specified.
25  // The table is copied when setup() is called, so it does not need to be retained after
26  // the call.
27  //
28  // The rules for specifying bins:
29  // 1. For each bin (BufferBins.bins[n]), specify the size of the buffers (bufferSize) in the
30  // bin and how many buffers for that bin (numBuffers).
31  // 2. The bins should be ordered based on an increasing bufferSize to allow BufferManager to
32  // search for available buffers. When receiving a request for a buffer, the component will
33  // search for the first buffer from the bins that is equal to or greater
34  // than the requested size, starting at the beginning of the table.
35  // 3. Any unused bins should have numBuffers set to 0.
36  // 4. A single bin can be specified if a single size is needed.
37  //
38  // If a buffer is requested that can't be found among available buffers, the call will
39  // return an Fw::Buffer with a size of zero. It is expected that the user will notice
40  // and have the appropriate response for the design. If an empty buffer is returned to
41  // the BufferManager instance, a warning event will be issued but no other action will
42  // be taken.
43  //
44  // Buffer manager will assert under the following conditions:
45  // 1. A returned buffer has the incorrect manager ID.
46  // 2. A returned buffer has an incorrect buffer ID.
47  // 3. A returned buffer is returned with a correct buffer ID, but it isn't already allocated.
48  // 4. A returned buffer has an indicated size larger than originally allocated.
49  // 5. A returned buffer has a pointer different than the one originally allocated.
50  //
51  // Note that a pointer to the Fw::MemAllocator used in setup() is stored for later memory cleanup.
52  // The instance of the allocator must persist beyond calling the cleanup() function or the
53  // destructor of BufferManager if cleanup() is not called. If a project-specific manual memory
54  // allocator is not needed, Fw::MallocAllocator can be used.
55 
56  class BufferManagerComponentImpl : public BufferManagerComponentBase
57  {
58 
59  public:
60 
61  // ----------------------------------------------------------------------
62  // Construction, initialization, and destruction
63  // ----------------------------------------------------------------------
64 
68  const char *const compName
69  );
70 
73  void init(
74  const NATIVE_INT_TYPE instance = 0
75  );
76 
77  // Defines a buffer bin
78  struct BufferBin
79  {
82  };
83 
84  // Set of bins for the BufferManager
85  struct BufferBins
86  {
88  };
89 
91 
92  void setup(
93  NATIVE_UINT_TYPE mgrID,
94  NATIVE_UINT_TYPE memID,
95  Fw::MemAllocator &allocator,
96  const BufferBins &bins
98  );
99 
100  void cleanup(void); // Free memory prior to end of program if desired. Otherwise,
101  // will be deleted in destructor
102 
106 
107  PRIVATE :
108 
109  // ----------------------------------------------------------------------
110  // Handler implementations for user-defined typed input ports
111  // ----------------------------------------------------------------------
112 
115  void
116  bufferSendIn_handler(
117  const NATIVE_INT_TYPE portNum,
118  Fw::Buffer &fwBuffer);
119 
122  Fw::Buffer bufferGetCallee_handler(
123  const NATIVE_INT_TYPE portNum,
124  U32 size);
125 
128  void schedIn_handler(
129  const NATIVE_INT_TYPE portNum,
130  NATIVE_UINT_TYPE context
131  );
132 
133 
134  bool m_setup;
135  bool m_cleaned;
136  NATIVE_UINT_TYPE m_mgrId;
137 
138  BufferBins m_bufferBins;
139 
140  struct AllocatedBuffer
141  {
142  Fw::Buffer buff;
143  U8 *memory;
144  U32 size;
145  bool allocated;
146  };
147 
148  AllocatedBuffer *m_buffers;
149  Fw::MemAllocator *m_allocator;
150  NATIVE_UINT_TYPE m_memId;
151  NATIVE_UINT_TYPE m_numStructs;
152 
153  // stats
154  U32 m_highWater;
155  U32 m_currBuffs;
156  U32 m_noBuffs;
157  U32 m_emptyBuffs;
158  };
159 
160 } // end namespace Svc
161 
162 #endif
Svc::BufferManagerComponentImpl::setup
void setup(NATIVE_UINT_TYPE mgrID, NATIVE_UINT_TYPE memID, Fw::MemAllocator &allocator, const BufferBins &bins)
set up configuration
Definition: BufferManagerComponentImpl.cpp:146
Svc::BufferManagerComponentImpl
Definition: BufferManagerComponentImpl.hpp:56
MemAllocator.hpp
Defines a base class for a memory allocator for classes.
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Svc::BufferManagerComponentImpl::BufferManagerComponentImpl
BufferManagerComponentImpl(const char *const compName)
Definition: BufferManagerComponentImpl.cpp:27
Svc::BufferManagerComponentImpl::BufferBin::numBuffers
NATIVE_UINT_TYPE numBuffers
number of buffers in this bin. Set to zero for unused bins.
Definition: BufferManagerComponentImpl.hpp:81
Svc::BufferManagerComponentImpl::BufferBin
Definition: BufferManagerComponentImpl.hpp:78
Fw::Buffer
Definition: Buffer.hpp:43
Svc::BUFFERMGR_MAX_NUM_BINS
static const NATIVE_UINT_TYPE BUFFERMGR_MAX_NUM_BINS
Definition: BufferManagerComponentImplCfg.hpp:7
Svc::BufferManagerComponentImpl::BufferBins
Definition: BufferManagerComponentImpl.hpp:85
BufferManagerComponentImplCfg.hpp
Svc::BufferManagerComponentImpl::BufferBins::bins
BufferBin bins[BUFFERMGR_MAX_NUM_BINS]
set of bins to define buffers
Definition: BufferManagerComponentImpl.hpp:87
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Svc::BufferManagerComponentImpl::BufferBin::bufferSize
NATIVE_UINT_TYPE bufferSize
size of the buffers in this bin. Set to zero for unused bins.
Definition: BufferManagerComponentImpl.hpp:80
Svc::BufferManagerComponentImpl::cleanup
void cleanup(void)
Definition: BufferManagerComponentImpl.cpp:60
Svc::BufferManagerComponentImpl::init
void init(const NATIVE_INT_TYPE instance=0)
Definition: BufferManagerComponentImpl.cpp:46
Fw::MemAllocator
Definition: MemAllocator.hpp:44
Svc::BufferManagerComponentImpl::~BufferManagerComponentImpl
~BufferManagerComponentImpl(void)
Definition: BufferManagerComponentImpl.cpp:54
Svc
Definition: ActiveRateGroupImplCfg.hpp:18
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29