00001 /****************************************************************************************/ 00002 /* DDMemMgr.c */ 00003 /* */ 00004 /* Author: John Pollard */ 00005 /* Description: Mini D3D memory manager */ 00006 /* */ 00007 /* The contents of this file are subject to the Genesis3D Public License */ 00008 /* Version 1.01 (the "License"); you may not use this file except in */ 00009 /* compliance with the License. You may obtain a copy of the License at */ 00010 /* http://www.genesis3d.com */ 00011 /* */ 00012 /* Software distributed under the License is distributed on an "AS IS" */ 00013 /* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */ 00014 /* the License for the specific language governing rights and limitations */ 00015 /* under the License. */ 00016 /* */ 00017 /* The Original Code is Genesis3D, released March 25, 1999. */ 00018 /*Genesis3D Version 1.1 released November 15, 1999 */ 00019 /* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */ 00020 /* */ 00021 /****************************************************************************************/ 00022 #include <Windows.h> 00023 #include <Assert.h> 00024 00025 #include "BaseType.h" 00026 #include "DDMemMgr.h" 00027 00028 #define DDMEMMGR_MAX_PARTITIONS 16 00029 00030 typedef struct DDMemMgr_Partition 00031 { 00032 geBoolean Active; 00033 uint32 FreeMem; 00034 uint32 TotalMem; 00035 00036 } DDMemMgr_Partition; 00037 00038 typedef struct DDMemMgr 00039 { 00040 uint32 TotalMem; 00041 uint32 FreeMem; 00042 DDMemMgr_Partition Partitions[DDMEMMGR_MAX_PARTITIONS]; 00043 } DDMemMgr; 00044 00045 //============================================================================ 00046 // DDMemMgr_Create 00047 //============================================================================ 00048 DDMemMgr *DDMemMgr_Create(uint32 Size) 00049 { 00050 DDMemMgr *MemMgr; 00051 00052 MemMgr = (DDMemMgr*)malloc(sizeof(DDMemMgr)); 00053 00054 if (!MemMgr) 00055 return NULL; 00056 00057 memset(MemMgr, 0, sizeof(DDMemMgr)); 00058 00059 MemMgr->TotalMem = Size; 00060 MemMgr->FreeMem = Size; 00061 00062 return MemMgr; 00063 } 00064 00065 //============================================================================ 00066 // DDMemMgr_Destroy 00067 //============================================================================ 00068 void DDMemMgr_Destroy(DDMemMgr *MemMgr) 00069 { 00070 assert(MemMgr); 00071 00072 free(MemMgr); 00073 } 00074 00075 //============================================================================ 00076 // DDMemMgr_Reset 00077 //============================================================================ 00078 void DDMemMgr_Reset(DDMemMgr *MemMgr) 00079 { 00080 int32 i; 00081 00082 assert(MemMgr); 00083 00084 MemMgr->FreeMem = MemMgr->TotalMem; 00085 00086 for (i=0; i<DDMEMMGR_MAX_PARTITIONS; i++) 00087 memset(&MemMgr->Partitions[i], 0, sizeof(DDMemMgr_Partition)); 00088 } 00089 00090 //============================================================================ 00091 // DDMemMgr_GetFreeMem 00092 //============================================================================ 00093 uint32 DDMemMgr_GetFreeMem(DDMemMgr *MemMgr) 00094 { 00095 assert(MemMgr); 00096 return MemMgr->FreeMem; 00097 } 00098 00099 //============================================================================ 00100 // DDMemMgr_PartitionCreate 00101 //============================================================================ 00102 DDMemMgr_Partition *DDMemMgr_PartitionCreate(DDMemMgr *MemMgr, uint32 Size) 00103 { 00104 int32 i; 00105 DDMemMgr_Partition *pPartition; 00106 00107 assert(MemMgr); 00108 00109 if (Size > MemMgr->FreeMem) 00110 return NULL; 00111 00112 pPartition = MemMgr->Partitions; 00113 00114 for (i=0; i< DDMEMMGR_MAX_PARTITIONS; i++, pPartition++) 00115 { 00116 if (!pPartition->Active) 00117 { 00118 assert(pPartition->TotalMem == 0); 00119 assert(pPartition->FreeMem == 0); 00120 00121 pPartition->TotalMem = Size; 00122 pPartition->FreeMem = Size; 00123 pPartition->Active = GE_TRUE; 00124 00125 MemMgr->FreeMem -= Size; 00126 00127 assert(MemMgr->FreeMem >= 0); 00128 00129 return pPartition; 00130 } 00131 } 00132 00133 return NULL; 00134 } 00135 00136 //============================================================================ 00137 // DDMemMgr_PartitionDestroy 00138 //============================================================================ 00139 void DDMemMgr_PartitionDestroy(DDMemMgr_Partition *Partition) 00140 { 00141 assert(Partition); 00142 assert(Partition->Active); 00143 00144 memset(Partition, 0, sizeof(DDMemMgr_Partition)); 00145 } 00146 00147 //============================================================================ 00148 // DDMemMgr_PartitionReset 00149 //============================================================================ 00150 void DDMemMgr_PartitionReset(DDMemMgr_Partition *Partition) 00151 { 00152 assert(Partition->Active); 00153 assert(Partition->FreeMem >= 0); 00154 00155 Partition->FreeMem = Partition->TotalMem; 00156 } 00157 00158 //============================================================================ 00159 // DDMemMgr_PArtitionGetTotalMem 00160 //============================================================================ 00161 uint32 DDMemMgr_PartitionGetTotalMem(DDMemMgr_Partition *Partition) 00162 { 00163 assert(Partition); 00164 assert(Partition->TotalMem >= 0); 00165 00166 return Partition->TotalMem; 00167 } 00168 00169 //============================================================================ 00170 // DDMemMgr_PArtitionGetFreeMem 00171 //============================================================================ 00172 uint32 DDMemMgr_PartitionGetFreeMem(DDMemMgr_Partition *Partition) 00173 { 00174 assert(Partition); 00175 assert(Partition->FreeMem >= 0); 00176 00177 return Partition->FreeMem; 00178 } 00179 00180 //============================================================================ 00181 // DDMemMgr_PartitionAllocMem 00182 //============================================================================ 00183 geBoolean DDMemMgr_PartitionAllocMem(DDMemMgr_Partition *Partition, uint32 Size) 00184 { 00185 assert(Partition->Active); 00186 00187 if (Partition->FreeMem < Size) 00188 return GE_FALSE; 00189 00190 Partition->FreeMem -= Size; 00191 00192 assert(Partition->FreeMem >= 0); 00193 00194 return GE_TRUE; 00195 }
1.3.2