<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.eiffelroom.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>eiffelroom - Protecting objects - Comments</title>
 <link>http://www.eiffelroom.org/article/protecting_objects</link>
 <description>Comments for &quot;Protecting objects&quot;</description>
 <language>en</language>
<item>
 <title>It won&#039;t be collected until</title>
 <link>http://www.eiffelroom.org/article/protecting_objects#comment-461</link>
 <description>&lt;p&gt;It won&#039;t be collected until eif_wean() is called.&lt;/p&gt;

</description>
 <pubDate>Wed, 11 Jun 2008 20:19:54 -0700</pubDate>
 <dc:creator>ted_eiffel</dc:creator>
 <guid isPermaLink="false">comment 461 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>When you eif_protect()</title>
 <link>http://www.eiffelroom.org/article/protecting_objects#comment-460</link>
 <description>&lt;p&gt;When you eif_protect() something in C, is this considered a reachable reference to the GC? i.e. if the Eiffel side of code loses reachability to an object that was passed to C and eif_protect&#039;ed, will it ever be collected?&lt;/p&gt;

</description>
 <pubDate>Wed, 11 Jun 2008 13:52:26 -0700</pubDate>
 <dc:creator>clemahieu</dc:creator>
 <guid isPermaLink="false">comment 460 at http://www.eiffelroom.org</guid>
</item>
<item>
 <title>Protecting objects</title>
 <link>http://www.eiffelroom.org/article/protecting_objects</link>
 <description>&lt;p&gt;Having looked at code for interfacing Eiffel to C and creating callbacks from C to Eiffel, I frequently found that things were not done properly, resulting in potential memory corruption or crashes.&lt;/p&gt;

&lt;p&gt;First, I like to point out the &lt;a href=&quot;http://docs.eiffel.com/eiffelstudio/technologies/cecil&quot;&gt;CECIL documentation&lt;/a&gt; which contains everything you need to know about interfacing C with Eiffel. It might be overwhelming so below is a simplified tutorial.&lt;/p&gt;


&lt;h2 id=&quot;toc9&quot;&gt;Typing&lt;/h2&gt;
 At the C level, Eiffel objects are known under 2 possible types: &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;geshifilter c&quot;&gt;EIF_REFERENCE&lt;/code&gt;: a direct reference to an Eiffel object&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;geshifilter c&quot;&gt;EIF_OBJECT&lt;/code&gt;: an indirect (or protected) reference to an Eiffel object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reason for having 2 types lies in the garbage collector (GC) which moves objects around at runtime. As a consequence, a direct reference is not always valid, meaning that if you use an EIF_REFERENCE variable, then it might points to the previous location of the object. This is why we have EIF_OBJECT, those variable are automatically updated by the GC. To access the Eiffel objects, one has to use the &lt;code class=&quot;geshifilter c&quot;&gt;eif_access&lt;/code&gt; function.&lt;/p&gt;


&lt;h2 id=&quot;toc10&quot;&gt;Callbacks&lt;/h2&gt;
 The corresponding C signature of an Eiffel routine is the same as the Eiffel routine except that there is an extra first argument for the target object of the call. In other words, the Eiffel code &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;a.&lt;span style=&quot;color: #000060;&quot;&gt;f&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; is equivalent to the C code &lt;div class=&quot;geshifilter c&quot; style=&quot;font-family: monospace;&quot;&gt;f&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;a, i&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;The types used for the declaration of the C routine are simply the Eiffel types prefixed by `EIF_&#039; for the basic types (INTEGER_XX, NATURAL_XX, POINTER, ....) and EIF_REFERENCE for all the other types. Therefore the following Eiffel routine: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;my_routine &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i: INTEGER_32&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; has the following C signature: &lt;div class=&quot;geshifilter c&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; my_routine &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;EIF_REFERENCE Current, EIF_INTEGER_32 i&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;


&lt;h2 id=&quot;toc11&quot;&gt;Passing an Eiffel routine to a C external&lt;/h2&gt;
 Instead of using the CECIL API for getting the address of an Eiffel routine, I&#039;m using an alternative which is much lighter and simpler to use in my opinion. This is the technique used in all the Eiffel Software libraries where a simple callback is needed.&lt;p&gt;Basically if you want to pass the function pointer of the Eiffel routine &lt;code class=&quot;geshifilter eiffel&quot;&gt;my_routine&lt;/code&gt; above, you simply have to wrap the following C routine: &lt;div class=&quot;geshifilter c&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; set_callback_function &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;*&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;EIF_REFERENCE, EIF_INTEGER_32&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt; into an Eiffel routine: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;set_callback_function &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_routine: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+POINTER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;POINTER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;external&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;C inline use &lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%&amp;quot;&lt;/span&gt;my_c_header.h&lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;alias&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;set_callback_function (($a_routine);&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt; And then simply call it and pass the address of the Eiffel routine: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;set_callback_function &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;$my_routine&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; We will suppose for the rest of the discussion that the &lt;code class=&quot;geshifilter c&quot;&gt;set_callback_function&lt;/code&gt; C routine sets a C variable &lt;code class=&quot;geshifilter c&quot;&gt;callback_function&lt;/code&gt;.&lt;/p&gt;


&lt;h2 id=&quot;toc12&quot;&gt;Passing the object target of the call to C&lt;/h2&gt;
 Now, we need to store the Eiffel objects on the C side. As we have seen above, we need to provide the C side with a protected reference to the Eiffel object, otherwise the reference might not be valid after a GC cycle. So on the C side, we need another C variable &lt;code class=&quot;geshifilter c&quot;&gt;eiffel_object&lt;/code&gt; which is of type EIF_OBJECT. To pass this object from the Eiffel side to the C side you can simply do the following: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;set_eiffel_object &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+POINTER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;POINTER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;external&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;C inline use &lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%&amp;quot;&lt;/span&gt;my_c_header.h&lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;alias&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;set_eiffel_object (eif_protect($a));&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt; which you call it that way: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Pass `my_object&#039; to the C side.&lt;/span&gt;&lt;br /&gt;
set_eiffel_object &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;$my_object&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; 
&lt;h2 id=&quot;toc13&quot;&gt;Calling the Eiffel routine from C&lt;/h2&gt;
 Then from the C code, calling the Eiffel routine is just: &lt;div class=&quot;geshifilter c&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;*&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;EIF_REFERENCE, EIF_INTEGER_32&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; callback_function&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;eif_access&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;eiffel_object&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, integer_value&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;</description>
 <comments>http://www.eiffelroom.org/article/protecting_objects#comments</comments>
 <category domain="http://www.eiffelroom.org/taxonomy/term/3">Tutorial</category>
 <category domain="http://www.eiffelroom.org/taxonomy/term/9">Advanced</category>
 <category domain="http://www.eiffelroom.org/tag/eiffelstudio">EiffelStudio</category>
 <category domain="http://www.eiffelroom.org/tag/c">C</category>
 <category domain="http://www.eiffelroom.org/tag/callback">callback</category>
 <category domain="http://www.eiffelroom.org/tag/cecil">CECIL</category>
 <pubDate>Tue, 15 Jan 2008 22:06:14 -0800</pubDate>
 <dc:creator>manus_eiffel</dc:creator>
 <guid isPermaLink="false">256 at http://www.eiffelroom.org</guid>
</item>
</channel>
</rss>
