[Mesa-dev] [PATCH 25/28] util: Add an aligned realloc function
Dylan Baker
dylan at pnwbakers.com
Fri Nov 9 18:40:18 UTC 2018
Mesa has one of these in imports.h, so u_memory needs one as well. This
is the version from mesa ported.
---
src/util/os_memory.h | 3 +++
src/util/os_memory_aligned.h | 35 +++++++++++++++++++++++++++++++++++
src/util/os_memory_stdc.h | 18 ++----------------
src/util/u_memory.h | 1 +
4 files changed, 41 insertions(+), 16 deletions(-)
diff --git a/src/util/os_memory.h b/src/util/os_memory.h
index 46a6b6e4572..681369a77b6 100644
--- a/src/util/os_memory.h
+++ b/src/util/os_memory.h
@@ -63,6 +63,9 @@ os_malloc_aligned(size_t size, size_t alignment);
void
os_free_aligned(void *ptr);
+void *
+os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignemnt);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/util/os_memory_aligned.h b/src/util/os_memory_aligned.h
index df398a32bfd..578e347d297 100644
--- a/src/util/os_memory_aligned.h
+++ b/src/util/os_memory_aligned.h
@@ -37,6 +37,7 @@
#include "pipe/p_compiler.h"
+#include "util/macros.h"
@@ -52,6 +53,22 @@ add_overflow_size_t(size_t a, size_t b, size_t *res)
}
+#if defined(HAVE_POSIX_MEMALIGN)
+
+static inline void *
+os_malloc_aligned(size_t size, size_t alignment)
+{
+ void *ptr;
+ alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
+ if(posix_memalign(&ptr, alignment, size) != 0)
+ return NULL;
+ return ptr;
+}
+
+#define os_free_aligned(_ptr) free(_ptr)
+
+#else
+
/**
* Return memory on given byte alignment
*/
@@ -96,3 +113,21 @@ os_free_aligned(void *ptr)
os_free(realAddr);
}
}
+
+#endif
+
+/**
+ * Reallocate memeory, with alignment
+ */
+static inline void *
+os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignment)
+{
+ const size_t copySize = MIN2(oldsize, newsize);
+ void *newBuf = os_malloc_aligned(newsize, alignment);
+ if (newBuf && ptr && copySize > 0) {
+ memcpy(newBuf, ptr, copySize);
+ }
+
+ os_free_aligned(ptr);
+ return newBuf;
+}
diff --git a/src/util/os_memory_stdc.h b/src/util/os_memory_stdc.h
index c9fde06d8ac..95e330d83d2 100644
--- a/src/util/os_memory_stdc.h
+++ b/src/util/os_memory_stdc.h
@@ -47,27 +47,13 @@
#define os_realloc( _old_ptr, _old_size, _new_size) \
realloc(_old_ptr, _new_size + 0*(_old_size))
-
-#if defined(HAVE_POSIX_MEMALIGN)
-
-static inline void *
-os_malloc_aligned(size_t size, size_t alignment)
-{
- void *ptr;
- alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
- if(posix_memalign(&ptr, alignment, size) != 0)
- return NULL;
- return ptr;
-}
-
-#define os_free_aligned(_ptr) free(_ptr)
-
-#elif defined(PIPE_OS_WINDOWS)
+#if defined(PIPE_OS_WINDOWS)
#include <malloc.h>
#define os_malloc_aligned(_size, _align) _aligned_malloc(_size, _align)
#define os_free_aligned(_ptr) _aligned_free(_ptr)
+#define os_realloc_aligned(_ptr, _oldsize, _newsize, _alignment) _aligned_realloc(_ptr, _newsize, _alignment)
#else
diff --git a/src/util/u_memory.h b/src/util/u_memory.h
index dc22ab0fd1a..8b27ebb00b7 100644
--- a/src/util/u_memory.h
+++ b/src/util/u_memory.h
@@ -61,6 +61,7 @@ extern "C" {
#define align_malloc(_size, _alignment) os_malloc_aligned(_size, _alignment)
#define align_free(_ptr) os_free_aligned(_ptr)
+#define align_realloc(_ptr, _oldsize, _newsize, _alignment) os_realloc_aligned(_ptr, _oldsize, _newsize, _alignment)
static inline void *
align_calloc(size_t size, unsigned long alignment)
--
2.19.1
More information about the mesa-dev
mailing list