b2FreeList.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <Box2D/Common/b2IntrusiveList.h>
  21. #include <Box2D/Common/b2Settings.h>
  22. /// When B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE is 1, b2FreeList::Free() will
  23. /// check that the deallocated node was allocated from the freelist.
  24. #ifndef B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
  25. #define B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE 0
  26. #endif // B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
  27. /// Fast - O(1) - list based allocator for items that can be inserted into
  28. /// b2IntrusiveListNode lists.
  29. class b2FreeList
  30. {
  31. public:
  32. /// Construct the free list.
  33. b2FreeList() { }
  34. /// Destroy the free list.
  35. ~b2FreeList() { }
  36. /// Allocate an item from the freelist.
  37. b2IntrusiveListNode* Allocate();
  38. /// Free an item from the freelist.
  39. void Free(b2IntrusiveListNode* node);
  40. /// Add an item to the freelist so that it can be allocated using
  41. /// b2FreeList::Allocate().
  42. void AddToFreeList(b2IntrusiveListNode* node);
  43. /// Remove all items (allocated and free) from the freelist.
  44. void RemoveAll();
  45. /// Get the list which tracks allocated items.
  46. const b2IntrusiveListNode& GetAllocatedList() const {
  47. return m_allocated;
  48. }
  49. /// Get the list which tracks free items.
  50. const b2IntrusiveListNode& GetFreeList() const {
  51. return m_free;
  52. }
  53. protected:
  54. /// List of allocated items.
  55. b2IntrusiveListNode m_allocated;
  56. /// List of free items.
  57. b2IntrusiveListNode m_free;
  58. };
  59. /// Typed b2FreeList which manages items of type T assuming T implements
  60. /// the GetInstanceFromListNode() and GetListNode() methods.
  61. template<typename T>
  62. class b2TypedFreeList {
  63. public:
  64. /// Construct the free list.
  65. b2TypedFreeList() { }
  66. /// Destroy the free list.
  67. ~b2TypedFreeList() { }
  68. /// Allocate an item from the free list.
  69. T* Allocate() {
  70. b2IntrusiveListNode* const node = m_freeList.Allocate();
  71. if (!node) return NULL;
  72. return T::GetInstanceFromListNode(node);
  73. }
  74. /// Free an item.
  75. void Free(T* instance) {
  76. b2Assert(instance);
  77. m_freeList.Free(instance->GetListNode());
  78. }
  79. /// Add an item to the freelist so that it can be allocated with
  80. /// b2TypedFreeList::Allocate().
  81. void AddToFreeList(T* instance)
  82. {
  83. b2Assert(instance);
  84. m_freeList.AddToFreeList(instance->GetListNode());
  85. }
  86. // Get the underlying b2FreeList.
  87. b2FreeList* GetFreeList() { return &m_freeList; }
  88. const b2FreeList* GetFreeList() const { return &m_freeList; }
  89. protected:
  90. b2FreeList m_freeList;
  91. };
  92. #endif // B2_FREE_LIST_H