/* execute query */
snprintf(sql, len, "SELECT srtext FROM spatial_ref_sys WHERE srid = %d LIMIT 1", srid);
spi_result = SPI_execute(sql, TRUE, 0);
- pfree(sql);
+ SPI_pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "rtpg_getSRTextSPI: Cannot find SRID (%d) in spatial_ref_sys", srid);
if (SPI_tuptable) SPI_freetuptable(tuptable);
return NULL;
}
- SPI_freetuptable(tuptable);
- SPI_finish();
-
- len = strlen(tmp);
- srs = (char *) palloc(sizeof(char) * (len + 1));
+ len = strlen(tmp) + 1;
+ srs = SPI_palloc(sizeof(char) * len);
if (NULL == srs) {
elog(ERROR, "rtpg_getSRTextSPI: Unable to allocate memory for srtext\n");
+ pfree(tmp);
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
return NULL;
}
- srs = strncpy(srs, tmp, len + 1);
+ strncpy(srs, tmp, len);
pfree(tmp);
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
+
return srs;
}
NULL, NULL,
TRUE, 0
);
- pfree(sql);
+ SPI_pfree(sql);
/* process resultset */
SPI_cursor_fetch(portal, TRUE, 1);
/* initialize rtn */
if (stats->count > 0) {
if (NULL == rtn) {
- rtn = (rt_bandstats) palloc(sizeof(struct rt_bandstats_t));
+ rtn = (rt_bandstats) SPI_palloc(sizeof(struct rt_bandstats_t));
if (NULL == rtn) {
elog(ERROR, "RASTER_summaryStatsCoverage: Unable to allocate memory for summary stats of coverage\n");
snprintf(sql, len, "SELECT min, max FROM _st_summarystats('%s','%s',%d,%d::boolean,%f)", tablename, colname, bandindex, (exclude_nodata_value ? 1 : 0), sample);
POSTGIS_RT_DEBUGF(3, "RASTER_histogramCoverage: %s", sql);
spi_result = SPI_execute(sql, TRUE, 0);
- pfree(sql);
+ SPI_pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_histogramCoverage: Could not get summary stats of coverage");
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
SPI_finish();
rt_raster_destroy(raster);
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
SPI_finish();
rt_raster_destroy(raster);
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covhist) free(covhist);
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
SRF_RETURN_DONE(funcctx);
/* coverage histogram */
if (NULL == covhist) {
- /*
- dustymugs 2011-08-25
- covhist is initialized using malloc instead of palloc due to
- strange memory issues where covvcnts is corrupted in
- subsequent calls of SRF
- */
- covhist = (rt_histogram) malloc(sizeof(struct rt_histogram_t) * count);
+ covhist = (rt_histogram) SPI_palloc(sizeof(struct rt_histogram_t) * count);
if (NULL == covhist) {
elog(ERROR, "RASTER_histogramCoverage: Unable to allocate memory for histogram of coverage");
+ pfree(hist);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (bin_width_count) pfree(bin_width);
- pfree(hist);
SRF_RETURN_DONE(funcctx);
}
}
/* do when there is no more left */
else {
- free(covhist2);
+ pfree(covhist2);
SRF_RETURN_DONE(funcctx);
}
}
snprintf(sql, len, "SELECT count FROM _st_summarystats('%s','%s',%d,%d::boolean,%f)", tablename, colname, bandindex, (exclude_nodata_value ? 1 : 0), sample);
POSTGIS_RT_DEBUGF(3, "stats sql: %s", sql);
spi_result = SPI_execute(sql, TRUE, 0);
- pfree(sql);
+ SPI_pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_quantileCoverage: Could not get summary stats of coverage");
SPI_cursor_fetch(portal, TRUE, 1);
}
+ covquant2 = SPI_palloc(sizeof(struct rt_quantile_t) * count);
+ for (i = 0; i < count; i++) {
+ covquant2[i].quantile = covquant[i].quantile;
+ covquant2[i].value = covquant[i].value;
+ }
+
+ if (NULL != covquant) pfree(covquant);
+ quantile_llist_destroy(&qlls, qlls_count);
+
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
- quantile_llist_destroy(&qlls, qlls_count);
if (quantiles_count) pfree(quantiles);
- /*
- dustymugs 2011-08-23
- covquant2 is initialized using malloc instead of palloc due to
- strange memory issues where covvcnts is corrupted in
- subsequent calls of SRF
- */
- covquant2 = malloc(sizeof(struct rt_quantile_t) * count);
- for (i = 0; i < count; i++) {
- covquant2[i].quantile = covquant[i].quantile;
- covquant2[i].value = covquant[i].value;
- }
- pfree(covquant);
-
POSTGIS_RT_DEBUGF(3, "%d quantiles returned", count);
/* Store needed information */
/* do when there is no more left */
else {
POSTGIS_RT_DEBUG(3, "done");
- free(covquant2);
+ pfree(covquant2);
SRF_RETURN_DONE(funcctx);
}
}
NULL, NULL,
TRUE, 0
);
- pfree(sql);
+ SPI_pfree(sql);
/* process resultset */
SPI_cursor_fetch(portal, TRUE, 1);
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covvcnts) free(covvcnts);
+ if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
SRF_RETURN_DONE(funcctx);
SPI_cursor_close(portal);
SPI_finish();
- if (NULL != covvcnts) free(covvcnts);
+ if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
SRF_RETURN_DONE(funcctx);
SPI_finish();
rt_raster_destroy(raster);
- if (NULL != covvcnts) free(covvcnts);
+ if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
SRF_RETURN_DONE(funcctx);
SPI_finish();
rt_raster_destroy(raster);
- if (NULL != covvcnts) free(covvcnts);
+ if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
SRF_RETURN_DONE(funcctx);
rt_raster_destroy(raster);
if (NULL == vcnts || !count) {
elog(NOTICE, "Unable to count the values for band at index %d", bandindex);
+
+ pfree(vcnts);
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_cursor_close(portal);
+ SPI_finish();
+
+ if (NULL != covvcnts) free(covvcnts);
+ if (search_values_count) pfree(search_values);
+
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "%d value counts returned", count);
if (NULL == covvcnts) {
- /*
- dustymugs 2011-08-23
- covvcnts is initialized using malloc instead of palloc due to
- strange memory issues where covvcnts is corrupted in
- subsequent calls of SRF
- */
- covvcnts = (rt_valuecount) malloc(sizeof(struct rt_valuecount_t) * count);
+ covvcnts = (rt_valuecount) SPI_palloc(sizeof(struct rt_valuecount_t) * count);
if (NULL == covvcnts) {
elog(ERROR, "RASTER_valueCountCoverage: Unable to allocate memory for value counts of coverage");
+ pfree(vcnts);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (search_values_count) pfree(search_values);
- pfree(vcnts);
SRF_RETURN_DONE(funcctx);
}
}
else {
covcount++;
- covvcnts = realloc(covvcnts, sizeof(struct rt_valuecount_t) * covcount);
+ covvcnts = SPI_repalloc(covvcnts, sizeof(struct rt_valuecount_t) * covcount);
if (NULL == covvcnts) {
elog(ERROR, "RASTER_valueCountCoverage: Unable to change allocated memory for value counts of coverage");
+ pfree(vcnts);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (search_values_count) pfree(search_values);
if (NULL != covvcnts) free(covvcnts);
- pfree(vcnts);
SRF_RETURN_DONE(funcctx);
}
}
/* do when there is no more left */
else {
- free(covvcnts2);
+ pfree(covvcnts2);
SRF_RETURN_DONE(funcctx);
}
}
sql = (char *) palloc(len + 1);
if (sql == NULL) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to allocate memory for expression parameter %d", exprpos[i]);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
+
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
+
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
if (argtype == NULL) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to allocate memory for prepared plan argtypes of expression parameter %d", exprpos[i]);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
+ pfree(sql);
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
- pfree(sql);
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
PG_RETURN_NULL();
}
if (spiplan[i] == NULL) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to create prepared plan of expression parameter %d", exprpos[i]);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
+ pfree(sql);
+ pfree(argtype);
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
- pfree(sql);
- pfree(argtype);
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
if (err != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to evaluate expression parameter %d", exprpos[i]);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
+ pfree(sql);
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
- pfree(sql);
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
PG_RETURN_NULL();
}
if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to get result of expression parameter %d", exprpos[i]);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
+ pfree(sql);
if (SPI_tuptable) SPI_freetuptable(tuptable);
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
- pfree(sql);
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
PG_RETURN_NULL();
}
if (err < 0) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to get pixel of %s raster", (i < 1 ? "FIRST" : "SECOND"));
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
if (calltype == TEXTOID) {
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
}
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
if (values == NULL) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to allocate memory for value parameters of prepared statement %d", i);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
pfree(values);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
memset(nulls, FALSE, argcount[i]);
if (err != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_mapAlgebra2: Unexpected error when running prepared statement %d", i);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to get result of prepared statement %d", i);
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
if (SPI_tuptable) SPI_freetuptable(tuptable);
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
if (rt_band_set_pixel(band, x, y, pixel) < 0) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to set pixel value of output raster");
- for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
- rt_raster_destroy(raster);
-
if (calltype == TEXTOID) {
for (k = 0; k < spicount; k++) SPI_freeplan(spiplan[k]);
SPI_finish();
}
+ for (k = 0; k < set_count; k++) rt_raster_destroy(_rast[k]);
+ rt_raster_destroy(raster);
+
PG_RETURN_NULL();
}
}