Skip to contentSkip to conversation

Define macros for multi-part listings in preamble

PATCH Applied Akira Yokosawa 2 participants 5 messages Jun 26 – Jun 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Define macros for multi-part listings in preamble

Paul said he has a plan to add listings similar to Listings 9.13 and 9.14 [1].

Move local definitions in rcuintro.tex for such multi-part listings into preamble so that they can be used elsewhere. Note that "VerbatimT" and "adjustwidth" envs have vertical spacing tweaks applied.

---
 defer/rcuintro.tex | 52 ++++++++++++++++++----------------------------
 perfbook-lt.tex    |  6 ++++++
 2 files changed, 26 insertions(+), 32 deletions(-)
defer/rcuintro.tex +20 -32
index 6997378b..898cdf5f 100644
@@ -2,10 +2,6 @@
 % mainfile: ../perfbook.tex
 % SPDX-License-Identifier: CC-BY-SA-3.0
 
-\newcommand{\adjvspaceabove}{\vspace*{2pt}}
-\newcommand{\adjvspacebelow}{\vspace*{-1pt}}
-\newcommand{\adjvspacebottom}{\vspace*{-2pt}}
-
 \subsection{Introduction to RCU}
 \label{sec:defer:Introduction to RCU}
 
@@ -75,27 +71,24 @@ that would normally be used in single-threaded code.
 \begin{listing}[tb]
 \begin{adjustwidth}{10pt}{5pt}
 \footnotesize
-\fvset{numbers=left,numbersep=5pt,fontsize=\scriptsize,frame=single,xrightmargin=5pt}
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny A\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = gp;
  do_something_with(p->a);
  do_something_with(p->b);
-\end{Verbatim}
-}\adjvspacebelow
+\end{VerbatimT}
+}
 Might be transformed to:
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny B\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = gp;
  do_something_with(p->a);
  p = gp;
  do_something_with(p->b);
-\end{Verbatim}
-}\adjvspacebelow
+\end{VerbatimT}
+}
 The compiler assumes normal variables do not spontaneously change,
 \co{do_something_with()} might use many machine registers, and this
 transformation reduces register pressure.
@@ -104,14 +97,13 @@ transformed code, the values of \co{p->a} and \co{p->b} will be mismatched.
 Prevent this by using \co{rcu_dereference()} as follows:
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny C\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = rcu_dereference(gp);
  do_something_with(p->a);
  do_something_with(p->b);
-\end{Verbatim}
-}\adjvspacebelow
-\end{adjustwidth}\adjvspacebottom
+\end{VerbatimT}
+}
+\end{adjustwidth}
 \caption{Compilers Can Reload Values}
 \label{lst:defer:Compilers Can Reload Values}
 \end{listing}
@@ -119,28 +111,25 @@ Prevent this by using \co{rcu_dereference()} as follows:
 \begin{listing}[tb]
 \begin{adjustwidth}{10pt}{5pt}
 \footnotesize
-\fvset{numbers=left,numbersep=5pt,fontsize=\scriptsize,frame=single,xrightmargin=5pt}
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny A\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = malloc(sizeof(*p));
  p->a = compute_value();
  p->b = 42;
  gp = p;
-\end{Verbatim}
-}\adjvspacebelow
+\end{VerbatimT}
+}
 Might be transformed to:
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny B\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = malloc(sizeof(*p));
  gp = p;
  p->a = compute_value();
  p->b = 42;
-\end{Verbatim}
-}\adjvspacebelow
+\end{VerbatimT}
+}
 The compiler assumes normal variables are not concurrently accessed,
 and thus that the order of stores does not matter.
 If \co{compute_value()} was inlined, this transformation might produce
@@ -151,15 +140,14 @@ garbage in \co{p->a} and \co{p->b}.
 Prevent this by using \co{rcu_assign_pointer()} as follows:
 {\renewcommand{\theFancyVerbLine}{%
   {\rmfamily\tiny C\arabic{FancyVerbLine}}}
-\adjvspaceabove%
-\begin{Verbatim}
+\begin{VerbatimT}
  p = malloc(sizeof(*p));
  p->a = compute_value();
  p->b = 42;
  rcu_assign_pointer(gp, p);
-\end{Verbatim}
-}\adjvspacebelow
-\end{adjustwidth}\adjvspacebottom
+\end{VerbatimT}
+}
+\end{adjustwidth}
 \caption{Compilers Can Reorder Accesses}
 \label{lst:defer:Compilers Can Reorder Accesses}
 \end{listing}
perfbook-lt.tex +6 -0
index 0dc05325..cb8299a5 100644
@@ -397,6 +397,12 @@
 {numbers=left,numbersep=3pt,xleftmargin=5pt,xrightmargin=5pt,frame=single}
 \DefineVerbatimEnvironment{VerbatimU}{Verbatim}%
 {numbers=none,xleftmargin=5pt,xrightmargin=5pt,samepage=true,frame=single}
+% for multi-part listings with explanation
+\DefineVerbatimEnvironment{VerbatimT}{Verbatim}%
+{numbers=left,numbersep=5pt,frame=single,xrightmargin=5pt}
+\BeforeBeginEnvironment{VerbatimT}{\vspace*{2pt}}
+\AfterEndEnvironment{VerbatimT}{\vspace*{-1pt}}
+\AfterEndEnvironment{adjustwidth}{\vspace*{-2pt}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}

base-commit: a9d5892eab1e41da0c12b0b093db1ce0fb06f7d3

Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 Redefine line count format at the beginning of VerbatimT env

Line count format in multi-part listings is customized by redefining fancyvrb's \theFancyVerbLine macro.

However, its definition is common to Verbatim and its derivative envs such as VerbatimL, VerbatimN, VerbatimU, etc.

Currently, to make a definition local to a specific snippet, it needs to be enclosed by "{ ... }" with its counterpart VerbatimT env, which may be error prone.

Instead, add the definition to the VerbatimT env:

\AtBeginEnvironment{VerbatimT}{%

    \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}

, where \myfvline can be defined in front of \begin{VerbatimT}:

\renewcommand{\myfvline}{A\arabic{FancyVerbLine}} % A1, A2, ... \begin{VerbatimT}

    ...

\end{VerbatimT}

---
 defer/rcuintro.tex | 24 ++++++------------------
 perfbook-lt.tex    |  4 ++++
 2 files changed, 10 insertions(+), 18 deletions(-)
defer/rcuintro.tex +6 -18
index 898cdf5f..46625c67 100644
@@ -71,38 +71,32 @@ that would normally be used in single-threaded code.
 \begin{listing}[tb]
 \begin{adjustwidth}{10pt}{5pt}
 \footnotesize
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{A\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = gp;
  do_something_with(p->a);
  do_something_with(p->b);
 \end{VerbatimT}
-}
 Might be transformed to:
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{B\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = gp;
  do_something_with(p->a);
  p = gp;
  do_something_with(p->b);
 \end{VerbatimT}
-}
 The compiler assumes normal variables do not spontaneously change,
 \co{do_something_with()} might use many machine registers, and this
 transformation reduces register pressure.
 But if some other thread changes \co{gp} between lines~B1 and~B3 of the
 transformed code, the values of \co{p->a} and \co{p->b} will be mismatched.
 Prevent this by using \co{rcu_dereference()} as follows:
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{C\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = rcu_dereference(gp);
  do_something_with(p->a);
  do_something_with(p->b);
 \end{VerbatimT}
-}
 \end{adjustwidth}
 \caption{Compilers Can Reload Values}
 \label{lst:defer:Compilers Can Reload Values}
@@ -111,25 +105,21 @@ Prevent this by using \co{rcu_dereference()} as follows:
 \begin{listing}[tb]
 \begin{adjustwidth}{10pt}{5pt}
 \footnotesize
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{A\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = malloc(sizeof(*p));
  p->a = compute_value();
  p->b = 42;
  gp = p;
 \end{VerbatimT}
-}
 Might be transformed to:
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{B\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = malloc(sizeof(*p));
  gp = p;
  p->a = compute_value();
  p->b = 42;
 \end{VerbatimT}
-}
 The compiler assumes normal variables are not concurrently accessed,
 and thus that the order of stores does not matter.
 If \co{compute_value()} was inlined, this transformation might produce
@@ -138,15 +128,13 @@ In this example, if some other thread loads \co{gp} immediately after
 line~B2 of the transformed code, that thread might see pre-initialization
 garbage in \co{p->a} and \co{p->b}.
 Prevent this by using \co{rcu_assign_pointer()} as follows:
-{\renewcommand{\theFancyVerbLine}{%
-  {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{C\arabic{FancyVerbLine}}
 \begin{VerbatimT}
  p = malloc(sizeof(*p));
  p->a = compute_value();
  p->b = 42;
  rcu_assign_pointer(gp, p);
 \end{VerbatimT}
-}
 \end{adjustwidth}
 \caption{Compilers Can Reorder Accesses}
 \label{lst:defer:Compilers Can Reorder Accesses}
perfbook-lt.tex +4 -0
index cb8299a5..f6d6efc2 100644
@@ -403,6 +403,10 @@
 \BeforeBeginEnvironment{VerbatimT}{\vspace*{2pt}}
 \AfterEndEnvironment{VerbatimT}{\vspace*{-1pt}}
 \AfterEndEnvironment{adjustwidth}{\vspace*{-2pt}}
+% for custom line count format such as A1, B2, etc.
+\newcommand{\myfvline}{\arabic{FancyVerbLine}} % redefine on-the-fly
+\AtBeginEnvironment{VerbatimT}{%
+  \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}
Signature · 1 line
2.43.0
#2 lore raw
In reply to Akira Yokosawa
Quoted 7 lines · Akira Yokosawa

Paul said he has a plan to add listings similar to Listings 9.13 and 9.14 [1].

Move local definitions in rcuintro.tex for such multi-part listings into preamble so that they can be used elsewhere. Note that "VerbatimT" and "adjustwidth" envs have vertical spacing tweaks applied.

Queued, thank you very much!!!

I queued the following commit on top of these two in order to create the first Tools of the Trade triple listing. Does this make sense, or is there a better way?

Thanx, Paul

------------------------------------------------------------------------

commit 72f863e04bf56ec4e95e90a789570a2434caf61e

Author: Paul E. McKenney <[email protected]>
Date:   Fri Jun 26 10:44:49 2026 -0700

Add definitions of \adjvspace{above,below,bottom}

perfbook-lt.tex +3 -0
index f6d6efc2..3c1e15f1 100644
@@ -407,6 +407,9 @@
 \newcommand{\myfvline}{\arabic{FancyVerbLine}} % redefine on-the-fly
 \AtBeginEnvironment{VerbatimT}{%
   \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
+\newcommand{\adjvspaceabove}{\vspace*{2pt}}
+\newcommand{\adjvspacebelow}{\vspace*{-1pt}}
+\newcommand{\adjvspacebottom}{\vspace*{-2pt}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}
#3 lore raw
Quoted 17 lines · Paul E. McKenney
Quoted 7 lines · Akira Yokosawa

Paul said he has a plan to add listings similar to Listings 9.13 and 9.14 [1].

Move local definitions in rcuintro.tex for such multi-part listings into preamble so that they can be used elsewhere. Note that "VerbatimT" and "adjustwidth" envs have vertical spacing tweaks applied.

Queued, thank you very much!!!

I queued the following commit on top of these two in order to create the first Tools of the Trade triple listing. Does this make sense, or is there a better way?

Thanx, Paul

Quoted 7 lines

------------------------------------------------------------------------

commit 72f863e04bf56ec4e95e90a789570a2434caf61e

Author: Paul E. McKenney <[email protected]>
Date:   Fri Jun 26 10:44:49 2026 -0700
    Add definitions of \adjvspace{above,below,bottom}

I removed those definitions in Patch 1/2 because (new) VerbatimT and adjustwidth envs now have those extra vertical spaces on their own.

perfbook-lt.tex +6 -0
index 0dc05325..cb8299a5 100644
@@ -397,6 +397,12 @@
 {numbers=left,numbersep=3pt,xleftmargin=5pt,xrightmargin=5pt,frame=single}
 \DefineVerbatimEnvironment{VerbatimU}{Verbatim}%
 {numbers=none,xleftmargin=5pt,xrightmargin=5pt,samepage=true,frame=single}
+% for multi-part listings with explanation
+\DefineVerbatimEnvironment{VerbatimT}{Verbatim}%
+{numbers=left,numbersep=5pt,frame=single,xrightmargin=5pt}
+\BeforeBeginEnvironment{VerbatimT}{\vspace*{2pt}}
+\AfterEndEnvironment{VerbatimT}{\vspace*{-1pt}}
+\AfterEndEnvironment{adjustwidth}{\vspace*{-2pt}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}
Quoted 8 lines
perfbook-lt.tex +0 -0
index f6d6efc2..3c1e15f1 100644

perfbook-lt.tex line 415

Quoted 9 lines
perfbook-lt.tex +3 -0
@@ -407,6 +407,9 @@
 \newcommand{\myfvline}{\arabic{FancyVerbLine}} % redefine on-the-fly
 \AtBeginEnvironment{VerbatimT}{%
   \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
+\newcommand{\adjvspaceabove}{\vspace*{2pt}}
+\newcommand{\adjvspacebelow}{\vspace*{-1pt}}
+\newcommand{\adjvspacebottom}{\vspace*{-2pt}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}

I think the easiest way forward for both of us is to keep your change without applying my changes. I'll rebase my changes after seeing your new multi-part listing(s) and submit v2.

Thanks, Akira

#4 lore raw
Quoted 62 lines · Akira Yokosawa
Quoted 17 lines · Paul E. McKenney
Quoted 7 lines · Akira Yokosawa

Paul said he has a plan to add listings similar to Listings 9.13 and 9.14 [1].

Move local definitions in rcuintro.tex for such multi-part listings into preamble so that they can be used elsewhere. Note that "VerbatimT" and "adjustwidth" envs have vertical spacing tweaks applied.

Queued, thank you very much!!!

I queued the following commit on top of these two in order to create the first Tools of the Trade triple listing. Does this make sense, or is there a better way?

Thanx, Paul

Quoted 7 lines

------------------------------------------------------------------------

commit 72f863e04bf56ec4e95e90a789570a2434caf61e

Author: Paul E. McKenney <[email protected]>
Date:   Fri Jun 26 10:44:49 2026 -0700
    Add definitions of \adjvspace{above,below,bottom}

I removed those definitions in Patch 1/2 because (new) VerbatimT and adjustwidth envs now have those extra vertical spaces on their own.

perfbook-lt.tex +6 -0
index 0dc05325..cb8299a5 100644
@@ -397,6 +397,12 @@
 {numbers=left,numbersep=3pt,xleftmargin=5pt,xrightmargin=5pt,frame=single}
 \DefineVerbatimEnvironment{VerbatimU}{Verbatim}%
 {numbers=none,xleftmargin=5pt,xrightmargin=5pt,samepage=true,frame=single}
+% for multi-part listings with explanation
+\DefineVerbatimEnvironment{VerbatimT}{Verbatim}%
+{numbers=left,numbersep=5pt,frame=single,xrightmargin=5pt}
+\BeforeBeginEnvironment{VerbatimT}{\vspace*{2pt}}
+\AfterEndEnvironment{VerbatimT}{\vspace*{-1pt}}
+\AfterEndEnvironment{adjustwidth}{\vspace*{-2pt}}
 
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}
Quoted 16 lines
perfbook-lt.tex +3 -0
index f6d6efc2..3c1e15f1 100644
@@ -407,6 +407,9 @@
 \newcommand{\myfvline}{\arabic{FancyVerbLine}} % redefine on-the-fly
 \AtBeginEnvironment{VerbatimT}{%
   \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
+\newcommand{\adjvspaceabove}{\vspace*{2pt}}
+\newcommand{\adjvspacebelow}{\vspace*{-1pt}}
+\newcommand{\adjvspacebottom}{\vspace*{-2pt}}
 
Quoted 6 lines

perfbook-lt.tex line 408

Quoted 2 lines
perfbook-lt.tex +0 -0
 \IfLmttForCode{
 \AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}

I think the easiest way forward for both of us is to keep your change without applying my changes. I'll rebase my changes after seeing your new multi-part listing(s) and submit v2.

OK, so I need to revert my addition of \adjvspace{above,below,bottom}, remove them from my toolsoftrade table, change the Verbatim instances to VerbatimT, then all should be well. (Hey, I can dream, can't I?)

Let me try that, then please do adjust on top if needed.

Thanx, Paul