eRPC API Reference  Rev. 1.7.0
NXP Semiconductors
erpc_static_queue.h
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef __embedded_rpc__static_queue__
37 #define __embedded_rpc__static_queue__
38 
39 #include <cstring>
40 
47 // Classes
50 
51 namespace erpc {
57 template <class T, uint32_t elementCount>
59 {
60 public:
67  {
68  m_capacity = elementCount;
69  m_head = 0;
70  m_tail = 0;
71  }
72 
78  ~StaticQueue(void) {}
87  bool add(T element)
88  {
89  if ((m_head + 1) % m_capacity != m_tail)
90  {
91  std::memcpy(m_storage[m_head], &element, sizeof(T));
92  m_head = (m_head + 1) % m_capacity;
93  return true;
94  }
95  return false;
96  }
97 
106  bool get(T *element)
107  {
108  if (m_tail != m_head)
109  {
110  std::memcpy(element, m_storage[m_tail], sizeof(T));
111  m_tail = (m_tail + 1) % m_capacity;
112  return true;
113  }
114  return false;
115  }
116 
122  int size(void)
123  {
124  if (m_head >= m_tail)
125  {
126  return m_head - m_tail;
127  }
128  return (m_capacity - m_tail + m_head);
129  }
130 
131 protected:
132  uint64_t m_storage[elementCount][(sizeof(T) + sizeof(uint64_t) - 1) /
133  sizeof(uint64_t)];
135  uint32_t m_capacity;
136  uint32_t volatile m_head;
137  uint32_t volatile m_tail;
138 };
139 
140 } // namespace erpc
141 
144 #endif // defined(__embedded_rpc__static_queue__)
uint32_t volatile m_head
Definition: erpc_static_queue.h:136
StaticQueue(void)
Constructor of StaticQueue class.
Definition: erpc_static_queue.h:66
int size(void)
This function returns number of elements in queue.
Definition: erpc_static_queue.h:122
uint64_t m_storage[elementCount][(sizeof(T)+sizeof(uint64_t)-1)/ sizeof(uint64_t)]
Definition: erpc_static_queue.h:133
Definition: erpc_arbitrated_client_manager.h:51
bool add(T element)
This function adds element to queue.
Definition: erpc_static_queue.h:87
uint32_t volatile m_tail
Definition: erpc_static_queue.h:137
~StaticQueue(void)
Destructor of StaticQueue class.
Definition: erpc_static_queue.h:78
Template class which implements static queue as ring buffer.
Definition: erpc_static_queue.h:58
uint32_t m_capacity
Definition: erpc_static_queue.h:135