Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

GMemMgr.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  GMemMgr.c                                                                           */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: Mini memory manager for glide                                          */
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 "GMemMgr.h"
00026 #include "BaseType.h"
00027 #include "Glide.h"
00028 
00029 //========================================================================================================
00030 //========================================================================================================
00031 typedef struct GMemMgr
00032 {
00033         GrChipID_t              Tmu;                                            // Tmu that this memory manager is in charge of
00034 
00035         uint32                  MinAddress;                                     // Min address allowed to use for this mgr
00036         uint32                  MaxAddress;                                     // Max address allowed to use for this mgr
00037 
00038         uint32                  CurrentAddress;                         
00039 } GMemMgr;
00040 
00041 //========================================================================================================
00042 //      GMemMgr_Create
00043 //      Create a memmgr on a tmu, and assumes it has all the data available to it...
00044 //========================================================================================================
00045 GMemMgr *GMemMgr_Create(GrChipID_t Tmu, uint32 MinAddress, uint32 MaxAddress)
00046 {
00047         GMemMgr *MemMgr;
00048 
00049         MemMgr = malloc(sizeof(GMemMgr));
00050 
00051         if (!MemMgr)
00052                 return NULL;
00053 
00054         // Clear the memmgr 
00055         memset(MemMgr, 0, sizeof(GMemMgr));
00056 
00057         // Set the default values...
00058         MemMgr->Tmu = Tmu;
00059 
00060         MemMgr->MinAddress = MinAddress;
00061         MemMgr->MaxAddress = MaxAddress;
00062 
00063         MemMgr->CurrentAddress = MinAddress;
00064 
00065         return MemMgr;
00066 }
00067 
00068 //========================================================================================================
00069 //      GMemMgr_Destroy
00070 //========================================================================================================
00071 void GMemMgr_Destroy(GMemMgr *MemMgr)
00072 {
00073         assert(MemMgr);
00074 
00075         free(MemMgr);
00076 }
00077 
00078 //========================================================================================================
00079 //      GMemMgr_GetTotalMemory
00080 //========================================================================================================
00081 uint32 GMemMgr_GetTotalMemory(GMemMgr *MemMgr)
00082 {
00083         return (MemMgr->MaxAddress - MemMgr->MinAddress);
00084 }
00085 
00086 //========================================================================================================
00087 //      GMemMgr_GetFreeMemory
00088 //========================================================================================================
00089 uint32 GMemMgr_GetFreeMemory(GMemMgr *MemMgr)
00090 {
00091         return (MemMgr->MaxAddress - MemMgr->CurrentAddress);
00092 }
00093 
00094 //========================================================================================================
00095 //      GMemMgr_AllocMem
00096 //========================================================================================================
00097 geBoolean GMemMgr_AllocMem(GMemMgr *MemMgr, uint32 Size, uint32 *Address)
00098 {
00099         #define TWO_MEG         (1024*1024*2)
00100         
00101         uint32          StartAddr;
00102 
00103         assert(MemMgr);
00104         assert(GMemMgr_GetFreeMemory(MemMgr) >= Size);
00105 
00106         if (GMemMgr_GetFreeMemory(MemMgr) < Size)
00107                 return GE_FALSE;
00108 
00109         if (((MemMgr->CurrentAddress+Size)%TWO_MEG) < Size)                     // Align on 2 meg boundry...
00110         {
00111                 MemMgr->CurrentAddress = ((MemMgr->CurrentAddress / TWO_MEG)+1) * TWO_MEG;
00112                 if (MemMgr->CurrentAddress+Size >= MemMgr->MaxAddress)
00113                         return GE_FALSE;
00114         }
00115         
00116         while ((MemMgr->CurrentAddress & 7) != 0)
00117         {
00118                 MemMgr->CurrentAddress++;
00119                 if (MemMgr->CurrentAddress+Size >= MemMgr->MaxAddress)
00120                         return GE_FALSE;
00121         }
00122 
00123         StartAddr = MemMgr->CurrentAddress;
00124 
00125         MemMgr->CurrentAddress += Size;
00126 
00127         *Address = StartAddr;
00128 
00129         return GE_TRUE;
00130 }
00131 
00132 //========================================================================================================
00133 //      GMemMgr_GetTmu
00134 //========================================================================================================
00135 GrChipID_t GMemMgr_GetTmu(GMemMgr *MemMgr)
00136 {
00137         assert(MemMgr);
00138 
00139         return MemMgr->Tmu;
00140 }
00141 
00142 //========================================================================================================
00143 //      GMemMgr_Reset
00144 //========================================================================================================
00145 void GMemMgr_Reset(GMemMgr *MemMgr)
00146 {
00147         MemMgr->CurrentAddress = MemMgr->MinAddress;
00148 }

Generated on Tue Sep 30 12:35:53 2003 for GTestAndEngine by doxygen 1.3.2