eRPC API Reference  Rev. 1.11.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 
21 // Classes
24 
25 namespace erpc {
31 template <class T, uint32_t elementCount>
33 {
34 public:
41  {
42  m_capacity = elementCount;
43  m_head = 0;
44  m_tail = 0;
45  (void)memset(m_storage, 0, sizeof(m_storage));
46  }
47 
53  ~StaticQueue(void) {}
62  bool add(T element)
63  {
64  if ((m_head + 1U) % m_capacity != m_tail)
65  {
66  (void)std::memcpy(m_storage[m_head], &element, sizeof(T));
67  m_head = (m_head + 1U) % m_capacity;
68  return true;
69  }
70  return false;
71  }
72 
81  bool get(T *element)
82  {
83  if (m_tail != m_head)
84  {
85  (void)std::memcpy(element, m_storage[m_tail], sizeof(T));
86  m_tail = (m_tail + 1U) % m_capacity;
87  return true;
88  }
89  return false;
90  }
91 
97  uint32_t size(void)
98  {
99  if (m_head >= m_tail)
100  {
101  return m_head - m_tail;
102  }
103  return (m_capacity - m_tail + m_head);
104  }
105 
106 protected:
107  uint64_t m_storage[elementCount]
108  [(sizeof(T) + sizeof(uint64_t) - 1U) / sizeof(uint64_t)];
110  uint32_t m_capacity;
111  uint32_t volatile m_head;
112  uint32_t volatile m_tail;
113 };
114 
115 } // namespace erpc
116 
119 #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:108
uint32_t volatile m_head
Definition: erpc_static_queue.hpp:111
StaticQueue(void)
Constructor of StaticQueue class.
Definition: erpc_static_queue.hpp:40
uint32_t size(void)
This function returns number of elements in queue.
Definition: erpc_static_queue.hpp:97
Definition: erpc_arbitrated_client_manager.hpp:25
bool add(T element)
This function adds element to queue.
Definition: erpc_static_queue.hpp:62
uint32_t volatile m_tail
Definition: erpc_static_queue.hpp:112
~StaticQueue(void)
Destructor of StaticQueue class.
Definition: erpc_static_queue.hpp:53
Template class which implements static queue as ring buffer.
Definition: erpc_static_queue.hpp:32
uint32_t m_capacity
Definition: erpc_static_queue.hpp:110