NvBlastFixedQueue.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
27 
28 
29 #ifndef NVBLASTFIXEDQUEUE_H
30 #define NVBLASTFIXEDQUEUE_H
31 
32 #include "NvBlastAssert.h"
33 #include "NvBlastMemory.h"
34 
35 namespace Nv
36 {
37 namespace Blast
38 {
39 
54 template <class T>
56 {
57 public:
58  explicit FixedQueue(uint32_t maxEntries) : m_num(0), m_head(0), m_tail(0), m_maxEntries(maxEntries)
59  {
60  }
61 
62  static size_t requiredMemorySize(uint32_t capacity)
63  {
64  return align16(sizeof(FixedQueue<T>)) + align16(capacity * sizeof(T));
65  }
66 
68  {
69  NVBLAST_ASSERT(m_num>0);
70 
71  m_num--;
72  T& element = data()[m_tail];
73  m_tail = (m_tail+1) % (m_maxEntries);
74  return element;
75  }
76 
77  T front()
78  {
79  NVBLAST_ASSERT(m_num>0);
80 
81  return data()[m_tail];
82  }
83 
84  T popBack()
85  {
86  NVBLAST_ASSERT(m_num>0);
87 
88  m_num--;
89  m_head = (m_head-1) % (m_maxEntries);
90  return data()[m_head];
91  }
92 
93  T back()
94  {
95  NVBLAST_ASSERT(m_num>0);
96 
97  uint32_t headAccess = (m_head-1) % (m_maxEntries);
98  return data()[headAccess];
99  }
100 
101  bool pushBack(const T& element)
102  {
103  if (m_num == m_maxEntries) return false;
104  data()[m_head] = element;
105 
106  m_num++;
107  m_head = (m_head+1) % (m_maxEntries);
108 
109  return true;
110  }
111 
112  bool empty() const
113  {
114  return m_num == 0;
115  }
116 
117  uint32_t size() const
118  {
119  return m_num;
120  }
121 
122 
123 private:
124  uint32_t m_num;
125  uint32_t m_head;
126  uint32_t m_tail;
127  uint32_t m_maxEntries;
128 
129  T* data()
130  {
131  return (T*)((char*)this + sizeof(FixedQueue<T>));
132  }
133 
134 private:
135  FixedQueue(const FixedQueue& that);
136 };
137 
138 } // namespace Blast
139 } // namespace Nv
140 
141 #endif // ifndef NVBLASTFIXEDQUEUE_H
bool empty() const
Definition: NvBlastFixedQueue.h:112
FixedQueue(uint32_t maxEntries)
Definition: NvBlastFixedQueue.h:58
bool pushBack(const T &element)
Definition: NvBlastFixedQueue.h:101
#define NVBLAST_ASSERT(exp)
Definition: NvBlastAssert.h:37
uint32_t size() const
Definition: NvBlastFixedQueue.h:117
T back()
Definition: NvBlastFixedQueue.h:93
Definition: NvBlastFixedQueue.h:55
T front()
Definition: NvBlastFixedQueue.h:77
T popFront()
Definition: NvBlastFixedQueue.h:67
NV_INLINE T align16(T value)
Definition: NvBlastMemory.h:46
T popBack()
Definition: NvBlastFixedQueue.h:84
Definition: NvBlastArray.h:37
static size_t requiredMemorySize(uint32_t capacity)
Definition: NvBlastFixedQueue.h:62