nux-0.9.48

NuxCore/Memory.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef NMEMORY_H
00024 #define NMEMORY_H
00025 
00026 #define NUX_DEFAULT_ALIGNMENT 4
00027 
00028 #define NUX_SAFE_DELETE(mem)    if(mem)             \
00029                                 {                   \
00030                                     INLDELETE (mem);   \
00031                                     (mem) = 0;      \
00032                                 }
00033 #define NUX_SAFE_DELETE_ARRAY(mem_array)    if(mem_array)               \
00034                                             {                           \
00035                                                 INLDELETEARRAY (mem_array);  \
00036                                                 (mem_array) = 0;        \
00037                                             }
00038 
00039 #define NUX_SAFE_FREE(mem)    if(mem)             \
00040 {                   \
00041     std::free(mem);      \
00042     (mem) = 0;      \
00043 }
00044 
00045 #if defined(NUX_OS_WINDOWS)
00046     #define NUX_SYS_MEMORY_MALLOC(size)                     malloc(size)
00047     #define NUX_SYS_MEMORY_MEM_ALIGN(align, size)           NUX_SYS_MEMORY_MALLOC(size)
00048     #define NUX_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
00049     #define NUX_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
00050     #define NUX_SYS_MEMORY_FREE(ptr)                        free(ptr)
00051     #define NUX_SYS_MEMORY_PTR_SIZE(ptr)                    _msize(ptr)
00052 #elif defined(NUX_OS_LINUX)
00053     #define NUX_SYS_MEMORY_MALLOC(size)                     malloc(size)
00054     #define NUX_SYS_MEMORY_MEM_ALIGN(align, size)           NUX_SYS_MEMORY_MALLOC(size)
00055     #define NUX_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
00056     #define NUX_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
00057     #define NUX_SYS_MEMORY_FREE(ptr)                        free(ptr)
00058     #define NUX_SYS_MEMORY_PTR_SIZE(ptr)                    0
00059 #endif
00060 
00061 namespace nux
00062 {
00063 
00064   t_u32 Memcmp ( const void *Buf1, const void *Buf2, t_u32 Count );
00065 
00066   bool MemIsZero ( const void *V, t_size Count );
00067 
00068   void *Memmove ( void *Dest, const void *Src, t_size Count );
00069 
00070   void Memset ( void *Dest, t_s32 C, t_size Count );
00071 
00072   void Memzero ( void *Dest, t_size Count );
00073 
00074   void Memcpy ( void *Dest, const void *Src, t_size Count );
00075 
00076   void Memswap ( void *Ptr1, void *Ptr2, t_size Size );
00077 
00079   bool IsMemoryAligned (void *data, t_u32 alignment);
00080 
00081   void *Malloc (t_size Count, t_u32 Alignment = NUX_DEFAULT_ALIGNMENT);
00082   void *Realloc (void *Original, t_size Count, t_u32 Alignment = NUX_DEFAULT_ALIGNMENT);
00083 
00084 }
00085 
00086 inline void inlFree ( void *Original )
00087 {
00088   return free ( Original );
00089 }
00090 
00092 #define NUX_MEMOP_ALLOC           1
00093 #define NUX_MEMOP_NEW             2
00094 #define NUX_MEMOP_NEWARRAY        3
00095 #define NUX_MEMOP_FREE            4
00096 #define NUX_MEMOP_DELETE          5
00097 #define NUX_MEMOP_DELETEARRAY     6
00098 
00099 
00100 #define NUX_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, File, Line, FunctionName)    \
00101         new(Memory::MemHelperAlloc< ClassName >(NUX_MEMOP_NEW,                 \
00102                     1,                                                              \
00103                     Allocator,                                                      \
00104                     ParentPtr,                                                      \
00105                     Comment,                                                        \
00106                     #ClassName,                                                     \
00107                     File,                                                           \
00108                     Line,                                                           \
00109                     FunctionName)) ClassName
00110 
00111 
00112 
00113 #define NUX_NEW(Allocator, ClassName, Comment, ParentPtr)     NUX_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
00114 #define inlNew(ClassName, Comment, ParentPtr) NUX_NEW(GetDefaultMemoryAllocator(), ClassName, Comment, ParentPtr)
00115 
00116 #define NUX_DELETE_EXPLICIT(ptrObject, File, Line, FunctionName)                \
00117         Memory::MemHelperDelete(NUX_MEMOP_DELETE, (ptrObject),                 \
00118                           File,                                                     \
00119                           Line,                                                     \
00120                           FunctionName);
00121 #define NUX_DELETE(ptrObject)     NUX_DELETE_EXPLICIT(ptrObject, __FILE__, __LINE__, __FUNCTION__)
00122 #define inlDelete(ptrObject) NUX_DELETE(ptrObject)
00123 
00124 
00125 
00126 #define NUX_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)      \
00127         Memory::MemHelperAlloc< ObjectType >(NUX_MEMOP_ALLOC,                                              \
00128                                               Count,                                                            \
00129                                               Allocator,                                                        \
00130                                               ParentPtr,                                                        \
00131                                               Comment,                                                          \
00132                                               #ObjectType,                                                      \
00133                                               File,                                                             \
00134                                               Line,                                                             \
00135                                               FunctionName)
00136 
00137 
00138 #define NUX_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName) NUX_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)
00139 #define inlMMAlloc(Allocator, ObjectType, Count, Comment, ParentPtr) NUX_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
00140 
00141 
00142 #define NUX_FREE_EXPLICIT(Allocator, Ptr, File, Line)       \
00143         Memory::MemHelperFastDelete( NUX_MEMOP_FREE,       \
00144                             Allocator,                          \
00145                             Ptr,                                \
00146                             File,                               \
00147                             Line);
00148 
00149 #define NUX_FREE(Allocator, Ptr)     NUX_FREE_EXPLICIT(Allocator, Ptr, __FILE__, __LINE__)
00150 #define inlMMFree(Allocator, Ptr)      NUX_FREE(Allocator, Ptr)
00151 
00152 #endif // NMEMORY_H