Check out the new USENIX Web site. next up previous
Next: Fine-grained Address Translation Up: Mixed-granularity Address Translation in Previous: Mixed-granularity Address Translation in

Smart Pointers

A smart pointer is a special C++ parameterized class such that instances of this class behave like regular pointers. Smart pointers support all standard pointer operations such as dereference, cast, indexing etc. However, since they are implemented using a C++ class with overloaded operators supporting these pointer operations, it is possible to execute arbitrary code as part of any such operation. While smart pointers were originally used in garbage collectors to implement write barriers [23,22], they are also suitable for implementing address translation; the overloaded pointer dereference operations (via the ``*'' and ``->'' operators) can implement the necessary translation from persistent pointers into transient pointers.

A smart pointer class declaration is typically of the following form:

template <class T> class Ptr
{
 public:
  Ptr (T *p = NULL); // constructor
  ~Ptr ();           // destructor
  T& operator * ();  // dereference
  T *operator -> (); // dereference
  operator T * ();   // cast to `T *'
  ...
};
Given the above declaration of a smart pointer class, we can then use it as follows:
class Node;        // assume defined
Node *node_p;      // regular pointer
Ptr<Node> node_sp; // smart pointer
...
node_p->some_method();
node_sp->some_method();

Note that we have only shown some of the operators in the declaration. Also, we avoid describing the private data members of the smart pointer because the interface is much more important than the internal representation; it does not matter how the class is structured as long as the interface is implemented correctly. In fact, as will be clear from our discussion about variations in fine-grained address translation mechanisms, the smart pointer will need to be implemented differently for different situations and implementation choices.

Smart pointers were designed with the goal of transparently replacing regular pointers (except for declarations), and providing additional flexibility because arbitrary code can be executed for every pointer operation. In essence, it is an attempt to introduce limited (compile-time) reflection [11] into C++ for builtin data types (i.e., pointers).16 However, as described in [7], it is impossible to truly replace the functionality of regular pointers in a completely transparent fashion. Part of the problem stems from some of the inconsistencies in the language definition and unspecified implementation dependence. Thus, we do not advocate smart pointers for arbitrary usage across the board, but they are useful in situations where further control is required over pointer operations.



Footnotes

... pointers).16
C++ already provides limited reflective capabilities in the form of operator overloading for user-defined types and classes. However, this fails to support completely transparent redefinition of pointer operations in arbitrary situations.

next up previous
Next: Fine-grained Address Translation Up: Mixed-granularity Address Translation in Previous: Mixed-granularity Address Translation in

Sheetal V. Kakkad