[GRASS-SVN] r61812 - in grass/trunk/raster/r.li: r.li.daemon r.li.dominance r.li.pielou r.li.renyi r.li.shannon r.li.simpson

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 5 19:42:55 PDT 2014


Author: wenzeslaus
Date: 2014-09-05 19:42:55 -0700 (Fri, 05 Sep 2014)
New Revision: 61812

Modified:
   grass/trunk/raster/r.li/r.li.daemon/avl.c
   grass/trunk/raster/r.li/r.li.daemon/avl.h
   grass/trunk/raster/r.li/r.li.dominance/dominance.c
   grass/trunk/raster/r.li/r.li.pielou/pielou.c
   grass/trunk/raster/r.li/r.li.renyi/renyi.c
   grass/trunk/raster/r.li/r.li.shannon/shannon.c
   grass/trunk/raster/r.li/r.li.simpson/simpson.c
Log:
r.li: fix memory handling (memory leak in avl_to_array function)

avl_to_array function was allocating one by one structures which were already allocated by callers. Callers were not freeing the memory allocated for structures. Also avl_to_array function was not respecting the size and type of the passed array, so the dereferencing was probbaly not working correctly. The type of array passed to the function is now AVL_table, not the pointer to it, because this is already a pointer.

The influenced modules now should run faster, for small resolutions, and with better (correct) results.

The naming of AVL_table and AVL_tableRow is still strange.


Modified: grass/trunk/raster/r.li/r.li.daemon/avl.c
===================================================================
--- grass/trunk/raster/r.li/r.li.daemon/avl.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.daemon/avl.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -193,7 +193,7 @@
 
 
 
-long avl_to_array(avl_node * root, long i, AVL_table * a)
+long avl_to_array(avl_node * root, long i, AVL_table a)
 {
 
     if (root != NULL) {
@@ -201,9 +201,9 @@
 	if (a == NULL)
 	    G_fatal_error("avl, avl_to_array: null value");
 	else {
-	    a[i] = G_malloc(sizeof(AVL_tableRow));
-	    a[i]->k = root->key;
-	    a[i]->tot = root->counter;
+	    //a[i] = G_malloc(sizeof(AVL_tableRow));
+	    a[i].k = root->key;
+	    a[i].tot = root->counter;
 	    i++;
 	    i = avl_to_array(root->right_child, i, a);
 	}

Modified: grass/trunk/raster/r.li/r.li.daemon/avl.h
===================================================================
--- grass/trunk/raster/r.li/r.li.daemon/avl.h	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.daemon/avl.h	2014-09-06 02:42:55 UTC (rev 61812)
@@ -37,7 +37,7 @@
 void avl_destroy(avl_tree root);
 avl_node *avl_find(const avl_tree root, const generic_cell k);
 int avl_add(avl_tree * root, const generic_cell k, const long n);
-long avl_to_array(avl_node * root, long i, AVL_table * a);
+long avl_to_array(avl_node * root, long i, AVL_table a);
 long howManyCell(const avl_tree root, const generic_cell k);
 
 

Modified: grass/trunk/raster/r.li/r.li.dominance/dominance.c
===================================================================
--- grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -123,7 +123,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -310,7 +310,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +456,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -497,7 +497,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +643,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;

Modified: grass/trunk/raster/r.li/r.li.pielou/pielou.c
===================================================================
--- grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -123,7 +123,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -310,7 +310,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +456,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -497,7 +497,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +643,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;

Modified: grass/trunk/raster/r.li/r.li.renyi/renyi.c
===================================================================
--- grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -147,7 +147,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -297,7 +297,7 @@
 	sum = 0;
 	sum2 = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
 	    sum2 += pi;
@@ -348,7 +348,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -498,7 +498,7 @@
 	sum = 0;
 	sum2 = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
 	    sum += pi;
@@ -549,7 +549,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -699,7 +699,7 @@
 	sum = 0;
 	sum2 = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
 	    sum2 += pi;

Modified: grass/trunk/raster/r.li/r.li.shannon/shannon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -123,7 +123,8 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
+    //AVL_tableRow *array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +270,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -310,7 +311,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +457,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;
@@ -497,7 +498,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +644,7 @@
 	/* calculate shannon */
 	shannon = 0;
 	for (i = 0; i < m; i++) {
-	    t = array[i]->tot;
+	    t = array[i].tot;
 	    perc = t / area;
 	    logarithm = log(perc);
 	    shannon += perc * logarithm;

Modified: grass/trunk/raster/r.li/r.li.simpson/simpson.c
===================================================================
--- grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-09-06 00:30:31 UTC (rev 61811)
+++ grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-09-06 02:42:55 UTC (rev 61812)
@@ -123,7 +123,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@
 	/* calculate simpson */
 	simpson = 0;
 	for (i = 0; i < m; i++) {
-	    t = (double)(array[i]->tot);
+	    t = (double)(array[i].tot);
 	    p = t / area;
 	    simpson += (p * p);
 	}
@@ -309,7 +309,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -455,7 +455,7 @@
 	/* calculate simpson */
 	simpson = 0;
 	for (i = 0; i < m; i++) {
-	    t = (double)(array[i]->tot);
+	    t = (double)(array[i].tot);
 	    p = t / area;
 	    simpson += (p * p);
 	}
@@ -495,7 +495,7 @@
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -641,7 +641,7 @@
 	/* calculate simpson */
 	simpson = 0;
 	for (i = 0; i < m; i++) {
-	    t = (double)(array[i]->tot);
+	    t = (double)(array[i].tot);
 	    p = t / area;
 	    simpson += (p * p);
 	}



More information about the grass-commit mailing list