5. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Memory addressing - real mode
All real mode memory addresses consist of a segment address
plus an offset address:
โข the segment address (in one of the segment registers) defines
the beginning address of any 64Kb memory segment
โข the offset address selects a location within the 64K byte
memory segment
ฯ = ฮฑ โ 16 + ฮฒ where ฮฑฯต{๐๐ , ๐ ๐ , ๐๐ , ๐๐ , ๐๐ , ๐๐ }
5
6. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Memory addressing โ protected mode
โข From logic address ๏ Linear address (32 bit)
โข If paging is disabled ๏ Linear address ๏ง๏จ Physical address
Assembly of segment register in protected mode:
6
15 3 2 1 0
PRLTIIndex
Selector
TI โ table index, if TI==0 ๏ GDT, if TI==1 ๏ LDT
PRL โ request priority level (in case of CS CPL)
8. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Memory addressing
โข Protected mode
The protected mode processors use a look up table to compute
the physical address. Memory addressing is based on โSegment
Descriptorsโ that define a memory-region and assign its
properties.
8
Type and
access-rights
Segment-
Base[23..16]
Word 3 Word 2
Segment-Base[15..0] Segment-Limit[15..0]
Word 1 Word 0
Segment-base
[31โฆ24]
FLAGS(G, DB, โฆ)
9. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Memory addressing - Protected mode
โข Type and access-rights
9
P DPL S X C/D R/W A
P = Present (1=yes, 0=no)
DPL = Descriptor Privilege Level (00=supervisor, 11=user)
S = System-segment (0=yes, 1=no)
X = executable: 1=yes (i.e., code-segment), 0=no (i.e., data-segment)
C/D = Conforming code (1=yes, 0=no) when X-bit equals 1
expands-Down (1=yes, 0=no) when X-bit equals 0
R/W = Readable (1=yes, 0=no) when X-bit equals 1
Writable (1=yes, 0=no) when X-bit equals 0
A = segment has been Accessed by the CPU (1=yes, 0=no)
10. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Memory addressing - Protected mode
โข Type and access-rights
10
S =1
S=0
I=0 E W
I=1 C R
TYPE(gates)
A
P โ is the segment in memory
DPL โ description privilege level
S โ system descriptor(=1) or no(=0)
I โ data segment(=0), code segment(=1)
E โ common data segment(0)
W โ is writable (if (E) assert(W==1);)
A โ Access flag
C โ conform(=1), non-*(=0) segment
R โ Code segment can be read
DPLP
14. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ static memory storage
Possible problems of static objects initialization:
โ static initialization order โfiascoโ problem โ here we have a 50%-50%
chance of corrupting the program(if y would be constructed before x).
14
// f1.cpp
#include โT1.hโ
T1 obj1;
// f2.cpp
#include โT2.hโ
T2 obj2;
// T2.cpp
#include โT2.hโ
T2::T2{
x.do_dmth();
}
15. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ static memory storage
Solution for the problem - Construct On First Use Idiom - i.e. replace
the namespace-scope/global T1 object with a namespace-
scope/global function f_obj() that returns the Fred object by
reference:
15
// f1.cpp
T1& get()
{
static T1* ptr = new T1();
return *ptr;
}
// f1.cpp
T1& get()
{
static T1 ref;
return ref;
}
?
need to be 100% sure our static object:
(a) gets constructed prior to its first use
(b) doesnโt get destructed until after its last use.
16. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ heap memory storage
โ new operator to allocate an object in memory
โ delete operator to deallocate an allocated object
Try to avoid heap memory fragmentations as much as possible.
Use placement new if you have enough knowledge about it:
16
void fn()
{
char memory[sizeof(T)];
void* p = memory;
T* f = new(p) T();
// do smth.
}
17. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ heap memory storage / placement new
17
void fn()
{
char memory[sizeof(T)];
void* p = memory;
T* f = new(p) T();
// do smth.
}
void fn()
{
char memory[sizeof(T)];
void* p = memory;
T* f = new(p) T();
// do smth.
f->~T();
}
18. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ โstackโ memory storage
โ Stack variables are local in nature.
โ There is a limit (varies with OS) on the size of variables that can be stored
on the stack.
โ The stack grows and shrinks as functions push and pop local variables
โ There is no need to manage the memory yourself, variables are allocated
and freed automatically
โ Stack variables only exist while the function that created them, is running
18
19. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ C++ memory storages โ stack vs heap
19
Stack Heap
very fast access (relatively) slower access
don't have to explicitly de-allocate variables no guaranteed efficient use of space
limit on stack size (OS-dependent) no limit on memory size
local variables only variables can be accessed globally
space is managed efficiently by CPU,
memory will not become fragmented
allocated memory must be managed
variables cannot be resized variables can be resized
using realloc()
20. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Structure Padding
โข Architecture of a computer processor is such a way that it can read 1 word
(4 byte in 32 bit processor) from memory at a time.
โข To make use of this advantage of processor, data are always aligned as 4
bytes package which leads to insert empty addresses between other
memberโs address.
โข Because of this size of the structure is always not same as what we think.
โข In order to align the data in memory, one or more empty bytes are
inserted between memory addresses which are allocated for other
structure members while memory allocation.
20
21. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Structure Padding
21
struct A {};
struct B
{bool b};
struct C
{bool b; int x;};
struct C
{bool b1, b2; int x;};
struct C
{bool b1; int x; bool b2};
[](){};
[x=true, y=2, z= true](){};
[x=true, y=2, z= true](){};
std::tuple<bool, int>
std::tuple<bool, bool,
int>
std::tuple<bool,int,
bool>
std::function<void()>
22. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ False sharing
22
class T
{ int x1, x2;};
static T obj;
int f()
{
int sum=0;
for (i=0;i<131313; ++i) sum +=obj.x1;
return sum;
}
void g()
{
for (i=0;i<131313; ++i) ++obj.x2;
}
When f and g are running concurrently
sum may need re-read x1 from main
memory event though concurrent
modification of x2 does not affect on
x1.
24. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Value categories in C++
Each C++ expression is characterized by two independent properties: a type
and a value. Each expression has some non-reference type and each
expression belongs to a value category.
โข C++98
An expression is either an:
โข lvalue โ occupies some identifiable location in memory
โข rvalue โ !lvalue
โข C++11
โข lvalue
โข rvalue
โข xvalue
โข prvalue
โข glvalue
24
25. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Value categories in C++11
Two properties of expressions
โข Can be moved from
Move constructor, move โ=โoperator, or another function overload that implements move
semantics can bind to the expression.
โข Has identity
Itโs possible to determine whether the expression refers to the same entity as another
expression, such as by comparing addresses of the objects or the functions they
identify(obtained directly or indirectly).
25
26. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Value categories in C++11
โข glvalue - has identity. Is either lvalue or xvalue.
โข rvalue โ can be moved from. Is either prvalue or xvalue.
โข lvalue โ has identity and canโt be moved from. { the name of a variable, a
function, function call whose return type is lvalue(++i), assignment and
compound assignment, built-in comma expression where the second
operand is lvalue, a ? b : c(lvalues), string literal, static_cast<T&> }
โข xvalue โ has identity and can be moved from. { std::move,
static_cast<T&&>, the member of object like a.m(where a is an rvalue and
m is non-static data member of non-reference type) }
โข prvalue- does not identity and canโt be moved from. {literal, i++, expr.
Whose return type non-ref, built-in address-of expression, static_cast<int>
}
26
29. Restricted ยฉ 2017 Mentor Graphics Corporation
Memory management
๏ฎ Things need to attention
โ const_cast
โ (a+b)-c != a+(b-c)
โ stack and realloc
โ using compile-time unknown expression to define size of stakโs objects
โ taking address of a bitfield
โ size of an array like arr[N] known at compile time! (T(&)[N])
29
32. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ Type deduction for templates
template <class T>
void f(const T& arg)
{ T par = arg; par.non_const_fn(); }
template <class T>
void f(T& arg)
{ T par = arg; par.non_const_fn(); }
template <class T>
void f(T arg)
{ T par = arg; par.non_const_fn(); }
32
33. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto
33
struct S
{
S(int x) :m(x){}
int m;
};
S operatorโโ_ma13
(unsigned long long x)
{
return S(static_cast<int>(x));
}
auto x1 = 13_ma13;
auto x2(13_ma13);
auto x3 = {13_ma13};
auto x4 {13_ma13};
auto x5 = {13_ma13, 13_ma13};
auto x6 {13_ma13, 13_ma13};
auto x7 ={13_ma13, 13};
template <class T>
void f(T);
f({13_ma13});
f({13_ma13, 13_ma13});
?
?
?
?
?
?
?
?
?
34. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto
34
auto x1 = 13_ma13;
auto x2(13_ma13);
auto x3 = {13_ma13};
auto x4 {13_ma13};
auto x5 = {13_ma13, 13_ma13};
auto x6 {13_ma13, 13_ma13};
auto x7 ={13_ma13, 13};
template <class T>
void f(T);
f(13_ma13);
f({13_ma13});
f({13_ma13, 13_ma13});
S
S
std::initializer_list<S>
S
std::initializer_list<S>
error โฆ. (add โ=โ before the {)
std::initializer_list<S>
S
error โ cannot deduce T
error โ cannot deduce T
35. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto
35
auto f()
{ return 13_ma13;}
auto fn1 = [] (auto s) {โฆ}
auto fn2 = [] (const auto& s)
{โฆ}
f1(13_ma13);
f1({13_ma13});
f2({13_ma13});
?
cannot
?
36. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto
36
auto f()
{ return 13_ma13;}
auto fn1 = [] (auto s) {โฆ}
auto fn2 = [] (const auto& s)
{โฆ}
f1(13_ma13);
f1({13_ma13});
f2({13_ma13});
S
a braced-enclosed list does not provide return value
a braced-enclosed list does not provide return value
37. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto
โข In general itโs the same as template type deduction. The main
difference is that auto assumes that a braced initializers
represents a std::initializer_list, but TTD-doesnโt.
โข Auto in function return type or a lambda parameter implies TTD,
not auto type deduction.
37
38. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ auto and decltype
38
int x[10];
auto f1(int i)
{ return x[i];}
decltype(auto) f2(int i)
{ return x[i];}
f1(0) = 4;
f2(0) = 4
39. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ decltype
โข Decltype almost always yields the type of expression without modification.
โข For lvaluesโ of type T decltype always reports a T&
39
40. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ decltype
40
decltype(auto) f()
{
bool b;
โฆ
return (b);
}
UNDEFINED BEHAVIOR!!!!!
41. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ partial specialization
๏ฎ static_assert
41
template <class T>
void g(T) {cout<<โAโ;}
template <>
void g<int>(int){cout<<โBโ;}
template <class T>
void f(T) { static_assert(std::is_same_v<T, int>); }
g(1); // B
g(1.); // A
f(1); // fine
f(1.); //compile error
42. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
๏ฎ std::enable_if
42
template<class T, typename = void>
class A{};
template<class T>
class A<T, typename =
std::enable_if_t<std::is_fundamental_v<T>>
{};
template<class T, typename = void>
class A{};
template<class T>
class A<T, typename =
std::enable_if_t<std::is_fundamental_v<T>>
{};
template<class T>
class A<T, typename =
std::enable_if_t<std::is_integral_v<T>>
{};
43. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
class D
{virtual void f(){}};
template<class T>
class D
{
virtual void f(){};
};
template<class T>
class D
{
template <class T2=T>
virtual void f(){};
};
43
?
?
?
44. Restricted ยฉ 2017 Mentor Graphics Corporation
Templates / Type deduction โ auto and decltype
class D
{virtual void f(){}};
template<class T>
class D
{
virtual void f(){};
};
template<class T>
class D
{
template <class T2=T>
virtual void f(){};
};
44
OK
OK
Error โ virtual is not allowed
in a function template
declaration
46. Restricted ยฉ 2017 Mentor Graphics Corporation
R-value references, Universal references, Move
semantics, Perfect Forwarding
๏ฎ R-value references
46
void f(S&& obj)
{/โฆ/}
f(13_ma13);
S x = 13_ma13;
f(x);
fine
an rvalue ref. cant be bound to an lvalue
47. Restricted ยฉ 2017 Mentor Graphics Corporation
R-value references, Universal references, Move
semantics, Perfect Forwarding
๏ฎ R-value references
47
struct S
{
void f1() & {}
void f2() && {}
};
auto x = 13_ma13;
x.f1();
x.f2();
fine
error- function canโt be called on lvalue
48. Restricted ยฉ 2017 Mentor Graphics Corporation
R-value references, Universal references, Move
semantics, Perfect Forwarding
๏ฎ Universal referances
48
template <class T>
void f(T&&)
{โฆ}
auto&& s = 13_ma13;
If a function template parameter has type T&& for a deduced
type, or an object is declared using auto&&, the paramet or
object is a UR.
UR correspond to rvalue if they are initialized with rvalues. The
same for lvalue.
52. Restricted ยฉ 2017 Mentor Graphics Corporation
R-value references, Universal references, Move
semantics, Perfect Forwarding
๏ฎ Use std::move on rvalue referances, std::forward on universal
referances.
๏ฎ Never use std::move or std::forward to local objects it they would
otherwise be eligible for the RVO.
52