HEAP.H 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** Command & Conquer(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: F:\projects\c&c\vcs\code\heap.h_v 2.15 16 Oct 1995 16:47:08 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 managment 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 annonymous. 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 ID(void const * pointer);
  52. int Count(void) {return ActiveCount;};
  53. int Length(void) {return TotalCount;};
  54. int Avail(void) {return TotalCount-ActiveCount;};
  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. protected:
  61. void * operator[](int index) {return ((char *)Buffer) + (index * Size);};
  62. /*
  63. ** If the memory block buffer was allocated by this class, then this flag
  64. ** will be true. The block must be deallocated by this class if true.
  65. */
  66. unsigned IsAllocated:1;
  67. /*
  68. ** This is the size of each sub-block within the buffer.
  69. */
  70. int Size;
  71. /*
  72. ** This records the absolute number of sub-blocks in the buffer.
  73. */
  74. int TotalCount;
  75. /*
  76. ** This is the total blocks allocated out of the heap. This number
  77. ** will never exceed Count.
  78. */
  79. int ActiveCount;
  80. /*
  81. ** Pointer to the heap's memory buffer.
  82. */
  83. void * Buffer;
  84. /*
  85. ** This is a boolean vector array of allocation flag bits.
  86. */
  87. BooleanVectorClass FreeFlag;
  88. private:
  89. // The assignment operator is not supported.
  90. FixedHeapClass & operator = (FixedHeapClass const &);
  91. // The copy constructor is not supported.
  92. FixedHeapClass(FixedHeapClass const &);
  93. };
  94. /**************************************************************************
  95. ** This template serves only as an interface to the heap manager class. By
  96. ** using this template, the object pointers are automatically converted
  97. ** to the correct type without any code overhead.
  98. */
  99. template<class T>
  100. class TFixedHeapClass : public FixedHeapClass
  101. {
  102. public:
  103. TFixedHeapClass(void) : FixedHeapClass(sizeof(T)) {};
  104. virtual ~TFixedHeapClass(void) {};
  105. int ID(T const * pointer) {return FixedHeapClass::ID(pointer);};
  106. virtual T * Alloc(void) {return (T*)FixedHeapClass::Allocate();};
  107. virtual int Free(T * pointer) {FixedHeapClass::Free(pointer);};
  108. protected:
  109. T & operator[](int index) {return *(((char *)Buffer) + (index * Size));};
  110. };
  111. /**************************************************************************
  112. ** This is a derivative of the fixed heap class. This class adds the
  113. ** ability to quickly iterate through the active (allocated) objects. Since the
  114. ** active array is a sequence of pointers, the overhead of this class
  115. ** is 4 bytes per potential allocated object (be warned).
  116. */
  117. class FixedIHeapClass : public FixedHeapClass
  118. {
  119. public:
  120. FixedIHeapClass(int size) : FixedHeapClass(size) {};
  121. virtual ~FixedIHeapClass(void) {};
  122. virtual int Set_Heap(int count, void * buffer=0);
  123. virtual void * Allocate(void);
  124. virtual void Clear(void);
  125. virtual int Free(void * pointer);
  126. virtual int Free_All(void);
  127. virtual void * Active_Ptr(int index) {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. template<class T>
  142. class TFixedIHeapClass : public FixedIHeapClass
  143. {
  144. public:
  145. TFixedIHeapClass(void) : FixedIHeapClass(sizeof(T)) {};
  146. virtual ~TFixedIHeapClass(void) {};
  147. int ID(T const * pointer) {return FixedIHeapClass::ID(pointer);};
  148. virtual T * Alloc(void) {return (T*)FixedIHeapClass::Allocate();};
  149. virtual int Free(T * pointer) {return FixedIHeapClass::Free(pointer);};
  150. virtual int Free(void * pointer) {return FixedIHeapClass::Free(pointer);};
  151. virtual int Save(FileClass & );
  152. virtual int Load(FileClass & );
  153. virtual void Code_Pointers(void);
  154. virtual void Decode_Pointers(void);
  155. virtual T * Ptr(int index) {return (T*)FixedIHeapClass::ActivePointers[index];};
  156. virtual T * Raw_Ptr(int index) {return (T*)((*this)[index]);};
  157. };
  158. #endif