eRPC API Reference  Rev. 1.12.0
NXP Semiconductors
erpc_static_queue.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2021 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef __embedded_rpc__static_queue__
11 #define __embedded_rpc__static_queue__
12 
13 #include <cstring>
14 #include <cstdint>
15 
22 // Classes
25 
26 namespace erpc {
32 template <class T, uint32_t elementCount>
34 {
35 public:
42  {
43  m_capacity = elementCount;
44  m_head = 0;
45  m_tail = 0;
46  (void)memset(m_storage, 0, sizeof(m_storage));
47  }
48 
54  ~StaticQueue(void) {}
63  bool add(T element)
64  {
65  if ((m_head + 1U) % m_capacity != m_tail)
66  {
67  (void)std::memcpy(m_storage[m_head], &element, sizeof(T));
68  m_head = (m_head + 1U) % m_capacity;
69  return true;
70  }
71  return false;
72  }
73 
82  bool get(T *element)
83  {
84  if (m_tail != m_head)
85  {
86  (void)std::memcpy(element, m_storage[m_tail], sizeof(T));
87  m_tail = (m_tail + 1U) % m_capacity;
88  return true;
89  }
90  return false;
91  }
92 
98  uint32_t size(void)
99  {
100  if (m_head >= m_tail)
101  {
102  return m_head - m_tail;
103  }
104  return (m_capacity - m_tail + m_head);
105  }
106 
107 protected:
108  uint64_t m_storage[elementCount]
109  [(sizeof(T) + sizeof(uint64_t) - 1U) / sizeof(uint64_t)];
111  uint32_t m_capacity;
112  uint32_t volatile m_head;
113  uint32_t volatile m_tail;
114 };
115 
116 } // namespace erpc
117 
120 #endif // defined(__embedded_rpc__static_queue__)
uint64_t m_storage[elementCount][(sizeof(T)+sizeof(uint64_t)-1U)/sizeof(uint64_t)]
Definition: erpc_static_queue.hpp:109
uint32_t volatile m_head
Definition: erpc_static_queue.hpp:112
StaticQueue(void)
Constructor of StaticQueue class.
Definition: erpc_static_queue.hpp:41
uint32_t size(void)
This function returns number of elements in queue.
Definition: erpc_static_queue.hpp:98
Definition: erpc_arbitrated_client_manager.hpp:25
bool add(T element)
This function adds element to queue.
Definition: erpc_static_queue.hpp:63
uint32_t volatile m_tail
Definition: erpc_static_queue.hpp:113
~StaticQueue(void)
Destructor of StaticQueue class.
Definition: erpc_static_queue.hpp:54
Template class which implements static queue as ring buffer.
Definition: erpc_static_queue.hpp:33
uint32_t m_capacity
Definition: erpc_static_queue.hpp:111