u_memory.h

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 
00034 #ifndef U_MEMORY_H
00035 #define U_MEMORY_H
00036 
00037 
00038 #include "util/u_pointer.h"
00039 #include "pipe/p_debug.h"
00040 
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 
00047 /* Define ENOMEM for WINCE */ 
00048 #if (_WIN32_WCE < 600)
00049 #ifndef ENOMEM
00050 #define ENOMEM 12
00051 #endif
00052 #endif
00053 
00054 
00055 #if defined(PIPE_OS_WINDOWS) && defined(DEBUG) 
00056 
00057 /* memory debugging */
00058 
00059 #include "pipe/p_debug.h"
00060 
00061 #define MALLOC( _size ) \
00062    debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
00063 #define CALLOC( _count, _size ) \
00064    debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
00065 #define FREE( _ptr ) \
00066    debug_free( __FILE__, __LINE__, __FUNCTION__,  _ptr )
00067 #define REALLOC( _ptr, _old_size, _size ) \
00068    debug_realloc( __FILE__, __LINE__, __FUNCTION__,  _ptr, _old_size, _size )
00069 
00070 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
00071 
00072 void * __stdcall
00073 EngAllocMem(
00074     unsigned long Flags,
00075     unsigned long MemSize,
00076     unsigned long Tag );
00077 
00078 void __stdcall
00079 EngFreeMem(
00080     void *Mem );
00081 
00082 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
00083 #define _FREE( _ptr ) EngFreeMem( _ptr )
00084 
00085 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
00086 
00087 void *
00088 ExAllocatePool(
00089     unsigned long PoolType, 
00090     size_t NumberOfBytes);
00091 
00092 void 
00093 ExFreePool(void *P);
00094 
00095 #define MALLOC(_size) ExAllocatePool(0, _size)
00096 #define _FREE(_ptr) ExFreePool(_ptr)
00097 
00098 #else
00099 
00100 #define MALLOC( SIZE )  malloc( SIZE )
00101 #define CALLOC( COUNT, SIZE )   calloc( COUNT, SIZE )
00102 #define FREE( PTR )  free( PTR )
00103 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE )  realloc( OLDPTR, NEWSIZE )
00104 
00105 #endif
00106 
00107 
00108 #ifndef CALLOC
00109 static INLINE void *
00110 CALLOC( unsigned count, unsigned size )
00111 {
00112    void *ptr = MALLOC( count * size );
00113    if( ptr ) {
00114       memset( ptr, 0, count * size );
00115    }
00116    return ptr;
00117 }
00118 #endif /* !CALLOC */
00119 
00120 #ifndef FREE
00121 static INLINE void
00122 FREE( void *ptr )
00123 {
00124    if( ptr ) {
00125       _FREE( ptr );
00126    }
00127 }
00128 #endif /* !FREE */
00129 
00130 #ifndef REALLOC
00131 static INLINE void *
00132 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
00133 {
00134    void *new_ptr = NULL;
00135 
00136    if (new_size != 0) {
00137       unsigned copy_size = old_size < new_size ? old_size : new_size;
00138       new_ptr = MALLOC( new_size );
00139       if (new_ptr && old_ptr && copy_size) {
00140          memcpy( new_ptr, old_ptr, copy_size );
00141       }
00142    }
00143 
00144    FREE( old_ptr );
00145    return new_ptr;
00146 }
00147 #endif /* !REALLOC */
00148 
00149 
00150 #define MALLOC_STRUCT(T)   (struct T *) MALLOC(sizeof(struct T))
00151 
00152 #define CALLOC_STRUCT(T)   (struct T *) CALLOC(1, sizeof(struct T))
00153 
00154 
00158 static INLINE void *
00159 align_malloc(size_t bytes, uint alignment)
00160 {
00161 #if defined(HAVE_POSIX_MEMALIGN)
00162    void *mem;
00163    alignment = (alignment + (uint)sizeof(void*) - 1) & ~((uint)sizeof(void*) - 1);
00164    if(posix_memalign(& mem, alignment, bytes) != 0)
00165       return NULL;
00166    return mem;
00167 #else
00168    char *ptr, *buf;
00169 
00170    assert( alignment > 0 );
00171 
00172    ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
00173    if (!ptr)
00174       return NULL;
00175 
00176    buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
00177    *(char **)(buf - sizeof(void *)) = ptr;
00178 
00179    return buf;
00180 #endif /* defined(HAVE_POSIX_MEMALIGN) */
00181 }
00182 
00186 static INLINE void
00187 align_free(void *ptr)
00188 {
00189 #if defined(HAVE_POSIX_MEMALIGN)
00190    FREE(ptr);
00191 #else
00192    void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
00193    void *realAddr = *cubbyHole;
00194    FREE(realAddr);
00195 #endif /* defined(HAVE_POSIX_MEMALIGN) */
00196 }
00197 
00198 
00202 static INLINE void *
00203 mem_dup(const void *src, uint size)
00204 {
00205    void *dup = MALLOC(size);
00206    if (dup)
00207       memcpy(dup, src, size);
00208    return dup;
00209 }
00210 
00211 
00215 #ifndef Elements
00216 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
00217 #endif
00218 
00219 
00223 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
00224 
00225 
00226 
00227 #ifdef __cplusplus
00228 }
00229 #endif
00230 
00231 
00232 #endif /* U_MEMORY_H */

Generated on Tue Sep 29 06:25:15 2009 for Gallium3D by  doxygen 1.5.4