Re: [Fwd: Refcounted allocator]

From: Robert Collins <robertc@dont-contact.us>
Date: 08 Nov 2002 18:41:13 +1100

On Fri, 2002-11-08 at 01:38, Henrik Nordstrom wrote:
> Can you expand a little on this please?

Sure.

> Note: Some recent experiences with cbdata indicates these could do good
> if converted to be based on refcounted pointers, with the addition of a
> invalidation method. And yes, it is entirely possible cbdata and rcdata
> can be merged into one single class in C++.

Well, the semantics are different. I'm not sure we *want* to merge them,
I was noting that we *could* during a transition.
The difference is that rcdata frees when the last references goes away,
but cbdata frees after an explicit free, and the last reference.

example of differing behaviour:

in cbdata:

if (cbdataReferenceValid(foo)){
   foo->something();
}

void
Foo::something(){
  /* We are still active, free is not desired */
  checkOtherReasonsToAbort();
}

in rcdata:

  foo->something();

void
Foo::something(){
    haveWeAborted();
    checkOtherReasonsToAbort();
}

in other words, in rc data, if the object decides to abort, it needs to
remember that fact, because all the pending callbacks may still occur.
in cb data, the object calls cbdatafree on itself, and then those
callbacks must not occur.

rcdata is better in most cases IMO, even though there is a little more
programmer work, because it applies to all objects, not just those
queued for callbacks.

> tor 2002-11-07 klockan 09.53 skrev Robert Collins:
>
> > > Finally, we can only use this in classes that call the destructor at
> > > some point, to be sure that SomeClassPointer objects go out of scope.

Consider this:

class OneObjectType{
  SomeClassPointer aPointer;
};

aPointer is a refcounting smart pointer. It will automatically decrement
the refcount variable when:
* it is set to a new value.
aPointer = NULL; or aPointer = someOtherObjectPointer;
* it goes out of scope. This occurs for class members during the object
destructor.

> > > It's trival to make a cbdata class call the destructor, and I'll
> > > document that in an email to this list before commiting the refcount
> > > code to HEAD..

the destructor can be called via placement delete with a cbdata free
callback. Alternatively you can override new and delete for the class
(new calls cbdataAlloc, delete calls cbdataFree).

> > > We can also convert cbdata classes to refcounted where
> > > appropriate (which may be everywhere, but that is a different
> > > discussion). It's also possible (as a transition strategy) to have
> > > CBDATA's SomeClassPointers, but that may not be needed so I won't code
> > > it up until it is :}).

For this I'd rather wait until we have a specific example to discuss.
It's really just a careful application of the above overriden delete.

Rob

Received on Fri Nov 08 2002 - 00:41:19 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:18:38 MST