Enable C++ use in selection methods.
authorTeemu Murtola <teemu.murtola@gmail.com>
Fri, 24 Aug 2012 09:56:42 +0000 (12:56 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Tue, 28 Aug 2012 03:45:13 +0000 (06:45 +0300)
The method that frees the internal data structure for a selection method
now also has the responsibility of freeing the data structure itself.
This makes it possible to allocate the data structure using operator new
in the initialization function, and then correctly free it.

Prerequisite for using C++ types in selection methods before #652 is
implemented.

Change-Id: I99e5a907cbdc4975d20becb0fb1d33e0026c135c

src/gromacs/selection/selelem.cpp
src/gromacs/selection/selmethod.h
src/gromacs/selection/sm_compare.cpp
src/gromacs/selection/sm_distance.cpp
src/gromacs/selection/sm_insolidangle.cpp
src/gromacs/selection/sm_keywords.cpp
src/gromacs/selection/sm_merge.cpp
src/gromacs/selection/sm_permute.cpp
src/gromacs/selection/sm_position.cpp
src/gromacs/selection/sm_same.cpp

index 1f40b07ba724d2e157704b5035350c7a053ce661..f841994ba8b880d8bb68b6767bcb721a2e1a98ac 100644 (file)
@@ -375,7 +375,10 @@ _gmx_selelem_free_method(gmx_ana_selmethod_t *method, void *mdata)
         {
             free_func(mdata);
         }
-        sfree(mdata);
+        else
+        {
+            sfree(mdata);
+        }
     }
 }
 
index 1aad394ab751d4f982e61b5e349bf5e36555bd57..dec0dc7e801d75b3149e9d1e5e843f4ae05b23ed 100644 (file)
  *    and \ref SMETH_VARNUMVAL methods.
  *  - sel_freefunc() should be provided if sel_datafunc() and/or
  *    sel_initfunc() allocate any dynamic memory in addition to the data
- *    structure itself.
+ *    structure itself (or allocates the data structure using some other means
+ *    than malloc()).
  *  - sel_updatefunc_pos() only makes sense for methods with \ref SMETH_DYNAMIC
  *    set.
  *  - At least one update function should be provided unless the method type is
@@ -477,9 +478,11 @@ typedef void  (*sel_outinitfunc)(t_topology *top, gmx_ana_selvalue_t *out,
  *
  * This function should be provided if the internal data structure contains
  * dynamically allocated data, and should free any such data.
- * The data structure itself should not be freed; this is handled automatically.
- * If there is no dynamically allocated data within the structure,
- * this function is not needed.
+ * The data structure itself should also be freed.
+ * For convenience, if there is no dynamically allocated data within the
+ * structure and the structure is allocated using malloc()/snew(), this
+ * function is not needed: the selection engine automatically frees the
+ * structure using sfree().
  * Any memory pointers received as values of parameters are managed externally,
  * and should not be freed.
  * Pointers set as the value pointer of \ref SPAR_ENUMVAL parameters should not
index dbe912800b0e2c58ab6f43f5e4356b0452ecbd14..0c4090b9915e829f0569b7e46e6e5775d755664f 100644 (file)
@@ -486,6 +486,7 @@ free_data_compare(void *data)
     {
         sfree(d->right.r);
     }
+    sfree(d);
 }
 
 /*!
index 4571f34d7189f030fa7b8843bfb17d11e7a5b105..7390b9f8c2e824637ee9e997ba188be7c35cf7e5 100644 (file)
@@ -236,6 +236,7 @@ free_data_common(void *data)
     {
         gmx_ana_nbsearch_free(d->nb);
     }
+    sfree(d);
 }
 
 /*!
index dd020b3bd16cb710fab21580f196855ba27cc871..0f4ecdcbac6bf1297f0f57173e200484ac72a1dd 100644 (file)
@@ -404,6 +404,7 @@ free_data_insolidangle(void *data)
     }
     free_surface_points(d);
     sfree(d->bin);
+    sfree(d);
 }
 
 /*!
index 5f61e9e7d9163572f2c299bad2e429c41ed1a76b..6533c651f183542968df01d3cbf019c094fcdc18 100644 (file)
@@ -564,6 +564,7 @@ free_data_kwstr(void *data)
         }
     }
     sfree(d->m);
+    sfree(d);
 }
 
 /*!
@@ -660,6 +661,7 @@ free_data_kweval(void *data)
     t_methoddata_kweval *d = (t_methoddata_kweval *)data;
 
     _gmx_selelem_free_method(d->kwmethod, d->kwmdata);
+    sfree(d);
 }
 
 /*!
index f9e5cfbd09735c4eca8d04327755452a2737da5d..c2ecf33a1b83ac316986cdd61f925704f5bf1eb0 100644 (file)
@@ -295,6 +295,7 @@ free_data_merge(void *data)
     t_methoddata_merge *d = (t_methoddata_merge *)data;
 
     gmx_ana_index_deinit(&d->g);
+    sfree(d);
 }
 
 /*!
index 117ce126892d4e5e3b184888024de7c9d7b0a9f2..388d6c3dc1ed4d7dc4094f15ae60a03f6fbbcdbb 100644 (file)
@@ -218,6 +218,7 @@ free_data_permute(void *data)
 
     gmx_ana_index_deinit(&d->g);
     sfree(d->rperm);
+    sfree(d);
 }
 
 /*!
index 53e1dc7819313996cd1caa429a9c86d7aa677015..2f15ec4ecd9a3611c3dc051231d37a5598e4c5a6 100644 (file)
@@ -336,6 +336,7 @@ free_data_pos(void *data)
 
     sfree(d->type);
     gmx_ana_poscalc_free(d->pc);
+    sfree(d);
 }
 
 /*!
index 94b944f4e7c6c204c56545ad4bfe6ceb8929130d..289c84ce41cb3c8dc88c6a227e4aa0c45512697d 100644 (file)
@@ -287,6 +287,7 @@ free_data_same(void *data)
     t_methoddata_same *d = (t_methoddata_same *)data;
 
     sfree(d->as_s_sorted);
+    sfree(d);
 }
 
 /*! \brief