]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Added custom allocation functions to spandsp
authorSteve Underwood <steveu@haswell.coppice.org>
Mon, 5 Aug 2013 15:50:28 +0000 (23:50 +0800)
committerSteve Underwood <steveu@haswell.coppice.org>
Mon, 5 Aug 2013 15:50:28 +0000 (23:50 +0800)
libs/spandsp/configure.ac
libs/spandsp/src/Makefile.am
libs/spandsp/src/alloc.c [new file with mode: 0644]
libs/spandsp/src/spandsp.h.in
libs/spandsp/src/spandsp/alloc.h [new file with mode: 0644]
libs/spandsp/tests/Makefile.am
libs/spandsp/tests/alloc_tests.c [new file with mode: 0644]
libs/spandsp/tests/v18_tests.c

index dacc3ae4edaa9d678cc5b74f31f28311b8e5c70d..8a6158fc80df57eb3fd0bf33cb2e0885d4fabbd3 100644 (file)
@@ -138,6 +138,7 @@ AC_ARG_ENABLE(avx,          [  --enable-avx         Enable AVX support])
 AC_ARG_ENABLE(avx2,         [  --enable-avx2        Enable AVX2 support])
 AC_ARG_ENABLE(neon,         [  --enable-neon        Enable NEON support])
 AC_ARG_ENABLE(fixed_point,  [  --enable-fixed-point Enable fixed point support])
+
 # The following is for MSVC, where we may be using a local copy of libtiff, built alongside spandsp
 AC_ARG_ENABLE(builtin_tiff,
     [AC_HELP_STRING([--enable-builtin-tiff],[build with builtin libtiff])],[enable_builtin_tiff="$enableval"],[enable_builtin_tiff="no"])
@@ -164,6 +165,8 @@ fi
 AX_C99_FLEXIBLE_ARRAY
 
 AC_CHECK_FUNCS([aligned_alloc])
+AC_CHECK_FUNCS([memalign])
+AC_CHECK_FUNCS([posix_memalign])
 AC_CHECK_FUNCS([memmove])
 AC_CHECK_FUNCS([memset])
 AC_CHECK_FUNCS([select])
index 3d63ae7274d85b8e2064efaac76c49411f38ccf0..a794e37fb566642d93e56b83f9bb25cf52568170 100644 (file)
@@ -84,6 +84,7 @@ lib_LTLIBRARIES = libspandsp.la
 
 libspandsp_la_SOURCES = ademco_contactid.c \
                         adsi.c \
+                        alloc.c \
                         async.c \
                         at_interpreter.c \
                         awgn.c \
@@ -178,6 +179,7 @@ libspandsp_la_LDFLAGS = -version-info @SPANDSP_LT_CURRENT@:@SPANDSP_LT_REVISION@
 
 nobase_include_HEADERS = spandsp/ademco_contactid.h \
                          spandsp/adsi.h \
+                         spandsp/alloc.h \
                          spandsp/async.h \
                          spandsp/arctan2.h \
                          spandsp/at_interpreter.h \
diff --git a/libs/spandsp/src/alloc.c b/libs/spandsp/src/alloc.c
new file mode 100644 (file)
index 0000000..e75c603
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * alloc.c - memory allocation handling.
+ *
+ * Written by Steve Underwood <steveu@coppice.org>
+ *
+ * Copyright (C) 2013 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#define __USE_ISOC11
+#include <stdlib.h>
+#if defined(HAVE_MALLOC_H)
+#include <malloc.h>
+#endif
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/alloc.h"
+
+#if defined(HAVE_POSIX_MEMALIGN)
+static void *fake_posix_memalign(size_t alignment, size_t size);
+#endif
+static void *fake_aligned_alloc(size_t alignment, size_t size);
+
+span_alloc_t __span_alloc = malloc;
+#if defined(HAVE_ALIGNED_ALLOC)
+span_aligned_alloc_t __span_aligned_alloc = aligned_alloc;
+#elif defined(HAVE_MEMALIGN)
+span_aligned_alloc_t __span_aligned_alloc = memalign;
+#elif defined(HAVE_POSIX_MEMALIGN)
+span_aligned_alloc_t __span_aligned_alloc = fake_posix_memalign;
+#else
+span_aligned_alloc_t __span_aligned_alloc = fake_aligned_alloc;
+#endif
+span_realloc_t __span_realloc = realloc;
+span_free_t __span_free = free;
+
+#if defined(HAVE_POSIX_MEMALIGN)
+static void *fake_posix_memalign(size_t alignment, size_t size)
+{
+    void *ptr;
+
+    /* Make posix_memalign look like the more modern aligned_alloc */
+    posix_memalign(&ptr, alignment, size);
+    return ptr;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static void *fake_aligned_alloc(size_t alignment, size_t size)
+{
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(void *) span_alloc(size_t size)
+{
+    return __span_alloc(size);
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(void *) span_aligned_alloc(size_t alignment, size_t size)
+{
+    return __span_aligned_alloc(alignment, size);
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(void *) span_realloc(void *ptr, size_t size)
+{
+    return __span_realloc(ptr, size);
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(void) span_free(void *ptr)
+{
+    __span_free(ptr);
+}
+/*- End of function --------------------------------------------------------*/
+
+SPAN_DECLARE(int) span_mem_allocators(span_alloc_t custom_alloc,
+                                      span_aligned_alloc_t custom_aligned_alloc,
+                                      span_realloc_t custom_realloc,
+                                      span_free_t custom_free)
+{
+    if (custom_alloc == NULL  ||  custom_realloc == NULL  ||  custom_free == NULL)
+        return -1;
+    __span_alloc = custom_alloc;
+    if (custom_aligned_alloc)
+        __span_aligned_alloc = custom_aligned_alloc;
+    else
+        __span_aligned_alloc = fake_aligned_alloc;
+    __span_realloc = custom_realloc;
+    __span_free = custom_free;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/
index 2764aa42898870edaee492862cdd27ad85c8d909..d5a7b4458137855a784903949a146b660689f885 100644 (file)
@@ -48,6 +48,7 @@
 #include <tiffio.h>
 
 #include <spandsp/telephony.h>
+#include <spandsp/alloc.h>
 #include <spandsp/fast_convert.h>
 #include <spandsp/logging.h>
 #include <spandsp/complex.h>
diff --git a/libs/spandsp/src/spandsp/alloc.h b/libs/spandsp/src/spandsp/alloc.h
new file mode 100644 (file)
index 0000000..8511897
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * alloc.h - memory allocation handling.
+ *
+ * Written by Steve Underwood <steveu@coppice.org>
+ *
+ * Copyright (C) 2013 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_ALLOC_H_)
+#define _SPANDSP_ALLOC_H_
+
+typedef void *(*span_alloc_t)(size_t size);
+typedef void *(*span_aligned_alloc_t)(size_t alignment, size_t size);
+typedef void *(*span_realloc_t)(void *ptr, size_t size);
+typedef void (*span_free_t)(void *ptr);
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/* Allocate size bytes of memory. */
+SPAN_DECLARE(void *) span_alloc(size_t size);
+
+/* Allocate size bytes allocated to ALIGNMENT bytes.  */
+SPAN_DECLARE(void *) span_aligned_alloc(size_t alignment, size_t size);
+
+/* Re-allocate the previously allocated block in ptr, making the new block size bytes long. */
+SPAN_DECLARE(void *) span_realloc(void *ptr, size_t size);
+
+/* Free a block allocated by span_alloc, span_aligned_alloc, or span_realloc. */
+SPAN_DECLARE(void) span_free(void *ptr);
+
+SPAN_DECLARE(int) span_mem_allocators(span_alloc_t custom_alloc,
+                                      span_aligned_alloc_t custom_aligned_alloc,
+                                      span_realloc_t custom_realloc,
+                                      span_free_t custom_free);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/
index e35408c3b7a87649f40cd4575df4dbf775353aa9..8eda2bb201aaabca8f12b373f3f35ad576c3437e 100644 (file)
@@ -52,6 +52,7 @@ LIBDIR = -L$(top_builddir)/src
 
 noinst_PROGRAMS =   ademco_contactid_tests \
                     adsi_tests \
+                    alloc_tests \
                     async_tests \
                     at_interpreter_tests \
                     awgn_tests \
@@ -146,6 +147,9 @@ ademco_contactid_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIB
 adsi_tests_SOURCES = adsi_tests.c
 adsi_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
 
+alloc_tests_SOURCES = alloc_tests.c
+alloc_tests_LDADD = $(LIBDIR) -lspandsp
+
 async_tests_SOURCES = async_tests.c
 async_tests_LDADD = $(LIBDIR) -lspandsp
 
diff --git a/libs/spandsp/tests/alloc_tests.c b/libs/spandsp/tests/alloc_tests.c
new file mode 100644 (file)
index 0000000..54dfb1e
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * alloc_tests.c - memory allocation handling tests.
+ *
+ * Written by Steve Underwood <steveu@coppice.org>
+ *
+ * Copyright (C) 2013 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+/*! \file */
+
+/*! \page alloc_tests_page Memory allocation tests
+\section alloc_tests_page_sec_1 What does it do?
+???.
+
+\section alloc_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include "spandsp.h"
+
+int main(int argc, char *argv[])
+{
+    void *a;
+    void *b;
+    void *c;
+    
+    if (span_mem_allocators(malloc,
+                            memalign,
+                            realloc,
+                            free))
+    {
+        printf("Failed\n");
+        exit(2);
+    }
+    a = span_alloc(42);
+    b = span_aligned_alloc(8, 42);
+    c = span_realloc(NULL, 42);
+    printf("%p %p %p\n", a, b, c);
+    span_free(a);
+    span_free(b);
+    span_free(c);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/
index 0c12678c8078a3d8b6046117dd1a4030506c598d..64a47a15935bf4544df7f3f6e81c7fb9ffd30378 100644 (file)
@@ -7473,7 +7473,7 @@ static int test_x_01(void)
 
     result[0][0] =
     result[1][0] = '\0';
-    v18_put(v18[0], "z", 1);
+    v18_put(v18[0], "zabcdefghijklmnopq", -1);
     for (i = 0;  i < 10000;  i++)
     {
         for (j = 0;  j < 2;  j++)
@@ -7518,7 +7518,7 @@ static int test_x_01(void)
     v18_free(v18[0]);
     v18_free(v18[1]);
     ref = "cdefghij";
-    printf("Result:\n%s\n", result[0]);
+    printf("Result:\n%s\n", result[1]);
     printf("Reference result:\n%s\n", ref);
     if (unexpected_echo  ||  strcmp(result[1], ref) != 0)
         return -1;
@@ -7864,9 +7864,18 @@ static int test_x_04(void)
 static void x_05_put_text_msg(void *user_data, const uint8_t *msg, int len)
 {
     if (user_data == NULL)
+    {
+        /* Gather the received characters, which should be like the transmitted characters,
+           but with the first three characters missing. */
         strcat(result[0], (const char *) msg);
+    }
     else
+    {
+        /* Receiving a character from the far end should block out its receiver
+           for a while. If we send a stream of DTMF back, the first few characters
+           (actually 3 for this particular text string) should be lost. */
         v18_put(v18[1], "behknqtwz", 9);
+    }
 }
 /*- End of function --------------------------------------------------------*/
 
@@ -7920,7 +7929,8 @@ static int test_x_05(void)
 
     result[0][0] =
     result[1][0] = '\0';
-    v18_put(v18[0], "e", 1);
+    /* Sending a character should block out the receiver for a while */
+    v18_put(v18[0], "z", 1);
 
     for (i = 0;  i < 1000;  i++)
     {