Преглед изворни кода

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.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61812 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras пре 10 година
родитељ
комит
b6a33f64df

+ 4 - 4
raster/r.li/r.li.daemon/avl.c

@@ -193,7 +193,7 @@ 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)
 {
 
     if (root != NULL) {
@@ -201,9 +201,9 @@ long avl_to_array(avl_node * root, long i, AVL_table * a)
 	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);
 	}

+ 1 - 1
raster/r.li/r.li.daemon/avl.h

@@ -37,7 +37,7 @@ avl_tree avl_make(const generic_cell k, const long n);
 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);
 
 

+ 6 - 6
raster/r.li/r.li.dominance/dominance.c

@@ -123,7 +123,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +456,7 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +643,7 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	/* 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;

+ 6 - 6
raster/r.li/r.li.pielou/pielou.c

@@ -123,7 +123,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +456,7 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +643,7 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	/* 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;

+ 6 - 6
raster/r.li/r.li.renyi/renyi.c

@@ -147,7 +147,7 @@ int calculate(int fd, struct area_entry *ad, char **par, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -297,7 +297,7 @@ int calculate(int fd, struct area_entry *ad, char **par, double *result)
 	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 @@ int calculateD(int fd, struct area_entry *ad, char **par, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -498,7 +498,7 @@ int calculateD(int fd, struct area_entry *ad, char **par, double *result)
 	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 @@ int calculateF(int fd, struct area_entry *ad, char **par, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -699,7 +699,7 @@ int calculateF(int fd, struct area_entry *ad, char **par, double *result)
 	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;

+ 7 - 6
raster/r.li/r.li.shannon/shannon.c

@@ -123,7 +123,8 @@ int calculate(int fd, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -456,7 +457,7 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -643,7 +644,7 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	/* 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;

+ 6 - 6
raster/r.li/r.li.simpson/simpson.c

@@ -123,7 +123,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = CELL_TYPE;
@@ -269,7 +269,7 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = DCELL_TYPE;
@@ -455,7 +455,7 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     long area = 0;
 
     avl_tree albero = NULL;
-    AVL_table *array;
+    AVL_table array;
     generic_cell uc;
 
     uc.t = FCELL_TYPE;
@@ -641,7 +641,7 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	/* 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);
 	}