F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
BufferManager.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title BufferManager.hpp
3 // \author bocchino
4 // \brief hpp file for BufferManager component implementation class
5 //
6 // \copyright
7 // Copyright 2015-2017, 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 
17 
18 namespace Svc {
19 
20  class BufferManager :
22  {
23 
24  PRIVATE:
25 
26  // ----------------------------------------------------------------------
27  // Warnings
28  // ----------------------------------------------------------------------
29 
30  class Warnings {
31 
32  public:
33 
35  struct Status {
36 
37  typedef enum {
39  SUCCESS,
41  STORE_SIZE_EXCEEDED,
43  TOO_MANY_BUFFERS,
44  } t;
45 
46  };
47 
48  PRIVATE:
49 
51  struct State {
52 
54  State(void);
55 
57  bool storeSizeExceeded;
58 
60  bool tooManyBuffers;
61 
62  };
63 
64  public:
65 
68  BufferManager& bufferManager
69  );
70 
71  public:
72 
74  void update(
75  const Status::t status
76  );
77 
79 
81  BufferManager& bufferManager;
82 
84  State state;
85 
86  };
87 
89 
90  // ----------------------------------------------------------------------
91  // The store
92  // ----------------------------------------------------------------------
93 
94  class Store {
95 
96  public:
97 
98  // ----------------------------------------------------------------------
99  // Construction and destruction
100  // ----------------------------------------------------------------------
101 
102  // Construct a Store
103  Store(
104  const U32 size
105  );
106 
107  // Destroy a Store
108  ~Store(void);
109 
110  public:
111 
112  // ----------------------------------------------------------------------
113  // Types
114  // ----------------------------------------------------------------------
115 
116  typedef enum { SUCCESS, FAILURE } Status;
117 
118  public:
119 
120  // ----------------------------------------------------------------------
121  // Methods
122  // ----------------------------------------------------------------------
123 
124  // Get the number of bytes currently allocated
125  U32 getAllocatedSize(void) const;
126 
127  // Allocate n bytes on the store
129  const U32 n,
130  U8* &result
131  );
132 
133  // Free bytes from the store
134  void free(
135  const U32 size,
136  U8 *const address
137  );
138 
140 
141  // ----------------------------------------------------------------------
142  // Constants
143  // ----------------------------------------------------------------------
144 
145  // The size of the store
146  const U32 totalSize;
147 
148  // Pointer to the base of the store memory
149  U8 *const memoryBase;
150 
151  // ----------------------------------------------------------------------
152  // Variables
153  // ----------------------------------------------------------------------
154 
157 
159  U32 padSize;
160 
163 
164  };
165 
167 
168  // ----------------------------------------------------------------------
169  // The allocation queue
170  // ----------------------------------------------------------------------
171 
172  class AllocationQueue {
173 
174  public:
175 
176  // ----------------------------------------------------------------------
177  // Construction and destruction
178  // ----------------------------------------------------------------------
179 
180  // Construct an AllocationQueue
181  AllocationQueue(const U32 size);
182 
183  // Destroy an AllocationQueue
185 
186  public:
187 
188  // ----------------------------------------------------------------------
189  // Types
190  // ----------------------------------------------------------------------
191 
192  // Allocation status
193  struct Allocate {
194 
195  typedef enum {
196  SUCCESS, // Allocation OK
197  FULL // No more room
199 
200  };
201 
202  // Free status
203  struct Free {
204 
205  typedef enum {
206  SUCCESS, // Free OK
207  EMPTY, // Nothing to free
208  ID_MISMATCH // ID supplied was not at head of queue
210 
211  };
212 
213  // An entry in the queue
214  typedef struct {
215  U32 id;
216  U32 size;
217  } Entry;
218 
219  public:
220 
221  // ----------------------------------------------------------------------
222  // Public methods
223  // ----------------------------------------------------------------------
224 
225  // Get the number of buffers currently allocated
226  U32 getAllocationSize(void) const;
227 
228  // Record an allocation of size 'size' and generate a new id
230  const U32 size,
231  U32& id
232  );
233 
234  // Record a deallocation
236  const U32 expectedId,
237  U32& sawId,
238  U32& size
239  );
240 
242 
243  // ----------------------------------------------------------------------
244  // Private methods
245  // ----------------------------------------------------------------------
246 
247  // Get the next ID
248  U32 getNextId(void);
249 
250  // Update a circular index
251  U32 getNextIndex(const U32 index);
252 
254 
255  // ----------------------------------------------------------------------
256  // Constants
257  // ----------------------------------------------------------------------
258 
259  // The queue size
260  const U32 totalSize;
261 
262  // Pointer to the queue data
263  Entry *const data;
264 
266 
267  // ----------------------------------------------------------------------
268  // Variables
269  // ----------------------------------------------------------------------
270 
271  // The next id
272  U32 nextId;
273 
274  // Index into the allocation end of the queue
276 
277  // Index into the free end of the queue
278  U32 freeIndex;
279 
280  // Number of elements allocated
282 
283  };
284 
285  public:
286 
287  // ----------------------------------------------------------------------
288  // Construction, initialization, and destruction
289  // ----------------------------------------------------------------------
290 
294  const char *const compName,
295  const U32 storeSize,
296  const U32 maxNumBuffers
297  );
298 
301  void init(
302  const NATIVE_INT_TYPE instance
303  );
304 
307  ~BufferManager(void);
308 
310 
311  // ----------------------------------------------------------------------
312  // Handler implementations for user-defined typed input ports
313  // ----------------------------------------------------------------------
314 
316  //
319  U32 size
320  );
321 
325  const NATIVE_INT_TYPE portNum,
326  Fw::Buffer &buffer
327  );
328 
330 
331  // ----------------------------------------------------------------------
332  // Variables
333  // ----------------------------------------------------------------------
334 
336  Warnings warnings;
337 
339  Store store;
340 
342  AllocationQueue allocationQueue;
343 
344  };
345 
346 }
347 
348 #endif
Svc::BufferManager::BufferManager
BufferManager(const char *const compName, const U32 storeSize, const U32 maxNumBuffers)
Definition: BufferManager.cpp:248
Svc::BufferManager::Allocate
Definition: BufferManager.hpp:193
Svc::BufferManager
Definition: BufferManager.hpp:22
Svc::BufferManagerComponentBase::bufferGetCallee_handler
virtual Fw::Buffer bufferGetCallee_handler(NATIVE_INT_TYPE portNum, U32 size)=0
Handler for input port bufferGetCallee.
Svc::BufferManager::allocatedSize
U32 allocatedSize
The total allocated size on the store.
Definition: BufferManager.hpp:162
PRIVATE
#define PRIVATE
overridable private for unit testing
Definition: BasicTypes.hpp:118
Svc::BufferManager::data
Entry *const data
Definition: BufferManager.hpp:263
Svc::BufferManager::Allocate::SUCCESS
@ SUCCESS
Definition: BufferManager.hpp:196
Svc::BufferManager::Allocate::Status
Status
Definition: BufferManager.hpp:195
Svc::BufferManager::Free::EMPTY
@ EMPTY
Definition: BufferManager.hpp:207
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Svc::BufferManagerComponentBase
Auto-generated base for BufferManager component.
Definition: BufferManagerComponentAc.hpp:44
Svc::BufferManager::bufferSendIn_handler
void bufferSendIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer &buffer)
Definition: BufferManager.cpp:317
Fw::Buffer
Definition: BufferSerializableAc.hpp:24
Svc::BufferManager::getAllocatedSize
U32 getAllocatedSize(void) const
Svc::BufferManager::SUCCESS
@ SUCCESS
Definition: BufferManager.hpp:116
Svc::BufferManager::freeIndex
U32 freeIndex
Pointer to the first free byte of store memory.
Definition: BufferManager.hpp:156
Svc::BufferManager::state
State state
The warning state.
Definition: BufferManager.hpp:84
BufferManagerComponentAc.hpp
Svc::BufferManager::store
Store store
The store.
Definition: BufferManager.hpp:339
Svc::BufferManager::Entry::size
U32 size
Definition: BufferManager.hpp:216
Svc::BufferManager::~BufferManager
~BufferManager(void)
Definition: BufferManager.cpp:268
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
Svc::BufferManager::free
Free::Status free(const U32 expectedId, U32 &sawId, U32 &size)
Svc::BufferManager::Free::Status
Status
Definition: BufferManager.hpp:205
Svc::BufferManager::Warnings
Warnings(BufferManager &bufferManager)
Construct a Warnings object.
Svc::BufferManager::update
void update(const Status::t status)
Update the warning state.
Svc::BufferManager::~Store
~Store(void)
Svc::BufferManager::~AllocationQueue
~AllocationQueue(void)
Svc::BufferManager::allocate
Allocate::Status allocate(const U32 size, U32 &id)
Svc::BufferManager::getNextIndex
U32 getNextIndex(const U32 index)
Svc::BufferManager::Entry::id
U32 id
Definition: BufferManager.hpp:215
Svc::BufferManager::allocationQueue
AllocationQueue allocationQueue
The allocation queue.
Definition: BufferManager.hpp:342
Svc::BufferManager::Free
Definition: BufferManager.hpp:203
Svc::BufferManager::size
PRIVATE U32 size
Definition: BufferManager.hpp:320
Svc::BufferManager::allocateIndex
U32 allocateIndex
Definition: BufferManager.hpp:275
Svc::BufferManager::allocate
Status allocate(const U32 n, U8 *&result)
Svc
Definition: ActiveLoggerComponentAc.cpp:22
Svc::BufferManager::padSize
U32 padSize
The amount of padding at the end of the store, added to allocations that otherwise would wrap around.
Definition: BufferManager.hpp:159
Svc::BufferManagerComponentBase::portNum
PRIVATE NATIVE_INT_TYPE portNum
Definition: BufferManagerComponentAc.hpp:481
Svc::BufferManager::memoryBase
U8 *const memoryBase
Definition: BufferManager.hpp:149
Svc::BufferManager::Free::SUCCESS
@ SUCCESS
Definition: BufferManager.hpp:206
Svc::BufferManager::getAllocationSize
U32 getAllocationSize(void) const
Svc::BufferManager::Entry
Definition: BufferManager.hpp:214
Svc::BufferManager::free
void free(const U32 size, U8 *const address)
Svc::BufferManager::allocationSize
U32 allocationSize
Definition: BufferManager.hpp:281
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Svc::BufferManager::FAILURE
@ FAILURE
Definition: BufferManager.hpp:116
Svc::BufferManager::Status
Status
Definition: BufferManager.hpp:116