b2FreeList.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <Box2D/Common/b2FreeList.h>
  19. #include <Box2D/Common/b2IntrusiveList.h>
  20. #include <Box2D/Common/b2Settings.h>
  21. /// Allocate an item from the freelist.
  22. b2IntrusiveListNode* b2FreeList::Allocate()
  23. {
  24. if (m_free.IsEmpty()) return NULL;
  25. b2IntrusiveListNode * const node = m_free.GetNext();
  26. node->Remove();
  27. m_allocated.InsertBefore(node);
  28. return node;
  29. }
  30. void b2FreeList::Free(b2IntrusiveListNode* node)
  31. {
  32. b2Assert(node);
  33. #if B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
  34. b2Assert(m_allocated.FindNodeInList(node));
  35. #endif // B2_FREE_LIST_CHECK_ALLOCATED_ON_FREE
  36. node->Remove();
  37. m_free.InsertAfter(node);
  38. }
  39. void b2FreeList::AddToFreeList(b2IntrusiveListNode* node)
  40. {
  41. b2Assert(node);
  42. b2Assert(!node->InList());
  43. m_free.InsertBefore(node);
  44. }
  45. void b2FreeList::RemoveAll()
  46. {
  47. while (!m_allocated.IsEmpty()) {
  48. m_allocated.GetNext()->Remove();
  49. }
  50. while (!m_free.IsEmpty()) {
  51. m_free.GetNext()->Remove();
  52. }
  53. }