summaryrefslogtreecommitdiffstats
path: root/src/mesa/pipe/p_util.h
diff options
context:
space:
mode:
authorBrian <[email protected]>2007-12-11 08:58:39 -0700
committerBrian <[email protected]>2007-12-11 09:00:01 -0700
commit5b8b542cb2f10c5c39b9db18cd95e553ad06061b (patch)
tree0d05e0a8dda867ec268b1af25990df81a751bb8b /src/mesa/pipe/p_util.h
parent9f0e5642d80d746aa757a1e67dec15b0fd404e5d (diff)
Move align_malloc(), align_free() to p_util.h
Diffstat (limited to 'src/mesa/pipe/p_util.h')
-rw-r--r--src/mesa/pipe/p_util.h52
1 files changed, 50 insertions, 2 deletions
diff --git a/src/mesa/pipe/p_util.h b/src/mesa/pipe/p_util.h
index 3c5e98453c9..898459293eb 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,56 @@ 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, unsigned long 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) )