HEAP.H 7.8 KB

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