]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use automatic memory management with GSL objects. 12324/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 27 May 2021 13:50:52 +0000 (07:50 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 27 May 2021 20:14:19 +0000 (14:14 -0600)
include/deal.II/base/function_cspline.h
source/base/function_cspline.cc

index b41d33cad3eddc3e8402a070479f347c09b38f3d..2c31db46aace78b97d12df0ef96b8029fd284ba9 100644 (file)
@@ -25,6 +25,8 @@
 
 #  include <gsl/gsl_spline.h>
 
+#  include <memory>
+
 DEAL_II_NAMESPACE_OPEN
 
 namespace Functions
@@ -82,11 +84,6 @@ namespace Functions
     CSpline(const std::vector<double> &interpolation_points,
             const std::vector<double> &interpolation_values);
 
-    /**
-     * Virtual destructor.
-     */
-    virtual ~CSpline() override;
-
     virtual double
     value(const Point<dim> & 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<gsl_interp_accel, void (*)(gsl_interp_accel *)> acc;
 
     /**
      * GSL cubic spline interpolator
      */
-    gsl_spline *cspline;
+    std::unique_ptr<gsl_spline, void (*)(gsl_spline *)> cspline;
 
     /**
      * A mutex for accelerator object.
index 46dc29241dcf29d418bcc8be686a421b2607cda7..da0ea69660dceab37ff3225ba3c778b01f83c044 100644 (file)
@@ -28,6 +28,10 @@ namespace Functions
                         const std::vector<double> &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 <int dim>
-  CSpline<dim>::~CSpline()
-  {
-    gsl_interp_accel_free(acc);
-    gsl_spline_free(cspline);
-    acc     = nullptr;
-    cspline = nullptr;
-  }
-
-
-
   template <int dim>
   double
   CSpline<dim>::value(const Point<dim> &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());
   }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.