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.

199 lines
5.8 KiB

  1. /*************************************************************************/
  2. /** Copyright(c) Microsoft Corp., 1993-1999 **/
  3. /*************************************************************************/
  4. /**************************************************************************
  5. File FreeListMgr.cxx
  6. Title :Get and Put functions for the MIDL compiler
  7. History :
  8. 13-Apr-94 GregJen Created from freelist.hxx
  9. **************************************************************************/
  10. #pragma warning ( disable : 4201 ) // Unnamed struct/union
  11. #pragma warning ( disable : 4514 ) // Unreferenced inline function
  12. /*************************************************************************
  13. ** includes
  14. *************************************************************************/
  15. #include "freelist.hxx"
  16. #include <windef.h> // REVIEW: Just give up and include windows.h?
  17. #include <basetsd.h>
  18. // checked compiler has list checking
  19. #ifndef NDEBUG
  20. // turn on the below flag to get checked freelist
  21. #define LIST_CHECK
  22. #endif
  23. /*************************************************************************
  24. ** definitions
  25. *************************************************************************/
  26. // The user should see allocated memory aligned at LONG_PTR alignment.
  27. // The allocator returns us memory with this alignment so make sure the
  28. // signature preserves it.
  29. typedef LONG_PTR HeapSignature;
  30. #define USED_SIGNATURE( pMgr, pNode ) \
  31. ( ((HeapSignature) pMgr) % ((HeapSignature) pNode) )
  32. #define FREE_SIGNATURE( pMgr, pNode ) \
  33. ( ((HeapSignature) pMgr) - ((HeapSignature) pNode) )
  34. void *
  35. FreeListMgr::Get (size_t size)
  36. {
  37. void * pEntry;
  38. #ifdef LIST_CHECK
  39. HeapSignature * pSignature;
  40. #endif
  41. /* Count this call (even if it fails).
  42. */
  43. #ifndef NDEBUG
  44. GetCount++;
  45. #endif
  46. /* Make sure the "size" requested is the same as the previous
  47. * requests.
  48. */
  49. MIDL_ASSERT (size == element_size);
  50. /* Get an entry from the free-list, if the free-list is not empty */
  51. if (pHead != NULL)
  52. {
  53. pEntry = pHead;
  54. pHead = pHead->next;
  55. #ifdef LIST_CHECK
  56. // check to make sure the entry is really OK
  57. // signature is before entry pointer
  58. // signature of free nodes is ( &mgr - &node )
  59. // signature of used nodes is ( &mgr % &node )
  60. pSignature = ((HeapSignature *)pEntry)-1;
  61. MIDL_ASSERT( *pSignature == FREE_SIGNATURE( this, pEntry ) );
  62. *pSignature = USED_SIGNATURE( this, pEntry );
  63. memset( pEntry, 0xB3, size );
  64. #endif
  65. return (void *) pEntry;
  66. }
  67. /* Get it from the allocator, since the free-list is empty */
  68. else
  69. {
  70. #ifdef LIST_CHECK
  71. pSignature = (HeapSignature *)
  72. AllocateOnceNew( size + sizeof( *pSignature ) );
  73. pEntry = ( (char *) pSignature ) + sizeof( *pSignature );
  74. *pSignature = USED_SIGNATURE( this, pEntry );
  75. memset( pEntry, 0xB2, size );
  76. return pEntry;
  77. #else
  78. return AllocateOnceNew(size);
  79. #endif
  80. }
  81. } /* Get */
  82. /*********************************************************************/
  83. // This routine "releases" the given element, by putting it on
  84. // the free-list for later re-use. The given element, must be
  85. // the same size as the elements provided by the "Get" function.
  86. /*********************************************************************/
  87. void
  88. FreeListMgr::Put (void * pEntry)
  89. {
  90. #ifdef LIST_CHECK
  91. HeapSignature * pSignature;
  92. #endif
  93. // Count this call.
  94. #ifndef NDEBUG
  95. PutCount++;
  96. #endif
  97. // Put the given element on the head of the free-list.
  98. #ifdef LIST_CHECK
  99. // check to make sure the entry is really OK
  100. // signature is before entry pointer
  101. // signature of free nodes is ( &mgr - &node )
  102. // signature of used nodes is ( &mgr % &node )
  103. pSignature = ((HeapSignature *)pEntry)-1;
  104. MIDL_ASSERT( *pSignature == USED_SIGNATURE( this, pEntry ) );
  105. *pSignature = FREE_SIGNATURE( this, pEntry );
  106. memset( pEntry, 0xA1, element_size );
  107. #endif
  108. ( (FreeListType *) pEntry ) -> next = pHead;
  109. pHead = (FreeListType *) pEntry;
  110. }; /* Put */
  111. #ifdef example
  112. //
  113. // Example of use...
  114. //
  115. // copy the following into a class definition and replace the X's with
  116. // the name of the class
  117. //
  118. /*********************************************************************/
  119. // here is the free list manager for a particular class. it should
  120. // NOT be inherited unless the derived classes have no extra data members.
  121. //
  122. // Otherwise, the derived classes should have their own new and delete
  123. // elsewhere.
  124. /*********************************************************************/
  125. private:
  126. static
  127. FreeListMgr MyFreeList( sizeof( X ) );
  128. public:
  129. /*********************************************************************/
  130. // Return a new element of the specified size.
  131. //
  132. // The FreeListMgr "Get" routine is used, so that the element may be
  133. // retrieved from a free-list, if possible, and extra get-memory calls
  134. // can thus be avoided.
  135. /*********************************************************************/
  136. X *
  137. operator new (size_t size)
  138. {
  139. return (MyFreeList.Get (size));
  140. }
  141. /*********************************************************************/
  142. // Release an element allocated by the "New" routine.
  143. //
  144. /*********************************************************************/
  145. void operator delete (X* pX)
  146. {
  147. MyFreeList.Put (pX);
  148. }
  149. #endif // example