Reference counting types
GLib uses reference counting semantics in basically all of its container data types, but every implementation has its own bespoke solution. Additionally, reference counting is pretty much the common memory management and garbage collection policy for the whole of the G* platform when it comes to complex data types.
GLib should offer reference counting types and functions to ensure that all implementations conform to similar semantics, especially with regards to saturation and potential integer overflow and wrapping.
In order to cover all possible bases, we have two distinct reference counting types and API:
grefcount, which provides a simple reference counter
gatomicrefcount, which provides a thread-safe reference counter with atomic semantics
Both reference counters share the C
int space, with
using the negative range, and
gatomicrefcount using the positive
range; this allows us to verify that we're not going to mix the two,
and that we can use the atomic integer operations.
Both reference counters check for saturation – i.e. when the counter reaches the maximum allowed value.