MemoryPool.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef CPLUSPLUS_MEMORYPOOL_H
  21. #define CPLUSPLUS_MEMORYPOOL_H
  22. #include "CPlusPlusForwardDeclarations.h"
  23. #include <new>
  24. namespace CPlusPlus {
  25. class MemoryPool;
  26. class RecursiveMemoryPool;
  27. class CPLUSPLUS_EXPORT MemoryPool
  28. {
  29. MemoryPool(const MemoryPool &other);
  30. void operator =(const MemoryPool &other);
  31. public:
  32. MemoryPool();
  33. ~MemoryPool();
  34. void reset();
  35. inline void *allocate(size_t size)
  36. {
  37. size = (size + 7) & ~7;
  38. if (_ptr && (_ptr + size < _end)) {
  39. void *addr = _ptr;
  40. _ptr += size;
  41. return addr;
  42. }
  43. return allocate_helper(size);
  44. }
  45. private:
  46. void *allocate_helper(size_t size);
  47. private:
  48. char **_blocks;
  49. int _allocatedBlocks;
  50. int _blockCount;
  51. char *_ptr;
  52. char *_end;
  53. enum
  54. {
  55. BLOCK_SIZE = 8 * 1024,
  56. DEFAULT_BLOCK_COUNT = 8
  57. };
  58. friend class RecursiveMemoryPool;
  59. };
  60. class CPLUSPLUS_EXPORT RecursiveMemoryPool
  61. {
  62. MemoryPool *_pool;
  63. int _blockCount;
  64. char *_ptr;
  65. char *_end;
  66. public:
  67. RecursiveMemoryPool(MemoryPool *pool);
  68. ~RecursiveMemoryPool();
  69. };
  70. class CPLUSPLUS_EXPORT Managed
  71. {
  72. Managed(const Managed &other);
  73. void operator = (const Managed &other);
  74. public:
  75. Managed();
  76. virtual ~Managed();
  77. void *operator new(size_t size, MemoryPool *pool);
  78. void operator delete(void *);
  79. void operator delete(void *, MemoryPool *);
  80. };
  81. } // namespace CPlusPlus
  82. #endif // CPLUSPLUS_MEMORYPOOL_H