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.

167 lines
5.0 KiB

  1. /*************************************************************************/
  2. /** Copyright(c) Microsoft Corp., 1993-1999 **/
  3. /*************************************************************************/
  4. /**************************************************************************
  5. File :FreeListMgr.hxx
  6. Title :Get and Put functions for the MIDL compiler
  7. History :
  8. 30-Oct-93 GregJen Created
  9. **************************************************************************/
  10. #ifndef __FREELIST_HXX__
  11. #define __FREELIST_HXX__
  12. /*************************************************************************
  13. ** includes
  14. *************************************************************************/
  15. extern "C" {
  16. #include "memory.h"
  17. }
  18. #include "common.hxx"
  19. /*************************************************************************
  20. ** definitions
  21. *************************************************************************/
  22. // Type used to keep (and link together) the elements
  23. // of the free-list. It is assumed that each element is large
  24. // enough to contain this structure.
  25. typedef struct FreeListTag
  26. {
  27. FreeListTag * next;
  28. }
  29. FreeListType;
  30. /*************************************************************************
  31. *** Class to Allocate and Release Memory from a Free-List
  32. *************************************************************************/
  33. // This class allows the caller to allocate elements and
  34. // release them. The freed elements are kept on a free-list
  35. // so they may be re-used later, without the overhead of
  36. // another get-memory call.
  37. class FreeListMgr
  38. {
  39. private:
  40. // Pointer to head of the free-list.
  41. // NULL => the list is empty.
  42. FreeListType *pHead;
  43. // Size of each element of the free-list. This is kept and
  44. // checked for debugging purposes only - each element is
  45. // �supposed� to be the same size.
  46. // This is initialized by the constructor.
  47. size_t element_size;
  48. // Number of "Get" and "Put" calls made, respectively.
  49. // Kept for debugging purposes only.
  50. unsigned long GetCount;
  51. unsigned long PutCount;
  52. public:
  53. /*********************************************************************/
  54. // Initialize the private data for this class.
  55. // Given: the size of each element to be allocated by this object.
  56. /*********************************************************************/
  57. FreeListMgr (size_t size)
  58. {
  59. pHead = NULL; /* Start with an empty free-list */
  60. // Make sure the "size" requested is big enough to hold the
  61. // link pointers. Save the size for later comparisons.
  62. MIDL_ASSERT (size >= sizeof (FreeListType));
  63. element_size = size;
  64. GetCount = 0; /* No "Get" calls have been made yet */
  65. PutCount = 0; /* No "Put" calls have been made yet */
  66. }
  67. /*********************************************************************/
  68. // This routine returns an element of the requested size to
  69. // the caller. "size" must be the value specified to the constructor.
  70. //
  71. // Returns:
  72. // A pointer - if everything went OK
  73. // exit - if unable to allocate another element
  74. // exit - if "size" is invalid, (fail assert)
  75. /*********************************************************************/
  76. void * Get (size_t size);
  77. /*********************************************************************/
  78. // This routine "releases" the given element, by putting it on
  79. // the free-list for later re-use. The given element, must be
  80. // the same size as the elements provided by the "Get" function.
  81. /*********************************************************************/
  82. void Put (void * pEntry);
  83. }; /* FreeListMgr */
  84. #ifdef example
  85. //
  86. // Example of use...
  87. //
  88. // copy the following into a class definition and replace the X's with
  89. // the name of the class
  90. //
  91. /*********************************************************************/
  92. // here is the free list manager for a particular class. it should
  93. // NOT be inherited unless the derived classes have no extra data members.
  94. //
  95. // Otherwise, the derived classes should have their own new and delete
  96. // elsewhere.
  97. /*********************************************************************/
  98. private:
  99. static
  100. FreeListMgr MyFreeList( sizeof( X ) );
  101. public:
  102. /*********************************************************************/
  103. // Return a new element of the specified size.
  104. //
  105. // The FreeListMgr "Get" routine is used, so that the element may be
  106. // retrieved from a free-list, if possible, and extra get-memory calls
  107. // can thus be avoided.
  108. /*********************************************************************/
  109. X *
  110. operator new (size_t size)
  111. {
  112. return (MyFreeList.Get (size));
  113. }
  114. /*********************************************************************/
  115. // Release an element allocated by the "New" routine.
  116. //
  117. /*********************************************************************/
  118. void operator delete (X* pX)
  119. {
  120. MyFreeList.Put (pX);
  121. }
  122. #endif // example
  123. #endif // __FREELIST_HXX__