diff --git a/kfc.c b/kfc.c index 270944a..bddb55a 100644 --- a/kfc.c +++ b/kfc.c @@ -68,7 +68,7 @@ void kfc_cleanup(void) kfc_cfg next=NULL; while (cur){ next = cur->next; - free(cur); + KISS_FFT_FREE(cur); cur=next; } ncached=0; diff --git a/kiss_fft.h b/kiss_fft.h index a568076..1400392 100644 --- a/kiss_fft.h +++ b/kiss_fft.h @@ -51,10 +51,10 @@ extern "C" { #ifdef HAVE_LASX # include # define kiss_fft_scalar __m256 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 31UL) & ~0x1FUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) aligned_alloc(32, KISS_FFT_ALIGN_SIZE_UP(nbytes)) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 31UL) & ~0x1FUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE free @@ -62,10 +62,10 @@ extern "C" { #elif defined(HAVE_LSX) # include # define kiss_fft_scalar __m128 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) aligned_alloc(16, KISS_FFT_ALIGN_SIZE_UP(nbytes)) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE free @@ -73,10 +73,10 @@ extern "C" { #else # include # define kiss_fft_scalar __m128 +# define KISS_FFT_ALIGN_CHECK(ptr) +# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # ifndef KISS_FFT_MALLOC # define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) -# define KISS_FFT_ALIGN_CHECK(ptr) -# define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) # endif # ifndef KISS_FFT_FREE # define KISS_FFT_FREE _mm_free diff --git a/kiss_fftnd.c b/kiss_fftnd.c index fde0c27..575e8b3 100644 --- a/kiss_fftnd.c +++ b/kiss_fftnd.c @@ -45,7 +45,7 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */ if (lenmem == NULL) {/* allocate for the caller*/ - ptr = (char *) malloc (memneeded); + ptr = (char *) KISS_FFT_MALLOC (memneeded); } else { /* initialize supplied buffer if big enough */ if (*lenmem >= memneeded) ptr = (char *) mem; diff --git a/kiss_fftndr.c b/kiss_fftndr.c index d395c05..bdc5857 100644 --- a/kiss_fftndr.c +++ b/kiss_fftndr.c @@ -60,7 +60,7 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void memneeded = KISS_FFT_PACK_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_PACK_SIZE_UP(nr) + KISS_FFT_PACK_SIZE_UP(nd) + KISS_FFT_PACK_SIZE_UP(ntmp); if (lenmem==NULL) { - ptr = (char*) malloc(memneeded); + ptr = (char*) KISS_FFT_MALLOC(memneeded); }else{ if (*lenmem >= memneeded) ptr = (char *)mem; diff --git a/kiss_fftndr.h b/kiss_fftndr.h index 0d56a1f..5c082a5 100644 --- a/kiss_fftndr.h +++ b/kiss_fftndr.h @@ -46,7 +46,7 @@ void KISS_FFT_API kiss_fftndri( */ -#define kiss_fftndr_free free +#define kiss_fftndr_free KISS_FFT_FREE #ifdef __cplusplus } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index abde829..6ca6a42 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -52,6 +52,8 @@ add_kissfft_test_executable(tsizeval test_size_validation.c) add_kissfft_test_executable(tndralloc test_fftndr_alloc.c) # includes ../kiss_fftr.c, ../kiss_fftnd.c and ../kiss_fftndr.c to reach their private structs add_kissfft_test_executable(tndalign test_nd_alignment.c) +# unity build: compiles the library sources itself with KISS_FFT_MALLOC/FREE overridden +add_kissfft_test_executable(tcustalloc test_custom_alloc.c) add_kissfft_test_executable(next_fast_size next_fast_size.c) diff --git a/test/Makefile b/test/Makefile index 6a14f09..7d0ead0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -46,6 +46,7 @@ ifneq ($(KISSFFT_OPENMP),1) TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE) TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE) TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE) + TESTCUSTOMALLOC = tcustalloc-$(KISSFFT_DATATYPE) TESTKFC = tkfc-$(KISSFFT_DATATYPE) TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) TESTCPP = testcpp-$(KISSFFT_DATATYPE) @@ -58,6 +59,7 @@ else TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)-openmp TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE)-openmp + TESTCUSTOMALLOC = tcustalloc-$(KISSFFT_DATATYPE)-openmp TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp @@ -109,7 +111,7 @@ endif # Target: "make all" # -all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTNDALIGN) $(TESTKFC) $(TESTFASTFILT) +all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTNDALIGN) $(TESTCUSTOMALLOC) $(TESTKFC) $(TESTFASTFILT) # # Individual test make rules @@ -134,6 +136,10 @@ $(TESTNDRALLOC): test_fftndr_alloc.c $(TESTNDALIGN): test_nd_alignment.c ../kiss_fftr.c ../kiss_fftnd.c ../kiss_fftndr.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm +# unity build: compiles the library sources itself with KISS_FFT_MALLOC/FREE overridden, so it must not link the library +$(TESTCUSTOMALLOC): test_custom_alloc.c ../kiss_fft.c ../kiss_fftr.c ../kiss_fftnd.c ../kiss_fftndr.c ../kfc.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -lm + $(BENCHKISS): benchkiss.c pstats.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm @@ -197,6 +203,8 @@ endif @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDRALLOC) $(warning ======multi-dimensional FFT state alignment regression test (type= $(KISSFFT_DATATYPE) )) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDALIGN) + $(warning ======custom KISS_FFT_MALLOC/KISS_FFT_FREE regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTCUSTOMALLOC) $(warning ======timing test (type=$(KISSFFT_DATATYPE))) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true @@ -208,4 +216,4 @@ endif # clean: - rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* tndalign-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c + rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* tndalign-* tcustalloc-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c diff --git a/test/test_custom_alloc.c b/test/test_custom_alloc.c new file mode 100644 index 0000000..dc903c8 --- /dev/null +++ b/test/test_custom_alloc.c @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression test for user-overridden KISS_FFT_MALLOC / KISS_FFT_FREE. + * + * The library sources are compiled INTO this test (unity build) with + * KISS_FFT_MALLOC/KISS_FFT_FREE replaced by tracking wrappers, so the test + * does not link against the prebuilt library. In addition, plain malloc() + * and free() are redirected while the library sources are compiled, to catch + * code that bypasses the overridable allocator: + * 1. every allocation made by kiss_fft_alloc, kiss_fftr_alloc, + * kiss_fftnd_alloc, kiss_fftndr_alloc and kfc_fft goes through + * KISS_FFT_MALLOC (no raw malloc) + * 2. kiss_fft_free, kiss_fftr_free, kiss_fftndr_free and kfc_cleanup only + * release blocks that KISS_FFT_MALLOC returned, and do so through + * KISS_FFT_FREE (no raw free) + * 3. nothing is left allocated afterwards + * 4. with USE_SIMD, overriding the allocator must still compile (the + * KISS_FFT_ALIGN_CHECK / KISS_FFT_ALIGN_SIZE_UP macros must stay defined) + */ +#include +#include +#include +#include +#include +#include + +/* the tracking allocator, defined below; must be visible before kiss_fft.h */ +static void *test_malloc(size_t n); +static void test_free(void *p); +#define KISS_FFT_MALLOC(nbytes) test_malloc(nbytes) +#define KISS_FFT_FREE(ptr) test_free(ptr) + +#include "kiss_fft.h" /* pull in every system header the library needs while */ +#include "_kiss_fft_guts.h" /* malloc/free are still the real functions */ + +/* ---- tracking allocator -------------------------------------------------- */ + +#define ALLOC_ALIGN 32 /* enough for SSE (16) and AVX/LASX (32) kiss_fft_scalar */ +#define MAX_BLOCKS 256 + +struct block { + void *user; /* pointer handed to the library */ + void *raw; /* pointer obtained from the C library */ + int owned; /* 1: returned by KISS_FFT_MALLOC, 0: library called raw malloc */ +}; + +static struct block blocks[MAX_BLOCKS]; +static int nblocks = 0; + +/* counters since the last begin_scenario() */ +static int n_wrapper_alloc = 0; /* KISS_FFT_MALLOC calls */ +static int n_raw_malloc = 0; /* library called malloc() directly */ +static int n_raw_free_owned = 0; /* library called free() on a KISS_FFT_MALLOC block */ +static int n_foreign_free = 0; /* KISS_FFT_FREE on a block KISS_FFT_MALLOC did not return */ +static int n_unknown_free = 0; /* free of a pointer nobody allocated */ + +static int failures = 0; + +static void *real_malloc(size_t n) +{ + return malloc(n); +} + +static void real_free(void *p) +{ + free(p); +} + +static int find_block(const void *p) +{ + int i; + for (i = 0; i < nblocks; ++i) + if (blocks[i].user == p) + return i; + return -1; +} + +static void add_block(void *user, void *raw, int owned) +{ + if (nblocks == MAX_BLOCKS) { + printf("FAIL: test allocation table full\n"); + exit(1); + } + blocks[nblocks].user = user; + blocks[nblocks].raw = raw; + blocks[nblocks].owned = owned; + ++nblocks; +} + +static void drop_block(int i) +{ + real_free(blocks[i].raw); + blocks[i] = blocks[--nblocks]; +} + +static void *test_malloc(size_t n) +{ + char *raw = (char *)real_malloc(n + ALLOC_ALIGN); + void *user; + if (raw == NULL) + return NULL; + user = (void *)(((uintptr_t)raw + ALLOC_ALIGN - 1) & ~(uintptr_t)(ALLOC_ALIGN - 1)); + add_block(user, raw, 1); + ++n_wrapper_alloc; + return user; +} + +static void test_free(void *p) +{ + int i; + if (p == NULL) + return; + i = find_block(p); + if (i < 0) { + ++n_unknown_free; + return; /* not ours: do not hand it to the C library */ + } + if (!blocks[i].owned) + ++n_foreign_free; + drop_block(i); +} + +/* what plain malloc()/free() inside the library sources turn into */ +static void *raw_malloc(size_t n) +{ + void *p = real_malloc(n); + if (p != NULL) { + add_block(p, p, 0); + ++n_raw_malloc; + } + return p; +} + +static void raw_free(void *p) +{ + int i; + if (p == NULL) + return; + i = find_block(p); + if (i < 0) { + ++n_unknown_free; + return; + } + if (blocks[i].owned) + ++n_raw_free_owned; + drop_block(i); +} + +#define malloc(nbytes) raw_malloc(nbytes) +#define free(ptr) raw_free(ptr) + +/* ---- library under test, compiled in with the overrides above ----------- */ + +/* the library itself is not built with -W; keep its sign-compare / maybe-uninitialized noise out of this test */ +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsign-compare" +# if !defined(__clang__) +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +# endif +#endif +#include "../kiss_fft.c" +#include "../kiss_fftr.c" +#include "../kiss_fftnd.c" +#include "../kiss_fftndr.c" +#include "../kfc.c" +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic pop +#endif + +/* ---- test ---------------------------------------------------------------- */ + +static void begin_scenario(void) +{ + n_wrapper_alloc = n_raw_malloc = n_raw_free_owned = 0; + n_foreign_free = n_unknown_free = 0; +} + +/* call right after the *_alloc call under test */ +static void expect_wrapper_alloc(const char *what) +{ + if (n_wrapper_alloc == 0) { + printf("FAIL: %s did not allocate through KISS_FFT_MALLOC\n", what); + ++failures; + } +} + +/* call after the object was released with the library's own free function */ +static void end_scenario(const char *what) +{ + int i, live_owned = 0, live_raw = 0; + + for (i = 0; i < nblocks; ++i) { + if (blocks[i].owned) + ++live_owned; + else + ++live_raw; + } + if (n_raw_malloc) { + printf("FAIL: %s called malloc() %d time(s) instead of KISS_FFT_MALLOC\n", what, n_raw_malloc); + ++failures; + } + if (n_raw_free_owned) { + printf("FAIL: %s called free() %d time(s) on KISS_FFT_MALLOC memory instead of KISS_FFT_FREE\n", + what, n_raw_free_owned); + ++failures; + } + if (n_foreign_free) { + printf("FAIL: %s passed %d block(s) not allocated by KISS_FFT_MALLOC to KISS_FFT_FREE\n", + what, n_foreign_free); + ++failures; + } + if (n_unknown_free) { + printf("FAIL: %s freed %d pointer(s) nobody allocated\n", what, n_unknown_free); + ++failures; + } + if (live_owned || live_raw) { + printf("FAIL: %s left %d KISS_FFT_MALLOC block(s) and %d malloc block(s) allocated\n", + what, live_owned, live_raw); + ++failures; + } + while (nblocks > 0) /* do not let one failing scenario leak into the next */ + drop_block(nblocks - 1); +} + +/* the redirection itself must work, otherwise a PASS below means nothing */ +static void test_harness(void) +{ + void *p; + + begin_scenario(); + p = malloc(8); + if (n_raw_malloc != 1 || nblocks != 1) { + printf("FAIL: test harness did not intercept malloc()\n"); + ++failures; + } + free(p); + p = KISS_FFT_MALLOC(8); + free(p); /* wrong way to release a KISS_FFT_MALLOC block */ + if (n_raw_free_owned != 1 || n_wrapper_alloc != 1 || nblocks != 0) { + printf("FAIL: test harness did not intercept free()\n"); + ++failures; + } +} + +static void test_kiss_fft(void) +{ + kiss_fft_cpx in[7], out[7]; + kiss_fft_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fft_alloc(7, 0, NULL, NULL); /* prime size: generic butterfly uses a temp buffer */ + expect_wrapper_alloc("kiss_fft_alloc"); + if (cfg != NULL) { + kiss_fft(cfg, in, out); + kiss_fft(cfg, in, in); /* "in-place" also uses a temp buffer */ + } + kiss_fft_free(cfg); + end_scenario("kiss_fft"); +} + +static void test_kiss_fftr(void) +{ + kiss_fft_scalar in[16]; + kiss_fft_cpx out[9]; + kiss_fftr_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftr_alloc(16, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftr_alloc"); + if (cfg != NULL) + kiss_fftr(cfg, in, out); + kiss_fftr_free(cfg); + end_scenario("kiss_fftr"); +} + +static void test_kiss_fftnd(void) +{ + static const int dims[2] = {4, 6}; + kiss_fft_cpx in[24], out[24]; + kiss_fftnd_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftnd_alloc(dims, 2, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftnd_alloc"); + if (cfg != NULL) + kiss_fftnd(cfg, in, out); + kiss_fft_free(cfg); + end_scenario("kiss_fftnd"); +} + +static void test_kiss_fftndr(void) +{ + static const int dims[2] = {4, 6}; + kiss_fft_scalar in[24]; + kiss_fft_cpx out[4 * 4]; + kiss_fftndr_cfg cfg; + + memset(in, 0, sizeof(in)); + begin_scenario(); + cfg = kiss_fftndr_alloc(dims, 2, 0, NULL, NULL); + expect_wrapper_alloc("kiss_fftndr_alloc"); + if (cfg != NULL) + kiss_fftndr(cfg, in, out); + kiss_fftndr_free(cfg); + end_scenario("kiss_fftndr"); +} + +static void test_kfc(void) +{ + kiss_fft_cpx in[8], out[8]; + + memset(in, 0, sizeof(in)); + begin_scenario(); + kfc_fft(8, in, out); + kfc_ifft(8, in, out); + expect_wrapper_alloc("kfc_fft"); + kfc_cleanup(); + end_scenario("kfc"); +} + +int main(void) +{ + test_harness(); + test_kiss_fft(); + test_kiss_fftr(); + test_kiss_fftnd(); + test_kiss_fftndr(); + test_kfc(); + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +}