Re: Lock-free reference counting
- From: Mark Wooding <mdw@xxxxxxxxxxxxxxxx>
- Date: Sun, 23 Nov 2008 14:50:54 +0000 (UTC)
Juha Nieminen <nospam@xxxxxxxxxxxxxx> wrote:
The only way 'a' would not have a pointer to the allocated object is
if f() nulled (or whatever) that pointer.
Huh? We're discussing code of the form
ptr<foo> a = ...;
f(a);
g();
However, if it does that, then the reference-counting smart pointer
(which 'a' is) would immediately delete the object in question when
f() does that.
You've gotten yourself in a muddle. We're comparing C++
reference-counting behaviour (which you describe accurately) with a
tracing garbage collector. The tracing GC will examine a root set
(registers, stack frames, and global (and thread-local) variables to
find out which objects are still live and which aren't. Note carefully
that a GC of this form operates on machine-level concepts (like
registers, stacks and so on), not language-level concepts (like scopes
and variable lifetimes).
Imagine an architecture in which functions are called by pushing
arguments on a stack, and the caller is responsible for both fixing up
the stack afterwards and saving any important values in registers.
(This shouldn't be difficult: i386 usually works like this.)
Therefore, when we write a call f(a), the compiler emits code to push a
onto the stack, and call f. It then fixes up the stack pointer (a
simple add). Any copy of a in registers was gone by the time f returns
(caller saves, remember?) and the stack pointer has now been nudged past
the copy of a on the stack. The compiler could have pushed a copy of a,
but it would be a waste, since it's not referred to again. Assuming
that a was the only reference to whatever object it referred to, there
just aren't any at all any more. The GC can therefore detect this and
reclaim the object at any time after f returns and the stack fixup is
done.
A GC engine has absolutely no way of knowing that "a is not used
anymore" if 'a' still has the pointer to the object inside it.
You're thinking at the wrong level of abstraction.
-- [mdw]
.
- Follow-Ups:
- Re: Lock-free reference counting
- From: Juha Nieminen
- Re: Lock-free reference counting
- References:
- Lock-free reference counting
- From: Juha Nieminen
- Re: Lock-free reference counting
- From: Jon Harrop
- Re: Lock-free reference counting
- From: Juha Nieminen
- Re: Lock-free reference counting
- From: Jon Harrop
- Re: Lock-free reference counting
- From: Juha Nieminen
- Re: Lock-free reference counting
- From: Mark Wooding
- Re: Lock-free reference counting
- From: Juha Nieminen
- Lock-free reference counting
- Prev by Date: Re: find polygon
- Next by Date: Re: find polygon
- Previous by thread: Re: Lock-free reference counting
- Next by thread: Re: Lock-free reference counting
- Index(es):
Relevant Pages
|