b2StackAllocator.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  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/b2StackAllocator.h>
  19. #include <Box2D/Common/b2Math.h>
  20. #include <string.h>
  21. b2StackAllocator::b2StackAllocator()
  22. {
  23. m_index = 0;
  24. m_allocation = 0;
  25. m_maxAllocation = 0;
  26. m_entryCount = 0;
  27. }
  28. b2StackAllocator::~b2StackAllocator()
  29. {
  30. b2Assert(m_index == 0);
  31. b2Assert(m_entryCount == 0);
  32. }
  33. void* b2StackAllocator::Allocate(int32 size)
  34. {
  35. b2Assert(m_entryCount < b2_maxStackEntries);
  36. const int32 roundedSize = (size + ALIGN_MASK) & ~ALIGN_MASK;
  37. b2StackEntry* entry = m_entries + m_entryCount;
  38. entry->size = roundedSize;
  39. if (m_index + roundedSize > b2_stackSize)
  40. {
  41. entry->data = (char*)b2Alloc(roundedSize);
  42. entry->usedMalloc = true;
  43. }
  44. else
  45. {
  46. entry->data = m_data + m_index;
  47. entry->usedMalloc = false;
  48. m_index += roundedSize;
  49. }
  50. m_allocation += roundedSize;
  51. m_maxAllocation = b2Max(m_maxAllocation, m_allocation);
  52. ++m_entryCount;
  53. return entry->data;
  54. }
  55. void* b2StackAllocator::Reallocate(void* p, int32 size)
  56. {
  57. b2Assert(m_entryCount > 0);
  58. b2StackEntry* entry = m_entries + m_entryCount - 1;
  59. b2Assert(p == entry->data);
  60. B2_NOT_USED(p);
  61. int32 incrementSize = size - entry->size;
  62. if (incrementSize > 0)
  63. {
  64. if (entry->usedMalloc)
  65. {
  66. void* data = b2Alloc(size);
  67. memcpy(data, entry->data, entry->size);
  68. b2Free(entry->data);
  69. entry->data = (char*)data;
  70. }
  71. else if (m_index + incrementSize > b2_stackSize)
  72. {
  73. void* data = b2Alloc(size);
  74. memcpy(data, entry->data, entry->size);
  75. m_index -= entry->size;
  76. entry->data = (char*)data;
  77. entry->usedMalloc = true;
  78. }
  79. else
  80. {
  81. m_index += incrementSize;
  82. m_allocation += incrementSize;
  83. m_maxAllocation = b2Max(m_maxAllocation, m_allocation);
  84. }
  85. entry->size = size;
  86. }
  87. return entry->data;
  88. }
  89. void b2StackAllocator::Free(void* p)
  90. {
  91. b2Assert(m_entryCount > 0);
  92. b2StackEntry* entry = m_entries + m_entryCount - 1;
  93. b2Assert(p == entry->data);
  94. if (entry->usedMalloc)
  95. {
  96. b2Free(p);
  97. }
  98. else
  99. {
  100. m_index -= entry->size;
  101. }
  102. m_allocation -= entry->size;
  103. --m_entryCount;
  104. p = NULL;
  105. }
  106. int32 b2StackAllocator::GetMaxAllocation() const
  107. {
  108. return m_maxAllocation;
  109. }