CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

HEAP.H (13949B)


      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 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header:   F:\projects\c&c\vcs\code\heap.h_v   2.15   16 Oct 1995 16:47:08   JOE_BOSTIC  $ */
     17 /*********************************************************************************************** 
     18  ***             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               *** 
     19  *********************************************************************************************** 
     20  *                                                                                             * 
     21  *                 Project Name : Command & Conquer                                            * 
     22  *                                                                                             * 
     23  *                    File Name : HEAP.H                                                       * 
     24  *                                                                                             * 
     25  *                   Programmer : Joe L. Bostic                                                * 
     26  *                                                                                             * 
     27  *                   Start Date : 02/18/95                                                     * 
     28  *                                                                                             * 
     29  *                  Last Update : February 18, 1995 [JLB]                                      * 
     30  *                                                                                             * 
     31  *---------------------------------------------------------------------------------------------* 
     32  * Functions:                                                                                  * 
     33  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     34 
     35 #ifndef HEAP_H
     36 #define HEAP_H
     37 
     38 #include "vector.h"
     39 
     40 /**************************************************************************
     41 **	This is a block memory managment handler. It is used when memory is to
     42 **	be treated as a series of blocks of fixed size. This is similar to an
     43 **	array of integral types, but unlike such an array, the memory blocks
     44 **	are annonymous. This facilitates the use of this class when overloading
     45 **	the new and delete operators for a normal class object.
     46 */
     47 class FixedHeapClass
     48 {
     49 	public:
     50 		FixedHeapClass(int size);
     51 		virtual ~FixedHeapClass(void);
     52 
     53 		int ID(void const * pointer);
     54 		int Count(void) {return ActiveCount;};
     55 		int Length(void) {return TotalCount;};
     56 		int Avail(void) {return TotalCount-ActiveCount;};
     57 
     58 		virtual int Set_Heap(int count, void * buffer=0);
     59 		virtual void * Allocate(void);
     60 		virtual void Clear(void);
     61 		virtual int Free(void * pointer);
     62 		virtual int Free_All(void);
     63 
     64 	protected:
     65 		void * operator[](int index) {return ((char *)Buffer) + (index * Size);};
     66 
     67 		/*
     68 		**	If the memory block buffer was allocated by this class, then this flag
     69 		**	will be true. The block must be deallocated by this class if true.
     70 		*/
     71 		unsigned IsAllocated:1;
     72 
     73 		/*
     74 		**	This is the size of each sub-block within the buffer.
     75 		*/
     76 		int Size;
     77 
     78 		/*
     79 		**	This records the absolute number of sub-blocks in the buffer.
     80 		*/
     81 		int TotalCount;
     82 
     83 		/*
     84 		**	This is the total blocks allocated out of the heap. This number
     85 		**	will never exceed Count.
     86 		*/
     87 		int ActiveCount;
     88 
     89 		/*
     90 		**	Pointer to the heap's memory buffer.
     91 		*/
     92 		void * Buffer;
     93 
     94 		/*
     95 		**	This is a boolean vector array of allocation flag bits.
     96 		*/
     97 		BooleanVectorClass FreeFlag;
     98 
     99 	private:
    100 		// The assignment operator is not supported.
    101 		FixedHeapClass & operator = (FixedHeapClass const &);
    102 
    103 		// The copy constructor is not supported.
    104 		FixedHeapClass(FixedHeapClass const &);
    105 };
    106 
    107 
    108 /**************************************************************************
    109 **	This template serves only as an interface to the heap manager class. By
    110 **	using this template, the object pointers are automatically converted
    111 **	to the correct type without any code overhead.
    112 */
    113 template<class T>
    114 class TFixedHeapClass : public FixedHeapClass
    115 {
    116 	public:
    117 		TFixedHeapClass(void) : FixedHeapClass(sizeof(T)) {};
    118 		virtual ~TFixedHeapClass(void) {};
    119 
    120 		int ID(T const * pointer) {return FixedHeapClass::ID(pointer);};
    121 
    122 		virtual T * Alloc(void) {return (T*)FixedHeapClass::Allocate();};
    123 		virtual int Free(T * pointer) {FixedHeapClass::Free(pointer);};
    124 
    125 	protected:
    126 		T & operator[](int index) {return *(((char *)Buffer) + (index * Size));};
    127 };
    128 
    129 
    130 /**************************************************************************
    131 **	This is a derivative of the fixed heap class. This class adds the 
    132 **	ability to quickly iterate through the active (allocated) objects. Since the
    133 **	active array is a sequence of pointers, the overhead of this class
    134 **	is 4 bytes per potential allocated object (be warned).
    135 */
    136 class FixedIHeapClass : public FixedHeapClass
    137 {
    138 	public:
    139 		FixedIHeapClass(int size) : FixedHeapClass(size) {};
    140 		virtual ~FixedIHeapClass(void) {};
    141 
    142 		virtual int Set_Heap(int count, void * buffer=0);
    143 		virtual void * Allocate(void);
    144 		virtual void Clear(void);
    145 		virtual int Free(void * pointer);
    146 		virtual int Free_All(void);
    147 
    148 		virtual void * Active_Ptr(int index) {return ActivePointers[index];};
    149 
    150 		/*
    151 		**	This is an array of pointers to allocated objects. Using this array
    152 		**	to control iteration through the objects ensures a minimum of processing.
    153 		**	It also allows access to this array so that custom sorting can be
    154 		**	performed.
    155 		*/
    156 		DynamicVectorClass<void *> ActivePointers;
    157 };
    158 
    159 
    160 /**************************************************************************
    161 **	This template serves only as an interface to the iteratable heap manager
    162 **	class. By using this template, the object pointers are automatically converted
    163 **	to the correct type without any code overhead.
    164 */
    165 template<class T>
    166 class TFixedIHeapClass : public FixedIHeapClass
    167 {
    168 	public:
    169 		TFixedIHeapClass(void) : FixedIHeapClass(sizeof(T)) {};
    170 		virtual ~TFixedIHeapClass(void) {};
    171 
    172 		int ID(T const * pointer) {return FixedIHeapClass::ID(pointer);};
    173 		virtual T * Alloc(void) {return (T*)FixedIHeapClass::Allocate();};
    174 		virtual int Free(T * pointer) {return FixedIHeapClass::Free(pointer);};
    175 		virtual int Free(void * pointer) {return FixedIHeapClass::Free(pointer);};
    176 		virtual int Save(FileClass & );
    177 		virtual int Load(FileClass & );
    178 		virtual void Code_Pointers(void);
    179 		virtual void Decode_Pointers(void);
    180 
    181 		virtual T * Ptr(int index) {return (T*)FixedIHeapClass::ActivePointers[index];};
    182 		virtual T * Raw_Ptr(int index) {return (index >= 0 && index < Length()) ? (T*)((*this)[index]) : NULL;};
    183 };
    184 
    185 
    186 /*********************************************************************************************** 
    187  * TFixedIHeapClass::Save -- Saves all active objects                                          *
    188  *                                                                                             * 
    189  * INPUT:   file      file to write to                                                         *
    190  *                                                                                             * 
    191  * OUTPUT:  true = OK, false = error                                                           *
    192  *                                                                                             * 
    193  * WARNINGS:   none                                                                            * 
    194  *                                                                                             * 
    195  * HISTORY:                                                                                    * 
    196  *   03/15/1995 BRR : Created.                                                                 *
    197  *=============================================================================================*/
    198 template<class T>
    199 int TFixedIHeapClass<T>::Save(FileClass &file)
    200 {
    201 	int i;				// loop counter
    202 	int idx;				// object index
    203 
    204 	/*
    205 	** Save the number of instances of this class
    206 	*/
    207 	if (file.Write(&ActiveCount, sizeof(ActiveCount)) != sizeof(ActiveCount)) {
    208 		return(false);
    209 	}
    210 
    211 	/*
    212 	** Save each instance of this class
    213 	*/
    214 	for (i = 0; i < ActiveCount; i++) {
    215 		/*
    216 		** Save the array index of the object, so it can be loaded back into the
    217 		** same array location (so TARGET translations will work)
    218 		*/
    219 		idx = ID(Ptr(i));
    220 		if (file.Write(&idx, sizeof(idx)) != sizeof(idx)) {
    221 			return(false);
    222 		}
    223 
    224 		/*
    225 		** Save the object itself
    226 		*/
    227 		if (!Ptr(i)->Save(file)) {
    228 			return(false);
    229 		}
    230 	}
    231 
    232 	return(true);
    233 }	
    234 
    235 
    236 /*********************************************************************************************** 
    237  * TFixedIHeapClass::Load -- Loads all active objects                                          *
    238  *                                                                                             * 
    239  * INPUT:   file      file to read from                                                        *
    240  *                                                                                             * 
    241  * OUTPUT:  true = OK, false = error                                                           *
    242  *                                                                                             * 
    243  * WARNINGS:   none                                                                            * 
    244  *                                                                                             * 
    245  * HISTORY:                                                                                    * 
    246  *   03/15/1995 BRR : Created.                                                                 *
    247  *=============================================================================================*/
    248 template<class T>
    249 int TFixedIHeapClass<T>::Load(FileClass &file)
    250 {
    251 	int i;			// loop counter
    252 	int idx;			// object index
    253 	T *ptr;			// object pointer
    254 	int a_count;
    255 
    256 	/*
    257 	** Read the number of instances of this class
    258 	*/
    259 	if (file.Read(&a_count, sizeof(a_count)) != sizeof(a_count)) {
    260 		return(false);
    261 	}
    262 
    263 	/*
    264 	** Error if more objects than we can hold
    265 	*/
    266 	if (a_count > TotalCount) {
    267 		return(false);
    268 	}
    269 
    270 	/*
    271 	** Read each class instance
    272 	*/
    273 	for (i = 0; i < a_count; i++) {
    274 		/*
    275 		** Read the object's array index
    276 		*/
    277 		if (file.Read (&idx, sizeof(idx)) != sizeof(idx)) {
    278 			return(false);
    279 		}
    280 
    281 		/*
    282 		** Get a pointer to the object, activate that object
    283 		*/
    284 		ptr = (T *)(*this)[idx];
    285 		FreeFlag[idx] = true;
    286 		ActiveCount++;
    287 		ActivePointers.Add(ptr);
    288 
    289 		/*
    290 		** Load the object
    291 		*/
    292 		if (!ptr->Load(file)) {
    293 			return(false);
    294 		}
    295 	}
    296 
    297 	return(true);
    298 }	
    299 
    300 
    301 /*********************************************************************************************** 
    302  * TFixedIHeapClass::Code_Pointers -- codes pointers for every object, to prepare for save     *
    303  *                                                                                             * 
    304  * INPUT:   file      file to read from                                                        *
    305  *                                                                                             * 
    306  * OUTPUT:  true = OK, false = error                                                           *
    307  *                                                                                             * 
    308  * WARNINGS:   none                                                                            * 
    309  *                                                                                             * 
    310  * HISTORY:                                                                                    * 
    311  *   03/15/1995 BRR : Created.                                                                 *
    312  *=============================================================================================*/
    313 template<class T>
    314 void TFixedIHeapClass<T>::Code_Pointers(void)
    315 {
    316 	int i;
    317 
    318 	for (i = 0; i < ActiveCount; i++) {
    319 		Ptr(i)->Code_Pointers();
    320 	}
    321 }
    322 
    323 
    324 /*********************************************************************************************** 
    325  * TFixedIHeapClass::Decode_Pointers -- Decodes all object pointers, for after loading         *
    326  *                                                                                             * 
    327  * INPUT:   file      file to read from                                                        *
    328  *                                                                                             * 
    329  * OUTPUT:  true = OK, false = error                                                           *
    330  *                                                                                             * 
    331  * WARNINGS:   none                                                                            * 
    332  *                                                                                             * 
    333  * HISTORY:                                                                                    * 
    334  *   03/15/1995 BRR : Created.                                                                 *
    335  *=============================================================================================*/
    336 template<class T>
    337 void TFixedIHeapClass<T>::Decode_Pointers(void)
    338 {
    339 	int i;
    340 
    341 	for (i = 0; i < ActiveCount; i++) {
    342 		Ptr(i)->Decode_Pointers();
    343 	}
    344 }
    345 
    346 
    347 #endif
    348 
    349