[postgis-tickets] r15370 - Restore debug log and improve memory context/allocations

bjorn at wololo.org bjorn at wololo.org
Tue Apr 25 14:10:58 PDT 2017


Author: bjornharrtell
Date: 2017-04-25 14:10:58 -0700 (Tue, 25 Apr 2017)
New Revision: 15370

Modified:
   trunk/postgis/lwgeom_out_geobuf.c
   trunk/postgis/lwgeom_out_mvt.c
   trunk/postgis/mvt.c
   trunk/postgis/uthash.h
Log:
Restore debug log and improve memory context/allocations

Modified: trunk/postgis/lwgeom_out_geobuf.c
===================================================================
--- trunk/postgis/lwgeom_out_geobuf.c	2017-04-25 20:53:32 UTC (rev 15369)
+++ trunk/postgis/lwgeom_out_geobuf.c	2017-04-25 21:10:58 UTC (rev 15370)
@@ -49,12 +49,11 @@
 	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 @@
 		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 @@
 	ctx->row = PG_GETARG_HEAPTUPLEHEADER(2);
 
 	geobuf_agg_transfn(ctx);
+
+	if (oldcxt)
+		MemoryContextSwitchTo(oldcxt);
+
 	PG_RETURN_POINTER(ctx);
 #endif
 }

Modified: trunk/postgis/lwgeom_out_mvt.c
===================================================================
--- trunk/postgis/lwgeom_out_mvt.c	2017-04-25 20:53:32 UTC (rev 15369)
+++ trunk/postgis/lwgeom_out_mvt.c	2017-04-25 21:10:58 UTC (rev 15370)
@@ -79,12 +79,11 @@
 	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 @@
 		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 @@
 	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
 }

Modified: trunk/postgis/mvt.c
===================================================================
--- trunk/postgis/mvt.c	2017-04-25 20:53:32 UTC (rev 15369)
+++ trunk/postgis/mvt.c	2017-04-25 21:10:58 UTC (rev 15370)
@@ -619,8 +619,7 @@
 	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);

Modified: trunk/postgis/uthash.h
===================================================================
--- trunk/postgis/uthash.h	2017-04-25 20:53:32 UTC (rev 15369)
+++ trunk/postgis/uthash.h	2017-04-25 21:10:58 UTC (rev 15370)
@@ -80,13 +80,13 @@
 #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)



More information about the postgis-tickets mailing list