Smart Pointer/Address Technique

HadronZoo smart pointers are implemented within classes and class templates that require them. There is no generic smart pointer class, in spite of the fact that the approach taken has been the same in all cases. There are reasons for this. A generic smart pointer class would be inelegant, and be of little benefit as smart pointers are easy to implement. More importantly, some HadronZoo classes allocate internal data space from purpose built allocation and management regimes, rather than from the heap. Currently, all allocation regimes assign allocated space 32-bit addresses, instead of 64-bit pointers. It would be difficult if not impossible for a generic smart pointer class to accommodate such regimes.

The technique used, imposes both method and structure on the origin class. In all cases the origin class itself will have just one member, namely the smart pointer. In the standard case, where the origin class allocates from the heap, the smart pointer is an actual pointer, to a subclass instance. The subclass has the same members and functions as the origin class would have, if it were not using smart pointers, but with a copy count as an extra member to effect soft copying. In the non-standard case, where the origin class is using an allocation regime, the smart pointer is a 32-bit address. Within member functions of the origin class, this address is translated to a pointer to the subclass instance. As with the standard case, the subclass has the same members and functions as the origin class, plus the copy count.

The term given to whatever construct the smart pointer points to (or addresses), is the 'data space'. And a class instance can only be populated by 'attaching' it to a data space. Attachment is a matter of incrementing the copy count in the target data space and then setting the pointer or address in the class instance to the target space. Detachment is the exact reverse, the pointer or address is set to NULL, then the copy count in the data space is decremented. The data space itself is not deleted until the copy count falls to zero. In an assignment A=B, A first detaches from any data space it is currently attached to, then attaches to the same data space as B.

Original Class Class members Class members Origin Class Smart Pointer Data Space or Subclass Instance Copy Count Class members

Smart Pointers in a Multithreaded Program

In multithreaded programs the copy count is incremented and decremented by _sync_add_and_fetch, which is one of a family of similar in-built atomic synchronization functions. There is often confusion as to exactly what these function do. An atomic function will once execution commences, run to completion before the calling thread can be suspended by a context switch, but this alone is not enough. An add operation is three machine instructions: A fetch from memory, an addition and then a write back to memory of the result. If two threads are adding to the same variable at the same time, that both operations will run to completion without interuption won't stop both from reading a 1 and writing a 2! To be clear, the variable is locked so there is no possibility of an addition or a subtraction being missed.

This suffices for the copy count but does not in any way, serve as a lock for the data entity as a whole. The data entity must either be separately locked or limited to read only operations while the modifying function builds a new version of it in quarentine. Furthermore, while _sync_add_and_fetch is atomic, the modifying function is not. A context switch could happen either directly before or aftr the sync call. This is why during attachment the increment is performed first and during detachment the decrement is performed last.