~om26er/ubuntu/oneiric/nux/sru-819721

« back to all changes in this revision

Viewing changes to NuxCore/Memory.h

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#ifndef NMEMORY_H
 
24
#define NMEMORY_H
 
25
 
 
26
#define NUX_DEFAULT_ALIGNMENT 4
 
27
 
 
28
#define NUX_SAFE_DELETE(mem)    if(mem)             \
 
29
                                {                   \
 
30
                                    INLDELETE (mem);   \
 
31
                                    (mem) = 0;      \
 
32
                                }
 
33
#define NUX_SAFE_DELETE_ARRAY(mem_array)    if(mem_array)               \
 
34
                                            {                           \
 
35
                                                INLDELETEARRAY (mem_array);  \
 
36
                                                (mem_array) = 0;        \
 
37
                                            }
 
38
 
 
39
#define NUX_SAFE_FREE(mem)    if(mem)             \
 
40
{                   \
 
41
    std::free(mem);      \
 
42
    (mem) = 0;      \
 
43
}
 
44
 
 
45
#if defined(NUX_OS_WINDOWS)
 
46
    #define NUX_SYS_MEMORY_MALLOC(size)                     malloc(size)
 
47
    #define NUX_SYS_MEMORY_MEM_ALIGN(align, size)           NUX_SYS_MEMORY_MALLOC(size)
 
48
    #define NUX_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
 
49
    #define NUX_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
 
50
    #define NUX_SYS_MEMORY_FREE(ptr)                        free(ptr)
 
51
    #define NUX_SYS_MEMORY_PTR_SIZE(ptr)                    _msize(ptr)
 
52
#elif defined(NUX_OS_LINUX)
 
53
    #define NUX_SYS_MEMORY_MALLOC(size)                     malloc(size)
 
54
    #define NUX_SYS_MEMORY_MEM_ALIGN(align, size)           NUX_SYS_MEMORY_MALLOC(size)
 
55
    #define NUX_SYS_MEMORY_REALLOC(ptr, size)               realloc(ptr, size)
 
56
    #define NUX_SYS_MEMORY_REALLOC_ALIGN(ptr, size, align)  realloc(ptr, size)
 
57
    #define NUX_SYS_MEMORY_FREE(ptr)                        free(ptr)
 
58
    #define NUX_SYS_MEMORY_PTR_SIZE(ptr)                    0
 
59
#endif
 
60
 
 
61
namespace nux
 
62
{
 
63
 
 
64
  t_u32 Memcmp ( const void *Buf1, const void *Buf2, t_u32 Count );
 
65
 
 
66
  bool MemIsZero ( const void *V, t_size Count );
 
67
 
 
68
  void *Memmove ( void *Dest, const void *Src, t_size Count );
 
69
 
 
70
  void Memset ( void *Dest, t_s32 C, t_size Count );
 
71
 
 
72
  void Memzero ( void *Dest, t_size Count );
 
73
 
 
74
  void Memcpy ( void *Dest, const void *Src, t_size Count );
 
75
 
 
76
  void Memswap ( void *Ptr1, void *Ptr2, t_size Size );
 
77
 
 
78
//! Check that the alignment is a power of two
 
79
  bool IsMemoryAligned (void *data, t_u32 alignment);
 
80
 
 
81
  void *Malloc (t_size Count, t_u32 Alignment = NUX_DEFAULT_ALIGNMENT);
 
82
  void *Realloc (void *Original, t_size Count, t_u32 Alignment = NUX_DEFAULT_ALIGNMENT);
 
83
 
 
84
}
 
85
 
 
86
inline void inlFree ( void *Original )
 
87
{
 
88
  return free ( Original );
 
89
}
 
90
 
 
91
/// Memory operation defines
 
92
#define NUX_MEMOP_ALLOC           1
 
93
#define NUX_MEMOP_NEW             2
 
94
#define NUX_MEMOP_NEWARRAY        3
 
95
#define NUX_MEMOP_FREE            4
 
96
#define NUX_MEMOP_DELETE          5
 
97
#define NUX_MEMOP_DELETEARRAY     6
 
98
 
 
99
 
 
100
#define NUX_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, File, Line, FunctionName)    \
 
101
        new(Memory::MemHelperAlloc< ClassName >(NUX_MEMOP_NEW,                 \
 
102
                    1,                                                              \
 
103
                    Allocator,                                                      \
 
104
                    ParentPtr,                                                      \
 
105
                    Comment,                                                        \
 
106
                    #ClassName,                                                     \
 
107
                    File,                                                           \
 
108
                    Line,                                                           \
 
109
                    FunctionName)) ClassName
 
110
 
 
111
 
 
112
 
 
113
#define NUX_NEW(Allocator, ClassName, Comment, ParentPtr)     NUX_NEW_EXPLICIT(Allocator, ClassName, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
 
114
#define inlNew(ClassName, Comment, ParentPtr) NUX_NEW(GetDefaultMemoryAllocator(), ClassName, Comment, ParentPtr)
 
115
 
 
116
#define NUX_DELETE_EXPLICIT(ptrObject, File, Line, FunctionName)                \
 
117
        Memory::MemHelperDelete(NUX_MEMOP_DELETE, (ptrObject),                 \
 
118
                          File,                                                     \
 
119
                          Line,                                                     \
 
120
                          FunctionName);
 
121
#define NUX_DELETE(ptrObject)     NUX_DELETE_EXPLICIT(ptrObject, __FILE__, __LINE__, __FUNCTION__)
 
122
#define inlDelete(ptrObject) NUX_DELETE(ptrObject)
 
123
 
 
124
 
 
125
 
 
126
#define NUX_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)      \
 
127
        Memory::MemHelperAlloc< ObjectType >(NUX_MEMOP_ALLOC,                                              \
 
128
                                              Count,                                                            \
 
129
                                              Allocator,                                                        \
 
130
                                              ParentPtr,                                                        \
 
131
                                              Comment,                                                          \
 
132
                                              #ObjectType,                                                      \
 
133
                                              File,                                                             \
 
134
                                              Line,                                                             \
 
135
                                              FunctionName)
 
136
 
 
137
 
 
138
#define NUX_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName) NUX_ALLOC_EXPLICIT(Allocator, ObjectType, Count, Comment, ParentPtr, File, Line, FunctionName)
 
139
#define inlMMAlloc(Allocator, ObjectType, Count, Comment, ParentPtr) NUX_ALLOC(Allocator, ObjectType, Count, Comment, ParentPtr, __FILE__, __LINE__, __FUNCTION__)
 
140
 
 
141
 
 
142
#define NUX_FREE_EXPLICIT(Allocator, Ptr, File, Line)       \
 
143
        Memory::MemHelperFastDelete( NUX_MEMOP_FREE,       \
 
144
                            Allocator,                          \
 
145
                            Ptr,                                \
 
146
                            File,                               \
 
147
                            Line);
 
148
 
 
149
#define NUX_FREE(Allocator, Ptr)     NUX_FREE_EXPLICIT(Allocator, Ptr, __FILE__, __LINE__)
 
150
#define inlMMFree(Allocator, Ptr)      NUX_FREE(Allocator, Ptr)
 
151
 
 
152
#endif // NMEMORY_H