b2TrackedBlock.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_TRACKED_BLOCK_H
  19. #define B2_TRACKED_BLOCK_H
  20. #include <Box2D/Common/b2IntrusiveList.h>
  21. #include <Box2D/Common/b2Settings.h>
  22. /// Alignment (in bytes) of user memory associated with b2TrackedBlock.
  23. const int32 b2_mallocAlignment = 32;
  24. /// Allocated block of memory that can be tracked in a b2IntrusiveList.
  25. class b2TrackedBlock : public b2TypedIntrusiveListNode<b2TrackedBlock>
  26. {
  27. private:
  28. // Initialize this block with a reference to "this".
  29. b2TrackedBlock();
  30. // Remove the block from the list.
  31. ~b2TrackedBlock() { }
  32. public:
  33. /// Get the allocated memory associated with this block.
  34. void* GetMemory() const;
  35. private:
  36. // Padding required to align the pointer to user memory in the block
  37. // to b2_mallocAlignment.
  38. uint8 m_padding[b2_mallocAlignment + sizeof(b2TrackedBlock**)];
  39. public:
  40. /// Allocate a b2TrackedBlock returning a pointer to memory of size
  41. /// bytes that can be used by the caller.
  42. static void* Allocate(uint32 size);
  43. /// Get a b2TrackedBlock from a pointer to memory returned by
  44. /// b2TrackedBlock::Allocate().
  45. static b2TrackedBlock* GetFromMemory(void *memory);
  46. /// Free a block of memory returned by b2TrackedBlock::Allocate()
  47. static void Free(void *memory);
  48. /// Free a b2TrackedBlock.
  49. static void Free(b2TrackedBlock *block);
  50. };
  51. /// Allocator of blocks which are tracked in a list.
  52. class b2TrackedBlockAllocator
  53. {
  54. public:
  55. /// Initialize.
  56. b2TrackedBlockAllocator() {}
  57. /// Free all allocated blocks.
  58. ~b2TrackedBlockAllocator()
  59. {
  60. FreeAll();
  61. }
  62. /// Allocate a block of size bytes using b2TrackedBlock::Allocate().
  63. void* Allocate(uint32 size);
  64. /// Free a block returned by Allocate().
  65. void Free(void *memory);
  66. /// Free all allocated blocks.
  67. void FreeAll();
  68. // Get the list of allocated blocks.
  69. const b2TypedIntrusiveListNode<b2TrackedBlock>& GetList() const
  70. {
  71. return m_blocks;
  72. }
  73. private:
  74. b2TypedIntrusiveListNode<b2TrackedBlock> m_blocks;
  75. };
  76. #endif // B2_TRACKED_BLOCK_H