1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
38 #include "util/u_pointer.h"
39 #include "util/u_debug.h"
47 /* Define ENOMEM for WINCE */
48 #if (_WIN32_WCE < 600)
55 #if defined(PIPE_OS_WINDOWS) && defined(DEBUG)
57 /* memory debugging */
59 #include "util/u_debug.h"
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 )
70 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
75 unsigned long MemSize
,
82 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
83 #define _FREE( _ptr ) EngFreeMem( _ptr )
85 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
89 unsigned long PoolType
,
90 size_t NumberOfBytes
);
95 #define MALLOC(_size) ExAllocatePool(0, _size)
96 #define _FREE(_ptr) ExFreePool(_ptr)
100 #define MALLOC( SIZE ) malloc( SIZE )
101 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
102 #define FREE( PTR ) free( PTR )
105 _REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
108 return realloc(old_ptr
, new_size
);
110 #define REALLOC( a, b, c ) _REALLOC( a, b, c )
116 CALLOC( unsigned count
, unsigned size
)
118 void *ptr
= MALLOC( count
* size
);
120 memset( ptr
, 0, count
* size
);
138 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
140 void *new_ptr
= NULL
;
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
);
153 #endif /* !REALLOC */
156 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
158 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
160 #define CALLOC_VARIANT_LENGTH_STRUCT(T,more_size) ((struct T *) CALLOC(1, sizeof(struct T) + more_size))
164 * Return memory on given byte alignment
167 align_malloc(size_t bytes
, uint alignment
)
169 #if defined(HAVE_POSIX_MEMALIGN)
171 alignment
= (alignment
+ (uint
)sizeof(void*) - 1) & ~((uint
)sizeof(void*) - 1);
172 if(posix_memalign(& mem
, alignment
, bytes
) != 0)
178 assert( alignment
> 0 );
180 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
184 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
185 *(char **)(buf
- sizeof(void *)) = ptr
;
188 #endif /* defined(HAVE_POSIX_MEMALIGN) */
192 * Free memory returned by align_malloc().
195 align_free(void *ptr
)
197 #if defined(HAVE_POSIX_MEMALIGN)
201 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
202 void *realAddr
= *cubbyHole
;
205 #endif /* defined(HAVE_POSIX_MEMALIGN) */
210 * Duplicate a block of memory.
213 mem_dup(const void *src
, uint size
)
215 void *dup
= MALLOC(size
);
217 memcpy(dup
, src
, size
);
223 * Number of elements in an array.
226 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
231 * Offset of a field in a struct, in bytes.
233 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
242 #endif /* U_MEMORY_H */