# Local allocation of registers

I saw the following code in an issue of TUGboat (Volume 9, 1988, No. 3). I have modified it slightly. The motive was to avoid allocating registers globally. My questions are (1) How safe is it for use, given that they make local assignments? (2) Why use \count19 as a limit instead of plain TeX's 255 (or thereabouts). Can we use eTeX's 32767 registers instead today?

\def\declarecount{\allocate0\count}
\def\declaredimen{\allocate1\dimen}
\def\declareskip{\allocate2\skip}
\def\declaremuskip{\allocate3\muskip}
\def\declarebox{\allocate4\char}
\def\declaretoks{\allocate5\toks}
\def\allocate#1#2#3{\relax
\ifnum\count1#1<\count19\else % why not or 255 or 32767 (eTeX)?
\@latexerr{No room for a new \string#2!}\@ehd
\fi
\csname\expandafter\@gobble\string#2def\endcsname#3=\count1#1
}

-
Local allocation has been in etex.sty for many years, but not used widely. We did try providing local allocation in expl3, but it did not really fit well with how TeX works (as you still need to track the scope groups rather than of functions): it was removed some time ago. So I'd say that while local allocation is possibly, it's not a good 'fit' for how TeX programming works, especially as e-TeX gives us lots of registers. –  Joseph Wright Jun 4 '12 at 8:05

Back in 1988, \count19 was used in Plain TeX for storing the most recently allocated \insert.

In the current Plain TeX (and also in LaTeX) the counter to be used is \count20, because \count19 keeps track of languages.

Here's an excerpt of latex.ltx (line 302 onwards)

\message{registers,}
\count10=22 % allocates \count registers 23, 24, ...
\count11=9 % allocates \dimen registers 10, 11, ...
\count12=9 % allocates \skip registers 10, 11, ...
\count13=9 % allocates \muskip registers 10, 11, ...
\count14=9 % allocates \box registers 10, 11, ...
\count15=9 % allocates \toks registers 10, 11, ...
\count16=-1 % allocates input streams 0, 1, ...
\count17=-1 % allocates output streams 0, 1, ...
\count18=3 % allocates math families 4, 5, ...
\count19=0 % allocates \language codes 1, 2, ...
\count20=255 % allocates insertions 254, 253, ...
\countdef\insc@unt=20
\countdef\allocationnumber=21


As far as the extended pool is concerned, you should have a look at the macros provided by etex.sty:

\documentclass{article}
\usepackage{etex}

\newcount\myalloc
\def\tryalloc{%
\begingroup
\loop\ifnum\myalloc<350
\expandafter\loccount\csname tempcount\romannumeral\myalloc\endcsname
\expandafter\locdimen\csname tempdimen\romannumeral\myalloc\endcsname
\expandafter\loctoks\csname temptoks\romannumeral\myalloc\endcsname
\expandafter\meaning\csname tempcount\romannumeral\myalloc\endcsname\par
\expandafter\meaning\csname tempdimen\romannumeral\myalloc\endcsname\par
\expandafter\meaning\csname temptoks\romannumeral\myalloc\endcsname\par
\repeat
\endgroup
\vskip 40pt
\expandafter\meaning\csname tempcount\romannumeral350\endcsname}

\begin{document}
\ttfamily\tryalloc
\end{document}


You'll see that \loc... allocates registers from the top (32767) downwards, and that after the group the meaning is forgotten.

These commands are briefly discussed in etex_src.html that should be available with texdoc etex_src. The file etex.sty is essentially a verbatim port to LaTeX of etex.src.

-
But \loc... isn't in the eTeX manual texdoc.net/texmf-dist/doc/etex/base/etex_man.pdf. Please where can I find the right manual? –  Ahmed Musa Jun 4 '12 at 13:59
@AhmedMusa I've added a pointer. –  egreg Jun 4 '12 at 14:04

Note that for instance for a count register, the content of \count10 is compared with the content of \count19, which used to count inserts, as egreg explained.

That's not the number 19, but the last insert allocated. That's a simple mechaninsm for avoiding conflicts, as an insert uses a box, a counter, a dimen and a skip register simultaneously. The allocation of inserts runs from the maximal number downwards.

In latex.ltx, \count20, which counts inserts nowadays, is set to 255:

\count20=255 % allocates insertions 254, 253, ...


etex.sty, which enables the new eTeX features, uses a more complicated scheme (which I couldn't understand at first glance but I'm confident it'll give access to all the new registers :)

By the way, it also seems to provide a scheme for local register allocations (at least the inline doc say so), so maybe it'd be a good idea to look into the documentation for a user interface.

-