From 156cdd329555e846e60ca69918701ecccc0632cc Mon Sep 17 00:00:00 2001
From: twinaphex <libretro@gmail.com>
Date: Wed, 2 Sep 2015 17:06:02 +0200
Subject: [PATCH] Create memalign.c

---
 Makefile.common                        |  1 +
 audio/drivers_resampler/cc_resampler.c | 39 ++++-----------------
 audio/drivers_resampler/sinc.c         | 34 ++++--------------
 griffin/griffin.c                      |  1 +
 libretro-common/include/memalign.h     | 40 +++++++++++++++++++++
 libretro-common/memmap/memalign.c      | 48 ++++++++++++++++++++++++++
 6 files changed, 102 insertions(+), 61 deletions(-)
 create mode 100644 libretro-common/include/memalign.h
 create mode 100644 libretro-common/memmap/memalign.c

diff --git a/Makefile.common b/Makefile.common
index 705f049b68..00a5e7c0ce 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -132,6 +132,7 @@ OBJ += frontend/frontend.o \
 		libretro-common/file/dir_list.o \
 		libretro-common/string/string_list.o \
 		libretro-common/string/stdstring.o \
+		libretro-common/memmap/memalign.o \
 		libretro-common/memmap/memmap.o \
 		dir_list_special.o \
 		file_ops.o \
diff --git a/audio/drivers_resampler/cc_resampler.c b/audio/drivers_resampler/cc_resampler.c
index b27110510f..2d1a26dc66 100644
--- a/audio/drivers_resampler/cc_resampler.c
+++ b/audio/drivers_resampler/cc_resampler.c
@@ -15,7 +15,6 @@
 
 /* Convoluted Cosine Resampler */
 
-#include "../audio_resampler_driver.h"
 #include <math.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -23,6 +22,9 @@
 #include <xmmintrin.h>
 #endif
 #include <retro_inline.h>
+#include <memalign.h>
+
+#include "../audio_resampler_driver.h"
 
 /* Since SSE and NEON don't provide support for trigonometric functions
  * we approximate those with polynoms
@@ -177,34 +179,6 @@ static void *resampler_CC_init(const struct resampler_config *config,
    return (void*)-1;
 }
 #else
-/* memalign() replacement functions
- * copied from sinc.c and changed signature so no conflict
- * happens when using griffin.c
- * these functions should probably be moved to a common header
- */
-
-static void *memalign_alloc__(size_t boundary, size_t size)
-{
-   void **place;
-   uintptr_t addr = 0;
-   void *ptr = malloc(boundary + size + sizeof(uintptr_t));
-   if (!ptr)
-      return NULL;
-
-   addr           = ((uintptr_t)
-         ptr + sizeof(uintptr_t) + boundary)
-      & ~(boundary - 1);
-   place          = (void**)addr;
-   place[-1]      = ptr;
-
-   return (void*)addr;
-}
-
-static void memalign_free__(void *ptr)
-{
-   void **p = (void**)ptr;
-   free(p[-1]);
-}
 
 #if defined(__SSE__)
 #define CC_RESAMPLER_IDENT "SSE"
@@ -530,7 +504,7 @@ static void resampler_CC_free(void *re_)
 {
    rarch_CC_resampler_t *re = (rarch_CC_resampler_t*)re_;
    if (re)
-      memalign_free__(re);
+      memalign_free(re);
 }
 
 static void *resampler_CC_init(const struct resampler_config *config,
@@ -538,15 +512,14 @@ static void *resampler_CC_init(const struct resampler_config *config,
 {
    int i;
    rarch_CC_resampler_t *re = (rarch_CC_resampler_t*)
-      memalign_alloc__(32, sizeof(rarch_CC_resampler_t));
+      memalign_alloc(32, sizeof(rarch_CC_resampler_t));
 
    /* TODO: lookup if NEON support can be detected at 
     * runtime and a funcptr set at runtime for either
     * C codepath or NEON codepath. This will help out
     * Android. */
    (void)mask;
-   (void)config;
-
+   (void)config; 
    if (!re)
       return NULL;
 
diff --git a/audio/drivers_resampler/sinc.c b/audio/drivers_resampler/sinc.c
index a468c0ee8f..8b773c7964 100644
--- a/audio/drivers_resampler/sinc.c
+++ b/audio/drivers_resampler/sinc.c
@@ -15,7 +15,6 @@
 
 /* Bog-standard windowed SINC implementation. */
 
-#include "../audio_resampler_driver.h"
 #include <math.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -24,7 +23,11 @@
 #ifdef __SSE__
 #include <xmmintrin.h>
 #endif
+
 #include <retro_inline.h>
+#include <memalign.h>
+
+#include "../audio_resampler_driver.h"
 
 /* Rough SNR values for upsampling:
  * LOWEST: 40 dB
@@ -212,30 +215,6 @@ static void init_sinc_table(rarch_sinc_resampler_t *resamp, double cutoff,
    }
 }
 
-/* No memalign() for us on Win32 ... */
-static void *aligned_alloc__(size_t boundary, size_t size)
-{
-   void **place;
-   uintptr_t addr = 0;
-   void *ptr = malloc(boundary + size + sizeof(uintptr_t));
-
-   if (!ptr)
-      return NULL;
-
-   addr           = ((uintptr_t)ptr + sizeof(uintptr_t) + boundary) 
-                    & ~(boundary - 1);
-   place          = (void**)addr;
-   place[-1]      = ptr;
-
-   return (void*)addr;
-}
-
-static void aligned_free__(void *ptr)
-{
-   void **p = (void**)ptr;
-   free(p[-1]);
-}
-
 #if !(defined(__AVX__) && ENABLE_AVX) && !defined(__SSE__)
 static INLINE void process_sinc_C(rarch_sinc_resampler_t *resamp,
       float *out_buffer)
@@ -462,7 +441,7 @@ static void resampler_sinc_free(void *re)
 {
    rarch_sinc_resampler_t *resampler = (rarch_sinc_resampler_t*)re;
    if (resampler)
-      aligned_free__(resampler->main_buffer);
+      memalign_free(resampler->main_buffer);
    free(resampler);
 }
 
@@ -503,8 +482,7 @@ static void *resampler_sinc_new(const struct resampler_config *config,
 #endif
    elems = phase_elems + 4 * re->taps;
 
-   re->main_buffer = (float*)
-      aligned_alloc__(128, sizeof(float) * elems);
+   re->main_buffer = (float*)memalign_alloc(128, sizeof(float) * elems);
    if (!re->main_buffer)
       goto error;
 
diff --git a/griffin/griffin.c b/griffin/griffin.c
index 60135a4b50..f7cef3f818 100644
--- a/griffin/griffin.c
+++ b/griffin/griffin.c
@@ -61,6 +61,7 @@ COMPATIBILITY
 ============================================================ */
 #include "../compat/compat.c"
 #include "../libretro-common/compat/compat_fnmatch.c"
+#include "../libretro-common/memmap/memalign.c"
 #include "../libretro-common/memmap/memmap.c"
 
 /*============================================================
diff --git a/libretro-common/include/memalign.h b/libretro-common/include/memalign.h
new file mode 100644
index 0000000000..674f7d24fc
--- /dev/null
+++ b/libretro-common/include/memalign.h
@@ -0,0 +1,40 @@
+/* Copyright  (C) 2010-2015 The RetroArch team
+ *
+ * ---------------------------------------------------------------------------------------
+ * The following license statement only applies to this file (memalign.h).
+ * ---------------------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge,
+ * to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _LIBRETRO_MEMALIGN_H
+#define _LIBRETRO_MEMALIGN_H
+
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void *memalign_alloc(size_t boundary, size_t size);
+
+void memalign_free(void *ptr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libretro-common/memmap/memalign.c b/libretro-common/memmap/memalign.c
new file mode 100644
index 0000000000..60f4ba4153
--- /dev/null
+++ b/libretro-common/memmap/memalign.c
@@ -0,0 +1,48 @@
+/* Copyright  (C) 2010-2015 The RetroArch team
+ *
+ * ---------------------------------------------------------------------------------------
+ * The following license statement only applies to this file (memalign.c).
+ * ---------------------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge,
+ * to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <memalign.h>
+
+void *memalign_alloc(size_t boundary, size_t size)
+{
+   void **place;
+   uintptr_t addr = 0;
+   void *ptr = malloc(boundary + size + sizeof(uintptr_t));
+   if (!ptr)
+      return NULL;
+
+   addr           = ((uintptr_t)ptr + sizeof(uintptr_t) + boundary)
+      & ~(boundary - 1);
+   place          = (void**)addr;
+   place[-1]      = ptr;
+
+   return (void*)addr;
+}
+
+void memalign_free(void *ptr)
+{
+   void **p = (void**)ptr;
+   free(p[-1]);
+}