summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/p_util.h
diff options
context:
space:
mode:
authorBen Skeggs <skeggsb@gmail.com>2007-12-12 13:11:19 +1100
committerBen Skeggs <skeggsb@gmail.com>2007-12-12 13:11:19 +1100
commit58915980127ab4e57b6b40a8c42f44be4a12aeae (patch)
tree176f379bb19fc8caacf96112a1ebeda8aea90a16 /src/mesa/pipe/p_util.h
parente282d22d512d2a5871d0fabb7d855a16b4593c50 (diff)
parentb2ad30d57197c2167789e4f3f5b34af6df56dde2 (diff)
Merge branch 'upstream-gallium-0.1' into darktama-gallium-0.1
Conflicts: src/mesa/pipe/Makefile
Diffstat (limited to 'src/mesa/pipe/p_util.h')
-rw-r--r--src/mesa/pipe/p_util.h77
1 files changed, 68 insertions, 9 deletions
diff --git a/src/mesa/pipe/p_util.h b/src/mesa/pipe/p_util.h
index 3c5e98453c..e6d284d932 100644
--- a/src/mesa/pipe/p_util.h
+++ b/src/mesa/pipe/p_util.h
@@ -30,6 +30,8 @@
#include "p_compiler.h"
#include <math.h>
+#include <stdint.h>
+
#ifdef WIN32
@@ -93,7 +95,7 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
#define GETENV( X ) NULL
-#else // WIN32
+#else /* WIN32 */
#define MALLOC( SIZE ) malloc( SIZE )
@@ -105,10 +107,55 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
#define GETENV( X ) getenv( X )
-#endif // WIN32
+#endif /* WIN32 */
#define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
+
+
+/**
+ * Return memory on given byte alignment
+ */
+static INLINE void *
+align_malloc(size_t bytes, uint alignment)
+{
+#if defined(HAVE_POSIX_MEMALIGN)
+ void *mem;
+ (void) posix_memalign(& mem, alignment, bytes);
+ return mem;
+#else
+ uintptr_t ptr, buf;
+
+ assert( alignment > 0 );
+
+ ptr = (uintptr_t) MALLOC(bytes + alignment + sizeof(void *));
+ if (!ptr)
+ return NULL;
+
+ buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
+ *(uintptr_t *)(buf - sizeof(void *)) = ptr;
+
+ return (void *) buf;
+#endif /* defined(HAVE_POSIX_MEMALIGN) */
+}
+
+/**
+ * Free memory returned by align_malloc().
+ */
+static INLINE void
+align_free(void *ptr)
+{
+#if defined(HAVE_POSIX_MEMALIGN)
+ FREE(ptr);
+#else
+ void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
+ void *realAddr = *cubbyHole;
+ FREE(realAddr);
+#endif /* defined(HAVE_POSIX_MEMALIGN) */
+}
+
+
+
#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
@@ -122,13 +169,25 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
static INLINE void *
align16( void *unaligned )
{
- union {
- void *p;
- uint64 u;
- } pu;
- pu.p = unaligned;
- pu.u = (pu.u + 15) & ~15;
- return pu.p;
+ if (sizeof(void *) == 64) {
+ union {
+ void *p;
+ uint64 u;
+ } pu;
+ pu.p = unaligned;
+ pu.u = (pu.u + 15) & ~15;
+ return pu.p;
+ }
+ else {
+ /* 32-bit pointers */
+ union {
+ void *p;
+ uint u;
+ } pu;
+ pu.p = unaligned;
+ pu.u = (pu.u + 15) & ~15;
+ return pu.p;
+ }
}