gallium: Fix typo in define name.
[mesa.git] / src / gallium / auxiliary / util / u_memory.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Memory functions
31 */
32
33
34 #ifndef U_MEMORY_H
35 #define U_MEMORY_H
36
37
38 #include "util/u_pointer.h"
39 #include "pipe/p_debug.h"
40
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46
47 /* Define ENOMEM for WINCE */
48 #if (_WIN32_WCE < 600)
49 #ifndef ENOMEM
50 #define ENOMEM 12
51 #endif
52 #endif
53
54
55 #if defined(PIPE_OS_WINDOWS) && defined(DEBUG)
56
57 /* memory debugging */
58
59 #include "pipe/p_debug.h"
60
61 #define MALLOC( _size ) \
62 debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
63 #define CALLOC( _count, _size ) \
64 debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
65 #define FREE( _ptr ) \
66 debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
67 #define REALLOC( _ptr, _old_size, _size ) \
68 debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
69
70 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
71
72 void * __stdcall
73 EngAllocMem(
74 unsigned long Flags,
75 unsigned long MemSize,
76 unsigned long Tag );
77
78 void __stdcall
79 EngFreeMem(
80 void *Mem );
81
82 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
83 #define _FREE( _ptr ) EngFreeMem( _ptr )
84
85 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
86
87 void *
88 ExAllocatePool(
89 unsigned long PoolType,
90 size_t NumberOfBytes);
91
92 void
93 ExFreePool(void *P);
94
95 #define MALLOC(_size) ExAllocatePool(0, _size)
96 #define _FREE(_ptr) ExFreePool(_ptr)
97
98 #else
99
100 #define MALLOC( SIZE ) malloc( SIZE )
101 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
102 #define FREE( PTR ) free( PTR )
103 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
104
105 #endif
106
107
108 #ifndef CALLOC
109 static INLINE void *
110 CALLOC( unsigned count, unsigned size )
111 {
112 void *ptr = MALLOC( count * size );
113 if( ptr ) {
114 memset( ptr, 0, count * size );
115 }
116 return ptr;
117 }
118 #endif /* !CALLOC */
119
120 #ifndef FREE
121 static INLINE void
122 FREE( void *ptr )
123 {
124 if( ptr ) {
125 _FREE( ptr );
126 }
127 }
128 #endif /* !FREE */
129
130 #ifndef REALLOC
131 static INLINE void *
132 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
133 {
134 void *new_ptr = NULL;
135
136 if (new_size != 0) {
137 unsigned copy_size = old_size < new_size ? old_size : new_size;
138 new_ptr = MALLOC( new_size );
139 if (new_ptr && old_ptr && copy_size) {
140 memcpy( new_ptr, old_ptr, copy_size );
141 }
142 }
143
144 FREE( old_ptr );
145 return new_ptr;
146 }
147 #endif /* !REALLOC */
148
149
150 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
151
152 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
153
154
155 /**
156 * Return memory on given byte alignment
157 */
158 static INLINE void *
159 align_malloc(size_t bytes, uint alignment)
160 {
161 #if defined(HAVE_POSIX_MEMALIGN)
162 void *mem;
163 alignment = (alignment + (uint)sizeof(void*) - 1) & ~((uint)sizeof(void*) - 1);
164 if(posix_memalign(& mem, alignment, bytes) != 0)
165 return NULL;
166 return mem;
167 #else
168 char *ptr, *buf;
169
170 assert( alignment > 0 );
171
172 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
173 if (!ptr)
174 return NULL;
175
176 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
177 *(char **)(buf - sizeof(void *)) = ptr;
178
179 return buf;
180 #endif /* defined(HAVE_POSIX_MEMALIGN) */
181 }
182
183 /**
184 * Free memory returned by align_malloc().
185 */
186 static INLINE void
187 align_free(void *ptr)
188 {
189 #if defined(HAVE_POSIX_MEMALIGN)
190 FREE(ptr);
191 #else
192 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
193 void *realAddr = *cubbyHole;
194 FREE(realAddr);
195 #endif /* defined(HAVE_POSIX_MEMALIGN) */
196 }
197
198
199 /**
200 * Duplicate a block of memory.
201 */
202 static INLINE void *
203 mem_dup(const void *src, uint size)
204 {
205 void *dup = MALLOC(size);
206 if (dup)
207 memcpy(dup, src, size);
208 return dup;
209 }
210
211
212 /**
213 * Number of elements in an array.
214 */
215 #ifndef Elements
216 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
217 #endif
218
219
220 /**
221 * Offset of a field in a struct, in bytes.
222 */
223 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
224
225
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231
232 #endif /* U_MEMORY_H */