PageRenderTime 736ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/ttn-post-libtool-1-4-3-upgrade/SWIG/Doc/Manual/Advanced.html

#
HTML | 350 lines | 293 code | 53 blank | 4 comment | 0 complexity | b02f2caa0c57eb30a07009175faca205 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
  3. <!-- And munged by Dave's special Python script -->
  4. <html>
  5. <head>
  6. <title>Advanced Topics</title>
  7. </head>
  8. <body bgcolor="#ffffff">
  9. <a name="n1"></a><H1>13 Advanced Topics</H1>
  10. <!-- INDEX -->
  11. <ul>
  12. <li><a href="#n2">Creating multi-module packages</a>
  13. <ul>
  14. <li><a href="#n3">Runtime support (and potential problems)</a>
  15. <li><a href="#n4">Why doesn't C++ inheritance work between modules?</a>
  16. <li><a href="#n5">The SWIG runtime library</a>
  17. <li><a href="#n6">A few dynamic loading gotchas</a>
  18. </ul>
  19. <li><a href="#n7">Dynamic Loading of C++ modules</a>
  20. <li><a href="#n8">Inside the SWIG type-checker</a>
  21. <ul>
  22. <li><a href="#n9">Type equivalence</a>
  23. <li><a href="#n10">Type casting</a>
  24. <li><a href="#n11">Why a name based approach?</a>
  25. <li><a href="#n12">Performance of the type-checker</a>
  26. </ul>
  27. </ul>
  28. <!-- INDEX -->
  29. <b>Caution: This chapter is under repair!</b>
  30. <a name="n2"></a><H2>13.1 Creating multi-module packages</H2>
  31. SWIG can be used to create packages consisting of many different modules. However, there are some technical aspects of doing this and techniques for managing the problem.<p>
  32. <a name="n3"></a><H3>13.1.1 Runtime support (and potential problems)</H3>
  33. All SWIG generated modules rely upon a small collection of functions that are used during run-time. These functions are primarily used for pointer type-checking, exception handling, and so on. When you run SWIG, these functions are included in the wrapper file (and declared as static). If you create a system consisting of many modules, each one will have an identical copy of these runtime libraries :<p>
  34. <center><img src="ch11.1.png"></center><p>
  35. <p>
  36. This duplication of runtime libraries is usually harmless since there are no namespace conflicts and memory overhead is minimal. However, there is serious problem related to the fact that modules do not share type-information. This is particularly a problem when working with C++ (as described next).<p>
  37. <a name="n4"></a><H3>13.1.2 Why doesn't C++ inheritance work between modules?</H3>
  38. Consider for a moment the following two interface files :<p>
  39. <p>
  40. <blockquote><pre>// File : a.i
  41. %module a
  42. // Here is a base class
  43. class a {
  44. public:
  45. a();
  46. ~a();
  47. void foo(double);
  48. };
  49. // File : b.i
  50. %module b
  51. // Here is a derived class
  52. %import a.i // Gets definition of base class
  53. class b : public a {
  54. public:
  55. bar();
  56. };
  57. </pre></blockquote>
  58. When compiled into two separate modules, the code does not work properly. In fact, you get a type error such as the following:<p>
  59. <p>
  60. <blockquote><pre>
  61. [beazley@guinness shadow]$ <b>python</b>
  62. Python 1.4 (Jan 16 1997) [GCC 2.7.2]
  63. Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  64. &gt;&gt;&gt; <b>from a import *</b>
  65. &gt;&gt;&gt; <b>from b import *</b>
  66. &gt;&gt;&gt; <b># Create a new "b"</b>
  67. &gt;&gt;&gt; <b>b = new_b()</b>
  68. &gt;&gt;&gt; <b># Call a function in the base class</b>
  69. ...
  70. &gt;&gt;&gt; <b>a_foo(b,3)</b>
  71. Traceback (innermost last):
  72. File "&lt;stdin&gt;", line 1, in ?
  73. TypeError: Type error in argument 1 of a_foo. Expected _a_p.
  74. &gt;&gt;&gt;
  75. </pre></blockquote>
  76. <p>
  77. However, from our class definitions we know that "b" is an "a" by inheritance and there should be no type-error. This problem is directly due to the lack of type-sharing between modules. If we look closely at the module modules created here, they look like this :<p>
  78. <center><img src="ch11.2.png"></center><p>
  79. <p>
  80. The type information listed shows the acceptable values for various C datatypes. In the "a" module, we see that "a" can only accept instances of itself. In the "b" module, we see that "a" can accept both "a" and "b" instances--which is correct given that a "b" is an "a" by inheritance.<p>
  81. <p>
  82. Unfortunately, this problem is inherent in the method by which SWIG makes modules. When we made the "a" module, we had no idea what derived classes might be used at a later time. However, it's impossible to produce the proper type information until after we know all of the derived classes. A nice problem to be sure, but one that can be fixed by making all modules share a single copy of the SWIG run-time library.<p>
  83. <a name="n5"></a><H3>13.1.3 The SWIG runtime library</H3>
  84. To reduce overhead and to fix type-handling problems, it is possible to share the SWIG run-time functions between multiple modules. This requires the use of the SWIG runtime library which is optionally built during SWIG installation. To use the runtime libraries, follow these steps:<p>
  85. <p>
  86. 1. Build the SWIG run-time libraries. The <tt>SWIG/Runtime</tt> directory contains a makefile for doing this. If successfully built, you will end up with 8 files that are usually installed in <tt>/usr/local/lib</tt>.<p>
  87. <p>
  88. <blockquote><pre>
  89. libswigpl.a # Perl library (static)
  90. libswigpl.so # Perl library (shared)
  91. libswigpy.a # Python library (static)
  92. libswigpy.so # Python library (shared)
  93. libswigtcl8.a # Tcl 8.x library (static)
  94. libswigtcl8.so # Tcl 8.x library (shared)
  95. libswigrb.a # Ruby library (static)
  96. libswigrb.so # Ruby library (shared)
  97. </pre></blockquote>
  98. <p>
  99. Note that certain libraries may be missing due to missing packages or unsupported features (like dynamic loading) on your machine.<p>
  100. <p>
  101. 2. Compile all SWIG modules using the <tt>-c</tt> option. For example :<p>
  102. <p>
  103. <blockquote><pre>
  104. % <b>swig -c -python a.i</b>
  105. % <b>swig -c -python b.i</b>
  106. </pre></blockquote>
  107. The <tt>-c</tt> option tells SWIG to omit runtime support. It's now up to you to provide it separately--which we will do using our libraries.<p>
  108. <p>
  109. 3. Build SWIG modules by linking against the appropriate runtime libraries.<p>
  110. <p>
  111. <blockquote><pre>
  112. % <b>swig -c -python a.i</b>
  113. % <b>swig -c -python b.i</b>
  114. % <b>gcc -c a_wrap.c b_wrap.c -I/usr/local/include</b>
  115. % <b>ld -shared a_wrap.o b_wrap.o -lswigpy -o a.so</b>
  116. </pre></blockquote>
  117. or if building a new executable (static linking)<p>
  118. <p>
  119. <blockquote><pre>
  120. % <b>swig -c -tcl -ltclsh.i a.i</b>
  121. % <b>gcc a_wrap.c -I/usr/local/include -L/usr/local/lib -ltcl -lswigtcl -lm -o mytclsh</b>
  122. </pre></blockquote>
  123. <p>
  124. When completed you should now end up with a collection of modules like this:<p>
  125. <center><img src="ch11.3.png"></center><p>
  126. <p>
  127. <p>
  128. In this configuration, the runtime library manages all datatypes and other information between modules. This management process is dynamic in nature--when new modules are loaded, they contribute information to the run-time system. In the C++ world, one could incrementally load classes as needed. As this process occurs, type information is updated and base-classes learn about derived classes as needed.<p>
  129. <a name="n6"></a><H3>13.1.4 A few dynamic loading gotchas</H3>
  130. When working with dynamic loading, it is critical to check that only one copy of the run-time library is being loaded into the system. When working with <tt>.a</tt> library files, problems can sometimes occur so there are a few approaches to the problem.<p>
  131. <p>
  132. 1. Rebuild the scripting language executable with the SWIG runtime library attached to it. This is actually, fairly easy to do using SWIG. For example :<p>
  133. <p>
  134. <blockquote><pre>%module mytclsh
  135. %{
  136. static void *__embedfunc(void *a) { return a};
  137. %}
  138. void *__embedfunc(void *);
  139. %include tclsh.i
  140. </pre></blockquote>
  141. <p>
  142. Now, run SWIG and compile as follows:<p>
  143. <p>
  144. <blockquote><pre>
  145. % <b>swig -c -tcl mytclsh.i</b>
  146. % <b>gcc mytclsh_wrap.c -I/usr/local/include -L/usr/local/lib -ltcl -lswigtcl -ldl -lm \
  147. -o tclsh</b>
  148. </pre></blockquote>
  149. This produces a new executable "<tt>tclsh</tt>" that contains a copy of the SWIG runtime library. The weird <tt>__embedfunc()</tt> function is needed to force the functions in the runtime library to be included in the final executable.<p>
  150. <p>
  151. To make new dynamically loadable SWIG modules, simply compile as follows :<p>
  152. <p>
  153. <blockquote><pre>
  154. % <b>swig -c -tcl example.i</b>
  155. % <b>gcc -c example_wrap.c -I/usr/local/include</b>
  156. % <b>ld -shared example_wrap.o -o example.so</b>
  157. </pre></blockquote>
  158. Linking against the <tt>swigtcl</tt> library is no longer necessary as all of the functions are now included in the <tt>tclsh</tt> executable and will be resolved when your module is loaded.<p>
  159. <p>
  160. 2. Using shared library versions of the runtime library<p>
  161. <p>
  162. If supported on your machine, the runtime libraries will be built as shared libraries (indicated by a <tt>.so</tt>, <tt>.sl</tt>, or .<tt>dll</tt> suffix). To compile using the runtime libraries, you link process should look something like this:<p>
  163. <blockquote><pre>
  164. % <b>ld -shared swigtcl_wrap.o -o libswigtcl.so</b> # Irix
  165. % <b>gcc -shared swigtcl_wrap.o -o libswigtcl.so</b> # Linux
  166. % <b>ld -G swigtcl_wrap.o -o libswigtcl.so</b> # Solaris
  167. </pre></blockquote>
  168. In order for the <tt>libswigtcl.so</tt> library to work, it needs to be placed in a location where the dynamic loader can find it. Typically this is a system library directory (ie. <tt>/usr/local/lib</tt> or <tt>/usr/lib</tt>).<p>
  169. <p>
  170. When running with the shared libary version, you may get error messages such as the following:<p>
  171. <p>
  172. <blockquote><pre>Unable to locate libswigtcl.so</pre></blockquote>
  173. This indicates that the loader was unable to find the shared libary at run-time. To find shared libaries, the loader looks through a collection of predetermined paths. If the <tt>libswigtcl.so</tt> file is not in any of these directories, it results in an error. On most machines, you can change the loader search path by changing the Unix environment variable <tt>LD_LIBRARY_PATH</tt>, e.g.<p>
  174. <p>
  175. <blockquote><pre>% <b>setenv LD_LIBRARY_PATH .:/home/beazley/packages/lib</b></pre></blockquote>
  176. A somewhat better approach is to link your module with the proper path encoded. This is typically done using the `<tt>-rpath</tt>' or `<tt>-R</tt>' option to your linker (see the man page). For example:<p>
  177. <p>
  178. <blockquote><pre>% <b>ld -shared example_wrap.o example.o -rpath /home/beazley/packages/lib \
  179. -L/home/beazley/packages/lib -lswigtcl.so -o example.so</b>
  180. </pre></blockquote>
  181. The <tt>-rpath</tt> option encodes the location of shared libraries into your modules and gets around having to set the <tt>LD_LIBRARY_PATH</tt> variable.<p>
  182. <p>
  183. If all else fails, pull up the man pages for your linker and start playing around.<p>
  184. <a name="n7"></a><H2>13.2 Dynamic Loading of C++ modules</H2>
  185. Dynamic loading of C++ modules presents a special problem for many systems. This is because C++ modules often need additional supporting code for proper initialization and operation. Static constructors are also a bit of a problem.<p>
  186. <p>
  187. While the process of building C++ modules is, by no means, and exact science, here are a few rules of thumb to follow :<p>
  188. <p>
  189. <ul>
  190. <li>Don't use static constructors if at all possible (not always avoidable).
  191. <li>Try linking your module with the C++ compiler using a command like `c++ -shared'. This often solves alot of problems.
  192. <li>Sometimes it is necessary to link against special libraries. For example, modules compiled with g++ often need to be linked against the <tt>libgcc.a</tt>, <tt>libg++.a</tt>, and <tt>libstdc++.a</tt> libraries.
  193. <li>Read the compiler and linker man pages over and over until you have them memorized (this may not help in some cases however).
  194. <li>Search articles on Usenet, particularly in <tt>comp.lang.tcl</tt>, <tt>comp.lang.perl</tt>, <tt>comp.lang.python</tt> and <tt>comp.lang.ruby</tt>. Building C++ modules is a common problem.
  195. </ul>
  196. <p>
  197. The SWIG distribution contains some additional documentation about C++ modules in the Doc directory as well.<p>
  198. <a name="n8"></a><H2>13.3 Inside the SWIG type-checker</H2>
  199. The SWIG runtime type-checker plays a critical role in the correct operation of SWIG modules. It not only checks the validity of pointer types, but also manages C++ inheritance, and performs proper type-casting of pointers when necessary. This section provides some insight into what it does, how it works, and why it is the way it is.<p>
  200. <a name="n9"></a><H3>13.3.1 Type equivalence</H3>
  201. SWIG uses a name-based approach to managing pointer datatypes. For example, if you are using a pointer like "<tt>double *</tt>", the type-checker will look for a particular string representation of that datatype such as "<tt>_double_p</tt>". If no match is found, a type-error is reported.<p>
  202. <p>
  203. However, the matching process is complicated by the fact that datatypes may use a variety of different names. For example, the following declarations<p>
  204. <p>
  205. <blockquote><pre>typedef double Real;
  206. typedef Real * RealPtr;
  207. typedef double Float;
  208. </pre></blockquote>
  209. define two sets of equivalent types :<p>
  210. <p>
  211. <blockquote><pre>{double, Real, Float}
  212. {RealPtr, Real *}
  213. </pre></blockquote>
  214. All of the types in each set are freely interchangable and the type-checker knows about the relationships by managing a table of equivalences such as the following :<p>
  215. <blockquote><pre>
  216. double =&gt; { Real, Float }
  217. Real =&gt; { double, Float }
  218. Float =&gt; { double, Real }
  219. RealPtr =&gt; { Real * }
  220. Real * =&gt; { RealPtr }
  221. </pre></blockquote>
  222. <p>
  223. When you declare a function such as the following :<p>
  224. <p>
  225. <blockquote><pre>void foo(Real *a);
  226. </pre></blockquote>
  227. SWIG first checks to see if the argument passed is a "<tt>Real *</tt>". If not, it checks to see if it is any of the other equivalent types (<tt>double *</tt>, <tt>RealPtr</tt>, <tt>Float *</tt>). If so, the value is accepted and no error occurs.<p>
  228. <p>
  229. Derived versions of the various datatypes are also legal. For example, if you had a function like this,<p>
  230. <p>
  231. <blockquote><pre>void bar(Float ***a);
  232. </pre></blockquote>
  233. The type-checker will accept pointers of type <tt>double ***</tt> and <tt>Real ***.</tt> However, the type-checker does not always capture the full-range of possibilities. For example, a datatype of `<tt>RealPtr **</tt>' is equivalent to a `<tt>Float ***</tt>' but would be flagged as a type error. If you encounter this kind of problem, you can manually force SWIG to make an equivalence as follows:<p>
  234. <p>
  235. <blockquote><pre>// Tell the type checker that `Float_ppp' and `RealPtr_pp' are equivalent.
  236. %init %{
  237. SWIG_RegisterMapping("Float_ppp","RealPtr_pp",0);
  238. %}
  239. </pre></blockquote>
  240. Doing this should hardly ever be necessary (I have never encountered a case where this was necessary), but if all else fails, you can force the run-time type checker into doing what you want.<p>
  241. <p>
  242. Type-equivalence of C++ classes is handled in a similar manner, but is encoded in a manner to support inheritance. For example, consider the following classes hierarchy :<p>
  243. <p>
  244. <blockquote><pre>class A { };
  245. class B : public A { };
  246. class C : public B { };
  247. class D {};
  248. class E : public C, public D {};
  249. </pre></blockquote>
  250. The type-checker encodes this into the following sets :<p>
  251. <p>
  252. <blockquote><pre>A =&gt; { B, C, E } "B isa A, C isa A, E isa A"
  253. B =&gt; { C, E } "C isa B, E isa B"
  254. C =&gt; { E } "E isa C"
  255. D =&gt; { E } "E isa D"
  256. E =&gt; { }
  257. </pre></blockquote>
  258. The encoding reflects the class hierarchy. For example, any object of type "A" will also accept objects of type B,C, and E because these are all derived from A. However, it is not legal to go the other way. For example, a function operating on a object from class E will not accept an object from class A.<p>
  259. <a name="n10"></a><H3>13.3.2 Type casting</H3>
  260. When working with C++ classes, SWIG needs to perform proper typecasting between derived and base classes. This is particularly important when working with multiple inheritance. To do this, conversion functions are created such as the following :<p>
  261. <p>
  262. <blockquote><pre>void *EtoA(void *ptr) {
  263. E *in = (E *) ptr;
  264. A *out = (A *) in; // Cast using C++
  265. return (void *) out;
  266. }
  267. </pre></blockquote>
  268. All pointers are internally represented as void *, but conversion functions are always invoked when pointer values are converted between base and derived classes in a C++ class hierarchy.<p>
  269. <a name="n11"></a><H3>13.3.3 Why a name based approach?</H3>
  270. SWIG uses a name-based approach to type-checking for a number of reasons :<p>
  271. <p>
  272. <ul>
  273. <li>One of SWIG's main uses is code development and debugging. In this environment, the type name of an object turns out to be a useful piece of information in tracking down problems.
  274. <li>In languages like Perl, the name of a datatype is used to determine things like packages and classes. By using datatype names we get a natural mapping between C and Perl.
  275. <li>I believe using the original names of datatypes is more intuitive than munging them into something completely different.
  276. </ul>
  277. <p>
  278. An alternative to a name based scheme would be to generate type-signatures based on the structure of a datatype. Such a scheme would result in perfect type-checking, but I think it would also result in a very confusing scripting language module. For this reason, I see SWIG sticking with the name-based approach--at least for the foreseeable future. <p>
  279. <a name="n12"></a><H3>13.3.4 Performance of the type-checker</H3>
  280. The type-checker performs the following steps when matching a datatype :<p>
  281. <p>
  282. <dl>
  283. <dt>1. Check a pointer against the type supplied in the original C declaration. If there is a perfect match, we're done.
  284. <dt>2. Check the supplied pointer against a cache of recently used datatypes.
  285. <dt>3. Search for a match against the full list of equivalent datatypes.
  286. <dt>4. If not found, report an error.
  287. </dl>
  288. <p>
  289. Most well-structured C codes will find an exact match on the first attempt, providing the best possible performance. For C++ codes, it is quite common to be passing various objects of a common base-class around between functions. When base-class functions are invoked, it almost always results in a miscompare (because the type-checker is looking for the base-type). In this case, we drop down to a small cache of recently used datatypes. If we've used a pointer of the same type recently, it will be in the cache and we can match against it. For tight loops, this results in about 10-15% overhead over finding a match on the first try. Finally, as a last resort, we need to search the internal pointer tables for a match. This involves a combination of hash table lookup and linear search. If a match is found, it is placed into the cache and the result returned. If not, we finally report a type-mismatch.<p>
  290. <p>
  291. As a rule of thumb, C++ programs require somewhat more processing than C programs, but this seems to be avoidable. Also, keep in mind that performance penalties in the type-checker don't necessarily translate into big penalties in the overall application. Performance is most greatly affected by the efficiency of the target scripting language and the types of operations your C code is performing.<p>
  292. <p>
  293. <p>
  294. <p><hr>
  295. <address>SWIG 1.1 - Last Modified : Mon Aug 4 10:47:13 1997</address>
  296. </body>
  297. </html>