Skip to content

Instantly share code, notes, and snippets.

@qinglee qinglee/l3alloc.tex
Created Nov 5, 2012

Embed
What would you like to do?
l3alloc: another implementation.
\cs_new_protected:Npn \__alloc_new:nnnN #1#2#3#4
{
\__alloc_setup_type:nnn {#1} {#2} {#3}
\cs_new_protected:cpn { #1 _new:N } ##1 { \__alloc_reg:nNN {#1} #4 ##1 }
}
\cs_new_protected:Npn \__alloc_setup_type:nnn #1#2#3
{
\tl_new:c { g__ #1 _allocation_tl }
\tl_gset:cx { g__ #1 _allocation_tl } { \int_eval:n {#2} }
\tl_const:cx { c__ #1 _allocation_max_tl } { \int_eval:n {#3} }
}
\cs_new:Npn \int_eval:n #1 { \tex_number:D \etex_numexpr:D #1 \scan_stop: }
\luatex_if_engine:TF
{ \__alloc_new:nnnN { box } \c_zero \c_max_register_int \tex_chardef:D }
{ \__alloc_new:nnnN { box } \c_zero \c_max_register_int \tex_mathchardef:D }
\__alloc_new:nnnN { dim } \c_zero \c_max_register_int \tex_dimendef:D
\__alloc_new:nnnN { int } { 11 } \c_max_register_int \tex_countdef:D
\__alloc_new:nnnN { muskip } \c_zero \c_max_register_int \tex_muskipdef:D
\__alloc_new:nnnN { skip } \c_zero \c_max_register_int \tex_skipdef:D
\cs_new_protected:Npn \__alloc_reserve_setup:nnn #1#2#3
{
\tl_const:cx { c__ #1 _allocation_reserve_begin_tl } { \int_eval:n {#2} }
\tl_const:cx { c__ #1 _allocation_reserve_stop_tl } { \int_eval:n {#3} }
}
\__alloc_reserve_setup:nnn { box } { 221 } { 255 }
\__alloc_reserve_setup:nnn { dim } { 221 } { 254 }
\__alloc_reserve_setup:nnn { int } { 221 } { 254 }
\__alloc_reserve_setup:nnn { skip } { 221 } { 254 }
\cs_new_protected:Npn \__alloc_reg:nNN #1#2#3
{
\int_compare:nNnTF
{ \tl_use:c { g__ #1 _allocation_tl } } >
{ \tl_use:c { c__ #1 _allocation_max_tl } }
{ \__msg_kernel_fatal:nnx { kernel } { out-of-registers } {#1} }
{
\__chk_if_free_cs:N #3
\tex_global:D #2 #3 \tl_use:c { g__ #1 _allocation_tl } \scan_stop:
\iow_log:x
{
\token_to_str:N #3 ~=~ #1 ~register~
\tl_use:c { g__ #1 _allocation_tl }
}
\__alloc_next:n {#1}
}
}
\cs_new_protected:Npn \__alloc_next:n #1
{
\tl_gset:cx { g__ #1 _allocation_tl }
{
\int_eval:n
{
\tl_if_exist:cTF { c__ #1 _allocation_reserve_begin_tl }
{
\int_compare:nNnTF
{ \tl_use:c { g__ #1 _allocation_tl } + 1 } =
{ \tl_use:c { c__ #1 _allocation_reserve_begin_tl } }
{ \tl_use:c { c__ #1 _allocation_reserve_stop_tl } }
{ \tl_use:c { g__ #1 _allocation_tl } }
}
{ \tl_use:c { g__ #1 _allocation_tl } }
+ 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.