HEAP.H 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/HEAP.H 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : HEAP.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 02/18/95 *
  30. * *
  31. * Last Update : February 18, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef HEAP_H
  37. #define HEAP_H
  38. #include "vector.h"
  39. /**************************************************************************
  40. ** This is a block memory management handler. It is used when memory is to
  41. ** be treated as a series of blocks of fixed size. This is similar to an
  42. ** array of integral types, but unlike such an array, the memory blocks
  43. ** are anonymously. This facilitates the use of this class when overloading
  44. ** the new and delete operators for a normal class object.
  45. */
  46. class FixedHeapClass
  47. {
  48. public:
  49. FixedHeapClass(int size);
  50. virtual ~FixedHeapClass(void);
  51. int Count(void) const {return ActiveCount;};
  52. int Length(void) const {return TotalCount;};
  53. int Avail(void) const {return TotalCount-ActiveCount;};
  54. virtual int ID(void const * pointer) const;
  55. virtual int Set_Heap(int count, void * buffer=0);
  56. virtual void * Allocate(void);
  57. virtual void Clear(void);
  58. virtual int Free(void * pointer);
  59. virtual int Free_All(void);
  60. void * operator[](int index) {return ((char *)Buffer) + (index * Size);};
  61. void const * operator[](int index) const {return ((char *)Buffer) + (index * Size);};
  62. protected:
  63. /*
  64. ** If the memory block buffer was allocated by this class, then this flag
  65. ** will be true. The block must be deallocated by this class if true.
  66. */
  67. unsigned IsAllocated:1;
  68. /*
  69. ** This is the size of each sub-block within the buffer.
  70. */
  71. int Size;
  72. /*
  73. ** This records the absolute number of sub-blocks in the buffer.
  74. */
  75. int TotalCount;
  76. /*
  77. ** This is the total blocks allocated out of the heap. This number
  78. ** will never exceed Count.
  79. */
  80. int ActiveCount;
  81. /*
  82. ** Pointer to the heap's memory buffer.
  83. */
  84. void * Buffer;
  85. /*
  86. ** This is a boolean vector array of allocation flag bits.
  87. */
  88. BooleanVectorClass FreeFlag;
  89. private:
  90. // The assignment operator is not supported.
  91. FixedHeapClass & operator = (FixedHeapClass const &);
  92. // The copy constructor is not supported.
  93. FixedHeapClass(FixedHeapClass const &);
  94. };
  95. /**************************************************************************
  96. ** This template serves only as an interface to the heap manager class. By
  97. ** using this template, the object pointers are automatically converted
  98. ** to the correct type without any code overhead.
  99. */
  100. template<class T>
  101. class TFixedHeapClass : public FixedHeapClass
  102. {
  103. public:
  104. TFixedHeapClass(void) : FixedHeapClass(sizeof(T)) {};
  105. virtual ~TFixedHeapClass(void) {};
  106. virtual int ID(T const * pointer) const {return FixedHeapClass::ID(pointer);};
  107. virtual T * Alloc(void) {return (T*)FixedHeapClass::Allocate();};
  108. virtual int Free(T * pointer) {return(FixedHeapClass::Free(pointer));};
  109. T & operator[](int index) {return *(T *)(((char *)Buffer) + (index * Size));};
  110. T const & operator[](int index) const {return *(T*)(((char *)Buffer) + (index * Size));};
  111. };
  112. /**************************************************************************
  113. ** This is a derivative of the fixed heap class. This class adds the
  114. ** ability to quickly iterate through the active (allocated) objects. Since the
  115. ** active array is a sequence of pointers, the overhead of this class
  116. ** is 4 bytes per potential allocated object (be warned).
  117. */
  118. class FixedIHeapClass : public FixedHeapClass
  119. {
  120. public:
  121. FixedIHeapClass(int size) : FixedHeapClass(size) {};
  122. virtual ~FixedIHeapClass(void) {};
  123. virtual int Set_Heap(int count, void * buffer=0);
  124. virtual void * Allocate(void);
  125. virtual void Clear(void);
  126. virtual int Free(void * pointer);
  127. virtual int Free_All(void);
  128. virtual int Logical_ID(void const * pointer) const;
  129. virtual int Logical_ID(int id) const {return(Logical_ID((*this)[id]));}
  130. virtual void * Active_Ptr(int index) {return ActivePointers[index];};
  131. virtual void const * Active_Ptr(int index) const {return ActivePointers[index];};
  132. /*
  133. ** This is an array of pointers to allocated objects. Using this array
  134. ** to control iteration through the objects ensures a minimum of processing.
  135. ** It also allows access to this array so that custom sorting can be
  136. ** performed.
  137. */
  138. DynamicVectorClass<void *> ActivePointers;
  139. };
  140. /**************************************************************************
  141. ** This template serves only as an interface to the iteratable heap manager
  142. ** class. By using this template, the object pointers are automatically converted
  143. ** to the correct type without any code overhead.
  144. */
  145. class FileClass;
  146. template<class T>
  147. class TFixedIHeapClass : public FixedIHeapClass
  148. {
  149. public:
  150. TFixedIHeapClass(void) : FixedIHeapClass(sizeof(T)) {};
  151. virtual ~TFixedIHeapClass(void) {};
  152. virtual int ID(T const * pointer) const {return FixedIHeapClass::ID(pointer);};
  153. virtual int Logical_ID(T const * pointer) const {return(FixedIHeapClass::Logical_ID(pointer));}
  154. virtual int Logical_ID(int id) const {return(FixedIHeapClass::Logical_ID(id));}
  155. virtual T * Alloc(void) {return (T*)FixedIHeapClass::Allocate();};
  156. virtual int Free(T * pointer) {return FixedIHeapClass::Free(pointer);};
  157. virtual int Free(void * pointer) {return FixedIHeapClass::Free(pointer);};
  158. virtual int Save(Pipe & file) const;
  159. virtual int Load(Straw & file);
  160. virtual void Code_Pointers(void);
  161. virtual void Decode_Pointers(void);
  162. virtual T * Ptr(int index) const {return (T*)FixedIHeapClass::ActivePointers[index];};
  163. virtual T * Raw_Ptr(int index) {return (T*)((*this)[index]);};
  164. };
  165. #endif