My Project: dynamics.mod/NewtonDynamics/source/core/dgFastQueue.h Source File

My Project

dgFastQueue.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 /****************************************************************************
23 *
24 * Visual C++ 6.0 created by: Julio Jerez
25 *
26 ****************************************************************************/
27 #ifndef __dgFastQueue__
28 #define __dgFastQueue__
29
30 #include "dgStdafx.h"
31
32 template<class T, dgInt32 sizeInPowerOfTwo>
33 class dgFastQueue
34 {
35 public:
36 DG_CLASS_ALLOCATOR_NEW(allocator)
37
38 dgFastQueue (dgMemoryAllocator* const allocator);
39 ~dgFastQueue ();
40
41 bool IsEmpty() const;
42 bool IsFull() const;
43 const T& GetHead() const;
44 void Pop();
45 void Push(T& object);
46
47 private:
48 T* m_pool;
49 dgMemoryAllocator* m_allocator;
50 dgInt32 m_head;
51 dgInt32 m_tail;
52 };
53
54
55
56 template<class T, dgInt32 sizeInPowerOfTwo>
57 dgFastQueue<T, sizeInPowerOfTwo>::dgFastQueue (dgMemoryAllocator* const allocator)
58 :m_allocator(allocator)
59 ,m_head(0)
60 ,m_tail(0)
61 {
62 dgAssert (((sizeInPowerOfTwo -1) & (-sizeInPowerOfTwo)) == 0);
63 m_pool = (T*) m_allocator->MallocLow(sizeInPowerOfTwo * sizeof (T));
64 }
65
66
67 template<class T, dgInt32 sizeInPowerOfTwo>
68 dgFastQueue<T, sizeInPowerOfTwo>::~dgFastQueue ()
69 {
70 m_allocator->FreeLow(m_pool);
71 }
72
73 template<class T, dgInt32 sizeInPowerOfTwo>
74 bool dgFastQueue<T, sizeInPowerOfTwo>::IsEmpty() const
75 {
76 return (m_head == m_tail);
77 }
78
79 template<class T, dgInt32 sizeInPowerOfTwo>
80 bool dgFastQueue<T, sizeInPowerOfTwo>::IsFull() const
81 {
82 return (((m_tail + 1) & (sizeInPowerOfTwo - 1)) == m_head);
83 }
84
85 template<class T, dgInt32 sizeInPowerOfTwo>
86 const T& dgFastQueue<T, sizeInPowerOfTwo>::GetHead() const
87 {
88 dgAssert (!IsEmpty());
89 return m_pool[m_head];
90 }
91
92 template<class T, dgInt32 sizeInPowerOfTwo>
93 void dgFastQueue<T, sizeInPowerOfTwo>::Pop()
94 {
95 dgAssert (!IsEmpty());
96 m_head = (m_head + 1) & (sizeInPowerOfTwo - 1);
97 }
98
99 template<class T, dgInt32 sizeInPowerOfTwo>
100 void dgFastQueue<T, sizeInPowerOfTwo>::Push(T& object)
101 {
102 dgAssert (!IsFull());
103 m_pool[m_tail] = object;
104 m_tail = (m_tail + 1) & (sizeInPowerOfTwo - 1);
105 }
106
107
108
109 #endif
110
111
112
113
DG_CLASS_ALLOCATOR_NEW
#define DG_CLASS_ALLOCATOR_NEW(allocator)
Definition: dgMemory.h:50
dgFastQueue::Push
void Push(T &object)
Definition: dgFastQueue.h:100
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgFastQueue::GetHead
const T & GetHead() const
Definition: dgFastQueue.h:86
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgMemoryAllocator::FreeLow
void FreeLow(void *const retPtr)
Definition: dgMemory.cpp:156
dgFastQueue::Pop
void Pop()
Definition: dgFastQueue.h:93
dgStdafx.h
dgFastQueue::~dgFastQueue
~dgFastQueue()
Definition: dgFastQueue.h:68
dgMemoryAllocator::MallocLow
void * MallocLow(dgInt32 size, dgInt32 alignment=DG_MEMORY_GRANULARITY)
Definition: dgMemory.cpp:138
dgMemoryAllocator
Definition: dgMemory.h:73
dgFastQueue::IsFull
bool IsFull() const
Definition: dgFastQueue.h:80
dgFastQueue::dgFastQueue
dgFastQueue(dgMemoryAllocator *const allocator)
Definition: dgFastQueue.h:57
dgFastQueue::IsEmpty
bool IsEmpty() const
Definition: dgFastQueue.h:74
dgFastQueue
Definition: dgFastQueue.h:33