next up previous contents
Next: 2.3.3 Synchronizing system resources Up: 2.3 Resource building blocks Previous: 2.3.1 Protection

2.3.2 Reference counting

Resources exported by the kernel will also be used by other kernel resources (e.g. page frames are used by address translations). To avoid resource deletion while its being used and to provide basic support for garbage collection we employ reference counters (RefCounter objects). When a RefCcounter comes down to zero an UNUSED exception may be raised to notify users of unreferenced (maybe unused) resources.

This feature is actually included as a builtin service in every Resource, so that we could save some typing and execution time using foo->reference() instead of foo->reference_counter->reference()..

The implementation is so simple that we have folded and inlined it.

<Off public methods for reference counting objects. >= (U-> U->)
// References  (unreferences) this object.
void reference( void )   { _rc++; };
void unreference( void ) { if (!--_rc) unused(); };
// Returns the number of references. 
natural_t get_num_refs(void) const {  return _rc; };

The only subtle point is that we will use the virtual method unused to raise any exception (and perhaps destroy the object).

<Off protected methods for reference counting objects. >= (U->)
// Raises UNUSED exceptions. 
virtual void unused(void)  {;}

And of course\ldots

<Off private members for reference counting objects. >= (U-> U->)
natural_t _rc;                 // # of references

The reference counter must be initialized to 0 on resource allocation.

<Off initialization for reference counting objects. >= (U-> U->)
_rc = 0;



Francisco J. Ballesteros
1998-05-25