From: Wolfgang Bangerth Date: Thu, 27 May 2021 13:50:52 +0000 (-0600) Subject: Use automatic memory management with GSL objects. X-Git-Tag: v9.4.0-rc1~1323^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7cace7167af467146543c7058f3f951f195b00c;p=dealii.git Use automatic memory management with GSL objects. --- diff --git a/include/deal.II/base/function_cspline.h b/include/deal.II/base/function_cspline.h index b41d33cad3..2c31db46aa 100644 --- a/include/deal.II/base/function_cspline.h +++ b/include/deal.II/base/function_cspline.h @@ -25,6 +25,8 @@ # include +# include + DEAL_II_NAMESPACE_OPEN namespace Functions @@ -82,11 +84,6 @@ namespace Functions CSpline(const std::vector &interpolation_points, const std::vector &interpolation_values); - /** - * Virtual destructor. - */ - virtual ~CSpline() override; - virtual double value(const Point & point, const unsigned int component = 0) const override; @@ -123,12 +120,12 @@ namespace Functions /** * GSL accelerator for spline interpolation */ - gsl_interp_accel *acc; + std::unique_ptr acc; /** * GSL cubic spline interpolator */ - gsl_spline *cspline; + std::unique_ptr cspline; /** * A mutex for accelerator object. diff --git a/source/base/function_cspline.cc b/source/base/function_cspline.cc index 46dc29241d..da0ea69660 100644 --- a/source/base/function_cspline.cc +++ b/source/base/function_cspline.cc @@ -28,6 +28,10 @@ namespace Functions const std::vector &y_) : interpolation_points(x_) , interpolation_values(y_) + , acc(gsl_interp_accel_alloc(), + [](gsl_interp_accel *p) { gsl_interp_accel_free(p); }) + , cspline(gsl_spline_alloc(gsl_interp_cspline, interpolation_points.size()), + [](gsl_spline *p) { gsl_spline_free(p); }) { Assert(interpolation_points.size() > 0, ExcCSplineEmpty(interpolation_points.size())); @@ -43,11 +47,9 @@ namespace Functions interpolation_points[i], interpolation_points[i + 1])); - acc = gsl_interp_accel_alloc(); const unsigned int n = interpolation_points.size(); - cspline = gsl_spline_alloc(gsl_interp_cspline, n); // gsl_spline_init returns something but it seems nobody knows what - gsl_spline_init(cspline, + gsl_spline_init(cspline.get(), interpolation_points.data(), interpolation_values.data(), n); @@ -55,17 +57,6 @@ namespace Functions - template - CSpline::~CSpline() - { - gsl_interp_accel_free(acc); - gsl_spline_free(cspline); - acc = nullptr; - cspline = nullptr; - } - - - template double CSpline::value(const Point &p, const unsigned int) const @@ -82,7 +73,7 @@ namespace Functions interpolation_points.front(), interpolation_points.back())); - return gsl_spline_eval(cspline, x, acc); + return gsl_spline_eval(cspline.get(), x, acc.get()); } @@ -103,7 +94,7 @@ namespace Functions interpolation_points.front(), interpolation_points.back())); - const double deriv = gsl_spline_eval_deriv(cspline, x, acc); + const double deriv = gsl_spline_eval_deriv(cspline.get(), x, acc.get()); Tensor<1, dim> res; res[0] = deriv; return res; @@ -127,7 +118,7 @@ namespace Functions interpolation_points.front(), interpolation_points.back())); - return gsl_spline_eval_deriv2(cspline, x, acc); + return gsl_spline_eval_deriv2(cspline.get(), x, acc.get()); }