How do reference-counting smart pointer's avoid or handle reference-counter overflows?

635 Views Asked by At

In a naive reference-counting smart pointer implementation, the reference-counter could overflow. How is this overflow avoided or handled in C++ standard library implementations?

1

There are 1 best solutions below

4
On BEST ANSWER

Snippets from stdlibc++ headers:

typedef int _Atomic_word;

class _Sp_counted_base
    /*snip*/
    _Atomic_word  _M_use_count;
    /*snip*/
    _M_weak_add_ref()
    { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }

/*snip*/
__atomic_add_dispatch(/*snip*/)
{
    /*snip*/
    __atomic_add_single(/*snip*/);
    /*snip*/
}

__atomic_add_single(/*snip*/)
{ *__mem += __val; }

Conclusion: This particular implementation "handles" reference-counter overflow by ignoring the possibility.