~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NMemory.h

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef NMEMORY_H
 
2
#define NMEMORY_H
 
3
 
 
4
#define INL_DEFAULT_ALIGNMENT 4
 
5
 
 
6
#define INL_SAFE_DELETE(mem)    if(mem)             \
 
7
                                {                   \
 
8
                                    INLDELETE (mem);   \
 
9
                                    (mem) = 0;      \
 
10
                                }   
 
11
#define INL_SAFE_DELETE_ARRAY(mem_array)    if(mem_array)               \
 
12
                                            {                           \
 
13
                                                INLDELETEARRAY (mem_array);  \
 
14
                                                (mem_array) = 0;        \
 
15
                                            }   
 
16
 
 
17
#define INL_SAFE_FREE(mem)    if(mem)             \
 
18
{                   \
 
19
    std::free(mem);      \
 
20
    (mem) = 0;      \
 
21
}   
 
22
 
 
23
#if defined(INL_OS_WINDOWS)
 
24
    #define INL_SYS_MEMORY_MALLOC(size)                     malloc(size)
 
25
    #define INL_SYS_MEMORY_MEM_ALIGN(align, size)           INL_SYS_MEMORY_MALLOC(size)
 
26
    #define INL_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
 
27
    #define INL_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
 
28
    #define INL_SYS_MEMORY_FREE(ptr)                        free(ptr)
 
29
    #define INL_SYS_MEMORY_PTR_SIZE(ptr)                    _msize(ptr)
 
30
#elif defined(INL_OS_LINUX)
 
31
    #define INL_SYS_MEMORY_MALLOC(size)                     malloc(size)
 
32
    #define INL_SYS_MEMORY_MEM_ALIGN(align, size)           INL_SYS_MEMORY_MALLOC(size)
 
33
    #define INL_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
 
34
    #define INL_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
 
35
    #define INL_SYS_MEMORY_FREE(ptr)                        free(ptr)
 
36
    #define INL_SYS_MEMORY_PTR_SIZE(ptr)                    0
 
37
#elif INL_PS3
 
38
    #define INL_SYS_MEMORY_MALLOC(size)                     malloc(size)
 
39
    #define INL_SYS_MEMORY_MEM_ALIGN(align, size)           memalign(align, size)
 
40
    #define INL_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
 
41
    #define INL_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  reallocalign(ptr, size, align)
 
42
    #define INL_SYS_MEMORY_FREE(ptr)                        free(ptr)
 
43
    #define INL_SYS_MEMORY_PTR_SIZE(ptr)                    0
 
44
#endif
 
45
 
 
46
NAMESPACE_BEGIN
 
47
 
 
48
t_u32 Memcmp( const void* Buf1, const void* Buf2, t_u32 Count );
 
49
 
 
50
bool MemIsZero( const void* V, t_size Count );
 
51
 
 
52
void* Memmove( void* Dest, const void* Src, t_size Count );
 
53
 
 
54
void Memset( void* Dest, t_s32 C, t_size Count );
 
55
 
 
56
void Memzero( void* Dest, t_size Count );
 
57
 
 
58
void Memcpy( void* Dest, const void* Src, t_size Count );
 
59
 
 
60
void Memswap( void* Ptr1, void* Ptr2, t_size Size );
 
61
 
 
62
//! Check that the alignment is a power of two
 
63
bool IsMemoryAligned(void* data, t_u32 alignment);
 
64
 
 
65
void* Malloc(t_size Count, t_u32 Alignment = INL_DEFAULT_ALIGNMENT);
 
66
void* Realloc(void* Original, t_size Count, t_u32 Alignment = INL_DEFAULT_ALIGNMENT);
 
67
 
 
68
NAMESPACE_END
 
69
 
 
70
    inline void inlFree( void* Original )
 
71
    {
 
72
        return free( Original );
 
73
    }
 
74
 
 
75
    /// Memory operation defines
 
76
    #define INL_MEMOP_ALLOC           1
 
77
    #define INL_MEMOP_NEW             2
 
78
    #define INL_MEMOP_NEWARRAY        3
 
79
    #define INL_MEMOP_FREE            4
 
80
    #define INL_MEMOP_DELETE          5
 
81
    #define INL_MEMOP_DELETEARRAY     6
 
82
 
 
83
 
 
84
    #define INL_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, File, Line, FunctionName)    \
 
85
        new(Memory::MemHelperAlloc< ClassName >(INL_MEMOP_NEW,                 \
 
86
                    1,                                                              \
 
87
                    Allocator,                                                      \
 
88
                    ParentPtr,                                                      \
 
89
                    Comment,                                                        \
 
90
                    #ClassName,                                                     \
 
91
                    File,                                                           \
 
92
                    Line,                                                           \
 
93
                    FunctionName)) ClassName
 
94
 
 
95
 
 
96
 
 
97
    #define INL_NEW(Allocator, ClassName, Comment, ParentPtr)     INL_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
 
98
    #define inlNew(ClassName, Comment, ParentPtr) INL_NEW(GetDefaultMemoryAllocator(), ClassName, Comment, ParentPtr)
 
99
 
 
100
    #define INL_DELETE_EXPLICIT(ptrObject, File, Line, FunctionName)                \
 
101
        Memory::MemHelperDelete(INL_MEMOP_DELETE, (ptrObject),                 \
 
102
                          File,                                                     \
 
103
                          Line,                                                     \
 
104
                          FunctionName);
 
105
    #define INL_DELETE(ptrObject)     INL_DELETE_EXPLICIT(ptrObject, __FILE__, __LINE__, __FUNCTION__)
 
106
    #define inlDelete(ptrObject) INL_DELETE(ptrObject)
 
107
 
 
108
 
 
109
 
 
110
    #define INL_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)      \
 
111
        Memory::MemHelperAlloc< ObjectType >(INL_MEMOP_ALLOC,                                              \
 
112
                                              Count,                                                            \
 
113
                                              Allocator,                                                        \
 
114
                                              ParentPtr,                                                        \
 
115
                                              Comment,                                                          \
 
116
                                              #ObjectType,                                                      \
 
117
                                              File,                                                             \
 
118
                                              Line,                                                             \
 
119
                                              FunctionName)
 
120
 
 
121
 
 
122
    #define INL_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName) INL_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)
 
123
    #define inlMMAlloc(Allocator, ObjectType, Count, Comment, ParentPtr) INL_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
 
124
 
 
125
 
 
126
    #define INL_FREE_EXPLICIT(Allocator, Ptr, File, Line)       \
 
127
        Memory::MemHelperFastDelete( INL_MEMOP_FREE,       \
 
128
                            Allocator,                          \
 
129
                            Ptr,                                \
 
130
                            File,                               \
 
131
                            Line);
 
132
 
 
133
    #define INL_FREE(Allocator, Ptr)     INL_FREE_EXPLICIT(Allocator, Ptr, __FILE__, __LINE__)
 
134
    #define inlMMFree(Allocator, Ptr)      INL_FREE(Allocator, Ptr)
 
135
 
 
136
#endif // NMEMORY_H