Merge branch 'master' into asm-shader-rework-1
[profile/ivi/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
104 static INLINE void *
105 _REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
106 {
107    (void) old_size;
108    return realloc(old_ptr, new_size);
109 }
110 #define REALLOC( a, b, c ) _REALLOC( a, b, c )
111 #endif
112
113
114 #ifndef CALLOC
115 static INLINE void *
116 CALLOC( unsigned count, unsigned size )
117 {
118    void *ptr = MALLOC( count * size );
119    if( ptr ) {
120       memset( ptr, 0, count * size );
121    }
122    return ptr;
123 }
124 #endif /* !CALLOC */
125
126 #ifndef FREE
127 static INLINE void
128 FREE( void *ptr )
129 {
130    if( ptr ) {
131       _FREE( ptr );
132    }
133 }
134 #endif /* !FREE */
135
136 #ifndef REALLOC
137 static INLINE void *
138 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
139 {
140    void *new_ptr = NULL;
141
142    if (new_size != 0) {
143       unsigned copy_size = old_size < new_size ? old_size : new_size;
144       new_ptr = MALLOC( new_size );
145       if (new_ptr && old_ptr && copy_size) {
146          memcpy( new_ptr, old_ptr, copy_size );
147       }
148    }
149
150    FREE( old_ptr );
151    return new_ptr;
152 }
153 #endif /* !REALLOC */
154
155
156 #define MALLOC_STRUCT(T)   (struct T *) MALLOC(sizeof(struct T))
157
158 #define CALLOC_STRUCT(T)   (struct T *) CALLOC(1, sizeof(struct T))
159
160 #define CALLOC_VARIANT_LENGTH_STRUCT(T,more_size)   ((struct T *) CALLOC(1, sizeof(struct T) + more_size))
161
162
163 /**
164  * Return memory on given byte alignment
165  */
166 static INLINE void *
167 align_malloc(size_t bytes, uint alignment)
168 {
169 #if defined(HAVE_POSIX_MEMALIGN)
170    void *mem;
171    alignment = (alignment + (uint)sizeof(void*) - 1) & ~((uint)sizeof(void*) - 1);
172    if(posix_memalign(& mem, alignment, bytes) != 0)
173       return NULL;
174    return mem;
175 #else
176    char *ptr, *buf;
177
178    assert( alignment > 0 );
179
180    ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
181    if (!ptr)
182       return NULL;
183
184    buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
185    *(char **)(buf - sizeof(void *)) = ptr;
186
187    return buf;
188 #endif /* defined(HAVE_POSIX_MEMALIGN) */
189 }
190
191 /**
192  * Free memory returned by align_malloc().
193  */
194 static INLINE void
195 align_free(void *ptr)
196 {
197 #if defined(HAVE_POSIX_MEMALIGN)
198    FREE(ptr);
199 #else
200    if (ptr) {
201       void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
202       void *realAddr = *cubbyHole;
203       FREE(realAddr);
204    }
205 #endif /* defined(HAVE_POSIX_MEMALIGN) */
206 }
207
208
209 /**
210  * Duplicate a block of memory.
211  */
212 static INLINE void *
213 mem_dup(const void *src, uint size)
214 {
215    void *dup = MALLOC(size);
216    if (dup)
217       memcpy(dup, src, size);
218    return dup;
219 }
220
221
222 /**
223  * Number of elements in an array.
224  */
225 #ifndef Elements
226 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
227 #endif
228
229
230 /**
231  * Offset of a field in a struct, in bytes.
232  */
233 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
234
235
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241
242 #endif /* U_MEMORY_H */