MemoryPool.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2008 Roberto Raggi <[email protected]>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #include "MemoryPool.h"
  21. #include "cppassert.h"
  22. #include <cstring>
  23. using namespace CPlusPlus;
  24. MemoryPool::MemoryPool()
  25. : _blocks(0),
  26. _allocatedBlocks(0),
  27. _blockCount(-1),
  28. _ptr(0),
  29. _end(0)
  30. { }
  31. MemoryPool::~MemoryPool()
  32. {
  33. if (_blocks) {
  34. for (int i = 0; i < _allocatedBlocks; ++i) {
  35. if (char *b = _blocks[i])
  36. std::free(b);
  37. }
  38. std::free(_blocks);
  39. }
  40. }
  41. void MemoryPool::reset()
  42. {
  43. _blockCount = -1;
  44. _ptr = _end = 0;
  45. }
  46. void *MemoryPool::allocate_helper(size_t size)
  47. {
  48. CPP_CHECK(size < BLOCK_SIZE);
  49. if (++_blockCount == _allocatedBlocks) {
  50. if (! _allocatedBlocks)
  51. _allocatedBlocks = DEFAULT_BLOCK_COUNT;
  52. else
  53. _allocatedBlocks *= 2;
  54. _blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks);
  55. for (int index = _blockCount; index < _allocatedBlocks; ++index)
  56. _blocks[index] = 0;
  57. }
  58. char *&block = _blocks[_blockCount];
  59. if (! block)
  60. block = (char *) std::malloc(BLOCK_SIZE);
  61. _ptr = block;
  62. _end = _ptr + BLOCK_SIZE;
  63. void *addr = _ptr;
  64. _ptr += size;
  65. return addr;
  66. }
  67. RecursiveMemoryPool::RecursiveMemoryPool(MemoryPool *pool)
  68. : _pool(pool),
  69. _blockCount(pool->_blockCount),
  70. _ptr(pool->_ptr),
  71. _end(pool->_end)
  72. {
  73. }
  74. RecursiveMemoryPool::~RecursiveMemoryPool()
  75. {
  76. _pool->_blockCount = _blockCount;
  77. _pool->_ptr = _ptr;
  78. _pool->_end = _end;
  79. }
  80. Managed::Managed()
  81. { }
  82. Managed::~Managed()
  83. { }
  84. void *Managed::operator new(size_t size, MemoryPool *pool)
  85. { return pool->allocate(size); }
  86. void Managed::operator delete(void *)
  87. { }
  88. void Managed::operator delete(void *, MemoryPool *)
  89. { }