Kurs om
C++ för minnesbegränsade system

Kurs om hur du implementerar C++ applikationer med egen minneshantering

Denna kurs går på djupet om hur man på olika sätt optimerar och hanterar system som av olika skäl är minnesbegränsade. Det kan vara inbyggda system eller transaktionsintensiva system. Kort sagt, programmering av system där minneshanteringen kräver en alldeles speciell form av omsorg.

Du får lära dig om hur man designar system som inte använder sig av system heapen. Hur man bygger fler-trådade system med effektiv minneshantering. Hur man bygger system av processer med delat minne. Hur man fångar och exekverar vidare efter ett null pekar fel. Hur man kan generera en stack-trace i ett C/C++ program samband med ett kastat undantag. Samt, mycket mer där till.

Välkommen till en spännande och inspirerande fördjupningskurs i Modern C++.

Snabbfakta

Namn
C++ för minnesbegränsade system
Längd
3 dagar
Nivå
Advanced
Målgrupp
Rutinerade C++ programmerare
Förkunskaper
Kunna programmera i C++
Programvara & Verktyg
  • GNU C++ Compiler, version 10 or later
  • JetBrains CLion
  • Ubuntu Linux

Detta får du lära dig på kursen

Här är ett sammandrag i punktform av vad du får lära dig på kursen. Eftersom kursmaterialet är författat på engelska, så återger vi sammandraget också på engelska.

  • Be able to use and implement smart pointers
  • Be able to catch a zero pointing error and continue the execution
  • Be able to generate a stack trace in a C / C ++ program
  • Know how to allocate dynamic memory on the stack
  • Be able to implement own memory allocation based on a block pool
  • Be able to implement programs with memory management outside the heap
  • Be able to use PMR (Polymorphic Memory Resource) which was introduced in C ++ 17
  • Be able to implement multi-threaded systems, where each thread has its own private heap
  • Be able to implement a program that loads a file into memory with mmap
  • Be able to implement systems with shared memory

Kursdatum

Här ser du vilka kursdatum som är tillgängliga. Klicka på en av datumknapparna för att anmäla dig till ett kurstillfälle. På kursen pratar läraren svenska, medan vårt kursmaterial alltid är författat på engelska.

Missa inte vår samfaktureringsrabatt! Är ni fler personer från samma företag/organisation som går på samma kurs, rabatteras tillkommande personer med 25%. Ni anmäler er till kursen en och en, men uppger samma företag, så ordnar vi resten. Samtliga deltagare från samma företag ingår på samma faktura, den första till fullt pris och resterande till rabatterat pris.

Klassrum

Du sitter bekvämt i ett av våra klassrum, vilka finns centralt placerade i Stockholms innerstad.

I priset ingår tryckt kursmaterial (och som PDF), samt kaffe/te med smörgås på förmiddagen och kaffe/te med bulle på eftermiddagen.

Pris: 18 000 kr + moms

Fjärrkurs

Du sitter bekvämt framför datorn och deltar i kursen via internet. Vi använder programvaran Zoom för alla våra fjärrkurser.

I priset ingår kursmaterial som PDF.

Pris: 14 000 kr + moms

Företagsinternt

Om ni är tre eller fler personer från samma företag eller organisation, kan ni beställa en företagsanpassad kurs. Då håller vi kursen på ett datum som passar er. Antingen på plats i era lokaler eller som en fjärrkurs. Vi kan också hålla den muntliga framställningen på engelska.

Klicka på knappen nedan för att be om en offert.

Företagsanpassad Kurs

Kursinnehåll

Eftersom kursmaterialet är författat på engelska, så återger vi innehållet också på engelska.

Brief about Modern C++

Just a quick recap of some topics from newer C++ which are essential for this course.

  • The new initialization syntax
  • Default initialization
  • Initialization trivial/primitive types
  • Initialization of STL containers
  • Type safety
  • Automatic type inference, usage of auto
  • For-each loops
  • Functions with auto return type
  • Multi-assignment, aka structured bindings
  • Lambda expressions
  • Captures by value and by reference
  • Modern type aliases

Understanding RAII

Discussion of the idiom Resource Acquisition Is Initialization and how important it is for memory management.

  • Principles of the idiom
  • Implementation of a trace class
  • Other examples of RAII

Usage of new & delete

Discussion of usage of new & delete, to lay the groundwork for the rest of the course.

  • Allocation of a single object
  • Disposing a single object
  • When the heap become full, how to detect it
  • Registering a heap-full hook
  • Allocation of an object array
  • Disposing an object array
  • The hidden secret of an object array, or how can delete [] ptr invoke the destructor of all objects
  • Placement new operator, the third form of new
  • Usage of the placement new operator
  • Overloading the new & delete operators
  • Non-throwing version of the new operator

Smart Pointers

Understand how to implement smart pointers and what is supported in Modern C++.

  • What is a smart pointer, anyway
  • A simple (incomplete) implementation
  • To delete or not to delete; that’s the question
  • Different pointer semantics
  • Single reference semantics using MonoPtr
  • Copy semantics using ClonePtr
  • Reference count semantics using MultiPtr
  • Complete implementation of MonoPtr
  • Implementation of a factory function for MonoPtr
  • Smart pointer support in C++11
  • Using std::unique_ptr
  • Using std::shared_ptr
  • Using a custom deleter
  • Using a free deleter for malloc allocated memory blocks
  • Understanding usage of std::weak_ptr

Address Space Organization

Understand where various data blocks get allocated in *NIX like systems.

  • Virtual memory pages
  • Page table
  • Address translation
  • Page fault
  • Compiled program code: the text area
  • Global static data: the data and bss areas
  • Dynamic data: the system heap
  • Local data: the call-stack
  • Organization of a single stack-frame
  • How a function call is performed on assembly level
  • Memory mapped data segments
  • C++ name mangling
  • Demangling linker names
  • Useful system commands such as size, nm, objdump, ldd, lsof
  • Useful online resources, such as cppinsights.io and compiler explorer
  • Classification of embedded systems

Understanding Alignment

Brief discussion of byte alignment and its support in C++.

  • What is alignment
  • The problem with unaligned memory access
  • What is a cache line and how that is related to alignment
  • Cache levels: L1, L2 and L3
  • Understanding array allocation and alignment
  • How the order of members within a struct (or class) affects padding and hence the struct size
  • Explicit alignment
  • Alignment of heap allocated objects

Understanding the System Heap

Discussion of the system heap.

  • Allocation functions in std libc
  • Allocation functions in *NIX OS, usage of brk() and sbrk()
  • How the malloc and free usually are implemented
  • Dealing with heap overflow
  • Allocation of very large memory blocks
  • Understanding usage of MMAP_THRESHOLD and how to configure using mallopt
  • What is a memory leak
  • Working with valgrind on the command-line and within JetBrains CLion

Usage of Exceptions

Deep-dive discussion of exceptions in C++.

  • Simple usage of exceptions
  • Catching different types
  • Marking a function as not throwing
  • Automatic destruction of local objects
  • Tracing exception throw
  • Usage of std exceptions, why you should always do that
  • How to reveal the type of unknown exception
  • Exceptions in terms of setjmp() and longjmp()
  • Throwing an exception from a signal handler
  • How to recover from a null pointer crash (SIGSEGV) and carry on executing
  • Usage of an alternate signal stack
  • Dealing with system errors in *NIX OS
  • Brief discussion of how exceptions really is implemented in modern systems
  • How to disable exception is you must to

Understanding RTTI

Deep-dive into RTTI, demangling and how to generate stack-traces in C++.

  • RTTI - Run-Time Type Identification
  • Usage of the typeid operator
  • How to disable RTTI, if you need to
  • Understanding C++ (link) name mangling
  • How to suppress name mangling
  • Programmatic de-mangling, for the Itanium ABI
  • Implementation of C++ demangle function
  • Principles of generating a stack-trace in C/C++ program
  • Complete implementation of stack-traces in C++ with unit tests in Catch2
  • Dealing with application exceptions
  • Dealing with system error exceptions
  • Dealing with crash exceptions and generate a stack-trace for a null pointer bug

Dealing with Text Strings

Short recap of the string support in Modern C++.

  • Understanding std::basic_string
  • String literals
  • Raw strings
  • What is SSO (Short String Optimization)
  • Usage of string_view and why it’s a really nice addition to the library

Memory Behaviour of STL Containers

Understanding of happens behind the curtains of STL containers.

  • The STL architecture
  • Overview of algorithms
  • What is a STL interval
  • Sequence containers
  • Associative containers
  • Binary tree vs. hash table implementations
  • Tracing new & delete usage
  • Understanding the allocation behaviour of std::vector
  • Why invoking reserve() is an excellent idea
  • Understanding the key difference between copy insertion and in-place insertions
  • Investigating the memory usage of emplace_back()

Stack Allocated Memory

The heap is not the only place to allocate data, here we discuss how to use the stack dynamic memory allocation.

  • Memory allocation outside the system heap
  • Using a stack-allocated buffer
  • Understanding RVO (Return Value Optimization)
  • Understanding move semantics
  • Allocation of memory blocks on top of the call-stack using alloca()

User-Defined Memory Allocation

The moment you start to use areas outside the system heap, you need to implement your allocation and de-allocation library, in this chapter we show you how.

  • Primary allocation strategies
  • Monotonic allocation
  • Circular allocation
  • Block-pool storage
  • Implementation of a static block pool
  • Limitations of the template system and how that might affect the usage of static pools
  • Implementation of a dynamic block pool
  • Using smart pointers with a block pool
  • Implementation of a custom deleter for a block pool
  • Implementation of factory function for a block pool with a deleter

Polymorphic Memory Resources

In this chapter we discuss the allocation support added in C++17 and available since GCC version 9.

  • What is PMR (Polymorphic Memory Resources)
  • Class & function overview
  • Provided memory resource types and allocators
  • Typical usage of a std::pmr::monotonic_buffer_resource with an upstream resource
  • STL container support for PMR
  • Usage of a std::vector using a std::pmr::monotonic_buffer_resource
  • Understanding SSO with PMR
  • How to implement your own memory resource
  • Implementation of a cyclic_buffer_resource
  • Understanding how to use std::pmr::unsynchronized_pool_resource
  • Implementation of spy tracing PMR classes
  • Using PMR in you own user-defined types

Multi-Threaded Applications

Brief discussion of the threading support in Modern C++, to serve as the basis for next chapter.

  • Threading API in C++11
  • Life-cycle methods
  • Several ways of defining the body of a thread
  • Mutex locks
  • Lock guards
  • Deadlock avoiding multi-locks
  • Read-write locks
  • Condition variables
  • Promise and future

Memory Allocation in Multi-Threaded Systems

Discussion of how to in practice implement thread private heaps using PMR.

  • Heap implementation in glibc
  • How to support private heaps
  • The minimum amount of code to send some data from one thread to another thread
  • Heap storage for messages
  • Implementation of a (blocking) message queue
  • A simple generic message type
  • Implementation a producer-consumer threaded system, using a private PMR based heap for incoming messages
  • Implementation of a more complex message sending pipeline of threads, all using PMR based private heaps
  • How to configure the stack-size in POSIX Threads

Shared Memory

Discussion of memory-mapped file and how to create a shared memory segment.

  • How is memory-mapped I/O working
  • Understanding mmap(), munmap() and msync()
  • Implementation of class MemoryMappedFile
  • What is shared memory and how is it working
  • How to create a SHM file and map it into the virtual address space
  • Usage of shm_open() and shm_unlink()
  • Understanding fork() and wait()
  • Implementation of class SharedMemory

Memory Allocation in Multi-Process Systems

Discussion of memory allocation and synchronization in shared memory between two processes.

  • Why C++11 threading types cannot be used in shared memory
  • Implementation of simple C++ wrappers around the POSIX Thread C API
  • Configuration of mutex and condition variables for usage in shared memory
  • A simple demo of a producer-consumer via shared memory using a static message queue
  • Demo of a client server system using dynamic memory allocation of messages in shared memory