From: Björn Harrtell Date: Tue, 25 Apr 2017 21:10:58 +0000 (+0000) Subject: Restore debug log and improve memory context/allocations X-Git-Tag: 2.4.0alpha~122 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3055fb5b590c4d89dbca37da0a9eeefe346d523;p=postgis Restore debug log and improve memory context/allocations git-svn-id: http://svn.osgeo.org/postgis/trunk@15370 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/postgis/lwgeom_out_geobuf.c b/postgis/lwgeom_out_geobuf.c index 30aaf5ee7..5d1cdcba1 100644 --- a/postgis/lwgeom_out_geobuf.c +++ b/postgis/lwgeom_out_geobuf.c @@ -49,12 +49,11 @@ Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS) lwerror("Missing libprotobuf-c"); PG_RETURN_NULL(); #else - MemoryContext aggcontext; + MemoryContext aggcontext, oldcxt = NULL; struct geobuf_agg_context *ctx; if (!AggCheckCallContext(fcinfo, &aggcontext)) - lwerror("pgis_asmvt_transfn: called in non-aggregate context"); - MemoryContextSwitchTo(aggcontext); + aggcontext = CurrentMemoryContext; if (PG_ARGISNULL(0)) { ctx = palloc(sizeof(*ctx)); @@ -63,6 +62,7 @@ Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS) ctx->geom_name = text_to_cstring(PG_GETARG_TEXT_P(1)); geobuf_agg_init_context(ctx); } else { + oldcxt = MemoryContextSwitchTo(aggcontext); ctx = (struct geobuf_agg_context *) PG_GETARG_POINTER(0); } @@ -71,6 +71,10 @@ Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS) ctx->row = PG_GETARG_HEAPTUPLEHEADER(2); geobuf_agg_transfn(ctx); + + if (oldcxt) + MemoryContextSwitchTo(oldcxt); + PG_RETURN_POINTER(ctx); #endif } diff --git a/postgis/lwgeom_out_mvt.c b/postgis/lwgeom_out_mvt.c index b797773c8..562fe424e 100644 --- a/postgis/lwgeom_out_mvt.c +++ b/postgis/lwgeom_out_mvt.c @@ -79,12 +79,11 @@ Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS) lwerror("Missing libprotobuf-c"); PG_RETURN_NULL(); #else - MemoryContext aggcontext; + MemoryContext aggcontext, oldcxt = NULL; struct mvt_agg_context *ctx; if (!AggCheckCallContext(fcinfo, &aggcontext)) - lwerror("pgis_asmvt_transfn: called in non-aggregate context"); - MemoryContextSwitchTo(aggcontext); + aggcontext = CurrentMemoryContext; if (PG_ARGISNULL(0)) { ctx = palloc(sizeof(*ctx)); @@ -101,6 +100,7 @@ Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS) PG_FREE_IF_COPY(geom_name, 3); mvt_agg_init_context(ctx); } else { + oldcxt = MemoryContextSwitchTo(aggcontext); ctx = (struct mvt_agg_context *) PG_GETARG_POINTER(0); } @@ -109,7 +109,11 @@ Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS) ctx->row = PG_GETARG_HEAPTUPLEHEADER(4); mvt_agg_transfn(ctx); + PG_FREE_IF_COPY(ctx->row, 4); + if (oldcxt) + MemoryContextSwitchTo(oldcxt); + PG_RETURN_POINTER(ctx); #endif } diff --git a/postgis/mvt.c b/postgis/mvt.c index f4780ca8e..d956e1d9b 100644 --- a/postgis/mvt.c +++ b/postgis/mvt.c @@ -619,8 +619,7 @@ void mvt_agg_transfn(struct mvt_agg_context *ctx) GSERIALIZED *gs = (GSERIALIZED *) PG_DETOAST_DATUM(datum); LWGEOM *lwgeom = lwgeom_from_gserialized(gs); - // TODO: find out why a POSTGIS_DEBUGF affects encoding/uthash (memory issue?) - //POSTGIS_DEBUGF(3, "mvt_agg_transfn encoded feature count: %d", layer->n_features); + POSTGIS_DEBUGF(3, "mvt_agg_transfn encoded feature count: %d", layer->n_features); layer->features[layer->n_features++] = feature; encode_geometry(ctx, lwgeom); diff --git a/postgis/uthash.h b/postgis/uthash.h index 57df956c5..5758e9fe7 100644 --- a/postgis/uthash.h +++ b/postgis/uthash.h @@ -80,13 +80,13 @@ typedef unsigned char uint8_t; #endif #ifndef uthash_fatal -#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */ +#define uthash_fatal(msg) lwerror("uthash: fatal error (out of memory,etc)") #endif #ifndef uthash_malloc -#define uthash_malloc(sz) malloc(sz) /* malloc fcn */ +#define uthash_malloc(sz) palloc(sz) /* malloc fcn */ #endif #ifndef uthash_free -#define uthash_free(ptr,sz) free(ptr) /* free fcn */ +#define uthash_free(ptr,sz) pfree(ptr) /* free fcn */ #endif #ifndef uthash_strlen #define uthash_strlen(s) strlen(s)