feat: add lab-rv32i-freertos-vector-raii card

This commit is contained in:
user
2026-07-21 19:14:20 +02:00
commit d1094e85f2
64 changed files with 33884 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
# K03 — Vector V1: destructor, move, ownership and RAII
## Position in the series
- Series: FreeRTOS C++
- Lesson: L02, card K03
- Executables: exactly one
- Kernel: unchanged FreeRTOS V11.3.0 in C
- C++ mode: freestanding C++17, no exceptions, RTTI or hosted `libstdc++`
- Starting artifact: the deliberately incomplete `Vector V0` shown on the card
The current K02 card concentrates on `Task<Derived>` and does not require a
separate Vector exercise. K03 therefore carries its short V0 starting snippet
on page 1. This removes a hidden prerequisite without spending a second card
on the broken implementation.
## Outcome
After 45 minutes the student can state and prove the one-owner invariant,
explain why copy operations are deleted, implement the release-before-transfer
order of move assignment, and show in Hazard3 that normal scope exit returns
the FreeRTOS heap to its baseline.
## Canonical experiment
```text
heap baseline
-> source(4) owns A
-> destination(2) owns B
-> destination = move(source)
free B
destination takes A
source becomes {nullptr, 0}
-> final_owner(move(destination)) owns A
-> final_owner destructor frees A
-> two empty moved-from objects destruct without another free
-> heap baseline restored
```
## Lesson plan — 45 minutes
| Time | Mode | Student evidence |
| --- | --- | --- |
| 05 | diagnose V0 | identify leak, shallow-copy double-free risk and missing ownership rule |
| 511 | destructor | connect object scope to `delete[]` and then to `vPortFree()` |
| 1117 | copy vs move | justify deleted copy and the empty moved-from state |
| 1725 | move assignment | order `release -> take -> clear source`; handle occupied destination and self-move |
| 2538 | Hazard3/GDB | replay checkpoints 17 and follow buffer A, buffer B, counts and heap bytes |
| 3843 | exercise | complete the three state-changing parts of move assignment and test the invariant |
| 4345 | exit | explain one-owner, baseline recovery and the limit of RAII under forced task deletion |
## Stable evidence contract
| Point | Required observation |
| --- | --- |
| 1 | `g_initial_free` is the reference after heap initialization |
| 2 | `source.data() == A`, allocation count 1 |
| 3 | `destination.data() == B`, `A != B`, allocation count 2 |
| 4 | first freed address is B; destination has A; source is empty |
| 5 | final owner has the same A; destination is empty |
| 6 | A is the second freed address; current free equals baseline |
| 7 | allocations=2, deallocations=2, live=0, `pass=1` |
Numeric heap values are recorded but the assessment relies on relations:
```text
after_two_owners < initial
minimum_ever <= after_two_owners
after_scope == initial
```
## Student exercise
The destination already owns B. The learner must put these operations in the
only safe order and explain each invariant boundary:
```cpp
if (this != &other) {
release();
elements_ = other.elements_;
size_ = other.size_;
other.elements_ = nullptr;
other.size_ = 0;
}
```
Required reasoning:
1. Taking A before releasing B loses the only pointer to B and leaks it.
2. Releasing after taking A would free A, the buffer just transferred.
3. Not clearing the source creates two owners and a later double free.
4. Omitting the self-move guard releases the only buffer before reading it.
## Acceptance
- host test passes under ASan and UBSan;
- a copy attempt fails to compile because the operation is deleted;
- ELF contains no vtable, RTTI, exceptions, dynamic initializer or hosted C++
dependency;
- all six required scalar/array, sized/unsized allocation functions exist;
- Hazard3 reaches checkpoint 7 with `pass=1`;
- student connects `delete[] -> operator delete[] -> vPortFree()`;
- student states that RAII requires normal C++ lifetime completion and does not
promise cleanup after arbitrary task termination.
+391
View File
@@ -0,0 +1,391 @@
% Generated from json/card_source.json by tools/render_card.py.
% Do not edit manually; update the JSON and regenerate.
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[polish]{babel}
\usepackage{csquotes}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage[a4paper,left=2.15cm,right=2.15cm,top=0.5cm,bottom=0.5cm,headheight=24mm,headsep=3mm,includehead]{geometry}
\usepackage{eso-pic}
\usepackage{marginnote}
\usepackage{array}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{pdflscape}
\usepackage{float}
\usepackage{silence}
\WarningFilter{soulutf8}{This package is obsolete}
\usepackage{pdfcomment}
\usepackage{enumitem}
\usepackage{needspace}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{microtype}
\usepackage[most]{tcolorbox}
\usepackage{qrcode}
\IfFileExists{references.bib}{%
\usepackage[backend=biber,style=numeric,sorting=none]{biblatex}%
\addbibresource{references.bib}%
}{}
\hypersetup{hidelinks}
\IfFileExists{build-meta.tex}{%
\input{build-meta.tex}%
}{%
\newcommand{\BuildCommit}{lokalna}%
}
\newcommand{\DocumentAuthor}{M. Pabiszczak}
\newcommand{\DocumentYear}{2026}
\newcommand{\CardSeries}{freertos-cpp}
\newcommand{\CardNumber}{03}
\newcommand{\CardCount}{16}
\newcommand{\CardSlug}{vector-raii}
\newcommand{\CardVersion}{v00.01}
\newcommand{\DocumentUUID}{de1a2818-4c30-5760-8b31-255114f82e90}
\newcommand{\EmptyCheck}{\(\square\)}
\newcommand{\EscAnswerLines}[1][3]{\par\noindent\dotfill\par\noindent\dotfill\par\noindent\dotfill}
\newcommand{\EscWriteRow}[1][2.8em]{\rule{0pt}{#1}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.45em}
\setlength{\headheight}{24mm}
\setlength{\headsep}{3mm}
\setlength{\footskip}{8mm}
\setlength{\marginparwidth}{1.5cm}
\setlength{\marginparsep}{0.25cm}
\renewcommand{\arraystretch}{1.22}
% Margin separator lines disabled for layout trial.
\newcommand{\ESCPageResourceHeader}{%
\begingroup%
\setlength{\parskip}{0pt}%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{0.35pt}%
\setlength{\arrayrulewidth}{0.35pt}%
\noindent\fbox{%
\begin{minipage}[c][23.5mm][c]{\dimexpr\textwidth-2\fboxrule\relax}%
\begin{minipage}[c][23mm][c]{\dimexpr\linewidth-24mm-0.35pt\relax}%
\setlength{\tabcolsep}{0pt}%
\renewcommand{\arraystretch}{0}%
\begin{tabularx}{\linewidth}{@{}p{\dimexpr0.50000000\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.15126050\linewidth-\arrayrulewidth\relax}|X@{}}%
\parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries TITLE}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{7.7}{7.9}\selectfont\ttfamily\bfseries Vector V1: destruktor, move i jeden właściciel}\hspace{0.45mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries VER.}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{6.6}{6.8}\selectfont\ttfamily\bfseries v00.01}\hspace{0.45mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries DATETIME}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{6.4}{6.6}\selectfont\ttfamily\bfseries 2026-07-19T00:00:00+02:00}\hspace{0.45mm}}\par} \\%
\end{tabularx}%
\par\nointerlineskip%
\hrule height0.35pt%
\nointerlineskip%
\begin{tabularx}{\linewidth}{@{}p{\dimexpr0.50000000\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.25210084\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.14285714\linewidth-\arrayrulewidth\relax}|X@{}}%
\parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries PROJECT}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{7.7}{7.9}\selectfont\ttfamily\bfseries Freestanding C++ nad FreeRTOS}\hspace{0.45mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries SERIES}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{6.4}{6.6}\selectfont\ttfamily\bfseries FreeRTOS C++}\hspace{0.45mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries CARD}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{6.4}{6.6}\selectfont\ttfamily\bfseries 03/16}\hspace{0.45mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{3.2}{3.4}\selectfont\ttfamily\bfseries SHEET}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{6.4}{6.6}\selectfont\ttfamily\bfseries \thepage/\pageref{LastPage}}\hspace{0.45mm}}\par} \\%
\end{tabularx}%
\par\nointerlineskip%
\hrule height0.35pt%
\nointerlineskip%
\begin{tabularx}{\linewidth}{@{}p{\dimexpr0.05882353\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.05042017\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.14705882\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.09243697\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.07563025\linewidth-\arrayrulewidth\relax}|p{\dimexpr0.07563025\linewidth-\arrayrulewidth\relax}|X@{}}%
\parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries SUBJ.}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries Inf.}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries PROG.}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries CORE}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries SCOPE}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries LEVEL}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hspace{0.15mm}{\fontsize{2.85}{3}\selectfont\ttfamily\bfseries POS.}\par\nointerlineskip\vspace{0.12mm}\noindent\makebox[\linewidth][r]{{\fontsize{4.45}{4.65}\selectfont\ttfamily\bfseries}\hspace{0.15mm}}\par} & \parbox[c][5.19414mm][c]{\linewidth}{\hbox to\linewidth{\hspace{0.35mm}{\fontsize{3.4}{3.75}\selectfont\ttfamily\bfseries GITEA UUID}\hfill{\fontsize{3.4}{3.75}\selectfont\ttfamily de1a2818-4c30-5760-8b31-255114f82e90}\hspace{0.35mm}}\par\nointerlineskip\hbox to\linewidth{\hspace{0.35mm}{\fontsize{3.4}{3.75}\selectfont\ttfamily\bfseries CARD UUID} {\fontsize{3.4}{3.75}\selectfont\ttfamily de1a2818-4c30-5760-8b31-255114f82e90}\hfill{\fontsize{3.4}{3.75}\selectfont\ttfamily\bfseries AUTHOR M. Pabiszczak}\hspace{0.35mm}}} \\%
\end{tabularx}%
\par\nointerlineskip%
\hrule height0.35pt%
\nointerlineskip%
\begin{tabularx}{\linewidth}{@{}X@{}}%
\parbox[c][3.46276mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{4.5}{4.85}\selectfont\ttfamily\bfseries GITEA} {\fontsize{4.5}{4.85}\selectfont\ttfamily\href{https://zsl-gitea.mpabi.pl/edu-freertos-cpp/lab-rv32i-freertos-vector-raii}{\nolinkurl{https://zsl-gitea.mpabi.pl/edu-freertos-cpp/lab-rv32i-freertos-vector-raii}}}} \\%
\end{tabularx}%
\par\nointerlineskip%
\hrule height0.35pt%
\nointerlineskip%
\begin{tabularx}{\linewidth}{@{}X@{}}%
\parbox[c][3.46276mm][c]{\linewidth}{\hspace{0.45mm}{\fontsize{4.5}{4.85}\selectfont\ttfamily\bfseries HTML} {\fontsize{4.5}{4.85}\selectfont\ttfamily\href{}{\nolinkurl{}}}} \\%
\end{tabularx}%
\end{minipage}%
\vrule width0.35pt%
\begin{minipage}[c][23mm][c]{24mm}\centering%
{\tiny QR wyłączony}%
\end{minipage}%
\end{minipage}%
}%
\endgroup%
}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{\ESCPageResourceHeader}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\newcommand{\ESCMarginTag}[4]{%
\hspace*{#1}\textcolor{#2}{#3~#4}%
}
\newcommand{\ESCSectionBlockStart}{%
\Needspace{8\baselineskip}%
\par\vspace{0.35em}%
\noindent\textcolor{black!28}{\rule{\textwidth}{0.35pt}}%
\par\vspace{0.15em}%
}
\newcommand{\ESCSectionBlockEnd}{%
\par\vspace{0.25em}%
\noindent\textcolor{black!18}{\rule{\textwidth}{0.25pt}}%
\par\vspace{0.45em}%
}
\newcommand{\ESCTinyStepSeparator}{%
\par\vspace{0.10em}%
\noindent{\color{black!18}\leaders\hbox{\rule{0.60em}{0.22pt}\hspace{0.38em}}\hfill\kern0pt}%
\par\vspace{0.10em}%
}
\newtcolorbox{ESCTaskFrame}[1]{enhanced,breakable,arc=0pt,boxrule=0.35pt,colback=white,colframe=black,boxsep=0pt,left=1.4mm,right=1.4mm,top=0.8mm,bottom=0.8mm,colbacktitle=black!7,coltitle=black,title={\ttfamily\bfseries TASK\quad #1}}
\newtcolorbox{ESCBlockFrame}[1]{enhanced,breakable,arc=0pt,boxrule=0.35pt,colback=white,colframe=black!55,boxsep=0pt,left=1.0mm,right=1.0mm,top=0.6mm,bottom=0.6mm,colbacktitle=black!4,coltitle=black,title={\ttfamily\bfseries BLOCK\quad #1}}
\newtcolorbox{ESCStepFrame}[1]{enhanced,breakable,arc=0pt,boxrule=0.35pt,colback=white,colframe=black!28,boxsep=0pt,left=0.8mm,right=0.8mm,top=0.45mm,bottom=0.45mm,colbacktitle=black!2,coltitle=black,title={\ttfamily STEP\quad #1}}
\newtcolorbox{ESCConclusionFrame}{enhanced,breakable,arc=0pt,boxrule=0.45pt,colback=blue!2,colframe=blue!45!black,boxsep=0pt,left=1.2mm,right=1.2mm,top=0.7mm,bottom=0.7mm,colbacktitle=blue!7,coltitle=black,title={\ttfamily\bfseries WNIOSEK}}
\newcommand{\ESCLearningTreeWidget}{%
\reversemarginpar
\marginnote[%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\begin{minipage}[t]{\marginparwidth}%
\raggedright
{\bfseries\textcolor{black!65}{TECH}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\hspace*{0.28em}\textcolor{orange!85!black}{EK~LOCAL~DBG.MEM}\textcolor{black!48}{\pdftooltip[width=\textwidth]{.01}{Śledzi adresy właścicieli i stan heapu na stabilnych checkpointach.}}\par%
\hspace*{0.54em}\textcolor{blue!70!black}{KW~LOCAL~DBG.MEM}\textcolor{black!48}{\pdftooltip[width=\textwidth]{.01}{Pokazuje B jako pierwszy free, A jako drugi free i powrót heapu do baseline.}}\par%
\end{minipage}%
}%
\end{minipage}%
]{}
\normalmarginpar
\marginnote{%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\hspace*{0.06cm}%
\begin{minipage}[t]{\dimexpr\marginparwidth-0.06cm\relax}%
\raggedright
{\bfseries\textcolor{black!65}{OG}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\hspace*{0.28em}\textcolor{green!50!black}{EN~LOCAL~EN~OOP.OWN}\textcolor{black!48}{\pdftooltip[width=\textwidth]{.01}{Formułuje inwariant i analizuje cztery błędne kolejności move assignment.}}\par%
\hspace*{0.54em}\textcolor{blue!70!black}{KW~LOCAL~KW~OOP.OWN}\textcolor{black!48}{\pdftooltip[width=\textwidth]{.01}{Uzasadnia copy=delete, move noexcept oraz pusty stan źródła.}}\par%
\end{minipage}%
}%
\end{minipage}%
}
}
\begin{document}
\sloppy
\vspace{1.0em}
\noindent{\Large\bfseries Cel karty}\par
\vspace{0.35em}
Uczeń buduje move-only \texttt{VectorV1}, formułuje inwariant jednego właściciela i śledzi ten sam adres bufora przez move assignment, move construction i destruktor.
\vspace{0.8em}
\noindent\textcolor{black!25}{\rule{\textwidth}{0.35pt}}
\vspace{0.7em}
\noindent{\Large\bfseries Zakres karty}\par
\vspace{0.35em}
Jedno zadanie używa prawdziwego \texttt{heap\_4.c} na Hazard3. Kopiowanie jest zabronione, move jest \texttt{noexcept}, a zwykłe i tablicowe warianty new/delete prowadzą do jednego heapu FreeRTOS. Poza zakresem są wzrost pojemności, bounds checking, allocator selection i uruchamianie tasków.
\vspace{0.8em}
\noindent\textcolor{black!25}{\rule{\textwidth}{0.35pt}}
\clearpage
\ESCSectionBlockStart
\section{V0 i inwariant jednego właściciela}
\reversemarginpar
\marginnote[%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\begin{minipage}[t]{\marginparwidth}%
\raggedright
{\bfseries\textcolor{black!65}{TECH}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
]{}[-3.1em]
\normalmarginpar
\marginnote{%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\hspace*{0.06cm}%
\begin{minipage}[t]{\dimexpr\marginparwidth-0.06cm\relax}%
\raggedright
{\bfseries\textcolor{black!65}{OG}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
}[-3.1em]
\begingroup
\scriptsize\ttfamily\color{black!60}\sloppy
\noindent drzewka: \pdftooltip[width=\textwidth]{D1}{K03.WE01.OG.LOCAL.OOP.OWN.01 | WE 01: Vector V1 i RAII. Zaprojektowanie i zbadanie move-only\textCR właściciela bufora nad heap\_4. | EN LOCAL OOP.OWN.01: Formułuje inwariant i analizuje cztery\textCR błędne kolejności move assignment. | KW LOCAL OOP.OWN.01: Uzasadnia copy=delete, move\textCR noexcept oraz pusty stan źródła.}\par
\vspace{0.10em}%
\noindent K1: Nazwij właściciela, zasób i dwa błędy Vector V0.\quad D1\par
\ESCTinyStepSeparator
\noindent K2: Uzasadnij destruktor oraz usunięcie operacji kopiowania.\quad D1\par
\par\vspace{0.18em}%
\endgroup
Zdiagnozuj wyciek V0 i ryzyko double free po dodaniu destruktora bez usunięcia kopiowania.
\ESCSectionBlockEnd
\ESCSectionBlockStart
\section{Move assignment do zajętego celu}
\reversemarginpar
\marginnote[%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\begin{minipage}[t]{\marginparwidth}%
\raggedright
{\bfseries\textcolor{black!65}{TECH}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
]{}[-3.1em]
\normalmarginpar
\marginnote{%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\hspace*{0.06cm}%
\begin{minipage}[t]{\dimexpr\marginparwidth-0.06cm\relax}%
\raggedright
{\bfseries\textcolor{black!65}{OG}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
}[-3.1em]
\begingroup
\scriptsize\ttfamily\color{black!60}\sloppy
\noindent drzewka: \pdftooltip[width=\textwidth]{D1}{K03.WE01.OG.LOCAL.OOP.OWN.01 | WE 01: Vector V1 i RAII. Zaprojektowanie i zbadanie move-only\textCR właściciela bufora nad heap\_4. | EN LOCAL OOP.OWN.01: Formułuje inwariant i analizuje cztery\textCR błędne kolejności move assignment. | KW LOCAL OOP.OWN.01: Uzasadnia copy=delete, move\textCR noexcept oraz pusty stan źródła.}\par
\vspace{0.10em}%
\noindent K1: Zwolnij B przed przejęciem A i wyjaśnij self-move guard.\quad D1\par
\ESCTinyStepSeparator
\noindent K2: Wyzeruj źródło i sprawdź stan moved-from.\quad D1\par
\par\vspace{0.18em}%
\endgroup
Dla source->A i destination->B ułóż release, transfer pól i wyzerowanie źródła.
\ESCSectionBlockEnd
\ESCSectionBlockStart
\section{Hazard3: checkpointy 17}
\reversemarginpar
\marginnote[%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\begin{minipage}[t]{\marginparwidth}%
\raggedright
{\bfseries\textcolor{black!65}{TECH}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
]{}[-3.1em]
\normalmarginpar
\marginnote{%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\hspace*{0.06cm}%
\begin{minipage}[t]{\dimexpr\marginparwidth-0.06cm\relax}%
\raggedright
{\bfseries\textcolor{black!65}{OG}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
}[-3.1em]
\begingroup
\scriptsize\ttfamily\color{black!60}\sloppy
\noindent drzewka: \pdftooltip[width=\textwidth]{D1}{K03.WE01.TECH.LOCAL.DBG.MEM.01 | WE 01: Vector V1 i RAII. Zaprojektowanie i zbadanie\textCR move-only właściciela bufora nad heap\_4. | EK LOCAL DBG.MEM.01: Śledzi adresy właścicieli i\textCR stan heapu na stabilnych checkpointach. | KW LOCAL DBG.MEM.01: Pokazuje B jako pierwszy\textCR free, A jako drugi free i powrót heapu do baseline.}\par
\vspace{0.10em}%
\noindent K1: Na STOP 3 zapisz dwa różne adresy i stan heapu.\quad D1\par
\ESCTinyStepSeparator
\noindent K2: Na STOP 4 pokaż B jako pierwszy zwolniony adres i A u celu.\quad D1\par
\ESCTinyStepSeparator
\noindent K3: Na STOP 7 potwierdź 2/2/0, baseline i PASS.\quad D1\par
\par\vspace{0.18em}%
\endgroup
Śledź A i B, liczniki allocation/deallocation oraz relacje current free i minimum-ever.
\ESCSectionBlockEnd
\ESCSectionBlockStart
\section{Granica RAII}
\reversemarginpar
\marginnote[%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\begin{minipage}[t]{\marginparwidth}%
\raggedright
{\bfseries\textcolor{black!65}{TECH}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
]{}[-3.1em]
\normalmarginpar
\marginnote{%
\begin{minipage}{\marginparwidth}%
\raggedright
{\fontsize{3.55}{3.95}\selectfont\ttfamily
\hspace*{0.06cm}%
\begin{minipage}[t]{\dimexpr\marginparwidth-0.06cm\relax}%
\raggedright
{\bfseries\textcolor{black!65}{OG}\par}%
\vspace{0.08em}%
\hspace*{0.00em}\textcolor{red}{WE~01}\textcolor{black!48}{}\par%
\end{minipage}%
}%
\end{minipage}%
}[-3.1em]
\begingroup
\scriptsize\ttfamily\color{black!60}\sloppy
\noindent drzewka: \pdftooltip[width=\textwidth]{D1}{K03.WE01.OG.LOCAL.OOP.OWN.01 | WE 01: Vector V1 i RAII. Zaprojektowanie i zbadanie move-only\textCR właściciela bufora nad heap\_4. | EN LOCAL OOP.OWN.01: Formułuje inwariant i analizuje cztery\textCR błędne kolejności move assignment. | KW LOCAL OOP.OWN.01: Uzasadnia copy=delete, move\textCR noexcept oraz pusty stan źródła.}\par
\vspace{0.10em}%
\noindent K1: Odróżnij normalne wyjście z zakresu od wymuszonego zakończenia taska.\quad D1\par
\par\vspace{0.18em}%
\endgroup
RAII wymaga normalnego zakończenia czasu życia C++; arbitralne usunięcie taska bez unwindingu nie uruchamia automatycznie destruktorów jego ramek.
\ESCSectionBlockEnd
\end{document}
+314
View File
@@ -0,0 +1,314 @@
\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[polish]{babel}
\usepackage[a4paper,margin=1.55cm]{geometry}
\usepackage{array,tabularx,booktabs}
\usepackage{amsmath,amssymb}
\usepackage{xcolor,listings}
\usepackage{hyperref,fancyhdr,lastpage,enumitem}
\definecolor{accent}{HTML}{16324A}
\definecolor{accentlight}{HTML}{EEF3F7}
\definecolor{rulegray}{HTML}{D7DEE5}
\hypersetup{colorlinks=true,linkcolor=accent,urlcolor=blue}
\IfFileExists{build-meta.tex}{\input{build-meta.tex}}{\newcommand{\BuildCommit}{local}}
\newcommand{\PublisherDomain}{mpabi}
\newcommand{\CardArea}{inf}
\newcommand{\CardSeries}{freertos-cpp}
\newcommand{\CardNumber}{03}
\newcommand{\CardCount}{16}
\newcommand{\CardSlug}{vector-raii}
\newcommand{\CardVersion}{v00.01}
\newcommand{\DocumentUUID}{de1a2818-4c30-5760-8b31-255114f82e90}
\newcommand{\blank}[1]{\rule{#1}{.2pt}}
\lstset{
language=C++,basicstyle=\ttfamily\scriptsize,columns=fullflexible,
keepspaces=true,frame=single,breaklines=true,showstringspaces=false,
numbers=none,backgroundcolor=\color{accentlight},rulecolor=\color{rulegray}
}
\pagestyle{fancy}
\fancyhf{}
\lhead{\textbf{K03 · FreeRTOS C++ · \texttt{VectorV1}}}
\rhead{\small L02 · ownership i RAII}
\lfoot{\scriptsize commit \BuildCommit}
\cfoot{\scriptsize \thepage/\pageref{LastPage}}
\rfoot{\scriptsize V11.3.0 / \CardVersion}
\setlength{\headheight}{14pt}
\setlength{\footskip}{19pt}
\setlist[itemize]{nosep,leftmargin=1.45em}
\setlist[enumerate]{nosep,leftmargin=1.65em}
\begin{document}
\sloppy
\begin{center}
{\LARGE\bfseries \texttt{VectorV1}: destruktor, move i jeden właściciel}\par
\vspace{.25em}
{\large od wycieku i płytkiej kopii do obserwowalnego RAII}\par
\end{center}
\noindent\begin{tabularx}{\textwidth}{@{}p{1.65cm}Xp{1.55cm}X@{}}
\toprule
Karta & K03 / \CardCount & Czas & 45 minut \\
Platforma & Hazard3 / RV32I & Język & freestanding C++17 \\
Allocator & FreeRTOS \texttt{heap\_4} & Kernel & V11.3.0, bez zmian \\
Wersja & \CardVersion & UUID karty & \texttt{de1a2818-...} \\
\bottomrule
\end{tabularx}
\section*{Punkt startowy: celowo niebezpieczny Vector V0}
\begin{lstlisting}
class VectorV0 {
public:
explicit VectorV0(size_t n)
: elements_{n ? new double[n] : nullptr}, size_{n} {}
private:
double *elements_;
size_t size_;
}; // brak destruktora; kopiowanie byloby plytkie
\end{lstlisting}
\noindent\fcolorbox{accent}{accentlight}{%
\begin{minipage}{.94\textwidth}
\textbf{Inwariant K03.} Każdy żywy bufor ma dokładnie jednego właściciela.
Obiekt po przeniesieniu jest pusty: \texttt{data()==nullptr} i
\texttt{size()==0}.
\end{minipage}}
\section*{Najpierw diagnoza}
\begin{tabularx}{\textwidth}{@{}p{4.2cm}X@{}}
\toprule
Operacja na V0 & Co może pójść źle? \\
\midrule
wyjście z zakresu & \blank{9cm} \\
domyślna kopia wskaźnika & \blank{9cm} \\
dodanie destruktora bez zakazu kopii & \blank{9cm} \\
przypisanie do obiektu, który już ma bufor & \blank{9cm} \\
\bottomrule
\end{tabularx}
\section*{Plan lekcji}
\begin{tabularx}{\textwidth}{@{}p{1.5cm}p{3.0cm}X@{}}
\toprule
Czas & Tryb & Dowód \\
\midrule
0--5 & V0 & wyciek, płytka kopia, brak reguły właściciela \\
5--11 & destruktor & \texttt{delete[]} prowadzi do \texttt{vPortFree()} \\
11--17 & copy / move & copy usunięte; źródło po move jest puste \\
17--25 & move assignment & \texttt{release -> take -> clear source} \\
25--38 & Hazard3/GDB & ten sam adres A, zwolnienie B, heap wraca do baseline \\
38--45 & ćwiczenie i wyjście & kolejność operacji oraz granica RAII \\
\bottomrule
\end{tabularx}
\newpage
\section{Co naprawdę posiada obiekt?}
\begin{center}
\texttt{VectorV1 object: [ elements\_=A | size\_=4 ]}
\qquad$\longrightarrow$\qquad
\texttt{heap\_4: [ A: 4 * double ]}
\end{center}
Obiekt ma dwa pola i może leżeć na stosie. Dane są osobnym blokiem w
\texttt{ucHeap}. Destruktor zwalnia \emph{bufor}, nie pamięć samego obiektu.
\subsection*{Pięć operacji właściciela}
\begin{tabularx}{\textwidth}{@{}p{3.3cm}p{2.7cm}X@{}}
\toprule
Operacja & K03 & Powód \\
\midrule
destruktor & implementowany & zwalnia jedyny posiadany bufor \\
copy constructor & \texttt{= delete} & płytka kopia stworzyłaby dwóch właścicieli \\
copy assignment & \texttt{= delete} & ten sam problem oraz możliwa utrata starego celu \\
move constructor & \texttt{noexcept} & przejmuje adres, czyści źródło \\
move assignment & \texttt{noexcept} & najpierw zwalnia stary bufor celu \\
\bottomrule
\end{tabularx}
\subsection*{Destruktor i stan pusty}
\begin{lstlisting}
~VectorV1() noexcept { release(); }
void release() noexcept {
delete[] elements_; // delete[] nullptr jest bezpieczne
elements_ = nullptr;
size_ = 0;
}
\end{lstlisting}
\subsection*{Move constructor: nowy obiekt nie ma starego zasobu}
\begin{lstlisting}
VectorV1(VectorV1&& other) noexcept
: elements_{other.elements_}, size_{other.size_} {
other.elements_ = nullptr;
other.size_ = 0;
}
\end{lstlisting}
\textbf{Predykcja.} Przed move: \texttt{source.elements\_=A}. Po move:
\begin{center}
\texttt{new.elements\_=\blank{1.5cm}}\qquad
\texttt{source.elements\_=\blank{1.5cm}}\qquad
liczba \texttt{delete[]} podczas samego move: \blank{1cm}
\end{center}
\vfill
\noindent\textbf{Wniosek:} move przenosi prawo do późniejszego zwolnienia.
Nie kopiuje bufora i nie alokuje drugiego.
\newpage
\section{Move assignment do zajętego celu}
\begin{center}
\texttt{source -> A}\qquad\texttt{destination -> B}\qquad $A\neq B$
\end{center}
Cel już posiada B. Wpisz numery 1--5 w jedynej bezpiecznej kolejności:
\noindent\begin{tabularx}{\textwidth}{@{}p{1.5cm}X@{}}
\toprule
Kolejność & Operacja \\
\midrule
\blank{1cm} & \texttt{other.elements\_ = nullptr; other.size\_ = 0;} \\
\blank{1cm} & \texttt{elements\_ = other.elements\_;} \\
\blank{1cm} & sprawdź \texttt{this != \&other} \\
\blank{1cm} & \texttt{release();} \\
\blank{1cm} & \texttt{size\_ = other.size\_;} \\
\bottomrule
\end{tabularx}
\subsection*{Sprawdź cztery błędne warianty}
\begin{enumerate}
\item Najpierw nadpisz \texttt{elements\_} adresem A. Co stało się z B?
\item Po przejęciu A wykonaj \texttt{release()}. Który bufor zwolnisz?
\item Nie wyzeruj źródła. Ile obiektów uważa, że posiada A?
\item Pomiń kontrolę self-move. Co zrobi \texttt{x = move(x)}?
\end{enumerate}
\subsection*{Odsłonięcie po predykcji}
\begin{lstlisting}
if (this != &other) {
release(); // free B
elements_ = other.elements_; // take A
size_ = other.size_;
other.elements_ = nullptr; // source no longer owns A
other.size_ = 0;
}
\end{lstlisting}
\section*{Most C++ $\rightarrow$ FreeRTOS C}
\begin{center}
\texttt{\~VectorV1 -> delete[] -> operator delete[] -> vPortFree}
\end{center}
Most alokacji definiuje komplet: \texttt{new}, \texttt{new[]}, zwykłe i
tablicowe \texttt{delete}, każde w wariancie sized oraz unsized. Zwykłe
\texttt{new} w tym profilu nie może zwrócić \texttt{nullptr}; błąd alokacji
kończy się kontrolowanym fail-fast.
\newpage
\section{Hazard3/GDB: śledź A i B, nie nazwy zmiennych}
\begin{lstlisting}[language=bash]
make check
riscv64-unknown-elf-gdb build/task01_vector_raii/prog.elf
b vector_raii_debug_checkpoint
\end{lstlisting}
\noindent\begin{tabularx}{\textwidth}{@{}p{1cm}p{4.2cm}X@{}}
\toprule
STOP & Stan & Obowiązkowa obserwacja \\
\midrule
1 & baseline & \texttt{g\_initial\_free} zapisane \\
2 & source ma A & allocation count = 1; A leży w \texttt{ucHeap} \\
3 & source ma A, destination ma B & dwa różne adresy; free spadło \\
4 & assignment wykonany & pierwszy free to B; destination ma A; source puste \\
5 & move construction & final owner ma nadal A; destination puste \\
6 & final owner poza zakresem & drugi free to A; free wróciło do baseline \\
7 & wynik & allocations=2, frees=2, live=0, pass=1 \\
\bottomrule
\end{tabularx}
\subsection*{Minimalny zestaw poleceń}
\begin{lstlisting}
p g_last_checkpoint
p/x g_source_buffer_before_move
p/x g_destination_old_buffer
p/x g_destination_buffer_after_assignment
p/x g_final_buffer_after_construction
p g_cpp_allocation_count
p g_cpp_deallocation_count
p g_cpp_live_allocations
p g_initial_free
p g_after_two_owners_free
p g_after_scope_free
p g_minimum_ever_free
\end{lstlisting}
\section*{Relacje do wpisania}
\begin{tabularx}{\textwidth}{@{}X p{4.2cm}@{}}
\toprule
Warunek & Odczyt / dowód \\
\midrule
\texttt{after\_two\_owners < initial} & \blank{4cm} \\
\texttt{minimum\_ever <= after\_two\_owners} & \blank{4cm} \\
\texttt{after\_scope == initial} & \blank{4cm} \\
adres po assignment = A & \blank{4cm} \\
adres po move construction = A & \blank{4cm} \\
kolejność freed addresses = B, A & \blank{4cm} \\
\bottomrule
\end{tabularx}
\newpage
\section*{Wyjście — odpowiedz bez uruchamiania programu}
\begin{enumerate}
\item Dlaczego samo dodanie destruktora do płytko kopiowalnego V0 pogarsza
błąd z wycieku do możliwego double free?\\[1.5em]
\item Dlaczego move assignment musi zwolnić B przed przejęciem A?\\[1.5em]
\item Dlaczego \texttt{other=nullptr} jest zmianą własności, a nie tylko
kosmetycznym „czyszczeniem”?\\[1.5em]
\item Kiedy destruktor C++ nie pomoże: normalny koniec zakresu czy wymuszone
usunięcie taska bez unwindingu?\\[1.5em]
\end{enumerate}
\section*{Zaliczenie}
\begin{itemize}
\item $\square$ formułuję inwariant „jeden bufor --- jeden właściciel”;
\item $\square$ uzasadniam \texttt{copy = delete} i \texttt{move noexcept};
\item $\square$ układam \texttt{release -> take -> clear source};
\item $\square$ wskazuję A po obu move oraz B jako pierwszy zwolniony adres;
\item $\square$ pokazuję \texttt{allocations=2 frees=2 live=0};
\item $\square$ pokazuję \texttt{after\_scope == initial} i \texttt{pass=1};
\item $\square$ łączę \texttt{delete[] -> operator delete[] -> vPortFree()}.
\end{itemize}
\section*{Granica RAII}
RAII działa wtedy, gdy kończy się czas życia obiektu zgodnie z regułami C++.
Nie obiecuje automatycznego sprzątania po zaniku zasilania, zatrzymaniu systemu
ani arbitralnym \texttt{vTaskDelete()} bez odwinięcia ramek C++. Dlatego późniejszy
wrapper taska nie będzie ukrywał wymuszonego usunięcia w destruktorze.
\vfill
\noindent\textbf{Następna karta K04:} stany schedulera, priorytety, time slicing
i typowane ticki. \texttt{VectorV1} pozostaje małym właścicielem pamięci; nie
staje się jeszcze kontenerem o zmiennym rozmiarze ani allocator-aware.
\end{document}