Purpose of the Override

The HadronZoo global new/delete override is a link time option invoked by calling make with the argument 'override=true', both when making the HadronZoo library and when making the target program. The override=true argument means that libHadronZoo_ovr_10.0.a will be the library archive, instead of the default of libHadronZoo_std_10.0.a.

The original override was a simple pass through to malloc() and free() that counted all bytes allocated to and deleted by, the program in question. This was created to provide more accurate reporting of memory consumption. The current override includes a multi-range allocation regime with three size ranges, 8-32, 40-128 and 136-256 bytes. Since the allocation regime must issue 64-bit pointers rather than 32-bit addresses, the granularity is 8 as all slots must be 8-byte aligned.

The override is considered experimental and not recommended for general use. Although it reduces allocation overheads, most of the opportunities for reduction are already exploited by the class specific allocation regimes, particularly that of hzString. The main reason for including the override in the offical release, is to illustrate how one would implement a global new/delete override, should this be desired.

The override is implemented as follows. In hzBasedefs.h, a file all HadronZoo based programs must ultimately include, a set of inlines are as follows:-

inline void* operator new (size_t size) { return hz_mem_allocate(size) ; } inline void* operator new[] (size_t size) { return hz_mem_allocate(size) ; } inline void operator delete (void* ptr) { hz_mem_release(ptr) ; } inline void operator delete[] (void* ptr) { hz_mem_release(ptr) ; }

Then in the library's hzMemory.cpp we have function pointers as follows:-

void* (*fnptr_Alloc) (unt32) = _regime_alloc_init ; void (*fnptr_Free) (void*) = _regime_free_norm ;

And the the functions hz_mem_allocate and hz_mem_release, called instead of new and delete, are implimented as functions that call the function pointers as follows:-

void* hz_mem_allocate (unt32 size) { return fnptr_Alloc(size) ; } void hz_mem_release (void* ptr) { fnptr_Free(ptr) ; }

Note that fnptr_Alloc, the function pointer called by hz_mem_allocate(), in other words by the new operator, is initially set to _regime_alloc_Init. This function is called to meet the first request for memory. It tests the value of _hzGlobal_XM and if false, sets fnptr_Alloc to _regime_alloc_spec which is just a call to malloc(). If the value of _hzGlobal_XM is true, a single hzHeap (heap regime class) instance is created and the pointers fnptr_Alloc and fnptr_Free are routed through to hzHeap allocation and release methods.