Debian Codegraph @

lib/XML/LibXML/Devel.pm

raw
1 # $Id: $
2 # This is free software, you may use it and distribute it under the same terms as
3 # Perl itself.
4 #
5 # Copyright 2011 Joachim Zobel
6 #
7 package XML::LibXML::Devel;
8
9 use strict;
10 use warnings;
11
12 use XML::LibXML;
13
14 use vars qw ($VERSION);
15 $VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE
16
17 use 5.008_000;
18
19 use parent qw(Exporter);
20
21 use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS );
22
23 # This allows declaration use XML::LibXML::Devel ':all';
24 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
25 # will save memory.
26 our %EXPORT_TAGS = ( 'all' => [ qw(
27 node_to_perl
28 node_from_perl
29 refcnt_inc
30 refcnt_dec
31 refcnt
32 fix_owner
33 mem_used
34 ) ] );
35
36 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
37
38 # Preloaded methods go here.
39
40 1;
41 __END__
42
43 =head1 NAME
44
45 XML::LibXML::Devel - makes functions from LibXML.xs available
46
47 =head1 SYNOPSIS
48
49 /**********************************************
50 * C functions you want to access
51 */
52 xmlNode *return_node();
53 void receive_node(xmlNode *);
54
55 ###############################################
56 # XS Code
57 void *
58 xs_return_node
59 CODE:
60 RETVAL = return_node();
61 OUTPUT:
62 RETVAL
63
64 void
65 xs_receive_node
66 void *n
67 CODE:
68 receive_node(n);
69
70 ###############################################
71 # Perl code
72 use XML::LibXML::Devel;
73
74 sub return_node
75 {
76 my $raw_node = xs_return_node();
77 my $node = XML::LibXML::Devel::node_to_perl($raw_node);
78 XML::LibXML::Devel::refcnt_inc($raw_node);
79 return $node;
80 }
81
82 sub receive_node
83 {
84 my ($node) = @_;
85 my $raw_node = XML::LibXML::Devel::node_from_perl($node);
86 xs_receive_node($raw_node);
87 XML::LibXML::Devel::refcnt_inc($raw_node);
88 }
89
90
91 =head1 DESCRIPTION
92
93 C<XML::LibXML::Devel> makes functions from LibXML.xs available that
94 are needed to wrap libxml2 nodes in and out of XML::LibXML::Nodes.
95 This gives cleaner dependencies than using LibXML.so directly.
96
97 To XS a library that uses libxml2 nodes the first step is to
98 do this so that xmlNodePtr is passed as void *. These raw nodes
99 are then turned into libxml nodes by using this C<Devel> functions.
100
101 Be aware that this module is currently rather experimental. The function
102 names may change if I XS more functions and introduce a reasonable
103 naming convention.
104
105 Be also aware that this module is a great tool to cause segfaults and
106 introduce memory leaks. It does however provide a partial cure by making
107 C<xmlMemUsed> available as C<mem_used>.
108
109 =head1 FUNCTIONS
110
111 =head2 NODE MANAGEMENT
112
113 =over 1
114
115 =item node_to_perl
116
117 node_to_perl($raw_node);
118
119 Returns a LibXML::Node object. This has a proxy node with a reference
120 counter and an owner attached. The raw node will be deleted as soon
121 as the reference counter reaches zero.
122 If the C library is keeping a
123 pointer to the raw node, you need to call refcnt_inc immediately.
124 You also need to replace xmlFreeNode by a call to refcnt_dec.
125
126 =item node_to_perl
127
128 node_from_perl($node);
129
130 Returns a raw node. This is a void * pointer and you can do nothing
131 but passing it to functions that treat it as an xmlNodePtr. The
132 raw node will be freed as soon as its reference counter reaches zero.
133 If the C library is keeping a
134 pointer to the raw node, you need to call refcnt_inc immediately.
135 You also need to replace xmlFreeNode by a call to refcnt_dec.
136
137 =item refcnt_inc
138
139 refcnt_inc($raw_node);
140
141 Increments the raw nodes reference counter. The raw node must already
142 be known to perl to have a reference counter.
143
144 =item refcnt_dec
145
146 refcnt_dec($raw_node);
147
148 Decrements the raw nodes reference counter and returns the value it
149 had before. if the counter becomes zero or less,
150 this method will free the proxy node holding the reference counter.
151 If the node is part of a
152 subtree, refcnt_dec will fix the reference counts and delete
153 the subtree if it is not required any more.
154
155 =item refcnt
156
157 refcnt($raw_node);
158
159 Returns the value of the reference counter.
160
161 =item fix_owner
162
163 fix_owner($raw_node, $raw_parent);
164
165 This functions fixes the reference counts for an entire subtree.
166 it is very important to fix an entire subtree after node operations
167 where the documents or the owner node may get changed. this method is
168 aware about nodes that already belong to a certain owner node.
169
170 =back
171
172 =head2 MEMORY DEBUGGING
173
174 =over 1
175
176 =item $ENV{DEBUG_MEMORY}
177
178 BEGIN {$ENV{DEBUG_MEMORY} = 1;};
179 use XML::LibXML;
180
181 This turns on libxml2 memory debugging. It must be set before
182 XML::LibXML is loaded.
183
184
185 =item mem_used
186
187 mem_used();
188
189 Returns the number of bytes currently allocated.
190
191 =back
192
193 =head2 EXPORT
194
195 None by default.
196
197 =head1 SEE ALSO
198
199 This was created to support the needs of Apache2::ModXml2. So this
200 can serve as an example.
201
202 =head1 AUTHOR
203
204 Joachim Zobel E<lt>[email protected]<gt>
205
206 =head1 COPYRIGHT AND LICENSE
207
208 Copyright (C) 2011 by Joachim Zobel
209
210 This library is free software; you can redistribute it and/or modify
211 it under the same terms as Perl itself, either Perl version 5.10.1 or,
212 at your option, any later version of Perl 5 you may have available.
213
214
215 =cut
216