MemoryPool.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // MemoryPool.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/MemoryPool.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: MemoryPool
  9. //
  10. // Definition of the MemoryPool class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_MemoryPool_INCLUDED
  38. #define Foundation_MemoryPool_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Mutex.h"
  41. #include <vector>
  42. #include <cstddef>
  43. namespace Poco {
  44. class Foundation_API MemoryPool
  45. /// A simple pool for fixed-size memory blocks.
  46. ///
  47. /// The main purpose of this class is to speed-up
  48. /// memory allocations, as well as to reduce memory
  49. /// fragmentation in situations where the same blocks
  50. /// are allocated all over again, such as in server
  51. /// applications.
  52. ///
  53. /// All allocated blocks are retained for future use.
  54. /// A limit on the number of blocks can be specified.
  55. /// Blocks can be preallocated.
  56. {
  57. public:
  58. MemoryPool(std::size_t blockSize, int preAlloc = 0, int maxAlloc = 0);
  59. /// Creates a MemoryPool for blocks with the given blockSize.
  60. /// The number of blocks given in preAlloc are preallocated.
  61. ~MemoryPool();
  62. void* get();
  63. /// Returns a memory block. If there are no more blocks
  64. /// in the pool, a new block will be allocated.
  65. ///
  66. /// If maxAlloc blocks are already allocated, an
  67. /// OutOfMemoryException is thrown.
  68. void release(void* ptr);
  69. /// Releases a memory block and returns it to the pool.
  70. std::size_t blockSize() const;
  71. /// Returns the block size.
  72. int allocated() const;
  73. /// Returns the number of allocated blocks.
  74. int available() const;
  75. /// Returns the number of available blocks in the pool.
  76. private:
  77. MemoryPool();
  78. MemoryPool(const MemoryPool&);
  79. MemoryPool& operator = (const MemoryPool&);
  80. enum
  81. {
  82. BLOCK_RESERVE = 128
  83. };
  84. typedef std::vector<char*> BlockVec;
  85. std::size_t _blockSize;
  86. int _maxAlloc;
  87. int _allocated;
  88. BlockVec _blocks;
  89. FastMutex _mutex;
  90. };
  91. //
  92. // inlines
  93. //
  94. inline std::size_t MemoryPool::blockSize() const
  95. {
  96. return _blockSize;
  97. }
  98. inline int MemoryPool::allocated() const
  99. {
  100. return _allocated;
  101. }
  102. inline int MemoryPool::available() const
  103. {
  104. return (int) _blocks.size();
  105. }
  106. } // namespace Poco
  107. #endif // Foundation_MemoryPool_INCLUDED