Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Mem/Alloc.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_MEM_ALLOC_H
00029 #define SAF_MEM_ALLOC_H
00030 
00031 #include <new>
00032 #include <stdlib.h>
00033 #include "../Core.h"
00034 #include "../Type.h"
00035 #include "../SourceLocation.h"
00036 
00037 // @todo Remove this with GCC 4.6
00038 #ifdef SAF_PLATFORM_COMPILER_GCC
00039     #define nullptr 0
00040 #endif
00041 
00042 // Saf memory allocation
00043 #define SAF_NEW new (SAF_SOURCE_LOCATION)
00044 
00045 // Saf overloaded operators new and delete
00046 SAF_DLL_EXPORT void *operator new(size_t nSize, const Saf::SourceLocation &loc);
00047 SAF_DLL_EXPORT void *operator new[](size_t nSize, const Saf::SourceLocation &loc);
00048 SAF_DLL_EXPORT void operator delete(void *ptr, const Saf::SourceLocation &loc);
00049 SAF_DLL_EXPORT void operator delete[](void *ptr, const Saf::SourceLocation &loc);
00050 
00051 namespace Saf
00052 {
00057     namespace Mem
00058     {        
00060         // New and Delete
00062 
00064         template <class T>
00065         void OperatorNew(T *&p, const Saf::SourceLocation &loc)
00066         {
00067             p = (T *)::operator new(sizeof(T), loc);
00068         }
00069 
00071         template <class T>
00072         void OperatorNew(T *&p, Size n, const Saf::SourceLocation &loc)
00073         {
00074             p = (T *)::operator new[](n * sizeof(T), loc);
00075         }
00076 
00078         template <class T>
00079         void OperatorDelete(T *&p)
00080         {
00081             ::operator delete(p);
00082             p = nullptr;
00083         }
00084 
00086         template <class T>
00087         void OperatorDelete(T *&p, Size n)
00088         {
00089             ::operator delete[](p);
00090             p = nullptr;
00091         }
00092 
00094         template <class T>
00095         void Delete(T *&p)
00096         {
00097             delete p;
00098             p = nullptr;
00099         }
00100 
00102         template <class T>
00103         void Delete(T *&p, Size n)
00104         {
00105             delete [] p;
00106             p = nullptr;
00107         }
00108     }
00109 }
00110 
00111 #endif