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 "pipe/p_debug.h"
47 /* Define ENOMEM for WINCE */
48 #if (_WIN32_WCE < 600)
55 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && defined(DEBUG)
57 /* memory debugging */
59 #include "pipe/p_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 )
103 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
110 CALLOC( unsigned count
, unsigned size
)
112 void *ptr
= MALLOC( count
* size
);
114 memset( ptr
, 0, count
* size
);
132 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
134 void *new_ptr
= NULL
;
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
);
147 #endif /* !REALLOC */
150 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
152 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
156 * Return memory on given byte alignment
159 align_malloc(size_t bytes
, uint alignment
)
161 #if defined(HAVE_POSIX_MEMALIGN)
163 alignment
= (alignment
+ (uint
)sizeof(void*) - 1) & ~((uint
)sizeof(void*) - 1);
164 if(posix_memalign(& mem
, alignment
, bytes
) != 0)
170 assert( alignment
> 0 );
172 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
176 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
177 *(char **)(buf
- sizeof(void *)) = ptr
;
180 #endif /* defined(HAVE_POSIX_MEMALIGN) */
184 * Free memory returned by align_malloc().
187 align_free(void *ptr
)
189 #if defined(HAVE_POSIX_MEMALIGN)
192 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
193 void *realAddr
= *cubbyHole
;
195 #endif /* defined(HAVE_POSIX_MEMALIGN) */
200 * Duplicate a block of memory.
203 mem_dup(const void *src
, uint size
)
205 void *dup
= MALLOC(size
);
207 memcpy(dup
, src
, size
);
213 * Number of elements in an array.
216 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
221 * Offset of a field in a struct, in bytes.
223 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
232 #endif /* U_MEMORY_H */