<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Sat May 25 11:59:16 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='128'>
<header><title>Ability to Install a Custom Memory Allocator</title><author address="mailto:udragon@users.sourceforge.net">Christophe Cap</author><author address="mailto:hhyJackson1275@who-got-mail.com">Mike Jackson</author><status type='project' state='rejected' tclversion="8.6" vote='after'>$Revision: 1.5 $</status><history></history><created day='13' month='mar' year='2003' /></header>
<abstract>This TIP alters Tcl to allow embedded uses of the Tcl library (and any extensions) to either use the Tcl memory allocators as their main allocator (especially in C++) or to set the memory allocator that Tcl uses for itself through <emph style="italic">ckalloc()</emph>.</abstract>
<body><section title="Background">
<para>A while ago I was experiencing troubles when allocating images ([image create photo]) while memory was already exhausted, my app crashed (due to known bug item #698571, which is in the HEAD by now!) This shouldn&apos;t happen anyway since my application had it&apos;s new handler set.</para>
<para>Tracing down the source of the allocators I noticed that Tcl uses <emph style="italic">HeapAlloc()</emph> (on Win32) to allocate its memory. Why not use <emph style="italic">malloc()</emph>?</para>
</section>
<section title="New/Malloc Handler">
<para>It would be nice to be able to catch memory allocation errors with a custom new handler.</para>
<para>A solution could be to replace <emph style="italic">HeapAlloc()</emph> (on Win32) and other platform specific memory handlers should be replaced by <emph style="italic">malloc()</emph>.</para>
<para>This way a new handler can by set through <emph style="italic">set_new_handler()</emph>.</para>
<para>Note that the Microsoft VC++ compiler has some ANSI incompatibility in that it uses <emph style="italic">_set_new_handler()</emph> rather than <emph style="italic">set_new_handler()</emph>. We would naturally conceal this platform difference.</para>
<para>For example:</para>
<verbatim><vline encoding='base64'>I2luY2x1ZGUgPG5ldz4g</vline><vline encoding='base64'></vline><vline encoding='base64'>Ly8=</vline><vline encoding='base64'>Ly8gTmV3IGhhbmRsZXIgZm9yIE1pY3Jvc29mdCBWaXN1YWwgQysrIGNvbXBpbGVy</vline><vline encoding='base64'>Ly8=</vline><vline encoding='base64'></vline><vline encoding='base64'>I2lmZGVmIF9NU0NfVkVS</vline><vline encoding='base64'>I2luY2x1ZGUgPG5ldy5oPg==</vline><vline encoding='base64'></vline><vline encoding='base64'>aW50IF9fY2RlY2wgX25ld0hhbmRsZXIoc2l6ZV90IHNpemUgKQ==</vline><vline encoding='base64'>ew==</vline><vline encoding='base64'>ICAgLy8gRG8gd2hhdGV2ZXI=</vline><vline encoding='base64'></vline><vline encoding='base64'>ICAgcmV0dXJuIDA7</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>I2Vsc2U=</vline><vline encoding='base64'></vline><vline encoding='base64'>Ly8=</vline><vline encoding='base64'>Ly8gQW5zaSBDL0MrKyBuZXcgaGFuZGxlcg==</vline><vline encoding='base64'>Ly8=</vline><vline encoding='base64'></vline><vline encoding='base64'>dm9pZCBfX2NkZWNsIF9uZXdIYW5kbGVyKCB2b2lkICk=</vline><vline encoding='base64'>ew==</vline><vline encoding='base64'>ICAgLy8gRG8gd2hhdGV2ZXI=</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'>I2VuZGlm</vline><vline encoding='base64'></vline><vline encoding='base64'>dm9pZCBzZXRoYW5kbGVycyh2b2lkKQ==</vline><vline encoding='base64'>ew==</vline><vline encoding='base64'>Ly8gTWljcm9zb2Z0IGNvbXBpbGVyIA==</vline><vline encoding='base64'>I2lmZGVmIF9NU0NfVkVSICA=</vline><vline encoding='base64'></vline><vline encoding='base64'>ICAgX3NldF9uZXdfaGFuZGxlciAoX25ld0hhbmRsZXIpOyAvLyBTZXR1cCBuZXcgaGFuZGxlcg==</vline><vline encoding='base64'>ICAgX3NldF9uZXdfbW9kZSggMSApOyAvLyBSZS1yb3V0ZSBtYWxsb2MgZmFpbHVyZXMgdG8gbmV3IGhhbmRsZXIgIQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>Ly8gQW5zaSBjb21waWxlciA=</vline><vline encoding='base64'></vline><vline encoding='base64'>I2Vsc2Ug</vline><vline encoding='base64'></vline><vline encoding='base64'>ICAgc2V0X25ld19oYW5kbGVyIChfbmV3SGFuZGxlcik7IC8vIEFOU0kgbmV3IGhhbmRsZXI=</vline><vline encoding='base64'></vline><vline encoding='base64'>I2VuZGlm</vline><vline encoding='base64'>fQ==</vline></verbatim>
</section>
<section title="Tcl Implementation">
<para>The above suggested solution could work for some compilers, but may not for all (some compilers might not support setting a malloc failure callback.) Therefore a Tcl custom new handler functionality could be implemented that handles Tcl specific memory allocation failures.</para>
<para>Something like: <emph style="italic">Tcl_SetMemHandler()</emph>?</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
