[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 1fba94b: Library (data.h): two functions to al
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 1fba94b: Library (data.h): two functions to allocate and free gal_data_t ** |
Date: |
Wed, 26 Aug 2020 13:24:31 -0400 (EDT) |
branch: master
commit 1fba94b33cb9e7ae51a3944418d7ebc4464be44b
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Library (data.h): two functions to allocate and free gal_data_t **
In many contexts, it is necessary to pre-allocate a list of pointers to
datasets which are then filled at later stages of the processing. With the
two new 'gal_data_array_ptr_calloc' and 'gal_data_array_ptr_free'
functions, it is now very easy to allocate such an array and later free it.
---
NEWS | 2 ++
doc/gnuastro.texi | 9 +++++++++
lib/data.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
lib/gnuastro/data.h | 5 +++++
4 files changed, 65 insertions(+)
diff --git a/NEWS b/NEWS
index 19f398d..a1b57c6 100644
--- a/NEWS
+++ b/NEWS
@@ -72,6 +72,8 @@ See the end of the file for license conditions.
- Spectral lines library: SiIII, OIII, CIV, NV and rest of Lyman series.
- GAL_CONFIG_HAVE_WCSLIB_DIS_H: if the host's WCSLIB supports distortions.
- GAL_CONFIG_HAVE_WCSLIB_MJDREF: if the host's WCSLIB reads MJDREF keyword.
+ - gal_data_array_ptr_calloc: Allocate array of pointers to gal_data_t
+ - gal_data_array_ptr_free: Free all the datasets within the array and
itself.
- gal_fits_key_list_title_add: Add a title key word to the list.
- gal_fits_key_list_title_add_end: Add a title key word to the list's end.
- GAL_INTERPOLATE_NEIGHBORS_METRIC_RADIAL: Radial metric for interpolation.
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index fd168cb..f7e1b37 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -20213,6 +20213,15 @@ element of all the datasets will also be freed, see
@ref{Generic data
container}.
@end deftypefun
+@deftypefun {gal_data_t **} gal_data_array_ptr_calloc (size_t @code{size})
+Allocate an array of pointers to Gnuastro's generic data structure and
initalize all pointers to @code{NULL}.
+This is useful when you want to allocate individual datasets later (for
example with @code{gal_data_alloc}).
+@end deftypefun
+
+@deftypefun void gal_data_array_ptr_free (gal_data_t @code{**dataptr}, size_t
@code{size}, int @code{free_array});
+Free all the individual datasets within the elements of @code{dataptr}, then
free @code{dataptr} itself (the array of pointers that was probably allocated
with @code{gal_data_array_ptr_calloc}.
+@end deftypefun
+
@node Copying datasets, , Arrays of datasets, Library data container
@subsubsection Copying datasets
diff --git a/lib/data.c b/lib/data.c
index a487bcf..f41e29e 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -391,6 +391,55 @@ gal_data_array_free(gal_data_t *dataarr, size_t size, int
free_array)
+/* Create an array of gal_data_t pointers and initializes them. */
+gal_data_t **
+gal_data_array_ptr_calloc(size_t size)
+{
+ size_t i;
+ gal_data_t **out;
+
+ /* Allocate the array to keep the pointers. */
+ errno=0;
+ out=malloc(size*sizeof *out);
+ if(out==NULL)
+ error(EXIT_FAILURE, errno, "%s: %zu bytes for 'out'", __func__,
+ size*sizeof *out);
+
+ /* Initialize all the pointers to NULL and return. */
+ for(i=0;i<size;++i) out[i]=NULL;
+ return out;
+}
+
+
+
+
+
+/* Assuming that we have an array of pointers to data structures, this
+ function frees them. */
+void
+gal_data_array_ptr_free(gal_data_t **dataptr, size_t size, int free_array)
+{
+ size_t i;
+ for(i=0;i<size;++i)
+ {
+ /* If the user doesn't want to free the array, it must be because
+ they are keeping its pointer somewhere else (that their own
+ responsability!), so we can just set it to NULL for the
+ 'gal_data_free' to not free it. */
+ if(free_array==0)
+ dataptr[i]->array=NULL;
+
+ /* Free this data structure. */
+ gal_data_free(dataptr[i]);
+ }
+
+ /* Free the 'gal_data_t **'. */
+ free(dataptr);
+}
+
+
+
+
diff --git a/lib/gnuastro/data.h b/lib/gnuastro/data.h
index 0d8d2f7..63c6967 100644
--- a/lib/gnuastro/data.h
+++ b/lib/gnuastro/data.h
@@ -263,6 +263,11 @@ gal_data_array_calloc(size_t size);
void
gal_data_array_free(gal_data_t *dataarr, size_t num, int free_array);
+gal_data_t **
+gal_data_array_ptr_calloc(size_t size);
+
+void
+gal_data_array_ptr_free(gal_data_t **dataptr, size_t size, int free_array);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnuastro-commits] master 1fba94b: Library (data.h): two functions to allocate and free gal_data_t **,
Mohammad Akhlaghi <=