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.

150 lines
5.3 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corp., 1991 **/
  4. /**********************************************************************/
  5. /*
  6. heapones.hxx
  7. One-shot heap classes: definition.
  8. A one-shot heap provides sugar for buffer suballocation.
  9. FILE HISTORY:
  10. beng 24-Dec-1991 Derived from heap.hxx
  11. */
  12. #ifndef _HEAPONES_HXX_
  13. #define _HEAPONES_HXX_
  14. #include "base.hxx"
  15. #include "uibuffer.hxx"
  16. #ifndef _SIZE_T_DEFINED
  17. #include <stddef.h>
  18. #endif
  19. DLL_CLASS ONE_SHOT_ITEM;
  20. DLL_CLASS ONE_SHOT_HEAP;
  21. /*************************************************************************
  22. NAME: ONE_SHOT_HEAP (osh)
  23. SYNOPSIS: Buffer prepared for suballocation.
  24. The one-shot heap class provides heap-style allocation for
  25. objects of finite and understood lifespan whose allocation/
  26. deallocation performance is critical.
  27. INTERFACE: ONE_SHOT_HEAP() - constructor
  28. Alloc() - allocation interface, used by op new
  29. PARENT: BUFFER
  30. NOTES:
  31. Optionally, ONE_SHOT_HEAPs automatically resize themselves to
  32. attempt to satisfy new allocation demands. (See "Alloc").
  33. CAVEATS:
  34. Auto-resizing is dangerous in a flat-model environment!
  35. Under Win32, the resized buffer will change its virtual
  36. address; pointers to within that buffer will no longer
  37. reference their original targets, and may soon reference
  38. new targets as new allocations use the old address.
  39. Bookkeeping of one-shot heaps is kept to a bare minimum.
  40. Nobody checks to prevent leaks, or to make sure that
  41. all allocations are deallocated.
  42. The client must construct a one-shot heap and associate it
  43. with the one-shot item class of interest (by its SetHeap
  44. member) before allocating any such items.
  45. HISTORY:
  46. DavidHov 2-25-91 Created
  47. beng 24-Dec-1991 Simplification and redesign
  48. beng 19-Mar-1992 Make auto-resizing optional
  49. **************************************************************************/
  50. DLL_CLASS ONE_SHOT_HEAP : public BUFFER
  51. {
  52. private:
  53. UINT _cbUsed;
  54. BOOL _fAutoResize;
  55. public:
  56. ONE_SHOT_HEAP( UINT cbInitialAllocSize = 16384,
  57. BOOL fAutoResize = FALSE );
  58. BYTE * Alloc( UINT cbBytes );
  59. };
  60. /*************************************************************************
  61. NAME: ONE_SHOT_ITEM
  62. SYNOPSIS: Template-style class used to derive objects which are
  63. allocated from a ONE_SHOT_HEAP. Operators "new" and
  64. "delete" are overridden, and allocation is done from
  65. a single ONE_SHOT_HEAP until it is exhausted.
  66. INTERFACE: SetHeap() -- set ONE_SHOT_HEAP from which to allocate
  67. QueryHeap() -- query ONE_SHOT_HEAP currently being used
  68. operator new() - Allocates an object from its heap.
  69. operator delete() - Discards an object.
  70. NOTES:
  71. To allocate items from a one-shot heap, first declare a class
  72. as using a one-shot heap by havint it inherit from ONE_SHOT_ITEM.
  73. Actually, the class should inherit from a form of ONE_SHOT_OF(),
  74. preceded by DECLARE forms etc. just as if it was inheriting from
  75. some collection class.
  76. Next, construct a one-shot heap. Before allocating any items,
  77. call class::SetHeap on the new heap; this will tell the class
  78. to perform all allocations from the named heap.
  79. Now call new and delete freely. Should you exhaust a one-shot
  80. heap (each being limited to the size of a BUFFER object), feel
  81. free to construct another and associate the new instance with
  82. the class. Just be sure to destroy all heaps when you are
  83. finished.
  84. CAVEATS:
  85. One-shot heap implementations must not reside in a DLL
  86. (due to static member fixup problems).
  87. HISTORY:
  88. DavidHov 2-25-91 Created
  89. beng 24-Dec-1991 Redesign, using templates and reducing size
  90. KeithMo 26-Aug-1992 Added a dummy "new" to shut-up the compiler.
  91. **************************************************************************/
  92. #define ONE_SHOT_OF(type) ONE_SHOT_OF##type
  93. #define DECLARE_ONE_SHOT_OF(type) \
  94. class ONE_SHOT_OF(type) : virtual public ALLOC_BASE \
  95. { \
  96. private: \
  97. static ONE_SHOT_HEAP * _poshCurrent; \
  98. public: \
  99. static VOID SetHeap( ONE_SHOT_HEAP * posh ) \
  100. { _poshCurrent = posh; } \
  101. static ONE_SHOT_HEAP * QueryHeap() \
  102. { return _poshCurrent; } \
  103. VOID * operator new( size_t cb ) \
  104. { return _poshCurrent->Alloc(cb); } \
  105. VOID operator delete( VOID * pbTarget ) {} \
  106. };
  107. #define DEFINE_ONE_SHOT_OF(type) \
  108. ONE_SHOT_HEAP * ONE_SHOT_OF(type)::_poshCurrent = NULL;
  109. #endif // _HEAPONES_HXX_ - end of file