]> granicus.if.org Git - postgis/commitdiff
Do not allocate inside of 1 raster mapalgebra loop for datum values and nulls.
authorDavid Zwarg <dzwarg@azavea.com>
Wed, 8 Feb 2012 21:31:36 +0000 (21:31 +0000)
committerDavid Zwarg <dzwarg@azavea.com>
Wed, 8 Feb 2012 21:31:36 +0000 (21:31 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@9120 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rt_pg.c

index 4951337f1c5d1efe6ee4688b6b393430b5001944..983746e822bd42d56e62c94d5d13ec8160ee7b88 100644 (file)
@@ -3053,6 +3053,32 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
 
         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: initexpr = %s", initexpr);
 
+        /* define values */
+        values = (Datum *) palloc(sizeof(Datum) * argcount);
+        if (values == NULL) {
+            elog(ERROR, "RASTER_mapAlgebraExpr: Unable to allocate memory for value parameters of prepared statement");
+
+            SPI_finish();
+
+            rt_raster_destroy(raster);
+            rt_raster_destroy(newrast);
+
+            PG_RETURN_NULL();
+        }
+
+        /* define nulls */
+        nulls = (char *)palloc(argcount);
+        if (nulls == NULL) {
+            elog(ERROR, "RASTER_mapAlgebraExpr: Unable to allocate memory for null parameters of prepared statement");
+
+            SPI_finish();
+
+            rt_raster_destroy(raster);
+            rt_raster_destroy(newrast);
+
+            PG_RETURN_NULL();
+        }
+
         /* Connect to SPI and prepare the expression */
         ret = SPI_connect();
         if (ret != SPI_OK_CONNECT) {
@@ -3096,31 +3122,7 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
             if (ret != -1 && FLT_NEQ(r, newnodatavalue)) {
                 if (skipcomputation == 0) {
                     if (initexpr != NULL) {
-                        /* define values */
-                        values = (Datum *) palloc(sizeof(Datum) * argcount);
-                        if (values == NULL) {
-                            elog(ERROR, "RASTER_mapAlgebraExpr: Unable to allocate memory for value parameters of prepared statement");
-
-                            SPI_finish();
-
-                            rt_raster_destroy(raster);
-                            rt_raster_destroy(newrast);
-
-                            PG_RETURN_NULL();
-                        }
-
-                        /* define nulls */
-                        nulls = (char *)palloc(argcount);
-                        if (nulls == NULL) {
-                            elog(ERROR, "RASTER_mapAlgebraExpr: Unable to allocate memory for null parameters of prepared statement");
-
-                            SPI_finish();
-
-                            rt_raster_destroy(raster);
-                            rt_raster_destroy(newrast);
-
-                            PG_RETURN_NULL();
-                        }
+                        /* Reset the null arg flags. */
                         memset(nulls, 'n', argcount);
 
                         for (i = 0; i < argkwcount; i++) {
@@ -3146,18 +3148,21 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
                         }
 
                         ret = SPI_execute_plan(spi_plan, values, nulls, FALSE, 0);
-                        pfree(values);
-                        pfree(nulls);
                         if (ret != SPI_OK_SELECT || SPI_tuptable == NULL ||
                                 SPI_processed != 1) {
-                            elog(ERROR, "RASTER_mapAlgebraExpr: Invalid construction"
-                                    " for expression. Aborting");
+                            elog(ERROR, "RASTER_mapAlgebraExpr: Error executing prepared plan."
+                                    " Aborting");
 
                             if (SPI_tuptable)
                                 SPI_freetuptable(tuptable);
 
                             SPI_freeplan(spi_plan);
                             SPI_finish();
+
+                            pfree(values);
+                            pfree(nulls);
+                            pfree(initexpr);
+
                             PG_RETURN_NULL();
                         }
 
@@ -3198,6 +3203,8 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
         SPI_freeplan(spi_plan);
         SPI_finish();
 
+        pfree(values);
+        pfree(nulls);
         pfree(initexpr);
     }
     else {