Mesa (master): util: Add an aligned realloc function
GitLab Mirror
gitlab-mirror at kemper.freedesktop.org
Tue Apr 21 20:52:39 UTC 2020
Module: Mesa
Branch: master
Commit: bb560f2d653dd5d080c7f03859936ce50bea5b72
URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=bb560f2d653dd5d080c7f03859936ce50bea5b72
Author: Dylan Baker <dylan at pnwbakers.com>
Date: Wed Sep 12 16:26:38 2018 -0700
util: Add an aligned realloc function
Mesa has one of these in imports.h, so u_memory needs one as well. This
is the version from mesa ported.
Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Reviewed-by: Kristian H. Kristensen <hoegsberg at google.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3024>
---
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, 40 insertions(+), 17 deletions(-)
diff --git a/src/util/os_memory.h b/src/util/os_memory.h
index 7a67e440dee..bfb70815118 100644
--- a/src/util/os_memory.h
+++ b/src/util/os_memory.h
@@ -60,6 +60,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 33eacb99c7f..08f12062a7f 100644
--- a/src/util/os_memory_aligned.h
+++ b/src/util/os_memory_aligned.h
@@ -36,7 +36,6 @@
#endif
-
/**
* Add two size_t values with integer overflow check.
* TODO: leverage __builtin_add_overflow where available
@@ -49,6 +48,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
*/
@@ -93,3 +108,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 a4a670d589a..bda5715998f 100644
--- a/src/util/os_memory_stdc.h
+++ b/src/util/os_memory_stdc.h
@@ -45,27 +45,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 DETECT_OS_WINDOWS
+#if DETECT_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 15a0bb419b5..4cdccb66aaf 100644
--- a/src/util/u_memory.h
+++ b/src/util/u_memory.h
@@ -60,6 +60,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)
More information about the mesa-commit
mailing list