From 3888d8f5e9bfb4ccb71f991703fdfbb709092add Mon Sep 17 00:00:00 2001 From: David Zwarg Date: Wed, 8 Feb 2012 21:31:36 +0000 Subject: [PATCH] Do not allocate inside of 1 raster mapalgebra loop for datum values and nulls. git-svn-id: http://svn.osgeo.org/postgis/trunk@9120 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_pg/rt_pg.c | 65 ++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 4951337f1..983746e82 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -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 { -- 2.40.0