[postgis-tickets] r17544 - PROJ: Use the cache memory context

Raul raul at rmr.ninja
Wed Jun 19 08:17:19 PDT 2019


Author: algunenano
Date: 2019-06-19 08:17:19 -0700 (Wed, 19 Jun 2019)
New Revision: 17544

Modified:
   trunk/liblwgeom/lwgeom_transform.c
   trunk/libpgcommon/lwgeom_cache.c
   trunk/libpgcommon/lwgeom_transform.c
   trunk/postgis/lwgeom_in_gml.c
Log:
PROJ: Use the cache memory context

Before it was using malloc/free manually, so with
this change we depend fully on Postgres allocator

References #4372


Modified: trunk/liblwgeom/lwgeom_transform.c
===================================================================
--- trunk/liblwgeom/lwgeom_transform.c	2019-06-19 15:16:15 UTC (rev 17543)
+++ trunk/liblwgeom/lwgeom_transform.c	2019-06-19 15:17:19 UTC (rev 17544)
@@ -326,7 +326,7 @@
 	uint8_t target_swapped = proj_crs_is_swapped(pj_target_crs);
 	proj_destroy(pj_target_crs);
 
-	LWPROJ *lp = malloc(sizeof(LWPROJ));
+	LWPROJ *lp = lwalloc(sizeof(LWPROJ));
 	lp->pj = pj;
 	lp->source_swapped = source_swapped;
 	lp->target_swapped = target_swapped;
@@ -365,7 +365,7 @@
 	int ret = lwgeom_transform(geom, lp);
 
 	proj_destroy(pj);
-	free(lp);
+	lwfree(lp);
 
 	return ret;
 }

Modified: trunk/libpgcommon/lwgeom_cache.c
===================================================================
--- trunk/libpgcommon/lwgeom_cache.c	2019-06-19 15:16:15 UTC (rev 17543)
+++ trunk/libpgcommon/lwgeom_cache.c	2019-06-19 15:17:19 UTC (rev 17544)
@@ -95,7 +95,9 @@
 		{
 			int i;
 
-			POSTGIS_DEBUGF(3, "Allocating PROJCache for portal with transform() MemoryContext %p", FIContext(fcinfo));
+			POSTGIS_DEBUGF(3,
+				       "Allocating PROJCache for portal with transform() MemoryContext %p",
+				       FIContext(fcinfo));
 			/* Put in any required defaults */
 			for (i = 0; i < PROJ_CACHE_ITEMS; i++)
 			{

Modified: trunk/libpgcommon/lwgeom_transform.c
===================================================================
--- trunk/libpgcommon/lwgeom_transform.c	2019-06-19 15:16:15 UTC (rev 17543)
+++ trunk/libpgcommon/lwgeom_transform.c	2019-06-19 15:17:19 UTC (rev 17544)
@@ -131,10 +131,10 @@
 		pj_free(pj->pj_from);
 	if (pj->pj_to)
 		pj_free(pj->pj_to);
-	free(pj);
+	pfree(pj);
 #else
 	proj_destroy(pj->pj);
-	free(pj);
+	pfree(pj);
 #endif
 }
 
@@ -311,7 +311,7 @@
 	/* Return the projection object from the hash */
 	he = (PJHashEntry *) hash_search(PJHash, key, HASH_FIND, NULL);
 
-	return he->projection;
+	return he ? he->projection : NULL;
 }
 
 
@@ -605,7 +605,7 @@
 static LWPROJ *
 AddToPROJSRSCache(PROJPortalCache *PROJCache, int32_t srid_from, int32_t srid_to)
 {
-	MemoryContext PJMemoryContext;
+	MemoryContext PJMemoryContext, oldContext;
 
 	PjStrs from_strs, to_strs;
 	char *pj_from_str, *pj_to_str;
@@ -621,8 +621,10 @@
 	if (!pjstrs_has_entry(&to_strs))
 		elog(ERROR, "got NULL for SRID (%d)", srid_to);
 
+	oldContext = MemoryContextSwitchTo(PROJCache->PROJSRSCacheContext);
+
 #if POSTGIS_PROJ_VERSION < 60
-	PJ* projection = malloc(sizeof(PJ));
+	PJ *projection = palloc(sizeof(PJ));
 	pj_from_str = from_strs.proj4text;
 	pj_to_str = to_strs.proj4text;
 	projection->pj_from = projpj_from_string(pj_from_str);
@@ -747,6 +749,7 @@
 	PROJCache->PROJSRSCache[PROJCache->PROJSRSCacheCount].projection_mcxt = PJMemoryContext;
 	PROJCache->PROJSRSCacheCount++;
 
+	MemoryContextSwitchTo(oldContext);
 	return projection;
 }
 

Modified: trunk/postgis/lwgeom_in_gml.c
===================================================================
--- trunk/postgis/lwgeom_in_gml.c	2019-06-19 15:16:15 UTC (rev 17543)
+++ trunk/postgis/lwgeom_in_gml.c	2019-06-19 15:17:19 UTC (rev 17544)
@@ -369,7 +369,7 @@
 		return NULL;
 	}
 	proj_destroy(pj);
-	free(lwp);
+	pfree(lwp);
 
 	return pa;
 }



More information about the postgis-tickets mailing list