]> granicus.if.org Git - postgis/commitdiff
Drop "lwgeom_init_allocators" need, add "lwgeom_set_handlers"
authorSandro Santilli <strk@keybit.net>
Thu, 27 Dec 2012 12:56:59 +0000 (12:56 +0000)
committerSandro Santilli <strk@keybit.net>
Thu, 27 Dec 2012 12:56:59 +0000 (12:56 +0000)
This change allows using liblwgeom from clients which cannot define
C-level methods for link-back (e.g. python ctypes). See #2089.

NOTE: existing clients should take care of calling the new function
      because their "lwgeom_init_allocators" won't be called anymore.
      Failure to do so will result in default allocators / reporters
      being used.

Thanks Giuseppe Sucameli for the base work on this

git-svn-id: http://svn.osgeo.org/postgis/trunk@10937 b70326c6-7e19-0410-871a-916f4a2858ee

14 files changed:
NEWS
doc/html/image_src/generator.c
liblwgeom/cunit/cu_tester.c
liblwgeom/liblwgeom.h.in
liblwgeom/lwutil.c
libpgcommon/lwgeom_pg.c
libpgcommon/lwgeom_pg.h
loader/shpcommon.c
postgis/postgis_module.c
raster/loader/raster2pgsql.c
raster/rt_pg/rt_pg.c
raster/test/core/testapi.c
raster/test/core/testwkb.c
raster/test/cunit/cu_tester.c

diff --git a/NEWS b/NEWS
index 0c366e5d9e20bfe1046123e2979e4e594227f7f2..79d733b1e0a409fd902c49d906f8cf419252aa93 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ PostGIS 2.1.0
 
 * Important / Breaking Changes  *
 
+  - #2089, liblwgeom: lwgeom_set_handlers replaces lwgeom_init_allocators.
   - #1653, Removed srid parameter from ST_Resample(raster) and variants
            with reference raster no longer apply reference raster's SRID.
   - #2026, ST_Union(raster) now unions all bands of all rasters
index 2fd32f3c583e0aee33693d3065572f8475f2166c..14628886221889ea2948a82856a1e3fdc90feaf9 100644 (file)
@@ -48,16 +48,6 @@ char *imageSize = "200x200";
 
 int getStyleName(char **styleName, char* line);
 
-/**
- * Set up liblwgeom to run in stand-alone mode using the
- * usual system memory handling functions.
- */
-void lwgeom_init_allocators(void)
-{
-       /* liblwgeom callback - install default handlers */
-       lwgeom_install_default_allocators();
-}
-
 /**
  * Writes the coordinates of a POINTARRAY to a char* where ordinates are
  * separated by a comma and coordinates by a space so that the coordinate
index 9e2998164652778e988a2896fad0ca4a0aa048e2..3dc8677dab10414c64c75fe8616225c358d955f3 100644 (file)
 #include "liblwgeom_internal.h"
 #include "cu_tester.h"
 
+/* Internal funcs */
+static void
+cu_errorreporter(const char *fmt, va_list ap);
+
 /* ADD YOUR SUITE HERE (1 of 2) */
 extern CU_SuiteInfo print_suite;
 extern CU_SuiteInfo algorithms_suite;
@@ -93,6 +97,9 @@ int main(int argc, char *argv[])
        int num_run;
        int num_failed;
 
+       /* install the custom error handler */
+       lwgeom_set_handlers(0, 0, 0, cu_errorreporter, 0);
+
        /* initialize the CUnit test registry */
        if (CUE_SUCCESS != CU_initialize_registry())
        {
@@ -232,17 +239,3 @@ cu_error_msg_reset()
 {
        memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
 }
-
-/*
-** Set up liblwgeom to run in stand-alone mode using the
-** usual system memory handling functions.
-*/
-void lwgeom_init_allocators(void)
-{
-       lwalloc_var = default_allocator;
-       lwrealloc_var = default_reallocator;
-       lwfree_var = default_freeor;
-       lwnotice_var = default_noticereporter;
-       lwerror_var = cu_errorreporter;
-}
-
index 6053c0ffe18efeed3bb24b441082f1be9a28991b..1cf353766f2619fb83b09aff1260236ce4713b38 100644 (file)
 * units tests at cunit/cu_tester.c and the loader/dumper programs at
 * ../loader/shp2pgsql.c are examples of non-PostGIS applications using liblwgeom.
 *
-* Programs using this library should set up the default memory managers and error
-* handlers by implementing an lwgeom_init_allocators() function, which can be as
-* a wrapper around the lwgeom_install_default_allocators() function if you want
-* no special handling for memory management and error reporting.
+* Programs using this library can install their custom memory managers and error
+* handlers by calling the lwgeom_set_handlers() function, otherwise the default 
+* ones will be used.
 */
 
 /**
@@ -177,25 +176,16 @@ 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
+* Install custom memory management and error handling functions you want your
 * application to use.
 * @ingroup system
+* @todo take a structure ?
 */
-extern void lwgeom_init_allocators(void);
-
-/**
-* Apply the default memory management (malloc() and free()) and error handlers.
-* Called inside lwgeom_init_allocators() generally.
-* @ingroup system
-*/
-extern void lwgeom_install_default_allocators(void);
+extern void lwgeom_set_handlers(lwallocator allocator, 
+        lwreallocator reallocator, lwfreeor freeor, lwreporter errorreporter,
+        lwreporter noticereporter);
 
 /**
  * Write a notice out to the notice handler.
@@ -217,16 +207,8 @@ void lwnotice(const char *fmt, ...);
  */
 void lwerror(const char *fmt, ...);
 
-/**
- * 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);
-void default_errorreporter(const char *fmt, va_list ap);
-void default_noticereporter(const char *fmt, va_list ap);
 
+/* TODO: move these elsewhere */
 extern int lw_vasprintf (char **result, const char *format, va_list args);
 extern int lw_asprintf
 #if __STDC__
index 549e095dc78924eed0e6b733dd2d8dfc9b396c70..3c4a6bbb069a75fb8e4b01938c8886b06ab025f4 100644 (file)
@@ -8,17 +8,19 @@
 #include "liblwgeom_internal.h"
 #include "lwgeom_log.h"
 
-void *init_allocator(size_t size);
-void init_freeor(void *mem);
-void *init_reallocator(void *mem, size_t size);
-void init_noticereporter(const char *fmt, va_list ap);
-void init_errorreporter(const char *fmt, va_list ap);
-
-lwallocator lwalloc_var = init_allocator;
-lwreallocator lwrealloc_var = init_reallocator;
-lwfreeor lwfree_var = init_freeor;
-lwreporter lwnotice_var = init_noticereporter;
-lwreporter lwerror_var = init_errorreporter;
+/* Default allocators */
+static void * default_allocator(size_t size);
+static void default_freeor(void *mem);
+static void * default_reallocator(void *mem, size_t size);
+lwallocator lwalloc_var = default_allocator;
+lwreallocator lwrealloc_var = default_reallocator;
+lwfreeor lwfree_var = default_freeor;
+
+/* Default reporters */
+static void default_noticereporter(const char *fmt, va_list ap);
+static void default_errorreporter(const char *fmt, va_list ap);
+lwreporter lwnotice_var = default_noticereporter;
+lwreporter lwerror_var = default_errorreporter;
 
 static char *lwgeomTypeName[] =
 {
@@ -41,7 +43,7 @@ static char *lwgeomTypeName[] =
 };
 
 /*
- * lwnotice/lwerror handlers
+ * Default lwnotice/lwerror handlers
  *
  * Since variadic functions cannot pass their parameters directly, we need
  * wrappers for these functions to convert the arguments into a va_list
@@ -74,57 +76,6 @@ lwerror(const char *fmt, ...)
        va_end(ap);
 }
 
-/*
- * Initialisation allocators
- *
- * These are used the first time any of the allocators are called
- * to enable executables/libraries that link into liblwgeom to
- * be able to set up their own allocators. This is mainly useful
- * for older PostgreSQL versions that don't have functions that
- * are called upon startup.
- */
-
-void *
-init_allocator(size_t size)
-{
-       lwgeom_init_allocators();
-
-       return lwalloc_var(size);
-}
-
-void
-init_freeor(void *mem)
-{
-       lwgeom_init_allocators();
-
-       lwfree_var(mem);
-}
-
-void *
-init_reallocator(void *mem, size_t size)
-{
-       lwgeom_init_allocators();
-
-       return lwrealloc_var(mem, size);
-}
-
-void
-init_noticereporter(const char *fmt, va_list ap)
-{
-       lwgeom_init_allocators();
-
-       (*lwnotice_var)(fmt, ap);
-}
-
-void
-init_errorreporter(const char *fmt, va_list ap)
-{
-       lwgeom_init_allocators();
-
-       (*lwerror_var)(fmt, ap);
-}
-
-
 /*
  * Default allocators
  *
@@ -133,27 +84,27 @@ init_errorreporter(const char *fmt, va_list ap)
  *
  */
 
-void *
+static void *
 default_allocator(size_t size)
 {
        void *mem = malloc(size);
        return mem;
 }
 
-void
+static void
 default_freeor(void *mem)
 {
        free(mem);
 }
 
-void *
+static void *
 default_reallocator(void *mem, size_t size)
 {
        void *ret = realloc(mem, size);
        return ret;
 }
 
-void
+static void
 default_noticereporter(const char *fmt, va_list ap)
 {
        char *msg;
@@ -171,7 +122,7 @@ default_noticereporter(const char *fmt, va_list ap)
        free(msg);
 }
 
-void
+static void
 default_errorreporter(const char *fmt, va_list ap)
 {
        char *msg;
@@ -190,21 +141,24 @@ default_errorreporter(const char *fmt, va_list ap)
        exit(1);
 }
 
-
-/*
- * This function should be called from lwgeom_init_allocators() by programs
- * which wish to use the default allocators above
+/**
+ * This function is called by programs which want to set up custom handling 
+ * for memory management and error reporting
+ *
+ * Only non-NULL values change their respective handler
  */
+void
+lwgeom_set_handlers(lwallocator allocator, lwreallocator reallocator,
+               lwfreeor freeor, lwreporter errorreporter,
+               lwreporter noticereporter) {
 
-void lwgeom_install_default_allocators(void)
-{
-       lwalloc_var = default_allocator;
-       lwrealloc_var = default_reallocator;
-       lwfree_var = default_freeor;
-       lwerror_var = default_errorreporter;
-       lwnotice_var = default_noticereporter;
-}
+       if ( allocator ) lwalloc_var = allocator;
+       if ( reallocator ) lwrealloc_var = reallocator;
+       if ( freeor ) lwfree_var = freeor;
 
+       if ( errorreporter ) lwerror_var = errorreporter;
+       if ( noticereporter ) lwnotice_var = noticereporter;
+}
 
 const char* 
 lwtype_name(uint8_t type)
index bb59080be12d7759965c019b85e3dfbebdfa476c..80cd3c9ee85e582fee0d91f34660ef36a80ad41c 100644 (file)
@@ -181,14 +181,10 @@ pg_notice(const char *fmt, va_list ap)
 }
 
 void
-lwgeom_init_allocators(void)
+pg_install_lwgeom_handlers(void)
 {
-       /* liblwgeom callback - install PostgreSQL handlers */
-       lwalloc_var = pg_alloc;
-       lwrealloc_var = pg_realloc;
-       lwfree_var = pg_free;
-       lwerror_var = pg_error;
-       lwnotice_var = pg_notice;
+       /* install PostgreSQL handlers */
+       lwgeom_set_handlers(pg_alloc, pg_realloc, pg_free, pg_error, pg_notice);
 }
 
 /**
index ab3d3ce09f4efac00c6c9983f79256eb76052763..594bff983d6ad6f1cb66d6de8983cfb330acce36 100644 (file)
 #include "liblwgeom.h"
 #include "pgsql_compat.h"
 
-void *pg_alloc(size_t size);
-void *pg_realloc(void *ptr, size_t size);
-void pg_free(void *ptr);
-void pg_error(const char *msg, va_list vp);
-void pg_notice(const char *msg, va_list vp);
-
+/* Install PosgreSQL handlers for liblwgeom use */
+void pg_install_lwgeom_handlers(void);
 
 /* Debugging macros */
 #if POSTGIS_DEBUG_LEVEL > 0
index 0c76d37ead26e4d5cd5ac802efde57a8ab7121b6..d2cb0a3f8b618acc03e676933a4b8062ba691c8c 100644 (file)
 
 #include <stdlib.h>
 #include "shpcommon.h"
-#include "../liblwgeom/liblwgeom.h" /* for lwgeom_install_default_allocators */
-
-
-/* liblwgeom allocator callback - install the defaults (malloc/free/stdout/stderr) */
-void lwgeom_init_allocators()
-{
-       lwgeom_install_default_allocators();
-}
 
 
 /**
index 62ca4e33561f4e41d9514e5aa45def59d6994fcc..6082df55d3424604a586002234ad98b49956a6f5 100644 (file)
@@ -92,6 +92,8 @@ _PG_init(void)
    );
 #endif
 
+    /* install PostgreSQL handlers */
+    pg_install_lwgeom_handlers();
 }
 
 /*
index e7f14ca48db5269d3c44f5b27a732bf4d4b7a12a..a293fc3bce480acdcef52e68ebb732318956fb52 100644 (file)
 #include "ogr_srs_api.h"
 #include <assert.h>
 
-/* This is needed by liblwgeom */
-void lwgeom_init_allocators(void) {
-       lwgeom_install_default_allocators();
-}
-
 static void
 loader_rt_error_handler(const char *fmt, va_list ap) {
        static const char *label = "ERROR: ";
index eac0e3796867436b6570c4ac37e31171146e68a0..b147e0d794d8898834f1bb353df071a3658bb01f 100644 (file)
 #define MAX_INT_CHARLEN 32
 
 /*
- * This is required for builds against pgsql 
+ * This is required for builds against pgsql
  */
 PG_MODULE_MAGIC;
 
+/*
+ * Module load callback
+ */
+void _PG_init(void);
+void
+_PG_init(void)
+{
+    /* Install liblwgeom handlers */
+    pg_install_lwgeom_handlers();
+
+    /* TODO: Install raster callbacks (see rt_init_allocators) */
+}
+
 /***************************************************************
  * Internal functions must be prefixed with rtpg_.  This is
  * keeping inline with the use of pgis_ for ./postgis C utility
@@ -17206,6 +17219,7 @@ Datum RASTER_clip(PG_FUNCTION_ARGS)
 
 /* ---------------------------------------------------------------- */
 /*  Memory allocation / error reporting hooks                       */
+/*  TODO: reuse the ones in libpgcommon ?                           */
 /* ---------------------------------------------------------------- */
 
 static void *
index fab6447f9a27e2167cf34d5163297850e9a00f81..5a8314e1d6fbdcfebf5f6a25fd7692328cad66e4 100644 (file)
@@ -8675,13 +8675,6 @@ main()
     return EXIT_SUCCESS;
 }
 
-/* This is needed by liblwgeom */
-void
-lwgeom_init_allocators(void)
-{
-    lwgeom_install_default_allocators();
-}
-
 
 void rt_init_allocators(void)
 {
index 1a7865e52a5c0748c8fa629ae6802f4ae1de4bf4..409e7780e7adcc34dddd1e989389353a06fa7e9f 100644 (file)
@@ -803,13 +803,6 @@ main()
     return EXIT_SUCCESS;
 }
 
-/* This is needed by liblwgeom */
-void
-lwgeom_init_allocators(void)
-{
-    lwgeom_install_default_allocators();
-}
-
 void rt_init_allocators(void)
 {
     rt_install_default_allocators();
index 1247a82ab68dec95ac7fc2497ba527d884fba6eb..1626a484aeca991d7da54f346cccca863421edb9 100644 (file)
 #include "CUnit/Basic.h"
 #include "cu_tester.h"
 
+/* Internal funcs */
+static void 
+cu_error_reporter(const char *fmt, va_list ap);
+
 /* ADD YOUR SUITE HERE (1 of 2) */
 extern CU_SuiteInfo pixtype_suite;
 extern CU_SuiteInfo raster_basics_suite;
@@ -62,6 +66,9 @@ int main(int argc, char *argv[])
        int num_run;
        int num_failed;
 
+       /* install the custom error handler */
+       lwgeom_set_handlers(0, 0, 0, cu_error_reporter, 0);
+
        /* initialize the CUnit test registry */
        if (CUE_SUCCESS != CU_initialize_registry())
        {
@@ -236,14 +243,6 @@ rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double
        return band;
 }
 
-void lwgeom_init_allocators(void) {
-       lwalloc_var = default_allocator;
-       lwrealloc_var = default_reallocator;
-       lwfree_var = default_freeor;
-       lwnotice_var = default_noticereporter;
-       lwerror_var = cu_error_reporter;
-}
-
 void rt_init_allocators(void) {
        rt_set_handlers(
                default_rt_allocator,