LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2FreeList.h
1 /*
2 * Copyright (c) 2014 Google, Inc.
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
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 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 #ifndef B2_FREE_LIST_H
19 #define B2_FREE_LIST_H
20 
21 #include <Box2D/Common/b2IntrusiveList.h>
23 
26 #ifndef B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
27 #define B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE 0
28 #endif // B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
29 
30 
34 {
35 public:
37  b2FreeList() { }
38 
41 
44 
46  void Free(b2IntrusiveListNode* node);
47 
51 
53  void RemoveAll();
54 
57  return m_allocated;
58  }
59 
62  return m_free;
63  }
64 
65 protected:
70 };
71 
72 
75 template<typename T>
77 public:
80 
83 
85  T* Allocate() {
86  b2IntrusiveListNode* const node = m_freeList.Allocate();
87  if (!node) return NULL;
88  return T::GetInstanceFromListNode(node);
89  }
90 
92  void Free(T* instance) {
93  b2Assert(instance);
94  m_freeList.Free(instance->GetListNode());
95  }
96 
99  void AddToFreeList(T* instance)
100  {
101  b2Assert(instance);
102  m_freeList.AddToFreeList(instance->GetListNode());
103  }
104 
105  // Get the underlying b2FreeList.
106  b2FreeList* GetFreeList() { return &m_freeList; }
107  const b2FreeList* GetFreeList() const { return &m_freeList; }
108 
109 protected:
110  b2FreeList m_freeList;
111 };
112 
113 #endif // B2_FREE_LIST_H
~b2FreeList()
Destroy the free list.
Definition: b2FreeList.h:40
b2FreeList()
Construct the free list.
Definition: b2FreeList.h:37
void RemoveAll()
Remove all items (allocated and free) from the freelist.
Definition: b2FreeList.cpp:49
Definition: b2IntrusiveList.h:64
T * Allocate()
Allocate an item from the free list.
Definition: b2FreeList.h:85
Definition: b2FreeList.h:76
b2IntrusiveListNode * Allocate()
Allocate an item from the freelist.
Definition: b2FreeList.cpp:23
b2TypedFreeList()
Construct the free list.
Definition: b2FreeList.h:79
void AddToFreeList(T *instance)
Definition: b2FreeList.h:99
const b2IntrusiveListNode & GetFreeList() const
Get the list which tracks free items.
Definition: b2FreeList.h:61
Definition: b2FreeList.h:33
void Free(b2IntrusiveListNode *node)
Free an item from the freelist.
Definition: b2FreeList.cpp:32
void AddToFreeList(b2IntrusiveListNode *node)
Definition: b2FreeList.cpp:42
~b2TypedFreeList()
Destroy the free list.
Definition: b2FreeList.h:82
void Free(T *instance)
Free an item.
Definition: b2FreeList.h:92
const b2IntrusiveListNode & GetAllocatedList() const
Get the list which tracks allocated items.
Definition: b2FreeList.h:56
b2IntrusiveListNode m_free
List of free items.
Definition: b2FreeList.h:69
b2IntrusiveListNode m_allocated
List of allocated items.
Definition: b2FreeList.h:67