Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // MemPool.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file describes the class memory_pool.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 08/04/1997 Original version.
  16. // 11/12/1997 Added the clear() method.
  17. // Added code to clobber freed chunks in debug builds.
  18. // 01/08/1998 SunDown changes.
  19. // 04/10/1998 Got rid of the wrapper around InterlockedExchangePointer
  20. // since it was causing some inefficient code to be generated.
  21. // 08/06/1998 Support pluggable allocators.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. #ifndef _MEMPOOL_H_
  25. #define _MEMPOOL_H_
  26. #include <guard.h>
  27. #include <nocopy.h>
  28. class crt_allocator
  29. {
  30. public:
  31. static void* allocate(size_t nbyte) throw ()
  32. {
  33. return malloc(nbyte);
  34. }
  35. static void deallocate(void* p) throw ()
  36. {
  37. free(p);
  38. }
  39. };
  40. class task_allocator
  41. {
  42. public:
  43. static void* allocate(size_t nbyte) throw ()
  44. {
  45. return CoTaskMemAlloc((ULONG)nbyte);
  46. }
  47. static void deallocate(void* p) throw ()
  48. {
  49. CoTaskMemFree(p);
  50. }
  51. };
  52. class virtual_allocator
  53. {
  54. public:
  55. static void* allocate(size_t nbyte) throw ()
  56. {
  57. return VirtualAlloc(NULL, (DWORD)nbyte, MEM_COMMIT, PAGE_READWRITE);
  58. }
  59. static void deallocate(void* p) throw ()
  60. {
  61. VirtualFree(p, 0, MEM_RELEASE);
  62. }
  63. };
  64. ///////////////////////////////////////////////////////////////////////////////
  65. //
  66. // CLASS
  67. //
  68. // memory_pool
  69. //
  70. // DESCRIPTION
  71. //
  72. // Implements a reusable pool of memory chunks of size 'Size'.
  73. //
  74. ///////////////////////////////////////////////////////////////////////////////
  75. template < size_t Size, class Allocator >
  76. class memory_pool
  77. : Guardable, NonCopyable
  78. {
  79. public:
  80. memory_pool() throw ()
  81. : pool(NULL)
  82. { }
  83. ~memory_pool() throw ()
  84. {
  85. clear();
  86. }
  87. void clear() throw ()
  88. {
  89. Lock();
  90. // Get the linked list from the pool ...
  91. void* p = pool;
  92. pool = NULL;
  93. Unlock();
  94. // ... and iterate through deleting each node.
  95. while (p)
  96. {
  97. void* next = *((void**)p);
  98. Allocator::deallocate(p);
  99. p = next;
  100. }
  101. }
  102. void* allocate() throw ()
  103. {
  104. Lock();
  105. void* p = pool;
  106. // Are there any chunks in the pool?
  107. if (p)
  108. {
  109. pool = *(void**)p;
  110. Unlock();
  111. return p;
  112. }
  113. Unlock();
  114. // The pool is empty, so call the new operator directly.
  115. return Allocator::allocate(Size);
  116. }
  117. void deallocate(void* p) throw()
  118. {
  119. if (p)
  120. {
  121. #ifdef DEBUG
  122. memset(p, 0xA3, Size);
  123. #endif
  124. Lock();
  125. *((void**)p) = pool;
  126. pool = p;
  127. Unlock();
  128. }
  129. }
  130. protected:
  131. void* pool;
  132. };
  133. #endif // _MEMPOOL_H_