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

Akira Yokosawa <[email protected]> Fri, 26 Jun 2026 20:34:47 +0900
Newsgroups org.kernel.vger.perfbook
Message-ID <[email protected]>
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.

Link: https://lore.kernel.org/perfbook/abeb5ab5-9c0f-4948-bfcb-392c7b77c6db@paulmck-laptop/ [1]
Signed-off-by: Akira Yokosawa <[email protected]>
---
 defer/rcuintro.tex | 52 ++++++++++++++++++----------------------------
 perfbook-lt.tex    |  6 ++++++
 2 files changed, 26 insertions(+), 32 deletions(-)

diff --git a/defer/rcuintro.tex b/defer/rcuintro.tex
index 6997378b..898cdf5f 100644
--- a/defer/rcuintro.tex
+++ b/defer/rcuintro.tex
@@ -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}
diff --git a/perfbook-lt.tex b/perfbook-lt.tex
index 0dc05325..cb8299a5 100644
--- a/perfbook-lt.tex
+++ b/perfbook-lt.tex
@@ -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
-- 
2.43.0