Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++11: what is its gc interface, and how to implement?

I was watching Bjarne Stroustrup's talk "The Essence of C++".

In 44:26 he mentioned "C++11 specifies a GC Interface".

May I ask what is the interface, and how to implement it? Any more detailed good introduction online, or some sample codes to demonstrate it pls?

like image 202
athos Avatar asked Jan 01 '15 03:01

athos


2 Answers

In addition to the good answer by quantdev, which I've upvoted, I wanted to provide a little more information here (which would not fit in a comment).

Here is a C++11 conforming program which demonstrates whether or not an implementation supports the GC interface:

#include <iostream>
#include <memory>

int
main()
{
#ifdef __STDCPP_STRICT_POINTER_SAFETY__
    std::cout << __STDCPP_STRICT_POINTER_SAFETY__ << '\n';
#endif
    switch (std::get_pointer_safety())
    {
    case std::pointer_safety::relaxed:
        std::cout << "relaxed\n";
        break;
    case std::pointer_safety::preferred:
        std::cout << "preferred\n";
        break;
    case std::pointer_safety::strict:
        std::cout << "strict\n";
        break;
    }
}

An output of:

relaxed

means that the implementation has a trivial implementation which does nothing at all.

libc++ outputs:

relaxed

VS-2015 outputs:

relaxed

gcc 5.0 outputs:

prog.cc: In function 'int main()':
prog.cc:10:13: error: 'get_pointer_safety' is not a member of 'std'
    switch (std::get_pointer_safety())
            ^
like image 83
Howard Hinnant Avatar answered Oct 27 '22 00:10

Howard Hinnant


Stroustrup extends this discussion in his C++ FAQ, the thing is that GC usage is optional, library vendors are free to implement one or not :

Garbage collection (automatic recycling of unreferenced regions of memory) is optional in C++; that is, a garbage collector is not a compulsory part of an implementation. However, C++11 provides a definition of what a GC can do if one is used and an ABI (Application Binary Interface) to help control its actions.

The rules for pointers and lifetimes are expressed in terms of "safely derived pointer" (3.7.4.3); roughly: "pointer to something allocated by new or to a sub-object thereof." to ordinary mortals: [...]

The functions in the C++ standard supporting this (the "interface" to which Stroustrup is referring to) are :

  • std::declare_reachable
  • std::undeclare_reachable
  • std::declare_no_pointers
  • std::undeclare_no_pointers

These functions are presented in the N2670 proposal :

Its purpose is to support both garbage collected implementations and reachability-based leak detectors. This is done by giving undefined behavior to programs that "hide a pointer" by, for example, xor-ing it with another value, and then later turn it back into an ordinary pointer and dereference it. Such programs may currently produce incorrect results with conservative garbage collectors, since an object referenced only by such a "hidden pointer" may be prematurely collected. For the same reason, reachability-based leak detectors may erroneously report that such programs leak memory.

Either your implementation supports "strict pointer safety" in which case implementing a GC is possible, or it has a "relaxed pointer safety" (by default), in which case it is not. You can determine that by looking at the result of std::get_pointer_safety(), if available.

I don't know of any actual standard C++ GC implementation, but at least the standard is preparing the ground for it to happen.

like image 30
quantdev Avatar answered Oct 26 '22 23:10

quantdev