Skip to content

Add helpers for making unique_ptrs with custom allocators

Olli Lupton requested to merge olupton_allocator_aware_unique into master

Adds a header EventLocalUnique.h with helpers for making std::unique_ptr objects using custom deleters/allocators.

Where

std::unique_ptr<Bar> foo = std::make_unique<Bar>( 42 );

would use the standard new/delete to allocate/deallocate storage for a Bar

auto foo = LHCb::make_event_local_unique( LHCb::getMemResource( evtCtx ), 42 );

uses the event-local custom allocator to make some std::unique_ptr<Bar, custom_deleter<Bar>> object, which can be expressed using the alias LHCb::event_local_unique_ptr<Bar>. Patterns such as

LHCb::event_local_unique_ptr<Base> = LHCb::make_event_local_unique<Derived>( ... );

should work as expected (with the usual caveat that Base should have a virtual destructor). The cost of this is that on a typical system sizeof( LHCb::event_local_unique_ptr<T> ) = 3 * sizeof( std::unique_ptr<T> )

Adding this to LHCb as it seems to be the natural home; will be used downstream in [at least] Rec.

cc: @graven (tips on making it more elegant?) @nnolte

Merge request reports