MsPASS C++ API  2.4.3.dev1+g61b06b96
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
GSLFFTResources.h
1#ifndef MSPASS_ALGORITHMS_DECONVOLUTION_GSLFFTRESOURCES_H
2#define MSPASS_ALGORITHMS_DECONVOLUTION_GSLFFTRESOURCES_H
3
4#include "mspass/utility/MsPASSError.h"
5#include <gsl/gsl_fft_complex.h>
6#include <memory>
7#include <new>
8#include <string>
9
10namespace mspass::algorithms::deconvolution::detail {
11
13 void operator()(gsl_fft_complex_wavetable *p) const noexcept {
14 if (p != nullptr)
15 gsl_fft_complex_wavetable_free(p);
16 }
17};
18
20 void operator()(gsl_fft_complex_workspace *p) const noexcept {
21 if (p != nullptr)
22 gsl_fft_complex_workspace_free(p);
23 }
24};
25
26using GSLFFTWavetablePtr =
27 std::unique_ptr<gsl_fft_complex_wavetable, GSLFFTWavetableDeleter>;
28using GSLFFTWorkspacePtr =
29 std::unique_ptr<gsl_fft_complex_workspace, GSLFFTWorkspaceDeleter>;
30
32 GSLFFTWavetablePtr wavetable;
33 GSLFFTWorkspacePtr workspace;
34};
35
36/* Allocate the two GSL objects as one transaction. The unique pointers make
37 * failure of the workspace allocation release the already-created wavetable
38 * before bad_alloc escapes. */
39inline GSLFFTResources AllocateGSLFFTResources(const int nfft,
40 const std::string &caller) {
41 if (nfft <= 0)
43 caller + ": fft length must be positive",
44 mspass::utility::ErrorSeverity::Fatal);
45 GSLFFTWavetablePtr wavetable(gsl_fft_complex_wavetable_alloc(nfft));
46 if (!wavetable)
47 throw std::bad_alloc();
48 GSLFFTWorkspacePtr workspace(gsl_fft_complex_workspace_alloc(nfft));
49 if (!workspace)
50 throw std::bad_alloc();
51 return {std::move(wavetable), std::move(workspace)};
52}
53
54} // namespace mspass::algorithms::deconvolution::detail
55
56#endif
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38