<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] al_get_memory_interface?</title>
		<link>http://www.allegro.cc/forums/view/606268</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 12 Feb 2011 04:26:37 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Will there be a function &#39;al_get_memory_interface&#39;, so as that our libraries and programs use the memory management routines set globally with Allegro?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Tue, 01 Feb 2011 22:53:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Isn&#39;t that what <span class="source-code"><a href="http://www.allegro.cc/manual/al_malloc"><span class="a">al_malloc</span></a><span class="k2">(</span><span class="k2">)</span></span>, etc, are for?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 01 Feb 2011 22:59:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>al_malloc etc are nice, but there is no way to get the functions that have been installed in Allegro and call them directly, or examine which functions have been installed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 02 Feb 2011 05:05:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it needed? If you call al_malloc instead of malloc then the allocation will use the user callback.</p><p>For more sophisticated tracking (like e.g. if you want to know if the memory is for a bitmap or a sound sample) you can&#39;t use this interface anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 02 Feb 2011 05:16:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Note that those functions are provided primarily so that you can override what Allegro does (for debugging, etc.), not so much for you to use as a generic memory management interface. That is, so you can plug Allegro into something else, not plug everything into Allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Wed, 02 Feb 2011 07:24:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have the following use case: I want to add diagnostic output information to the current malloc and free functions Allegro uses. In order to do that, I need the pointer to the Allegro&#39;s malloc and free functions, so as that I can install my own handlers and then call the original handlers.</p><p>Without an al_get_memory_interface function, I cannot do this.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 02 Feb 2011 16:37:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why do you want to call the original handlers? Just do your own malloc calls (that way you can even piggyback diagnostic info to each block, if let&#39;s say you want to add checks for leaked or double-freed blocks).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 02 Feb 2011 18:03:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Because there may be a problem with a 3rd party malloc function installed via Allegro to which I don&#39;t have access to. If I set the allegro&#39;s malloc with my own, the 3rd party library doesn&#39;t work any more. I&#39;d like to see what goes into and what comes out of the 3rd party library malloc function.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 02 Feb 2011 18:08:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sounds like a good reason to have such a function.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Wed, 02 Feb 2011 18:22:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901626#target">axilmar</a> said:</div><div class="quote"><p>Because there may be a problem with a 3rd party malloc function installed via Allegro to which I don&#39;t have access to.</p></div></div><p>How often does that happen? Why do you think thats even a remotely useful use case?</p><p>The majority of allegro programs will never even touch allegro&#39;s malloc functions, let alone replace them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 03 Feb 2011 02:30:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If we add the function, in 99.9% of cases al_get_memory_interface would simply return NULL. And otherwise return whatever you already passed to it. You can also call the function only once and before al_init (in any scenario I can think of at least) so I don&#39;t see how a third party library or addon could do that for you.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 03 Feb 2011 03:10:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901618#target">axilmar</a> said:</div><div class="quote"><p>In order to do that, I need the pointer to the Allegro&#39;s malloc and free functions, so as that I can install my own handlers and then call the original handlers.</p></div></div><p> Couldn&#39;t you just require the user to call your set_mem_interface function too, if they&#39;ve called Allegro&#39;s?  It could even take ALLEGRO_MEMORY_INTERFACE as a parameter, so they could reuse the struct they&#39;ve already prepared.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Thu, 03 Feb 2011 03:45:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901678#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>
How often does that happen? Why do you think thats even a remotely useful use case?
</p></div></div><p>

It will be quite useful for debugging purposes. Suppose you have a memory issue...instead of using your own allocator, you take the existing malloc and free functions and replace them with ones that print out diagnostic information and call the original functions. In this way, you have the exact same behavior as before, with the addition of your diagnostic messages.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901685#target">Elias</a> said:</div><div class="quote"><p>
in 99.9% of cases al_get_memory_interface would simply return NULL.
</p></div></div><p>

Why wouldn&#39;t it return the currently installed functions?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
 You can also call the function only once and before al_init (in any scenario I can think of at least).
</p></div></div><p>

No such thing is mentioned in the docs though. What will happen if I call the function al_set_memory_interface after al_init and before any allocation?</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901692#target">torhu</a> said:</div><div class="quote"><p>
Couldn&#39;t you just require the user to call your set_mem_interface function too, if they&#39;ve called Allegro&#39;s?
</p></div></div><p>

It could be possible, but it is not a good solution.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Thu, 03 Feb 2011 05:07:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s beginning to sound like your use case is exactly what al_set_memory_interface is for...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Thu, 03 Feb 2011 05:28:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901707#target">axilmar</a> said:</div><div class="quote"><p>It will be quite useful for debugging purposes. Suppose you have a memory issue...instead of using your own allocator, you take the existing malloc and free functions and replace them with ones that print out diagnostic information and call the original functions. In this way, you have the exact same behavior as before, with the addition of your diagnostic messages.</p></div></div><p>

Indeed. When you want to replace the malloc functions, just do it. Since allegro by default uses libc&#39;s malloc/free should there not be an interface setup, you need not worry about which its using. Just wrap libc&#39;s functions with your own, and call al_set_memory_interface with a table of your wrappers.</p><p>This is exactly what it was intended for.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 03 Feb 2011 05:43:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901707#target">axilmar</a> said:</div><div class="quote"><p>Why wouldn&#39;t it return the currently installed functions?</p></div></div><p>

Usually none are installed and the memory interface is just NULL (and Allegro will use the normal malloc and so on).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>No such thing is mentioned in the docs though. What will happen if I call the function al_set_memory_interface after al_init and before any allocation?</p></div></div><p>

al_init also will allocate things already I think. But yes, the documentation should be clear about when the function can be called. But I doubt there&#39;s a requirement for system drivers to not use al_malloc which means they likely use it already and so the memory interface needs to be set before.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 03 Feb 2011 08:31:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901714#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>
Indeed. When you want to replace the malloc functions, just do it. Since allegro by default uses libc&#39;s malloc/free should there not be an interface setup, you need not worry about which its using. Just wrap libc&#39;s functions with your own, and call al_set_memory_interface with a table of your wrappers.</p><p>This is exactly what it was intended for.
</p></div></div><p>

What if I don&#39;t have access to the malloc functions? </p><p>What if I use a 3rd party library that has its own malloc functions installed via Allegro? </p><p>What if I need to override the existing malloc functions for a specific task, and then restore the originals?</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901740#target">Elias</a> said:</div><div class="quote"><p>
al_init also will allocate things already I think...and so the memory interface needs to be set before.
</p></div></div><p>

That doesn&#39;t exclude the use case of overriding the existing behaviors for debugging purposes.</p><p>Anyway, it&#39;s not that important. If you feel it&#39;s such a burden to implement, then please ignore this request.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Thu, 03 Feb 2011 16:44:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901781#target">axilmar</a> said:</div><div class="quote"><p>Anyway, it&#39;s not that important. If you feel it&#39;s such a burden to implement, then please ignore this request.
</p></div></div><p>

It&#39;s not a burden at all - you&#39;d be the one sending the patch anyway <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Just trying to see the use case, especially with the current behavior where the mem interface is usually just a NULL pointer. If you make the patch (including documentation) I don&#39;t see a problem applying it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 03 Feb 2011 17:19:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901783#target">Elias</a> said:</div><div class="quote"><p>
you&#39;d be the one sending the patch anyway
</p></div></div><p>

how can I do that?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Thu, 03 Feb 2011 17:39:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Get allegro sources from SVN, edit your files, then create a patch file, which you then attach to a posting here or send to the allegro developers mailing list.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 03 Feb 2011 17:41:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901785#target">tobing</a> said:</div><div class="quote"><p> Get allegro sources from SVN, edit your files, then create a patch file, which you then attach to a posting here or send to the allegro developers mailing list.</p></div></div><p>Wouldn&#39;t be easier if one of the developers added the function? it&#39;s just one simple function with the most trivial implementation there is: copy the internal memory interface structure to the one given in the parameter. Here it is:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> al_get_memory_interface<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_MEMORY_INTERFACE"><span class="a">ALLEGRO_MEMORY_INTERFACE</span></a> <span class="k3">*</span>ami<span class="k2">)</span> <span class="k2">{</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_48.html" target="_blank">assert</a><span class="k2">(</span>ami<span class="k2">)</span><span class="k2">;</span>
    <span class="k1">if</span> <span class="k2">(</span>mem<span class="k2">)</span> <span class="k2">{</span>
        <span class="k3">*</span>ami <span class="k3">=</span> <span class="k3">*</span>mem<span class="k2">;</span>
    <span class="k2">}</span> <span class="k1">else</span> <span class="k2">{</span>
        ami-&gt;mi_malloc <span class="k3">=</span> NULL<span class="k2">;</span>
        ami-&gt;mi_free <span class="k3">=</span> NULL<span class="k2">;</span>
        ami-&gt;mi_realloc <span class="k3">=</span> NULL<span class="k2">;</span>
        ami-&gt;mi_calloc <span class="k3">=</span> NULL<span class="k2">;</span>
    <span class="k2">}</span> 
<span class="k2">}</span>
</pre></div></div><p>

By the way, I noticed the following in the ALLEGRO code:</p><ul><li><p>the ALLEGRO_MEMORY_INTERFACE pointer passed to the function &#39;al_set_memory_interface&#39; is kept by the Allegro library. Wouldn&#39;t it be better if the library kept a copy of the struct? the docs do not say that the ALLEGRO_MEMORY_INTERFACE struct must not be allocated on the stack.</p><br /></li><li><p>the memory interface functions have an &#39;if&#39; in them: &#39;if (mem) ...&#39;. Wouldn&#39;t it be better if the &#39;if&#39; was skipped, the internal ALLEGRO_MEMORY_INTERFACE struct was setup to point to the appropriate functions, and those functions called the default libc implementations? the &#39;if&#39; could be skipped.
</p></li></ul></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Thu, 03 Feb 2011 19:51:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I guess just adding this function is the easy part. What about testing and adding appropriate documentation? </p><p>Edit: Maybe I&#39;ll look into this during the weekend. No promise, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Thu, 03 Feb 2011 20:06:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606268/901793#target">tobing</a> said:</div><div class="quote"><p>
Edit: Maybe I&#39;ll look into this during the weekend. No promise, though.
</p></div></div><p>

Thanks a lot everyone.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Thu, 03 Feb 2011 20:39:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here&#39;s a patch, adding two functions: al_get_memory_interface, and al_get_default_memory_interface. Wasn&#39;t too difficult after all.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 11 Feb 2011 23:02:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not make a generic add-on library for debugging memory?  Then in most cases there would be no reason for anyone to use those new functions.  They would instead use the add-on library interface.  You could then debug everything that uses <span class="source-code"><a href="http://www.allegro.cc/manual/al_malloc"><span class="a">al_malloc</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span> and get whatever information you need...</p><p>Not sure if this is relevant, but something I did a while back for debugging in windows was to create a header file that would override all libc functions that allocated, reallocated or freed memory (but I may have missed some).  As long as the header file was included after all other header files it captures all allocations of memory...</p><p>It would put everything into a sort of garbage collector. If the program exited without freeing all the allocated memory then it would dump a list of each leak.  The dump included the source file and line where it was allocated, the size in bytes and even a short memory dump of each memory leak.</p><p>It&#39;s a crude hack for a specific project/purpose, but I attached it in case it inspires anyone...  May need a little modification to work for you, like removal of cbuf/fifo stuff, but it should work should you decide to hack it...</p><p>Just define these in leak.c and it should compile
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Also comment these out!</span>
<span class="c">//#define INCLUDE_FIFO</span>
<span class="c">//#include "ucode/cbuf.h"</span>


<span class="p">#define OMF_DEBUG_FILE_NAME "omfdebug.txt"</span>
<span class="p">#define DLL_EXPORT</span>
<span class="p">#define DEBUG_LEAKS</span>
<span class="p">#define REMOVE_ON_FREE</span>
<span class="p">#define FREE_LEAKS</span>
</pre></div></div><p>
Then add &#39;atexit(debug_atexit);&#39; in your &#39;main()&#39;.</p><p>Arg... I must be bored...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Novy)</author>
		<pubDate>Sat, 12 Feb 2011 04:26:37 +0000</pubDate>
	</item>
</rss>
