<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>LOCK_FUNCTION / LOCK_VARIABLE</title>
		<link>http://www.allegro.cc/forums/view/397684</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 21 Aug 2004 16:51:45 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it save to call these macros several times?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Fri, 20 Aug 2004 19:35:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>All it does is <a href="http://www.delorie.com/djgpp/doc/dpmi/api/310600.html">this</a>. </p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>This function may be called more than once for a given page; the DPMI host maintains a lock count for each page.</p></div></div><p>

Note that it uses different methods on different platforms.. so I&#39;m not sure if it does exactly that on all platforms.</p><p>I&#39;d recommend keeping track of whether or not you&#39;ve done it anyways, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Fri, 20 Aug 2004 19:45:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t see why not. I&#39;m assuming that if the memory is already locked, at worst it&#39;ll return a failure value. Generally speaking, I know everything I&#39;ll need to lock at run time, so I&#39;ve never actually ran into this particular situation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Sirocco)</author>
		<pubDate>Fri, 20 Aug 2004 19:54:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wondering, why would that happen? You lock variables only once. I don&#39;t believe it will return a failure, because technically speaking, you asked to lock the memory, and the memory was already locked.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Fri, 20 Aug 2004 19:57:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The question is: Do I need to keep track of the fact that I&#39;ve locked the var, or is it ok to lock it several times?</p><p>It&#39;s not a real problem, it would be pretty easy to avoid it, but it would require some work. If there&#39;s some sort of UNLOCK_XXX macro, it would be ok as well.</p><p>I&#39;ve several states, the init rule of the state does all the init stuff (suprise, heh), the done rule clears up. I lock the var in the init rule, but since I can&#39;t unlock it in the done rule it&#39;s possible that the same var gets locked several times.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Fri, 20 Aug 2004 20:05:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Do I need to keep track of the fact that I&#39;ve locked the var, or is it ok to lock it several times?</p></div></div><p>You don&#39;t need to keep track, and it is ok to lock it several times.</p><p>There shouldn&#39;t be a reason you lock it several times, though. Initialization on locked variables should only occur once. </p><p>It&#39;s like setting a graphics mode. You can do it more than once, but that&#39;d just be silly.</p><p>That&#39;s just coding practice, though.. </p><p>[edit: btw, <a href="http://www.delorie.com/djgpp/doc/dpmi/api/310601.html">you can unlock memory</a>, but allegro doesn&#39;t have a macro to do it.]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Fri, 20 Aug 2004 20:09:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I believe if you lock something several times, if it throws errors, you would not matter them. As long as it does not throw an exception, you should be able to recover from the error.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Fri, 20 Aug 2004 20:14:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My bad, allegro DOES have unlock macro&#39;s.</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define LOCK_DATA(d, s)             _go32_dpmi_lock_data(d, s)</span>
<span class="p">#define LOCK_CODE(c, s)             _go32_dpmi_lock_code(c, s)</span>
<span class="p">#define UNLOCK_DATA(d,s)            _unlock_dpmi_data(d, s)</span>
<span class="p">#define LOCK_VARIABLE(x)            LOCK_DATA((void *)&amp;x, sizeof(x))</span>
<span class="p">#define LOCK_FUNCTION(x)            LOCK_CODE((void *)FP_OFF(x), (long)FP_OFF(x##_end) - (long)FP_OFF(x))</span>
</pre></div></div><p>
UNLOCK_DATA((void*)&amp;var, sizeof(var)) is what you&#39;d want for that, I bet..</p><p>By the looks of it that&#39;s only for variables..
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Fri, 20 Aug 2004 20:16:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>There shouldn&#39;t be a reason you lock it several times, though. Initialization on locked variables should only occur once.
</p></div></div><p>
Well, not really. View it like locking the screen, since this is a pretty good analogy: You lock the screen, do your stuff and unlock it.</p><p>The problem is, that I can&#39;t unlock the variable. There&#39;s no symetric function/macro for the LOCK_XXX macros.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>It&#39;s like setting a graphics mode. You can do it more than once, but that&#39;d just be silly.</p></div></div><p>
Unless you want to switch to a different gfx mode or color depth, eh?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
That&#39;s just coding practice, though..
</p></div></div><p>

Well, maybe you can tell me how you&#39;d handle this? I&#39;m always willing to learn <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Right now the only way I can think of would be to store the fact that I&#39;ve locked the variable in a state variable. 
</p><div class="source-code snippet"><div class="inner"><pre>init-rule: 
    <span class="k1">if</span> <span class="k2">(</span>varLocked is <span class="k1">not</span> <span class="k1">true</span><span class="k2">)</span> <span class="k2">{</span>
        <a href="http://www.delorie.com/djgpp/doc/libc/libc_539.html" target="_blank">lock</a><span class="k2">(</span>var<span class="k2">)</span><span class="k2">;</span>
        varLocked <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
    <span class="k2">}</span>
</pre></div></div><p>

What&#39;d I&#39;d prefer would be:
</p><div class="source-code snippet"><div class="inner"><pre>init-rule: <a href="http://www.delorie.com/djgpp/doc/libc/libc_539.html" target="_blank">lock</a><span class="k2">(</span>var<span class="k2">)</span>
done-rule: <a href="http://www.delorie.com/djgpp/doc/libc/libc_840.html" target="_blank">unlock</a><span class="k2">(</span>var<span class="k2">)</span>
</pre></div></div><p>
That would keep everything symetric.</p><p>EDIT: You&#39;re too fast <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Fri, 20 Aug 2004 20:19:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Well, maybe you can tell me how you&#39;d handle this? I&#39;m always willing to learn <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p></div></div><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include "gui.h"</span></td></tr><tr><td class="number">3</td><td><span class="p">#include "platformer.h"</span></td></tr><tr><td class="number">4</td><td><span class="p">#include "timers.h"</span></td></tr><tr><td class="number">5</td><td><span class="p">#include "graphics.h"</span></td></tr><tr><td class="number">6</td><td><span class="p">#include "tiles.h"</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">int</span> fpsCounter<span class="k2">;</span></td></tr><tr><td class="number">9</td><td><span class="k1">int</span> fps<span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k1">int</span> ms, sec, min, hr<span class="k2">;</span></td></tr><tr><td class="number">11</td><td><span class="k1">int</span> logicUpdates<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="k1">void</span> LogicTimer<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td> <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>  logicUpdates<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td> <span class="k2">}</span></td></tr><tr><td class="number">17</td><td><a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>LogicTimer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td><span class="k1">void</span> ClockTimer<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span></td></tr><tr><td class="number">20</td><td> <span class="k2">{</span></td></tr><tr><td class="number">21</td><td>  ms<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  <span class="k1">if</span><span class="k2">(</span>ms <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">100</span><span class="k2">)</span></td></tr><tr><td class="number">23</td><td>   <span class="k2">{</span></td></tr><tr><td class="number">24</td><td>    ms <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>    sec<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>    <span class="k1">if</span><span class="k2">(</span>sec <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">60</span><span class="k2">)</span></td></tr><tr><td class="number">27</td><td>     <span class="k2">{</span></td></tr><tr><td class="number">28</td><td>      sec <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>      min<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>      <span class="k1">if</span><span class="k2">(</span>min <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">60</span><span class="k2">)</span></td></tr><tr><td class="number">31</td><td>       <span class="k2">{</span></td></tr><tr><td class="number">32</td><td>        hr<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>       <span class="k2">}</span></td></tr><tr><td class="number">34</td><td>     <span class="k2">}</span></td></tr><tr><td class="number">35</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">36</td><td> <span class="k2">}</span></td></tr><tr><td class="number">37</td><td><a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>ClockTimer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">38</td><td>&#160;</td></tr><tr><td class="number">39</td><td><span class="k1">void</span> FPSTimer<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span></td></tr><tr><td class="number">40</td><td> <span class="k2">{</span></td></tr><tr><td class="number">41</td><td>  fps <span class="k3">=</span> fpsCounter<span class="k2">;</span></td></tr><tr><td class="number">42</td><td>  fpsCounter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">43</td><td> <span class="k2">}</span></td></tr><tr><td class="number">44</td><td><a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>FPSTimer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">45</td><td>&#160;</td></tr><tr><td class="number">46</td><td><span class="k1">int</span> InitTimers<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">47</td><td> <span class="k2">{</span></td></tr><tr><td class="number">48</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>LogicTimer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">49</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>ClockTimer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">50</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>ms<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>sec<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">52</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>min<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">53</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>hr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">54</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>logicUpdates<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">55</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>fps<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">56</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>fpsCounter<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">57</td><td>&#160;</td></tr><tr><td class="number">58</td><td>  <a href="http://www.allegro.cc/manual/install_int" target="_blank"><span class="a">install_int</span></a><span class="k2">(</span>LogicTimer,<span class="n">30</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">59</td><td>  <a href="http://www.allegro.cc/manual/install_int" target="_blank"><span class="a">install_int</span></a><span class="k2">(</span>ClockTimer,<span class="n">10</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">60</td><td>  <a href="http://www.allegro.cc/manual/install_int" target="_blank"><span class="a">install_int</span></a><span class="k2">(</span>FPSTimer,<span class="n">1000</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">61</td><td>&#160;</td></tr><tr><td class="number">62</td><td>  <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">63</td><td> <span class="k2">}</span></td></tr></tbody></table></div></div><p>
I call InitTimers(), done. Never touch any of that again.</p><p>Those variables never get touched ever again. No unlocking, no locking, just reading. With the case of logicUpdate, I subtract one each time I make a logic update. With fpsCounter, I add one each time I draw a frame.</p><p>Note: Variables need to be initialized to 0.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Unless you want to switch to a different gfx mode or color depth, eh?</p></div></div><p>Where locking variables is concerned, you can&#39;t switch the way you do it. So I think the analogy still stands, but I mean setting it to the same thing. Changing it from 640x480x16 to 640x480x16 <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Fri, 20 Aug 2004 20:28:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
I do it the same as Derezo. Never had any problems. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Fri, 20 Aug 2004 20:31:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I call InitTimers(), done. Never touch any of that again.</p></div></div><p>
Well, that&#39;s great. Now, please tell me how you do it with the state machine I&#39;ve described? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>I choose the easy way and added a watcher for the locked state. <br />Just to get some &quot;OH NOW! EVIL MACROS!&quot; shouts, here&#39;re my new timer macros:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#define TIMER_DECL(VAR)          \</span></td></tr><tr><td class="number">2</td><td><span class="p">    volatile int VAR = 0;           \ </span></td></tr><tr><td class="number">3</td><td><span class="p">    int VAR##isLocked = 0; \ </span></td></tr><tr><td class="number">4</td><td><span class="p">    static void VAR##Updater(){  \ </span></td></tr><tr><td class="number">5</td><td><span class="p">        VAR++;                   \ </span></td></tr><tr><td class="number">6</td><td><span class="p">    } END_OF_STATIC_FUNCTION(VAR##Updater) </span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="p">#define TIMER_SET(VAR, FPS)      \</span></td></tr><tr><td class="number">9</td><td><span class="p">    if (!VAR##isLocked) { \ </span></td></tr><tr><td class="number">10</td><td><span class="p">        LOCK_FUNCTION(VAR##Updater); \ </span></td></tr><tr><td class="number">11</td><td><span class="p">        LOCK_VARIABLE(VAR);          \ </span></td></tr><tr><td class="number">12</td><td><span class="p">        VAR##isLocked = 1; \ </span></td></tr><tr><td class="number">13</td><td><span class="p">    } \ </span></td></tr><tr><td class="number">14</td><td><span class="p">    install_int_ex(VAR##Updater, BPS_TO_TIMER(FPS)) </span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Fri, 20 Aug 2004 21:02:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Now, please tell me how you do it with the state machine I&#39;ve described? </p></div></div><p>I don&#39;t understand the difference. You&#39;re using the same variables each time, so it should be no different. <br />Lock them once in a single swoop (such as after calling install_timer), and never touch them again. Locking variables is not something you should even have to consider doing more than once during a program&#39;s execution. </p><p>Set it and forget it!</p><p>Evil macro&#39;s work, too, though <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Fri, 20 Aug 2004 21:08:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Will you declare a lot of variables to be timer incremented? Because allegro has a low limit on number on timer ints. I think it&#39;s a total of 16 (and some subsystems already take some of those).</p><p>In this case, I&#39;d only install 1 high resolution timer and update all variables in this timer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Fri, 20 Aug 2004 21:17:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I don&#39;t understand the difference. You&#39;re using the same variables each time, so it should be no different.<br />Lock them once in a single swoop (such as after calling install_timer), and never touch them again. Locking variables is not something you should even have to consider doing more than once during a program&#39;s execution. </p></div></div><p>

The difference is that each module is like your program. <br />If you call your program trice in a row, you rely on the fact that the OS clears the system every time. <br />If you don&#39;t have that luxury, you need to take care of it yourself.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Fri, 20 Aug 2004 22:58:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The LOCK_* macros are only applicable in DOS. And I think the fact that Allegro tries to unlock data before freeing it (in the case of pointers) means that the data should be unlocked. However, I don&#39;t think this applies to variables since Allegro never states anyewhere that you should unlock variables before closing the program.</p><p>So I think you only need to worry about unlocking if you&#39;re locking a piece of malloc&#39;d memory that will be free&#39;d.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 21 Aug 2004 05:10:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What is the ## notation in spellcaster&#39;s macros?  It seems to concatenate VAR as an identifier.  If so, I like!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gideon Weems)</author>
		<pubDate>Sat, 21 Aug 2004 12:26:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>The difference is that each module is like your program. </p></div></div><p>
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>program_init <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>   foreach module <span class="k2">{</span></td></tr><tr><td class="number">3</td><td>      module-&gt;init<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">5</td><td><span class="k2">}</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>program_run <span class="k2">{</span></td></tr><tr><td class="number">8</td><td>   <span class="c">// selected_module-&gt;start();</span></td></tr><tr><td class="number">9</td><td>   selected_module-&gt;run<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>   <span class="c">// selected_module-&gt;end();</span></td></tr><tr><td class="number">11</td><td><span class="k2">}</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td>program_exit <span class="k2">{</span></td></tr><tr><td class="number">14</td><td>   foreach module <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>      module-&gt;exit<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Something like that anyhow.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 21 Aug 2004 12:32:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>What is the ## notation in spellcaster&#39;s macros? It seems to concatenate VAR as an identifier. If so, I like!</p></div></div><p>
It&#39;s the concat operator. There&#39;s also a string operator #</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>program_init {<br />   foreach module {<br />      module-&gt;init();<br />   }<br />}</p><p>program_run {<br />   // selected_module-&gt;start();<br />   selected_module-&gt;run();<br />   // selected_module-&gt;end();<br />}</p><p>program_exit {<br />   foreach module {<br />      module-&gt;exit();<br />   }<br />}
</p></div></div><p>

Yep. Now, imagine that these modules are dynamic.</p><p>This code shoulnd&#39;t leak resources:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
    Object foo <span class="k3">=</span> <span class="k1">new</span> Object<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k1">delete</span> foo<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
In the same way, these calls shouldn&#39;t leak resources
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
    state_init<span class="k2">(</span>state<span class="k2">)</span><span class="k2">;</span>
    state_done<span class="k2">(</span>state<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Agreed?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 21 Aug 2004 12:48:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Uhhh...</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
    state_init<span class="k2">(</span>state<span class="k2">)</span><span class="k2">;</span>
    state_done<span class="k2">(</span>state<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

add a few states. Like &quot;start&quot; and &quot;end&quot; leave &quot;init&quot; and &quot;done&quot; for the stuff that CAN&#39;T be done over and over. Which is what I was &quot;trying&quot; to say with the last bit of code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 21 Aug 2004 13:39:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, init() is the constructor. done() the destructor <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />What you&#39;re doing is to design-by-contract. You force the user (or yourself) to call those routines only once. <br />If you have start and init what you do is to force the player to keep the state in mind. he has to make sure that start or init (just from the name, I can&#39;t even tell what method should be called only once) is called just once.<br />So, all you do is delegating the responsibilty to the user. I consider this to be rude. </p><p><b>You should be able to create and free a state without wasting resources.</b> I think we agree here?<br />And if I have to waste resources, it should be the responsibility of the state to ensure that the least amount possible is wasted.</p><p>With your approach, you either have to know all states at compile time (so no data driven aopproach is possible) or the user has to keep book whether he has called the &quot;once in a life time method&quot; or the state has to have a &quot;isStartNeeded()/isInitNeeded()&quot; the user can call... or it simply does the check itself - which is my current solution <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>What you&#39;re trying to do will result in exactly the same thing as I do. But you force the user to do the work, my code does it automatically. So I&#39;m not sure I see your point.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 21 Aug 2004 15:48:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What wasted resources? Its Locking a variable <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 21 Aug 2004 15:51:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, that&#39;s the point. Nobody was really quite sure whether it was save to call it sveral times or not, and what happens if it&#39;s called several times.<br />But there should be a symetric call to unlock it. <br />With your point of view, there&#39;s either no need at all to worry (then most of your posts in this thread don&#39;t make much sense any more (&quot;why having a once in a programs-lifetime-method if it doesn&#39;t matter if it&#39;s called twice)).<br />With my point of view, stuff that is locked should be freed, since I don&#39;t know what the lock call does. It might differ from platform to platform, etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 21 Aug 2004 15:56:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Except that its a legacy hold over from the DOS code, and really isn&#39;t required in the other platforms <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 21 Aug 2004 15:58:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, it&#39;s not yet depricated. If it&#39;s not required at all, then most of this discussion was pretty pointless <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 21 Aug 2004 16:51:45 +0000</pubDate>
	</item>
</rss>
