Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# deMemory.hpp

Go to the documentation of this file.

    00001 ///////////////////////////////////////////////////////////////////////////////
    00002 /// @file deMemory.hpp
    00003 ///
    00004 /// @brief Destiny3D Memory Manager
    00005 ///
    00006 /// @author Lightning
    00007 ///
    00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
    00009 /// contents of this file is subject to the Destiny3D Member License which
    00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
    00011 ///
    00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
    00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
    00014 /// for any particular purpose.
    00015 ///
    00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
    00017 ///
    00018 /// <hr>
    00019 ///                                 Change History
    00020 /// <hr>
    00021 ///
    00022 /// @date Feb 2001
    00023 /// @author Lightning
    00024 /// @remarks Creation
    00025 ///
    00026 /// @date Oct 2001
    00027 /// @author Hootie
    00028 /// @remarks Started Conversion to C++
    00029 ///
    00030 /// @date Oct 2002
    00031 /// @author Lightning
    00032 /// @remarks Scrapped old, started from scratch
    00033 ///
    00034 ///////////////////////////////////////////////////////////////////////////////
    00035
    00036 #ifndef DEMEMORY_HPP
    00037 #define DEMEMORY_HPP
    00038
    00039 //include the headers
    00040 #include "deGlobalTypes.hpp"
    00041 #include "deMemory.hpp"
    00042
    00043 #if defined(DEMEMORY_DLL_EXPORTS) || defined(DESTINY3D_EXPORT_ALL)
    00044 #   define DEMEMORY_API DEDLL_EXPORT
    00045 #elif defined(DESTINY3D_STATIC_LINK)
    00046 #   define DEMEMORY_API
    00047 #else
    00048 #   define DEMEMORY_API DEDLL_IMPORT
    00049 #endif
    00050
    00051 //class IdeMemoryManager
    00052 DE3D_INTERFACE_(IdeMemoryManager)
    00053 {
    00054     protected:
    00055         ~IdeMemoryManager() {}
    00056
    00057     public:
    00058         enum MallocType
    00059         {
    00060             RegMalloc = 1,
    00061             Calloc,
    00062             New,
    00063             NewArray
    00064         };
    00065
    00066         enum FreeType
    00067         {
    00068             RegFree = 1,
    00069             Delete,
    00070             DeleteArray
    00071         };
    00072
    00073         ///function to see if we initialized properly
    00074         virtual deBoolean IsInitialized() = 0;
    00075
    00076         //all the functions that are used for overridding
    00077         virtual void * Malloc(unsigned int Size, MallocType Type, const char *File, long Line) = 0;
    00078         virtual void Free(void *Ptr, FreeType Type, const char *File, long Line) = 0;
    00079         virtual void * MemCpy(void *To, const void *From, unsigned int Size, const char *File, long Line) = 0;
    00080         virtual void * MemSet(void *To, int Value, unsigned int Size, const char *File, long Line) = 0;
    00081 };
    00082
    00083 //export our function for getting a copy
    00084 extern "C"
    00085 {
    00086     DEMEMORY_API IdeMemoryManager *IdeMemory_GetManager(char *ConfigFile);
    00087 }
    00088
    00089 //our static variable for the class that dicates the functions
    00090 #if defined(DEMEMORY_DLL_EXPORTS) || defined(DESTINY3D_EXPORT_ALL)
    00091     //if building the memory manager, init it to null
    00092     static IdeMemoryManager *deMemory = NULL;
    00093 #else
    00094     static IdeMemoryManager *deMemory = IdeMemory_GetManager(NULL);
    00095 #endif
    00096
    00097 /*
    00098 //all the override functions
    00099 #undef malloc
    00100 #define malloc(Size) deMemory->Malloc(Size, IdeMemoryManager::RegMalloc, __FILE__, __LINE__);
    00101
    00102 #undef calloc
    00103 #define calloc(Size, Count) deMemory->Malloc(Size*Count, IdeMemoryManager::Calloc, __FILE__, __LINE__);
    00104
    00105 #undef free
    00106 #define free(Ptr) deMemory->Free(Ptr, IdeMemoryManager::RegFree, __FILE__, __LINE__);
    00107
    00108 #undef memcpy
    00109 #define memcpy(To, From, Size) deMemory->MemCpy(To, From, Size, __FILE__, __LINE__);
    00110
    00111 #undef memset
    00112 #define memset(To, Value, Size) deMemory->MemSet(To, Value, Size, __FILE__, __LINE__);
    00113
    00114 //the functions to override new, new[]
    00115 static inline void* operator new(unsigned int Size, const char *File, long Line){ return deMemory->Malloc(Size, IdeMemoryManager::New, File, Line); }
    00116 static inline void* operator new[](unsigned int Size, const char *File, long Line){ return deMemory->Malloc(Size, IdeMemoryManager::NewArray, File, Line); }
    00117
    00118 //redefine new so we get file and line
    00119 #define new new(__FILE__, __LINE__)
    00120
    00121 //now, due to being unable to overwride delete[] the same way you can with new[], delete and delete[] have to be done differently to allow
    00122 //the file and line to be obtained upon a delete and still have delete[] work when compiling
    00123 #ifdef _DEBUG
    00124     //debug version. Debug does stack checking so we cant just push/pop values
    00125     static inline void operator delete(void *Pointer)
    00126     {
    00127         char *File; long Line;
    00128         __asm
    00129         {
    00130             mov ebx, [esp-4]
    00131             mov [File], ebx
    00132             mov ebx, [esp-8]
    00133             mov [Line], ebx
    00134         }
    00135         deMemory->Free(Pointer, IdeMemoryManager::Delete, File, Line);
    00136     }
    00137
    00138     static inline void operator delete[](void *Pointer)
    00139     {
    00140         char *File; long Line;
    00141         __asm
    00142         {
    00143             //retrieve the values
    00144             mov ebx, [esp-4]
    00145             mov [File], ebx
    00146             mov ebx, [esp-8]
    00147             mov [Line], ebx
    00148         }
    00149         deMemory->Free(Pointer, IdeMemoryManager::DeleteArray, File, Line);
    00150     }
    00151
    00152     static inline void PushDelStack(const char *File, long Line)
    00153     {
    00154         __asm
    00155         {
    00156             //move the values higher into the stack
    00157             mov ebx, [File]
    00158             mov [esp-8], ebx
    00159             mov ebx, [Line]
    00160             mov [esp-12], ebx
    00161         }
    00162         return;
    00163     }
    00164 #else
    00165     //release mode version
    00166     static inline void operator delete(void *Ptr)
    00167     {
    00168         char *File; long Line;
    00169         //a better/faster way would be just to move the other 2 params so they go into the File and Line
    00170         //but then you risk release not actually creating a stack for them and throwing a warning
    00171         __asm
    00172         {
    00173             //take them off the stack
    00174             pop File;
    00175             pop Line;
    00176         }
    00177         deMemory->Free(Ptr, IdeMemoryManager::Delete, File, Line);
    00178     }
    00179
    00180     static inline void operator delete[](void *Ptr)
    00181     {
    00182         char *File; long Line;
    00183         //a better/faster way would be just to move the other 2 params so they go into the File and Line
    00184         //but then you risk release not actually creating a stack for them and throwing a warning
    00185         __asm
    00186         {
    00187             //take them off the stack
    00188             pop File;
    00189             pop Line;
    00190         }
    00191         deMemory->Free(Ptr, IdeMemoryManager::DeleteArray, File, Line);
    00192     }
    00193
    00194     //the function to allow delete[] to work
    00195     static inline void PushDelStack(const char *File, long Line)
    00196     {
    00197         __asm
    00198         {
    00199             //put them on the stack
    00200             push Line;
    00201             push File;
    00202         }
    00203         return;
    00204     }
    00205
    00206 #endif
    00207
    00208 //the special define of delete so we can obtain file and line
    00209 #define delete PushDelStack(__FILE__, __LINE__); delete
    00210 */
    00211
    00212 #endif