122
This means that a copy or move constructor is trivial if it is not user provided.
The class has nothing virtual in it, and this property holds recursively for all the members
of the class and for the base class. As you can see, the C++ standard and libpmemobj
transaction implementation limit the possible objects type to store on persistent
memory to satisfy requirements of trivial types, but the layout of our objects must be
taken into account.
Object Layout
Object representation, also referred to as the layout, might differ between compilers,
compiler flags, and application binary interface (ABI). The compiler may do some
layout-related optimizations and is free to shuffle order of members with same specifier
type – for example, public then protected, then public again. Another problem related
to unknown object layout is connected to polymorphic types. Currently there is no
reliable and portable way to implement vtable rebuilding after reopening the memory
pool, so polymorphic objects cannot be supported with persistent memory.
If we want to store objects on persistent memory using memory-mapped files and
to follow the SNIA NVM programming model, we must ensure that the following casting
will be always valid:
someType A = *reinterpret_cast(mmap(...));
The bit representation of a stored object type must be always the same, and our
application should be able to retrieve the stored object from the memory-mapped file
without serialization.
It is possible to ensure that specific types satisfy the aforementioned requirements.
C++11 provides another type trait called std::is_standard_layout. The standard
mentions that it is useful for communicating with other languages, such as for creating
language bindings to native C++ libraries as an example, and that's why a standardlayout class has the same memory layout of the equivalent C struct or union. A general
rule is that standard-layout classes must have all non-static data members with the same
access control. We mentioned this at the beginning of this section – that a C++ compliant
compiler is free to shuffle access ranges of the same class definition.
When using inheritance, only one class in the whole inheritance tree can have nonstatic data members, and the first non-static data member cannot be of a base class type
because this could break aliasing rules. Otherwise, it is not a standard-layout class.
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Précédent

- 148/457

Suivant