xref: /plan9-contrib/sys/man/9/ref (revision 2747dda25ec9e46ad7aa8de7850394ed7e10af69)
REF 9
NAME
Ref, incref, decref - reference counts
SYNOPSIS

int incref(Ref *r)

int decref(Ref *r)

DESCRIPTION
A Ref structure holds a reference count for a data structure:
.EX typedef struct struct Ref { Lock; long ref; } Ref;

The reference count proper is found in ref ; the Lock prevents concurrent updates (see lock (9)).

Incref atomically increments the reference count r , and returns the new count.

Decref atomically decrements the reference count r , and returns the new count.

EXAMPLES
Release a structure containing a Ref on last use.
.EX if(decref(s) == 0) free(s);
SOURCE
/sys/src/9/port/chan.c
DIAGNOSTICS
Decref will panic (9) if the count goes negative, revealing a reference counting bug.