[GRASS-SVN] r36047 - grass-addons/raster/r.univar2.zonal

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Feb 22 09:59:30 EST 2009


Author: mmetz
Date: 2009-02-22 09:59:29 -0500 (Sun, 22 Feb 2009)
New Revision: 36047

Modified:
   grass-addons/raster/r.univar2.zonal/globals.h
   grass-addons/raster/r.univar2.zonal/r.univar_main.c
   grass-addons/raster/r.univar2.zonal/stats.c
Log:
less memory used

Modified: grass-addons/raster/r.univar2.zonal/globals.h
===================================================================
--- grass-addons/raster/r.univar2.zonal/globals.h	2009-02-22 13:35:41 UTC (rev 36046)
+++ grass-addons/raster/r.univar2.zonal/globals.h	2009-02-22 14:59:29 UTC (rev 36047)
@@ -39,6 +39,7 @@
     CELL *cell_array;
     int map_type;
     void *nextp;
+    int n_alloc;
 } univar_stat;
 
 typedef struct

Modified: grass-addons/raster/r.univar2.zonal/r.univar_main.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/r.univar_main.c	2009-02-22 13:35:41 UTC (rev 36046)
+++ grass-addons/raster/r.univar2.zonal/r.univar_main.c	2009-02-22 14:59:29 UTC (rev 36047)
@@ -36,7 +36,7 @@
 	_("Raster map used for zoning, must be of type CELL");
 
     param.output_file = G_define_standard_option(G_OPT_F_OUTPUT);
-    param.output_file->required = YES;
+    param.output_file->required = NO;
     param.output_file->description =
 	_("Name for output file (if omitted or \"-\" output to stdout)");
 
@@ -290,6 +290,7 @@
 
 	for (col = 0; col < cols; col++) {
 
+	    /* skip NULL cells in zone map altogether */
 	    if (G_is_c_null_value(zptr)) {
 		ptr = G_incr_void_ptr(ptr, value_sz);
 		zptr++;
@@ -306,7 +307,30 @@
 		continue;
 	    }
 
-	    if (stats[zone].nextp) {
+	    if (param.extended->answer) {
+		/* check allocated memory */
+		if (stats[zone].n >= stats[zone].n_alloc) {
+		    stats[zone].n_alloc += 1000;
+		    size_t msize;
+		    if (map_type == DCELL_TYPE) {
+			msize = stats[zone].n_alloc * sizeof(DCELL);
+			stats[zone].dcell_array =
+			    (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize);
+			stats[zone].nextp = (void *)&(stats[zone].dcell_array[stats[zone].n]);
+		    }
+		    else if (map_type == FCELL_TYPE) {
+			msize = stats[zone].n_alloc * sizeof(FCELL);
+			stats[zone].fcell_array =
+			    (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize);
+			stats[zone].nextp = (void *)&(stats[zone].fcell_array[stats[zone].n]);
+		    }
+		    else if (map_type == CELL_TYPE) {
+			msize = stats[zone].n_alloc * sizeof(CELL);
+			stats[zone].cell_array =
+			    (CELL *)G_realloc((void *)stats[zone].cell_array, msize);
+			stats[zone].nextp = (void *)&(stats[zone].cell_array[stats[zone].n]);
+		    }
+		}
 		/* put the value into stats->XXXcell_array */
 		memcpy(stats[zone].nextp, ptr, value_sz);
 		stats[zone].nextp = G_incr_void_ptr(stats[zone].nextp, value_sz);

Modified: grass-addons/raster/r.univar2.zonal/stats.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/stats.c	2009-02-22 13:35:41 UTC (rev 36046)
+++ grass-addons/raster/r.univar2.zonal/stats.c	2009-02-22 14:59:29 UTC (rev 36047)
@@ -42,17 +42,23 @@
 	stats[i].cell_array = NULL;
 	stats[i].map_type = map_type;
 
+	stats[i].n_alloc = 0;
+
 	/* allocate memory for extended computation */
+	/* changed to on-demand block allocation */
+
 	if (param.extended->answer) {
-	    if (map_type == DCELL_TYPE)
-		stats[i].dcell_array =
-		    (DCELL *) G_calloc(size, sizeof(DCELL));
-	    if (map_type == FCELL_TYPE)
-		stats[i].fcell_array =
-		    (FCELL *) G_calloc(size, sizeof(FCELL));
-	    if (map_type == CELL_TYPE)
-		stats[i].cell_array = (CELL *) G_calloc(size, sizeof(CELL));
+	    if (map_type == DCELL_TYPE) {
+		stats[i].dcell_array = NULL;
+	    }
+	    else if (map_type == FCELL_TYPE) {
+		stats[i].fcell_array =NULL;
+	    }
+	    else if (map_type == CELL_TYPE) {
+		stats[i].cell_array = NULL;
+	    }
 	}
+
     }
 
     return stats;



More information about the grass-commit mailing list