[Gc] Determining if pointer GC allocated

Hans Aberg haberg-1 at telia.com
Thu Dec 22 01:33:08 PST 2011

On 22 Dec 2011, at 08:47, Ivan Maidanski wrote:

> Of course, not a problem to add it to public API (for gc7.3) in case it would be really useful.

It is hard to tell if it is useful enough, but the feature appears if one wants to implement dynamic objects. Example:

Suppose one has a polymorphic base class A (with virtual functions), and an object class O that boxes objects in the hierarchy at need,
  class O { A* data_; public: O(const &A); ... };

If a class B is derived from A, when used for automatic objects as in
  O f() { B b; return b; }
  O x = f();  // b expires here.

So if one wants to permit this syntax, one might let O box a clone of B. This is not needed if b is allocated using 'new'. But it is not possible to tell the difference if f() is a method in the class B returning *this or 'this': it depends on how the object was allocated. C++ does not provide that information, nor can one let 'new' tell the object, as the latter does not exist when 'new' is called.

One way to implement it, is to let the object check the pointer with a function like GC_base(), and further cache the result, so one does not need to look up more than once.

A more advanced variation would be to use the reference of the automatic object above (that is, not clone it), and move the object when it expires if it is referenced by an O object. It think it might be done using by a handle, which the destructor of B manipulates.

This might be GC related, too, because in order to make fast handles, one may use special allocation.

When implementing dynamic languages, one is using a mixture of unboxed, boxed and and handles, but it is hard to do that using C++ - basically one ends up with using a reference count.


More information about the Gc mailing list