From e4f2312d2d9cfbdc5e742308b265410d850c5798 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 27 Dec 2012 12:56:59 +0000 Subject: [PATCH] Drop "lwgeom_init_allocators" need, add "lwgeom_set_handlers" 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 --- NEWS | 1 + doc/html/image_src/generator.c | 10 --- liblwgeom/cunit/cu_tester.c | 21 ++---- liblwgeom/liblwgeom.h.in | 36 +++-------- liblwgeom/lwutil.c | 114 ++++++++++----------------------- libpgcommon/lwgeom_pg.c | 10 +-- libpgcommon/lwgeom_pg.h | 8 +-- loader/shpcommon.c | 8 --- postgis/postgis_module.c | 2 + raster/loader/raster2pgsql.c | 5 -- raster/rt_pg/rt_pg.c | 16 ++++- raster/test/core/testapi.c | 7 -- raster/test/core/testwkb.c | 7 -- raster/test/cunit/cu_tester.c | 15 ++--- 14 files changed, 80 insertions(+), 180 deletions(-) diff --git a/NEWS b/NEWS index 0c366e5d9..79d733b1e 100644 --- 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 diff --git a/doc/html/image_src/generator.c b/doc/html/image_src/generator.c index 2fd32f3c5..146288862 100644 --- a/doc/html/image_src/generator.c +++ b/doc/html/image_src/generator.c @@ -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 diff --git a/liblwgeom/cunit/cu_tester.c b/liblwgeom/cunit/cu_tester.c index 9e2998164..3dc8677da 100644 --- a/liblwgeom/cunit/cu_tester.c +++ b/liblwgeom/cunit/cu_tester.c @@ -16,6 +16,10 @@ #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; -} - diff --git a/liblwgeom/liblwgeom.h.in b/liblwgeom/liblwgeom.h.in index 6053c0ffe..1cf353766 100644 --- a/liblwgeom/liblwgeom.h.in +++ b/liblwgeom/liblwgeom.h.in @@ -32,10 +32,9 @@ * 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__ diff --git a/liblwgeom/lwutil.c b/liblwgeom/lwutil.c index 549e095dc..3c4a6bbb0 100644 --- a/liblwgeom/lwutil.c +++ b/liblwgeom/lwutil.c @@ -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) diff --git a/libpgcommon/lwgeom_pg.c b/libpgcommon/lwgeom_pg.c index bb59080be..80cd3c9ee 100644 --- a/libpgcommon/lwgeom_pg.c +++ b/libpgcommon/lwgeom_pg.c @@ -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); } /** diff --git a/libpgcommon/lwgeom_pg.h b/libpgcommon/lwgeom_pg.h index ab3d3ce09..594bff983 100644 --- a/libpgcommon/lwgeom_pg.h +++ b/libpgcommon/lwgeom_pg.h @@ -22,12 +22,8 @@ #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 diff --git a/loader/shpcommon.c b/loader/shpcommon.c index 0c76d37ea..d2cb0a3f8 100644 --- a/loader/shpcommon.c +++ b/loader/shpcommon.c @@ -14,14 +14,6 @@ #include #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(); -} /** diff --git a/postgis/postgis_module.c b/postgis/postgis_module.c index 62ca4e335..6082df55d 100644 --- a/postgis/postgis_module.c +++ b/postgis/postgis_module.c @@ -92,6 +92,8 @@ _PG_init(void) ); #endif + /* install PostgreSQL handlers */ + pg_install_lwgeom_handlers(); } /* diff --git a/raster/loader/raster2pgsql.c b/raster/loader/raster2pgsql.c index e7f14ca48..a293fc3bc 100644 --- a/raster/loader/raster2pgsql.c +++ b/raster/loader/raster2pgsql.c @@ -31,11 +31,6 @@ #include "ogr_srs_api.h" #include -/* 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: "; diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index eac0e3796..b147e0d79 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -65,10 +65,23 @@ #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 * diff --git a/raster/test/core/testapi.c b/raster/test/core/testapi.c index fab6447f9..5a8314e1d 100644 --- a/raster/test/core/testapi.c +++ b/raster/test/core/testapi.c @@ -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) { diff --git a/raster/test/core/testwkb.c b/raster/test/core/testwkb.c index 1a7865e52..409e7780e 100644 --- a/raster/test/core/testwkb.c +++ b/raster/test/core/testwkb.c @@ -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(); diff --git a/raster/test/cunit/cu_tester.c b/raster/test/cunit/cu_tester.c index 1247a82ab..1626a484a 100644 --- a/raster/test/cunit/cu_tester.c +++ b/raster/test/cunit/cu_tester.c @@ -15,6 +15,10 @@ #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, -- 2.40.0