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 once it commences, is guaranteed to complete and return, before its thread can be suspended by a context switch. The _sync_add_and_fetch reads a memory location, performs an addition to the value found, writes the result back to the location, and returns the same result to the caller. If these exact steps were performed by a function that was not atomic and the process was switched out, on resumption the function could well write back an out of date value to the location, if indeed the location had not been freed back to the OS by another thread. With _sync_add_and_fetch there is no possibility of this. Nor is there any possibility of two or more calls to _sync_add_and_fetch, occuring at exactly the same time in separate threads, resulting in missed additions. The clue is in the 'sync' part of the name.

Because of this behaviour, the atomic sync functions can be used to lock resources, and most HadronZoo locks are atomic. In the case of smart pointer copy counts, any function that increases the copy count must do so BEFORE executing any other instructions. Conversely, any function that decrements the copy count (adds -1), must do so AFTER executing all other instructions.