CnC_Remastered_Collection

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

HEAP.H (8009B)


      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: /CounterStrike/HEAP.H 1     3/03/97 10:24a 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 management 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 anonymously. 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 Count(void) const {return ActiveCount;};
     54 		int Length(void) const {return TotalCount;};
     55 		int Avail(void) const {return TotalCount-ActiveCount;};
     56 
     57 		virtual int ID(void const * pointer) const;
     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 		void * operator[](int index) {return ((char *)Buffer) + (index * Size);};
     65 		void const * operator[](int index) const {return ((char *)Buffer) + (index * Size);};
     66 
     67 	protected:
     68 		/*
     69 		**	If the memory block buffer was allocated by this class, then this flag
     70 		**	will be true. The block must be deallocated by this class if true.
     71 		*/
     72 		unsigned IsAllocated:1;
     73 
     74 		/*
     75 		**	This is the size of each sub-block within the buffer.
     76 		*/
     77 		int Size;
     78 
     79 		/*
     80 		**	This records the absolute number of sub-blocks in the buffer.
     81 		*/
     82 		int TotalCount;
     83 
     84 		/*
     85 		**	This is the total blocks allocated out of the heap. This number
     86 		**	will never exceed Count.
     87 		*/
     88 		int ActiveCount;
     89 
     90 		/*
     91 		**	Pointer to the heap's memory buffer.
     92 		*/
     93 		void * Buffer;
     94 
     95 		/*
     96 		**	This is a boolean vector array of allocation flag bits.
     97 		*/
     98 		BooleanVectorClass FreeFlag;
     99 
    100 	private:
    101 		// The assignment operator is not supported.
    102 		FixedHeapClass & operator = (FixedHeapClass const &);
    103 
    104 		// The copy constructor is not supported.
    105 		FixedHeapClass(FixedHeapClass const &);
    106 };
    107 
    108 
    109 /**************************************************************************
    110 **	This template serves only as an interface to the heap manager class. By
    111 **	using this template, the object pointers are automatically converted
    112 **	to the correct type without any code overhead.
    113 */
    114 template<class T>
    115 class TFixedHeapClass : public FixedHeapClass
    116 {
    117 	public:
    118 		TFixedHeapClass(void) : FixedHeapClass(sizeof(T)) {};
    119 		virtual ~TFixedHeapClass(void) {};
    120 
    121 
    122 		virtual int ID(T const * pointer) const {return FixedHeapClass::ID(pointer);};
    123 		virtual T * Alloc(void) {return (T*)FixedHeapClass::Allocate();};
    124 		virtual int Free(T * pointer) {return(FixedHeapClass::Free(pointer));};
    125 
    126 		T & operator[](int index) {return *(T *)(((char *)Buffer) + (index * Size));};
    127 		T const & operator[](int index) const {return *(T*)(((char *)Buffer) + (index * Size));};
    128 };
    129 
    130 
    131 /**************************************************************************
    132 **	This is a derivative of the fixed heap class. This class adds the
    133 **	ability to quickly iterate through the active (allocated) objects. Since the
    134 **	active array is a sequence of pointers, the overhead of this class
    135 **	is 4 bytes per potential allocated object (be warned).
    136 */
    137 class FixedIHeapClass : public FixedHeapClass
    138 {
    139 	public:
    140 		FixedIHeapClass(int size) : FixedHeapClass(size) {};
    141 		virtual ~FixedIHeapClass(void) {};
    142 
    143 		virtual int Set_Heap(int count, void * buffer=0);
    144 		virtual void * Allocate(void);
    145 		virtual void Clear(void);
    146 		virtual int Free(void * pointer);
    147 		virtual int Free_All(void);
    148 		virtual int Logical_ID(void const * pointer) const;
    149 		virtual int Logical_ID(int id) const {return(Logical_ID((*this)[id]));}
    150 
    151 		virtual void * Active_Ptr(int index) {return ActivePointers[index];};
    152 		virtual void const * Active_Ptr(int index) const {return ActivePointers[index];};
    153 
    154 		/*
    155 		**	This is an array of pointers to allocated objects. Using this array
    156 		**	to control iteration through the objects ensures a minimum of processing.
    157 		**	It also allows access to this array so that custom sorting can be
    158 		**	performed.
    159 		*/
    160 		DynamicVectorClass<void *> ActivePointers;
    161 };
    162 
    163 
    164 /**************************************************************************
    165 **	This template serves only as an interface to the iteratable heap manager
    166 **	class. By using this template, the object pointers are automatically converted
    167 **	to the correct type without any code overhead.
    168 */
    169 class FileClass;
    170 template<class T>
    171 class TFixedIHeapClass : public FixedIHeapClass
    172 {
    173 	public:
    174 		TFixedIHeapClass(void) : FixedIHeapClass(sizeof(T)) {};
    175 		virtual ~TFixedIHeapClass(void) {};
    176 
    177 		virtual int ID(T const * pointer) const {return FixedIHeapClass::ID(pointer);};
    178 		virtual int Logical_ID(T const * pointer) const {return(FixedIHeapClass::Logical_ID(pointer));}
    179 		virtual int Logical_ID(int id) const {return(FixedIHeapClass::Logical_ID(id));}
    180 		virtual T * Alloc(void) {return (T*)FixedIHeapClass::Allocate();};
    181 		virtual int Free(T * pointer) {return FixedIHeapClass::Free(pointer);};
    182 		virtual int Free(void * pointer) {return FixedIHeapClass::Free(pointer);};
    183 		virtual int Save(Pipe & file) const;
    184 		virtual int Load(Straw & file);
    185 		virtual void Code_Pointers(void);
    186 		virtual void Decode_Pointers(void);
    187 
    188 		virtual T * Ptr(int index) const {return (T*)FixedIHeapClass::ActivePointers[index];};
    189 		virtual T * Raw_Ptr(int index) {return (index >= 0 && index < Length()) ? (T*)((*this)[index]) : NULL;};
    190 };
    191 
    192 
    193 #endif
    194 
    195