From ab29cf7e641eca15124acb550e4244edd6b0784a Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Fri, 23 Sep 2011 02:10:07 +0200 Subject: [PATCH] Do weird dances to get php5-swig garbage collection going. Something tells me this is a lot more complex than it needs to be... --- php5-swig/attribute-empty.php | 2 +- php5-swig/attribute.php | 2 +- php5-swig/attribute2.php | 2 +- php5-swig/attribute3.php | 2 +- php5-swig/clone.php | 2 +- php5-swig/include.php | 2 +- php5-swig/namespace.php | 2 +- php5-swig/namespace2.php | 2 +- php5-swig/passthru.php | 2 +- php5-swig/simple.php | 2 +- php5-swig/structure.php | 2 +- php5-swig/xml-template.swig | 34 ++++++++++++++++++++++++++-------- 12 files changed, 37 insertions(+), 19 deletions(-) diff --git a/php5-swig/attribute-empty.php b/php5-swig/attribute-empty.php index 3886e7d..98555a2 100644 --- a/php5-swig/attribute-empty.php +++ b/php5-swig/attribute-empty.php @@ -9,5 +9,5 @@ $doc = XML_Template_process_file('../xml/clone.xml', array( 'color' => 'blue', '#things' => array(NULL) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/attribute.php b/php5-swig/attribute.php index d63cf69..003cf2b 100644 --- a/php5-swig/attribute.php +++ b/php5-swig/attribute.php @@ -10,5 +10,5 @@ $doc = XML_Template_process_file('../xml/clone.xml', array( array( 'li' => 'Warm, woolen mittens', 'li/class' => 'even' ) ) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/attribute2.php b/php5-swig/attribute2.php index 4b33237..0202885 100644 --- a/php5-swig/attribute2.php +++ b/php5-swig/attribute2.php @@ -10,5 +10,5 @@ $doc = XML_Template_process_file('../xml/clone.xml', array( array( 'li' => 'Warm, woolen mittens' ), ), array('odd', 'even')) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/attribute3.php b/php5-swig/attribute3.php index f3afc60..2156215 100644 --- a/php5-swig/attribute3.php +++ b/php5-swig/attribute3.php @@ -11,5 +11,5 @@ $doc = XML_Template_process_file('../xml/clone.xml', array( array( 'li' => 'Warm, woolen mittens' ), ), array('odd', 'even')) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/clone.php b/php5-swig/clone.php index a0f98a7..1193ea0 100644 --- a/php5-swig/clone.php +++ b/php5-swig/clone.php @@ -10,7 +10,7 @@ $doc = XML_Template_process_file('../xml/clone.xml', array( array( 'li' => 'Warm, woolen mittens' ) ) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/include.php b/php5-swig/include.php index b24e59f..92b71d6 100644 --- a/php5-swig/include.php +++ b/php5-swig/include.php @@ -9,5 +9,5 @@ $master = XML_Template_process_file('../xml/master.xml', array( 'h1' => 'Nice heading here', 'contents' => $doc ), true); -output_to_fd_and_free($master, 1); +output_to_fd($master, 1); ?> diff --git a/php5-swig/namespace.php b/php5-swig/namespace.php index f17fb74..9a653dc 100644 --- a/php5-swig/namespace.php +++ b/php5-swig/namespace.php @@ -8,5 +8,5 @@ $doc = XML_Template_process_file('../xml/namespace.xml', array( 'tagname' => 'foo', '#moretest' => 'bar' ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/namespace2.php b/php5-swig/namespace2.php index 06c7bd6..04622cf 100644 --- a/php5-swig/namespace2.php +++ b/php5-swig/namespace2.php @@ -5,5 +5,5 @@ $doc = XML_Template_process_file('../xml/namespace2.xml', array( 'title' => 'Namespace tests', '#hello' => 'Replaced.', ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/passthru.php b/php5-swig/passthru.php index 3b3822b..2496b18 100644 --- a/php5-swig/passthru.php +++ b/php5-swig/passthru.php @@ -2,7 +2,7 @@ dl("XML_Template_SWIG.so"); $doc = XML_Template_process_file('../xml/passthru.xml', array(), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/simple.php b/php5-swig/simple.php index 2f29d51..a5b7f04 100644 --- a/php5-swig/simple.php +++ b/php5-swig/simple.php @@ -5,7 +5,7 @@ $doc = XML_Template_process_file('../xml/simple.xml', array( 'title' => 'A very basic example', '#hello' => 'Hello world!' ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/structure.php b/php5-swig/structure.php index 2bc8c44..b27a040 100644 --- a/php5-swig/structure.php +++ b/php5-swig/structure.php @@ -8,5 +8,5 @@ $doc = XML_Template_process_file('../xml/structure.xml', array( array( '#inner' => 'Three' ), ) ), true); -output_to_fd_and_free($doc, 1); +output_to_fd($doc, 1); ?> diff --git a/php5-swig/xml-template.swig b/php5-swig/xml-template.swig index a2dd2d1..225f8bb 100644 --- a/php5-swig/xml-template.swig +++ b/php5-swig/xml-template.swig @@ -1,9 +1,21 @@ %module XML_Template_SWIG +struct XmlDocPtrWrapper { + ~XmlDocPtrWrapper(); +}; + %{ +#include + #include "../c++11/xml-template.h" - + +struct XmlDocWrapper { + ~XmlDocWrapper() { xmlFreeDoc(ptr); } + xmlDocPtr ptr; +}; +typedef std::shared_ptr XmlDocPtrWrapper; + bool is_associative_array(HashTable *ht) { if (ht->nNumOfElements == 0) { @@ -59,11 +71,11 @@ Directive* convert_php_objects_to_directive(zval *obj) return new Replace(str); } case IS_RESOURCE: { - xmlDocPtr *doc; - if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_xmlDocPtr, 0) < 0 || doc == NULL) { + XmlDocPtrWrapper *doc; + if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_XmlDocPtrWrapper, 0) < 0 || doc == NULL) { return NULL; } - return new ReplaceInclude(xmlCopyDoc(*doc, 1)); + return new ReplaceInclude(xmlCopyDoc(doc->get()->ptr, 1)); } case IS_NULL: return NULL; @@ -75,11 +87,17 @@ Directive* convert_php_objects_to_directive(zval *obj) return NULL; } -xmlDocPtr XML_Template_process_file(const char *input_filename, Directive *root_directive, bool clean) +XmlDocPtrWrapper XML_Template_process_file(const char *input_filename, Directive *root_directive, bool clean) { xmlDocPtr ret = process_file(input_filename, root_directive, clean); delete root_directive; - return ret; + return XmlDocPtrWrapper(new XmlDocWrapper { ret }); +} + +void output_to_fd(XmlDocPtrWrapper doc, int fd) +{ + xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL); + xmlSaveFileTo(buf, doc->ptr, NULL); } %} @@ -88,6 +106,6 @@ xmlDocPtr XML_Template_process_file(const char *input_filename, Directive *root_ $1 = convert_php_objects_to_directive(*$input); } -xmlDocPtr XML_Template_process_file(const char *input_filename, Directive *root_directive, bool clean); -void output_to_fd_and_free(xmlDocPtr doc, int fd); +XmlDocPtrWrapper XML_Template_process_file(const char *input_filename, Directive *root_directive, bool clean); +void output_to_fd(XmlDocPtrWrapper doc, int fd); -- 2.39.2