Merge commit 'origin/draw-vbuf-interface'
[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 "util/u_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 "util/u_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 #define CALLOC_VARIANT_LENGTH_STRUCT(T,more_size) ((struct T *) CALLOC(1, sizeof(struct T) + more_size))
155
156
157 /**
158 * Return memory on given byte alignment
159 */
160 static INLINE void *
161 align_malloc(size_t bytes, uint alignment)
162 {
163 #if defined(HAVE_POSIX_MEMALIGN)
164 void *mem;
165 alignment = (alignment + (uint)sizeof(void*) - 1) & ~((uint)sizeof(void*) - 1);
166 if(posix_memalign(& mem, alignment, bytes) != 0)
167 return NULL;
168 return mem;
169 #else
170 char *ptr, *buf;
171
172 assert( alignment > 0 );
173
174 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
175 if (!ptr)
176 return NULL;
177
178 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
179 *(char **)(buf - sizeof(void *)) = ptr;
180
181 return buf;
182 #endif /* defined(HAVE_POSIX_MEMALIGN) */
183 }
184
185 /**
186 * Free memory returned by align_malloc().
187 */
188 static INLINE void
189 align_free(void *ptr)
190 {
191 #if defined(HAVE_POSIX_MEMALIGN)
192 FREE(ptr);
193 #else
194 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
195 void *realAddr = *cubbyHole;
196 FREE(realAddr);
197 #endif /* defined(HAVE_POSIX_MEMALIGN) */
198 }
199
200
201 /**
202 * Duplicate a block of memory.
203 */
204 static INLINE void *
205 mem_dup(const void *src, uint size)
206 {
207 void *dup = MALLOC(size);
208 if (dup)
209 memcpy(dup, src, size);
210 return dup;
211 }
212
213
214 /**
215 * Number of elements in an array.
216 */
217 #ifndef Elements
218 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
219 #endif
220
221
222 /**
223 * Offset of a field in a struct, in bytes.
224 */
225 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
226
227
228
229 #ifdef __cplusplus
230 }
231 #endif
232
233
234 #endif /* U_MEMORY_H */