\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#128: Ability to Install a Custom Memory Allocator}
\date{March 13, 2003}
\author{Christophe Cap, Mike Jackson}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#128}&\textbf{Ability to Install a Custom Memory Allocator}\\\hline
Author&

Christophe Cap \tipmail{udragon}{users.sourceforge.net}
\par
Mike Jackson \tipmail{hhyJackson1275}{who-got-mail.com}
\\
Created&Thursday, \(\text{13}^{\text{th}}\) March 2003\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.5 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

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 \textit{ckalloc()}.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background}

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't happen anyway since my application had it's new handler set.

Tracing down the source of the allocators I noticed that Tcl uses \textit{HeapAlloc()} (on Win32) to allocate its memory. Why not use \textit{malloc()}?

\section{New/Malloc Handler}

It would be nice to be able to catch memory allocation errors with a custom new handler.

A solution could be to replace \textit{HeapAlloc()} (on Win32) and other platform specific memory handlers should be replaced by \textit{malloc()}.

This way a new handler can by set through \textit{set\_new\_handler()}.

Note that the Microsoft VC++ compiler has some ANSI incompatibility in that it uses \textit{\_set\_new\_handler()} rather than \textit{set\_new\_handler()}. We would naturally conceal this platform difference.

For example:
\begingroup\small\begin{verbatim}
#include <new> 

//
// New handler for Microsoft Visual C++ compiler
//

#ifdef _MSC_VER
#include <new.h>

int __cdecl _newHandler(size_t size )
{
   // Do whatever

   return 0;
}

#else

//
// Ansi C/C++ new handler
//

void __cdecl _newHandler( void )
{
   // Do whatever
}
#endif

void sethandlers(void)
{
// Microsoft compiler 
#ifdef _MSC_VER  

   _set_new_handler (_newHandler); // Setup new handler
   _set_new_mode( 1 ); // Re-route malloc failures to new handler !

// Ansi compiler 

#else 

   set_new_handler (_newHandler); // ANSI new handler

#endif
}
\end{verbatim}
\endgroup

\section{Tcl Implementation}

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.

Something like: \textit{Tcl\_SetMemHandler()}?

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
