[postgis-commits] svn - r3502 - in trunk: liblwgeom lwgeom

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Thu Jan 8 09:04:01 PST 2009


Author: pramsey
Date: 2009-01-08 09:04:00 -0800 (Thu, 08 Jan 2009)
New Revision: 3502

Modified:
   trunk/liblwgeom/Makefile.in
   trunk/liblwgeom/liblwgeom.h
   trunk/liblwgeom/lwcollection.c
   trunk/liblwgeom/lwgeom.c
   trunk/liblwgeom/lwgeom_api.c
   trunk/liblwgeom/lwline.c
   trunk/liblwgeom/lwmline.c
   trunk/liblwgeom/lwmpoint.c
   trunk/liblwgeom/lwmpoly.c
   trunk/liblwgeom/lwpoint.c
   trunk/liblwgeom/lwpoly.c
   trunk/liblwgeom/ptarray.c
   trunk/lwgeom/lwgeom_functions_analytic.c
   trunk/lwgeom/lwgeom_geojson.c
Log:
Alter lwfree_* to deep-free memory. Add variants of *_release to support shallow-free. Go through lwgeom/ and replace any lwfree_* instances with *_release where appropriate. (Surprisingly few.)


Modified: trunk/liblwgeom/Makefile.in
===================================================================
--- trunk/liblwgeom/Makefile.in	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/Makefile.in	2009-01-08 17:04:00 UTC (rev 3502)
@@ -49,6 +49,9 @@
 	lwalgorithm.h
 
 all: liblwgeom.a
+ifeq ($(HAVE_CUNIT_H),1)
+	make -C cunit 
+endif
 
 liblwgeom.a: $(SA_OBJS) $(SA_HEADERS) 
 	ar rs liblwgeom.a $(SA_OBJS) 	

Modified: trunk/liblwgeom/liblwgeom.h
===================================================================
--- trunk/liblwgeom/liblwgeom.h	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/liblwgeom.h	2009-01-08 17:04:00 UTC (rev 3502)
@@ -1,3 +1,17 @@
+/**********************************************************************
+ * $Id$
+ *
+ * PostGIS - Spatial Types for PostgreSQL
+ * http://postgis.refractions.net
+ * Copyright 2001-2006 Refractions Research Inc.
+ * Copyright 2007-2008 Mark Cave-Ayland
+ * Copyright 2008 Paul Ramsey <pramsey at cleverelephant.ca>
+ *
+ * This is free software; you can redistribute and/or modify it under
+ * the terms of the GNU General Public Licence. See the COPYING file.
+ * 
+ **********************************************************************/
+
 #ifndef _LIBLWGEOM_H
 #define _LIBLWGEOM_H 1
 
@@ -43,9 +57,9 @@
 #define LW_FALSE 0
 
 /*
- * this will change to NaN when I figure out how to
- * get NaN in a platform-independent way
- */
+* this will change to NaN when I figure out how to
+* get NaN in a platform-independent way
+*/
 #define NO_VALUE 0.0
 #define NO_Z_VALUE NO_VALUE
 #define NO_M_VALUE NO_VALUE
@@ -57,6 +71,18 @@
 
 #endif
 
+/** 
+* Global functions for memory/logging handlers. 
+*/
+typedef void* (*lwallocator)(size_t size);
+typedef void* (*lwreallocator)(void *mem, size_t size);
+typedef void (*lwfreeor)(void* mem);
+typedef void (*lwreporter)(const char* fmt, va_list ap);
+extern lwreallocator lwrealloc_var;
+extern lwallocator lwalloc_var;
+extern lwfreeor lwfree_var;
+extern lwreporter lwerror_var;
+extern lwreporter lwnotice_var;
 
 /**
 * Supply the memory management and error handling functions you want your
@@ -64,6 +90,7 @@
 * @ingroup system
 */
 extern void lwgeom_init_allocators(void);
+
 /**
 * Apply the default memory management (malloc() and free()) and error handlers.
 * Called inside lwgeom_init_allocators() generally.
@@ -71,13 +98,13 @@
 */
 extern void lwgeom_install_default_allocators(void);
 
-
 /**
 * Write a notice out to the notice handler. Uses standard printf() substitutions.
 * Use for messages you always want output. For debugging, use LWDEBUG() or LWDEBUGF().
 * @ingroup logging
 */
 void lwnotice(const char *fmt, ...);
+
 /**
 * Write a notice out to the error handler. Uses standard printf() substitutions.
 * Use for errors you always want output. For debugging, use LWDEBUG() or LWDEBUGF().
@@ -85,19 +112,9 @@
 */
 void lwerror(const char *fmt, ...);
 
-
-/* Globals for memory/logging handlers. */
-typedef void* (*lwallocator)(size_t size);
-typedef void* (*lwreallocator)(void *mem, size_t size);
-typedef void (*lwfreeor)(void* mem);
-typedef void (*lwreporter)(const char* fmt, va_list ap);
-extern lwreallocator lwrealloc_var;
-extern lwallocator lwalloc_var;
-extern lwfreeor lwfree_var;
-extern lwreporter lwerror_var;
-extern lwreporter lwnotice_var;
-
-/* The default memory/logging handlers installed by lwgeom_install_default_allocators() */
+/**
+* The default memory/logging handlers installed by lwgeom_install_default_allocators() 
+*/
 void *default_allocator(size_t size);
 void *default_reallocator(void *mem, size_t size);
 void default_freeor(void *ptr);
@@ -740,14 +757,10 @@
  */
 LWGEOM *lwgeom_deserialize(uchar *serializedform);
 
-/*
- * Release memory associated with LWGEOM.
- * POINTARRAYs are not released as they are usually
- * pointers to user-managed memory.
- * BBOX is released.
- */
-void lwgeom_release(LWGEOM *lwgeom);
 
+
+
+
 /******************************************************************
  * LWMULTIx and LWCOLLECTION functions
  ******************************************************************/
@@ -937,24 +950,51 @@
 /* Check if to boxes are equal (considering FLOAT approximations) */
 char box2d_same(BOX2DFLOAT4 *box1, BOX2DFLOAT4 *box2);
 
+
+
 /****************************************************************
- * memory management -- these only delete the memory associated
- *  directly with the structure - NOT the stuff pointing into
- *  the original de-serialized info
+ * MEMORY MANAGEMENT
  ****************************************************************/
 
-
-extern void lwfree_inspected(LWGEOM_INSPECTED *inspected);
-extern void lwfree_point    (LWPOINT *pt);
-extern void lwfree_line     (LWLINE  *line);
-extern void lwfree_polygon  (LWPOLY  *poly);
+/* 
+ * The lwfree_* family of functions frees *all* memory associated 
+ * with the pointer, including the serialized__pointlist in the
+ * point arrays. Do not use these on LWGEOMs de-serialized from
+ * PG_LWGEOMs or they will try to free an underlying structure
+ * managed by PgSQL. Only use these on LWGEOMs you have 
+ * constructed yourself.
+ */
+ 
+extern void lwfree_point(LWPOINT *pt);
+extern void lwfree_line(LWLINE  *line);
+extern void lwfree_polygon(LWPOLY  *poly);
 extern void lwfree_pointarray(POINTARRAY *pa);
-extern void lwfree_mpoint   (LWMPOINT *mpt);
-extern void lwfree_mline   (LWMLINE *mline);
-extern void lwfree_mpolygon   (LWMPOLY *mpoly);
-extern void lwfree_collection   (LWCOLLECTION *col);
-extern void lwfree_geom (LWGEOM *geom);
+extern void lwfree_mpoint(LWMPOINT *mpt);
+extern void lwfree_mline(LWMLINE *mline);
+extern void lwfree_mpolygon(LWMPOLY *mpoly);
+extern void lwfree_collection(LWCOLLECTION *col);
+extern void lwfree_geom(LWGEOM *geom);
 
+extern void lwfree_inspected(LWGEOM_INSPECTED *inspected); /* TODO: make this deep free... */
+
+/*
+ * The *_release family of functions frees the LWGEOM structures
+ * surrounding the POINTARRAYs but leaves the POINTARRAYs 
+ * intact. Use these on LWGEOMs that have been de-serialized
+ * from PG_LWGEOMs. Do not use these on LWGEOMs you have 
+ * constructed yourself, or you will leak lots of memory.
+ */
+
+extern void lwgeom_release(LWGEOM *lwgeom);
+extern void lwpoly_release(LWPOLY *lwpoly);
+extern void lwline_release(LWLINE *lwline);
+extern void lwpoint_release(LWPOINT *lwpoint);
+extern void lwmpoly_release(LWMPOLY *lwpoly);
+extern void lwmline_release(LWMLINE *lwline);
+extern void lwmpoint_release(LWMPOINT *lwpoint);
+extern void lwcollection_release(LWCOLLECTION *lwcollection);
+
+
 /****************************************************************
  * utility
  ****************************************************************/
@@ -984,9 +1024,9 @@
 extern float nextafterf_custom(float x, float y);
 
 
-#define LW_MAX(a,b)	((a) >	(b) ? (a) : (b))
-#define LW_MIN(a,b)	((a) <= (b) ? (a) : (b))
-#define LW_ABS(a)	((a) <	(0) ? (-a) : (a))
+#define LW_MAX(a,b) ((a) >	(b) ? (a) : (b))
+#define LW_MIN(a,b) ((a) <= (b) ? (a) : (b))
+#define LW_ABS(a)   ((a) <	(0) ? (-a) : (a))
 
 
 /* general utilities */
@@ -1157,11 +1197,11 @@
 extern void deparse_hex(uchar str, char *result);
 
 /* Parser check flags */
-#define PARSER_CHECK_MINPOINTS	1
-#define PARSER_CHECK_ODD	2
-#define PARSER_CHECK_CLOSURE	4
+#define PARSER_CHECK_MINPOINTS  1
+#define PARSER_CHECK_ODD        2
+#define PARSER_CHECK_CLOSURE    4
 
-#define PARSER_CHECK_NONE	0
+#define PARSER_CHECK_NONE   0
 #define PARSER_CHECK_ALL	(PARSER_CHECK_MINPOINTS | PARSER_CHECK_ODD | PARSER_CHECK_CLOSURE)
 
 /*
@@ -1179,12 +1219,12 @@
 /*
  * Parser error messages (these must match the message array in lwgparse.c)
  */
-#define PARSER_ERROR_MOREPOINTS 	1
-#define PARSER_ERROR_ODDPOINTS		2	
-#define PARSER_ERROR_UNCLOSED		3 
-#define PARSER_ERROR_MIXDIMS		4	
-#define PARSER_ERROR_INVALIDGEOM	5
-#define PARSER_ERROR_INVALIDWKBTYPE	6
+#define PARSER_ERROR_MOREPOINTS     1
+#define PARSER_ERROR_ODDPOINTS      2	
+#define PARSER_ERROR_UNCLOSED       3 
+#define PARSER_ERROR_MIXDIMS        4	
+#define PARSER_ERROR_INVALIDGEOM    5
+#define PARSER_ERROR_INVALIDWKBTYPE 6
 
 
 /*
@@ -1284,11 +1324,11 @@
         LWGEOM **geoms;
 } LWMSURFACE;
 
-#define CURVETYPE       8
-#define COMPOUNDTYPE    9
-#define CURVEPOLYTYPE   13
-#define MULTICURVETYPE          14
-#define MULTISURFACETYPE        15
+#define CURVETYPE         8
+#define COMPOUNDTYPE      9
+#define CURVEPOLYTYPE    13
+#define MULTICURVETYPE   14
+#define MULTISURFACETYPE 15
 
 /******************************************************************
  * LWCURVE functions

Modified: trunk/liblwgeom/lwcollection.c
===================================================================
--- trunk/liblwgeom/lwcollection.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwcollection.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -18,6 +18,13 @@
 
 #define CHECK_LWGEOM_ZM 1
 
+void
+lwcollection_release(LWCOLLECTION *lwcollection)
+{
+  lwgeom_release(lwcollection_as_lwgeom(lwcollection));
+}
+
+
 LWCOLLECTION *
 lwcollection_construct(unsigned int type, int SRID, BOX2DFLOAT4 *bbox,
 	unsigned int ngeoms, LWGEOM **geoms)

Modified: trunk/liblwgeom/lwgeom.c
===================================================================
--- trunk/liblwgeom/lwgeom.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwgeom.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -14,7 +14,6 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
-/*#include "lwgeom_pg.h"*/
 #include "liblwgeom.h"
 #include "wktparse.h"
 

Modified: trunk/liblwgeom/lwgeom_api.c
===================================================================
--- trunk/liblwgeom/lwgeom_api.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwgeom_api.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -767,8 +767,7 @@
  *       points to.  No data conversion is done.
  */
 POINTARRAY *
-pointArray_construct(uchar *points, char hasz, char hasm,
-	uint32 npoints)
+pointArray_construct(uchar *points, char hasz, char hasm, uint32 npoints)
 {
 	POINTARRAY  *pa;
 	
@@ -1764,10 +1763,6 @@
 }
 
 
-void lwfree_pointarray(POINTARRAY *pa)
-{
-	lwfree(pa);
-}
 
 
 

Modified: trunk/liblwgeom/lwline.c
===================================================================
--- trunk/liblwgeom/lwline.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwline.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -243,7 +243,10 @@
 
 void lwfree_line (LWLINE  *line)
 {
-	lwfree(line->points);
+	if ( line->bbox ) 
+		lwfree(line->bbox);
+
+	lwfree_pointarray(line->points);
 	lwfree(line);
 }
 
@@ -368,6 +371,12 @@
 }
 
 void
+lwline_release(LWLINE *lwline)
+{
+  lwgeom_release(lwline_as_lwgeom(lwline));
+}
+
+void
 lwline_reverse(LWLINE *line)
 {
 	ptarray_reverse(line->points);

Modified: trunk/liblwgeom/lwmline.c
===================================================================
--- trunk/liblwgeom/lwmline.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwmline.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -15,6 +15,12 @@
 #include <string.h>
 #include "liblwgeom.h"
 
+void
+lwmline_release(LWMLINE *lwmline)
+{
+  lwgeom_release(lwmline_as_lwgeom(lwmline));
+}
+
 LWMLINE *
 lwmline_deserialize(uchar *srl)
 {

Modified: trunk/liblwgeom/lwmpoint.c
===================================================================
--- trunk/liblwgeom/lwmpoint.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwmpoint.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -15,6 +15,13 @@
 #include <string.h>
 #include "liblwgeom.h"
 
+void
+lwmpoint_release(LWMPOINT *lwmpoint)
+{
+  lwgeom_release(lwmpoint_as_lwgeom(lwmpoint));
+}
+
+
 LWMPOINT *
 lwmpoint_deserialize(uchar *srl)
 {

Modified: trunk/liblwgeom/lwmpoly.c
===================================================================
--- trunk/liblwgeom/lwmpoly.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwmpoly.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -16,6 +16,13 @@
 #include "liblwgeom.h"
 
 
+void
+lwmpoly_release(LWMPOLY *lwmpoly)
+{
+  lwgeom_release(lwmpoly_as_lwgeom(lwmpoly));
+}
+
+
 LWMPOLY *
 lwmpoly_deserialize(uchar *srl)
 {

Modified: trunk/liblwgeom/lwpoint.c
===================================================================
--- trunk/liblwgeom/lwpoint.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwpoint.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -300,9 +300,10 @@
 	return result;
 }
 
-void lwfree_point    (LWPOINT *pt)
+void lwfree_point(LWPOINT *pt)
 {
-	lwfree_pointarray(pt->point);
+	if(pt->point)
+		lwfree_pointarray(pt->point);
 	lwfree(pt);
 }
 
@@ -423,6 +424,13 @@
 	return result;
 }
 
+void
+lwpoint_release(LWPOINT *lwpoint)
+{
+  lwgeom_release(lwpoint_as_lwgeom(lwpoint));
+}
+
+
 /* check coordinate equality  */
 char
 lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)

Modified: trunk/liblwgeom/lwpoly.c
===================================================================
--- trunk/liblwgeom/lwpoly.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/lwpoly.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -358,11 +358,18 @@
 {
 	int t;
 
+	if ( poly->bbox )
+		lwfree(poly->bbox);
+
 	for (t=0;t<poly->nrings;t++)
 	{
-		lwfree_pointarray(poly->rings[t]);
+		if( poly->rings[t] )
+			lwfree_pointarray(poly->rings[t]);
 	}
 
+	if ( poly->rings ) 
+		lwfree(poly->rings);
+
 	lwfree(poly);
 }
 
@@ -482,6 +489,11 @@
 	}
 }
 
+void
+lwpoly_release(LWPOLY *lwpoly)
+{
+  lwgeom_release(lwpoly_as_lwgeom(lwpoly));
+}
 
 void
 lwpoly_reverse(LWPOLY *poly)

Modified: trunk/liblwgeom/ptarray.c
===================================================================
--- trunk/liblwgeom/ptarray.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/liblwgeom/ptarray.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -37,6 +37,17 @@
 
 }
 
+void lwfree_pointarray(POINTARRAY *pa)
+{
+	/* TODO 
+	*  Turn this on after retrofitting all calls to lwfree_ in /lwgeom 
+	if( pa->serialized_pointlist )
+		lwfree(pa->serialized_pointlist);
+  */
+  
+	lwfree(pa);
+}
+
 void
 ptarray_reverse(POINTARRAY *pa)
 {

Modified: trunk/lwgeom/lwgeom_functions_analytic.c
===================================================================
--- trunk/lwgeom/lwgeom_functions_analytic.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/lwgeom/lwgeom_functions_analytic.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -380,7 +380,8 @@
 		                           1);
 		point = lwpoint_construct(line->SRID, 0, opa);
 		srl = lwpoint_serialize(point);
-		lwfree_point(point);
+		/* We shouldn't need this, the memory context is getting freed on the next line.
+		lwfree_point(point); */
 		PG_RETURN_POINTER(PG_LWGEOM_construct(srl, line->SRID, 0));
 	}
 
@@ -415,7 +416,8 @@
 			                           1);
 			point = lwpoint_construct(line->SRID, 0, opa);
 			srl = lwpoint_serialize(point);
-			lwfree_point(point);
+			/* We shouldn't need this, the memory context is getting freed on the next line
+			lwfree_point(point); */
 			PG_RETURN_POINTER(PG_LWGEOM_construct(srl, line->SRID, 0));
 		}
 		tlength += slength;
@@ -430,7 +432,8 @@
 	                           1);
 	point = lwpoint_construct(line->SRID, 0, opa);
 	srl = lwpoint_serialize(point);
-	lwfree_point(point);
+	/* We shouldn't need this, the memory context is getting freed on the next line
+	lwfree_point(point); */
 	PG_RETURN_POINTER(PG_LWGEOM_construct(srl, line->SRID, 0));
 }
 /***********************************************************************

Modified: trunk/lwgeom/lwgeom_geojson.c
===================================================================
--- trunk/lwgeom/lwgeom_geojson.c	2009-01-08 16:06:48 UTC (rev 3501)
+++ trunk/lwgeom/lwgeom_geojson.c	2009-01-08 17:04:00 UTC (rev 3502)
@@ -470,7 +470,7 @@
                 if (i) ptr += sprintf(ptr, ",");
                 point=lwgeom_getpoint_inspected(insp, i);
 		ptr += pointArray_to_geojson(point->point, ptr, precision);
-                lwfree_point(point);
+                lwpoint_release(point);
 	}
 	ptr += sprintf(ptr, "]}");
 
@@ -538,7 +538,7 @@
 		ptr += pointArray_to_geojson(line->points, ptr, precision);
                 ptr += sprintf(ptr, "]");
 		
-                lwfree_line(line);
+                lwline_release(line);
 	}
 
 	ptr += sprintf(ptr, "]}");
@@ -614,7 +614,7 @@
                 	ptr += sprintf(ptr, "]");
 		}
                 ptr += sprintf(ptr, "]");
-                lwfree_polygon(poly);
+                lwpoly_release(poly);
 	}
 	ptr += sprintf(ptr, "]}");
 
@@ -721,19 +721,19 @@
 		case POINTTYPE:
 			point=lwgeom_getpoint_inspected(insp, 0);
                         size = asgeojson_point_size(point, NULL, bbox, precision);
-        	        lwfree_point(point);
+        	        lwpoint_release(point);
 			break;
 
 		case LINETYPE:
 			line=lwgeom_getline_inspected(insp, 0);
                         size = asgeojson_line_size(line, NULL, bbox, precision);
-       	                lwfree_line(line);
+       	                lwline_release(line);
 			break;
 			
 		case POLYGONTYPE:
 			poly=lwgeom_getpoly_inspected(insp, 0);
                         size = asgeojson_poly_size(poly, NULL, bbox, precision);
-			lwfree_polygon(poly);
+			lwpoly_release(poly);
 			break;
 
 		case MULTIPOINTTYPE:
@@ -776,19 +776,19 @@
 		case POINTTYPE:
 			point=lwgeom_getpoint_inspected(insp, 0);
                         ptr += asgeojson_point_buf(point, NULL, ptr, bbox, precision);
-        	        lwfree_point(point);
+        	        lwpoint_release(point);
 			break;
 
 		case LINETYPE:
 			line=lwgeom_getline_inspected(insp, 0);
                         ptr += asgeojson_line_buf(line, NULL, ptr, bbox, precision);
-       	                lwfree_line(line);
+       	                lwline_release(line);
 			break;
 			
 		case POLYGONTYPE:
 			poly=lwgeom_getpoly_inspected(insp, 0);
 			ptr += asgeojson_poly_buf(poly, NULL, ptr, bbox, precision);
-			lwfree_polygon(poly);
+			lwpoly_release(poly);
 			break;
 
 		case MULTIPOINTTYPE:



More information about the postgis-commits mailing list