[GRASS-SVN] r67840 - grass-addons/grass7/raster/r.futures/r.futures.pga

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 15 21:05:07 PST 2016


Author: wenzeslaus
Date: 2016-02-15 21:05:07 -0800 (Mon, 15 Feb 2016)
New Revision: 67840

Modified:
   grass-addons/grass7/raster/r.futures/r.futures.pga/main.c
Log:
r.futures: improve memory handling

Reduce memory usage, free resources
and provide better error messages
when out of memory.


Modified: grass-addons/grass7/raster/r.futures/r.futures.pga/main.c
===================================================================
--- grass-addons/grass7/raster/r.futures/r.futures.pga/main.c	2016-02-16 02:56:25 UTC (rev 67839)
+++ grass-addons/grass7/raster/r.futures/r.futures.pga/main.c	2016-02-16 05:05:07 UTC (rev 67840)
@@ -173,7 +173,7 @@
     /** array of predictor variables ordered as p1,p2,p3,p1,p2,p3 */
     float *predictors;
     /** multiplicative factor on the probabilities */
-    double *consWeight;
+    float *consWeight;
 } t_Landscape;
 
 
@@ -435,6 +435,7 @@
                 ii++;
             }
         }
+        G_free(buffer);
         Rast_close(fd);
         G_verbose_message("Done");
     }
@@ -479,6 +480,8 @@
             ii++;
         }
     }
+    G_free(buffer);
+    Rast_close(fd);
     pParams->num_Regions = count_regions;
     G_verbose_message("Done");
 }
@@ -497,14 +500,14 @@
 
     G_verbose_message("Reading input rasters...");
     bRet = 0;
-    szBuff = (char *)malloc(_N_MAX_DYNAMIC_BUFF_LEN * sizeof(char));
+    szBuff = (char *) G_malloc(_N_MAX_DYNAMIC_BUFF_LEN * sizeof(char));
     if (szBuff) {
         for (j = 0; j < 3; j++) {
             /* workaround to skip loading constraint map so that it can be omitted in input */
             if (j == 2) {
                 pLandscape->consWeight = NULL;
                 if (pParams->consWeightFile)
-                    pLandscape->consWeight = (double *)G_malloc(pLandscape->totalCells * sizeof(double));
+                    pLandscape->consWeight = (float *)G_malloc(pLandscape->totalCells * sizeof(float));
                 else
                     continue;
             }
@@ -645,6 +648,7 @@
 
                 }
             }
+            G_free(buffer);
             Rast_close(fd);
 
         }
@@ -693,6 +697,7 @@
         }
         Rast_put_c_row(out_fd, out_row);
     }
+    G_free(out_row);
     Rast_close(out_fd);
 
     struct Colors colors;
@@ -836,9 +841,9 @@
                                 _N_NEIGHBOUR_LIST_BLOCK_SIZE;
                             pNeighbours->aCandidates =
                                 (t_candidateNeighbour *)
-                                realloc(pNeighbours->aCandidates,
-                                        pNeighbours->nSpace *
-                                        sizeof(t_candidateNeighbour));
+                                G_realloc(pNeighbours->aCandidates,
+                                          pNeighbours->nSpace *
+                                          sizeof(t_candidateNeighbour));
                             if (!pNeighbours->aCandidates) {
                                 G_fatal_error("Memory error in addNeighbourIfPoss()");
                             }
@@ -1079,7 +1084,7 @@
     nWantToConvert = pLandscape->aParcelSizes[(int)
                                               (uniformRandom() *
                                                pLandscape->parcelSizes)];
-    anToConvert = (int *)malloc(sizeof(int) * nWantToConvert);
+    anToConvert = (int *) G_malloc(sizeof(int) * nWantToConvert);
     if (anToConvert) {
         /* in here goes code to fill up list of neighbours */
         nToConvert =
@@ -1364,7 +1369,7 @@
     G_verbose_message("Reading patch sizes...");
     fIn = fopen(pParams->parcelSizeFile, "rb");
     if (fIn) {
-        szBuff = (char *)malloc(_N_MAX_DYNAMIC_BUFF_LEN * sizeof(char));
+        szBuff = (char *) G_malloc(_N_MAX_DYNAMIC_BUFF_LEN * sizeof(char));
         if (szBuff) {
             /* just scan the file twice */
             nMaxParcels = 0;
@@ -1374,7 +1379,7 @@
             rewind(fIn);
             if (nMaxParcels) {
                 pLandscape->aParcelSizes =
-                    (int *)malloc(sizeof(int) * nMaxParcels);
+                    (int *) G_malloc(sizeof(int) * nMaxParcels);
                 if (pLandscape->aParcelSizes) {
                     while (fgets(szBuff, _N_MAX_DYNAMIC_BUFF_LEN, fIn)) {
                         pLandscape->aParcelSizes[pLandscape->parcelSizes] =
@@ -1704,8 +1709,8 @@
                 sParams.nProbLookup = atoi(inBuff + 1);
                 if (sParams.nProbLookup > 0) {
                     sParams.adProbLookup =
-                            (double *)malloc(sizeof(double) *
-                                             sParams.nProbLookup);
+                            (double *) G_malloc(sizeof(double) *
+                                                sParams.nProbLookup);
                     if (sParams.adProbLookup) {
                         parsedOK = 1;
                         i = 0;



More information about the grass-commit mailing list