gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master c62b01e 031/125: Corrected incrementation issu


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c62b01e 031/125: Corrected incrementation issue with and and or operators
Date: Sun, 23 Apr 2017 22:36:31 -0400 (EDT)

branch: master
commit c62b01e539f5ad2f19bc425b35eaf9188aed2c88
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Corrected incrementation issue with and and or operators
    
    The `&&' and `||' operators will not necessarily read their second
    operand. So the default `BINARY_OP_OT_RT_LT_SET' wouldn't read the second
    array sometimes and this caused unsynchronized incrementations. So with
    this commit we have a new `BINARY_OP_INCR_OT_RT_LT_SET' macro specially for
    these types of operators. In this new macro, the second operand is
    incremented in a a separate expression.
    
    Also, the freeing of the keys linked list after reading keywords in
    `gal_fits_read_img_hdu' was moved after `gal_data_alloc' so the values are
    freed. In the old way, the two strings were left unfreed until the end of
    the program, because they were artificially set to NULL (mistakenly
    assuming that we won't need to copy them, while `gal_data_alloc' copies
    them).
---
 lib/data-arithmetic-binary.c | 18 ++++++++++++++++--
 lib/fits.c                   | 15 ++++++++-------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/lib/data-arithmetic-binary.c b/lib/data-arithmetic-binary.c
index 1bd24e7..4accfd1 100644
--- a/lib/data-arithmetic-binary.c
+++ b/lib/data-arithmetic-binary.c
@@ -265,6 +265,20 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 
 
+/* This is for operators like `&&' and `||', where the right operator is
+   not necessarily read (and thus incremented) incremented. */
+#define BINARY_OP_INCR_OT_RT_LT_SET(OP, OT, RT, LT) {                   \
+    LT *la=l->array;                                                    \
+    RT *ra=r->array;                                                    \
+    OT *oa=o->array, *of=oa + o->size;                                  \
+    if(l->size==r->size) do {*oa = *la++ OP *ra; ++ra;} while(++oa<of); \
+    else if(l->size==1)  do {*oa = *la   OP *ra; ++ra;} while(++oa<of); \
+    else                 do  *oa = *la++ OP *ra;        while(++oa<of); \
+  }
+
+
+
+
 
 /* For operators whose type may be any of the given inputs. */
 #define BINARY_OP_RT_LT_SET(OP, RT, LT)                            \
@@ -312,10 +326,10 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
       BINARY_OP_OT_RT_LT_SET(!=, unsigned char, RT, LT);           \
       break;                                                       \
     case GAL_DATA_OPERATOR_AND:                                    \
-      BINARY_OP_OT_RT_LT_SET(&&, unsigned char, RT, LT);           \
+      BINARY_OP_INCR_OT_RT_LT_SET(&&, unsigned char, RT, LT);      \
       break;                                                       \
     case GAL_DATA_OPERATOR_OR:                                     \
-      BINARY_OP_OT_RT_LT_SET(||, unsigned char, RT, LT);           \
+      BINARY_OP_INCR_OT_RT_LT_SET(||, unsigned char, RT, LT);      \
       break;                                                       \
     default:                                                       \
       error(EXIT_FAILURE, 0, "operator code %d not recognized in " \
diff --git a/lib/fits.c b/lib/fits.c
index 96f60e6..44b4cd8 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -1275,23 +1275,24 @@ gal_fits_read_img_hdu(char *filename, char *hdu, char 
*maskname,
 
 
   /* Read the possibly existing useful keywords. Note that the values are
-     in allocated strings in the keys[i] data structures. We don't want to
-     allocated them again, so we will just copy the pointers within the
-     `img' data structure and set the original value to NULL so it isn't
-     freed. */
+     in allocated strings in the keys[i] data structures. Note that we need
+     the linked list of keys to keep the `name' and `unit' pointers. We can
+     free the linked list after `gal_data_alloc' has read/copied the
+     values.*/
   gal_data_add_to_ll(&keysll, NULL, GAL_DATA_TYPE_STRING, 1, &dsize_key,
                         NULL, 0, -1, "EXTNAME", NULL, NULL);
   gal_data_add_to_ll(&keysll, NULL, GAL_DATA_TYPE_STRING, 1, &dsize_key,
                         NULL, 0, -1, "BUNIT", NULL, NULL);
   gal_fits_read_keywords_fptr(fptr, keysll, 0, 0);
-  if(keysll->status==0)       {str=keysll->array;       unit=*str; *str=NULL;}
-  if(keysll->next->status==0) {str=keysll->next->array; name=*str; *str=NULL;}
-  gal_data_free_ll(keysll);
+  if(keysll->status==0)       {str=keysll->array;       unit=*str; }
+  if(keysll->next->status==0) {str=keysll->next->array; name=*str; }
+
 
   /* Allocate the space for the array and for the blank values. */
   img=gal_data_alloc(NULL, type, (long)ndim, dsize, NULL, 0, minmapsize,
                      name, unit, NULL);
   blank=gal_data_alloc_blank(type);
+  gal_data_free_ll(keysll);
   free(dsize);
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]