Source code of Windows XP (NT5)
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.

124 lines
3.9 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // INTEL Corporation Proprietary Information
  3. // This listing is supplied under the terms of a license agreement with Intel
  4. // Corporation and many not be copied nor disclosed except in accordance
  5. // with the terms of that agreement.
  6. // Copyright (c) 1995, 1996 Intel Corporation.
  7. //
  8. //
  9. // Module Name: freelist.h
  10. // Abstract: header file. a data structure for maintaining a pool of memory.
  11. // Environment: MSVC 4.0, OLE 2
  12. /////////////////////////////////////////////////////////////////////////////////
  13. //NOTE: This structure is useful if you will be making several calls to new and delete
  14. // during the course of a program. To use this structure, you create a new freelist.
  15. // You must give it a size and quantitity. To get a chunk of uninitialized memory you use
  16. // Get(). This is useful for structures and types that don't need any initialization.
  17. //
  18. // If you are planning to use this memory as a class then use the overloaded new operator
  19. // This will gurantee that the constructor is called. (see bottom of file) the overloaded
  20. // new operator takes two paramters. A size and a pointer to a free list. The size of the
  21. // object must be bigger than sizeof(QueItem).
  22. //
  23. // To put memory back in the free list you call Free().
  24. /////////////////////////////////////////////////////////////////////////////////
  25. #ifndef FREELIST_H
  26. #define FREELIST_H
  27. #include "que.h"
  28. #include "debug.h" //for ASSERT macro, assert.h
  29. #include <wtypes.h>
  30. extern long numHeapCreate;
  31. extern long numHeapCreateFailed;
  32. #define FREE_LIST_TAG 0x112233aa
  33. #define FREE_LIST_SIG_SIZE 8
  34. class FreeList {
  35. private:
  36. Queue m_List;
  37. DWORD m_Tag;
  38. CRITICAL_SECTION m_CritSect;
  39. size_t m_Size;
  40. unsigned m_HighWaterCount;
  41. unsigned m_AllocatedCount;
  42. unsigned m_Increment;
  43. HANDLE m_hMemAlloc;
  44. // This is a private function to allocate memory and enqueue in
  45. // the queue. This function will get used by more than one function
  46. // in this class.
  47. int AllocateAndEnqueue( int NumElements );
  48. //only need this if I Get is private. But you might want to call get
  49. //if you want a free list of things that are not classes and therefore
  50. //don't need there constructors called.
  51. //friend void * operator new(size_t size, FreeList *l);
  52. public:
  53. //inline default constructor do I want this?
  54. FreeList(HRESULT *phr);
  55. //2nd constructor. Size must be at least the size of a QueItem
  56. FreeList(int NumElements, size_t Size, HRESULT *phr);
  57. //3rd constructor. Size must be at least the size of a QueItem
  58. // HighWaterCount is the count till which dynamic allocation will take
  59. // place. Initial allocation will be based on NumElements
  60. FreeList(int NumElements, size_t Size, unsigned HighWaterCount,
  61. unsigned Increment, HRESULT *phr);
  62. ~FreeList();
  63. //inline. Gets a chunk of memeory from list, but does NOT do any initialization.
  64. //Need to type cast return value to correct type
  65. void * Get();
  66. //inline. Frees piece of memory.
  67. HRESULT Free(void * Element);
  68. //inline. returns true if list is empty; false otherwise
  69. BOOL Is_Empty() const
  70. {return m_List.Is_Empty();};
  71. //returns the size of the fragments that it creates and stores.
  72. size_t Size() const
  73. {return m_Size;};
  74. // returns the current count
  75. int Count() const
  76. {
  77. return(m_List.NumItems());
  78. }
  79. int MaxCount() const
  80. {
  81. return(m_AllocatedCount);
  82. }
  83. }; //end class FreeList
  84. //inline. Overload the new operator.
  85. //Gets a chunk of memory from free list and calls the constructor on that chunk of memory.
  86. //The constructor is that of the object new is called on. The returned
  87. //pointer is of the correct type. (just like the real new operator)
  88. //example:
  89. //class FooClass;
  90. // pFoo = new(pSomeFreeList) FooClass;
  91. inline void * operator new(size_t size, FreeList *l)
  92. {
  93. //lsc
  94. ASSERT(l->Size() == size); //Is this needed beyond compile time? If so, output message
  95. //instead
  96. return l->Get();
  97. }
  98. #endif