F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
FIFOBufferQueue.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FIFOBufferQueue.hpp
3 // \author dinkel
4 // \brief An implementation of BufferQueue which uses a FIFO data
5 // structure for the queue. Priority is ignored.
6 //
7 // \copyright
8 // Copyright 2009-2015, by the California Institute of Technology.
9 // ALL RIGHTS RESERVED. United States Government Sponsorship
10 // acknowledged.
11 //
12 // ======================================================================
13 
15 #include <Fw/Types/Assert.hpp>
16 
17 #include <cstring>
18 #include <new>
19 
20 // This is a simple FIFO queue implementation which ignores priority
21 namespace Os {
22 
24  // Queue handler:
26 
27  struct FIFOQueue {
28  U8* data;
31  };
32 
34  // Class functions:
36 
37  bool BufferQueue::initialize(NATIVE_UINT_TYPE depth, NATIVE_UINT_TYPE msgSize) {
38  U8* data = new(std::nothrow) U8[depth*(sizeof(msgSize) + msgSize)];
39  if (nullptr == data) {
40  return false;
41  }
42  FIFOQueue* fifoQueue = new(std::nothrow) FIFOQueue;
43  if (nullptr == fifoQueue) {
44  return false;
45  }
46  fifoQueue->data = data;
47  fifoQueue->head = 0;
48  fifoQueue->tail = 0;
49  this->m_queue = fifoQueue;
50  return true;
51  }
52 
53  void BufferQueue::finalize() {
54  FIFOQueue* fQueue = static_cast<FIFOQueue*>(this->m_queue);
55  if (nullptr != fQueue)
56  {
57  U8* data = fQueue->data;
58  if (nullptr != data) {
59  delete [] data;
60  }
61  delete fQueue;
62  }
63  this->m_queue = nullptr;
64  }
65 
66  bool BufferQueue::enqueue(const U8* buffer, NATIVE_UINT_TYPE size, NATIVE_INT_TYPE priority) {
67  (void) priority;
68 
69  FIFOQueue* fQueue = static_cast<FIFOQueue*>(this->m_queue);
70  U8* data = fQueue->data;
71 
72  // Store the buffer to the queue:
73  NATIVE_UINT_TYPE index = getBufferIndex(fQueue->tail);
74  this->enqueueBuffer(buffer, size, data, index);
75 
76  // Increment tail of fifo:
77  ++fQueue->tail;
78  return true;
79  }
80 
81  bool BufferQueue::dequeue(U8* buffer, NATIVE_UINT_TYPE& size, NATIVE_INT_TYPE &priority) {
82  (void) priority;
83 
84  FIFOQueue* fQueue = static_cast<FIFOQueue*>(this->m_queue);
85  U8* data = fQueue->data;
86 
87  // Get the buffer from the queue:
88  NATIVE_UINT_TYPE index = getBufferIndex(fQueue->head);
89  bool ret = this->dequeueBuffer(buffer, size, data, index);
90  if(!ret) {
91  return false;
92  }
93 
94  // Increment head of fifo:
95  ++fQueue->head;
96  return true;
97  }
98 }
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
Definition: File.cpp:6
NATIVE_UINT_TYPE tail
NATIVE_UINT_TYPE head