<div dir="ltr">Hi guys, <div><br></div><div>OK, I have a first working version and so I wanted to share it with you.</div><div><br></div><div>I have not yet the time to start writing the doc since I just finished the first pass on the code. Tomorrow I will start with the doc. But I thought some of you may be interested in taking a look even without formal &quot;doc&quot; (and some feedback/iteration may avoid re-writing docs..).</div><div><br></div><div>If you have no clue what I am talking about, then this summary is for you:</div><div><i><br></i></div><div><i>----------</i></div><div><div><i>When we  use FFI  to call a certain library it&#39;s quite common that we need to pass as argument certain constants (for example, SIGKILL to kill()). These constants are defined in C header files and can even change it&#39;s value in different paltforms. </i></div><div><i>These constants also are sometimes defined by the C preprocessor and so there is not way to get those values from FFI. If you don&#39;t have the value of those constants, you cannot make the FFI call. </i></div></div><div><i>----------</i></div><div><br></div><div>I have tested the tool in OSX and CentOS using latest Pharo 5.0. It won&#39;t work in Windows right now.  As usual, all classes and methods have comments and there are enough tests. </div><div><br></div><div>At the end, I decided the C program will output a very naive Smalltalk literal array kind of thingy. The tool then parses that output and directly creates a init method (which is compiled into the SharedPool class) for that platform which is then called automatically at startup (only if initialization is needed). </div><div><br></div><div>As for real examples, I started to write constants for libc:  signal.h (to use kill()) , wait.h (to use wait() famility), fcntl.h (to use ... xxx()) , and errno.h. You can take a look to the package &#39;FFICHeaderExtractor-LibC&#39;.</div><div><br></div><div>Note that for running the tests you need &#39;cc&#39; findable by path in OSX and &#39;gcc&#39; in Unix. </div><div><br></div><div>To load the code in a latest Pharo 5.0, execute:</div><div><br></div><div>Metacello new</div><div>    baseline: &#39;FFICHeaderExtractor&#39;;</div><div>    repository: &#39;github://marianopeck/FFICHeaderExtractor:master/repository&#39;;</div><div>    load.</div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre">Any feedback is appreciated. </span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre">I will start writing the doc now.</span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre">BTW:  Big thanks to Eliot Miranda which helped me answering noob questions and providing useful code and guidelines. </span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre">Best,</span></div><div><br></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre"><br></span></div><div><span class="" style="white-space:pre">        </span></div><div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 23, 2016 at 1:12 PM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="auto"><div>Hi Denis,</div><div><br>On Jan 23, 2016, at 7:30 AM, Denis Kudriashov &lt;<a href="mailto:dionisiydk@gmail.com" target="_blank">dionisiydk@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><span></span></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">2016-01-22 22:35 GMT+01:00 Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="gmail_quote"><div>Let&#39;s measure this.  Let&#39;s say we have 8 platforms (that&#39;s an underestimate, because different Linux distributions may have different values for certain constants), but 8, which is 4 basic platforms times 32- &amp; 64-bits.  We have Mac x86 32-bit, Mac x64 64-bit, Windows x86 32-bit, Windows x64 64-bit, Linux x86 32-bit, Linux ARM 32-bit, Linux x64 64-bit, and soon enough there will be more.  Further, there may be different versions over time.</div><div><br></div><div>So each of those initialization methods has</div><div>- 1 slot for the global variable to be assigned</div><div>- 1 slot for the literal value to assign to it</div><div>- 3 bytes of bytecode per initialization for small methods, 4 for large methods.  Let&#39;s say 4.</div><div><br></div><div>So the overhead in 32-bits is 12 bytes per constant, and in 64-bits is 20 bytes.  So the overhead per constant for all platforms is 96 bytes per constant in 32-bits and 160 bytes per constant for 64-bits.  A full system with sockets, files, a database connexion etc could easily exceed 100 constants.  I think it would be nearer 1000.  So the overheads are in the 10- to 100-k byte range (100k ~= 0.5% of the image) on 32-bits.  That&#39;s low but it&#39;s also pure overhead.  Every GC has to visit them.  Every senders and implementors has to visit them, but they offer nothing of value.  Whereas the small parser for whatever notation is used to store the constants externally (if they are needed in a given deployment) has a small constant overhead; its simple code.</div><div><br></div><div>Further, you still need the machinery to export the constants to be able to generate these initialization methods.  If you&#39;ve got the machinery and you don&#39;t need the methods why bother to generate the methods?</div></div><div class="gmail_extra"><br></div>As the Scots say, many a mickle makes a muckle.</blockquote></div><br>Thank&#39;s Eliot for such detailed explanation. It makes sense. </div><div class="gmail_extra">But personally I prefer Smalltalk solution although Smalltalk itself is pure overhead comparing to C.</div></div></div></blockquote><div><br></div>I can see the draw of the pure Smalltalk. Simplicity and brows ability.  But imagine a tiny headless image deployed on containers, say 2mb.  Now 100kb of initialization code doesn&#39;t look so good :-).  Anyway I&#39;m beating a dead horse.  Mariano is generating initialization methods.<div><br></div><div><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra">My question was raised by Mariano idea to save ston files in methods. I think it can reduce problems which you described.</div><div class="gmail_extra">But then literal array syntax can be more suitable than ston. </div></div>
</blockquote><div><br></div>I just want to be clear, I&#39;m neutral about the notation used to export info from the C file.  Liberal array syntax, chunk source format, ston, xml.  It doesn&#39;t matter as long as it&#39;s convenient at expressing an attribute dictionary from names to attributes such as value, size, offset.  Don&#39;t get hung up on the specific notation.  If one were to go with the external file the only real requirements are that it be reasonably compact and quick to parse.  That might kill xml but leave plenty of other candidates.<br><div><span style="background-color:rgba(255,255,255,0)"><br><br>_,,,^..^,,,_ (phone)</span></div></div></div><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Mariano<br><a href="http://marianopeck.wordpress.com" target="_blank">http://marianopeck.wordpress.com</a><br></div>
</div></div></div>