1. Home
  2. Courses
  3. Cxx
  4. Cxx-memory-constrained-systems

Online Kurs omC++ för minnesbegränsade system

Avancerad kurs i C++ om minnesbegränsade system, såsom inbyggda system och transaktions-intensiva system

Fakta om kursen

Kursens namn
C++ för minnesbegränsade system
Längd
4 dagar
Nivå
Advanced
Målgrupp
C++ programmerare
Förkunskaper
Rutinerad i C++
Verktyg
  • Ubuntu Linux (@ WSL or native)
  • JetBrains CLion
  • GCC/G++ version 9+

Online

Du sitter bekvämt hemma och deltar i kursen över internet. Läraren undervisar på svenska. Om du önskar du kursen på engelska, kontakta oss så skapar vi ett kursdatum för dig där kursen ges på engelska via Zoom.
Pris: 16,000 kr (€1,519)

Klassrum

Våra klassrum finns centralt i Stockholm. Läraren undervisar på svenska.
Pris: 24,000 kr

Vi ger 20% rabatt för tillkommande deltagare från samma företag vid samma kurs-tillfälle.

Företagsinternt

Du kan också beställa kursen som företagsintern. Då kommer vi till er och håller kursen i era lokaler. Alternativt så håller vi kursen online om ni så önskar. Ni väljer om ni vill ha kursen på svenska eller engelska. Skicka en kurs-förfrågan via knappen nedan.

Företagsintern Kurs

Den kursen 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ännanden och inspirerande fördjupningskurs i Modern C++.

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

  • Kunna använda och implementera smarta pekare
  • Kunna fånga ett null pekar fel och fortsätta exkveringen
  • Kunna generera en stack-trace i ett C/C++ program
  • Veta hur man allokerar dynamiskt minne på stacken
  • Kunna implementera egen minnes allokering baserat på en block pool
  • Kunna implementera program med minneshantering utanför heapen
  • Kunna använda PMR (Polymorphic Memory Resource)som infördes i C++17
  • Kunna implementera multi-trådade system, där varje tråd/thread har sin egen privata heap
  • Kunna implementera ett program som laddar in en fil till minnet med mmap
  • Kunna implementera system med delat minne (shared memory)

Kursinnehåll

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 ground work 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 obhect 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<Y>
  • Copy semantics using ClonePtr<Y>
  • Reference count semantics using MultiPtr<T>
  • Complete implementation of MonoPtr<Y>
  • Implementation of a factory function for MonoPtr<Y>
  • Smart pointer support in C++11
  • Using std::unique_ptr<T>
  • Using std::shared_ptr<T>
  • Using a custom deleter
  • Using a free deleter for malloc allocated memory blocks
  • Understanding usage of std::weak_ptr<T>

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 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 unalignment 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
  • Algnment 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 an 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 supress 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<T>
  • 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<T>
  • Why invoking reserve() is a really good 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<T> 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 different 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
  • 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
  • Implemenation 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()
  • Implementaion 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
  • 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