Added custom allocation functions to spandsp

This commit is contained in:
Steve Underwood 2013-08-05 23:50:28 +08:00
parent 95ae3732a8
commit 7c744ce1d8
8 changed files with 292 additions and 3 deletions

View 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])

View 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 \

128
libs/spandsp/src/alloc.c Normal file
View File

@ -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 ------------------------------------------------------------*/

View 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>

View File

@ -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 ------------------------------------------------------------*/

View 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

View File

@ -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 ------------------------------------------------------------*/

View 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++)
{