llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Initial implementation of P2719

Open ojhunt opened this issue 4 months ago • 3 comments

This is a basic implementation of P2719: "Type-aware allocation and deallocation functions" described at http://wg21.link/P2719

The proposal includes some more details but the basic change in functionality is the addition of support for an additional implicit parameter in operators new and delete to act as a type tag. Tag is of type std::type_identity<T> where T is the concrete type being allocated. So for example, a custom type specific allocator for int say can be provided by the declaration of

void operator new(std::type_identity, size_t); void operator delete(std::type_identity, void);

However this becomes more powerful by specifying templated declarations, for example

template <typename T> void operator new(std::type_identity<T>, size_t); template <typename T> void operator delete(std::type_identity<T>, void);

Where the operators being resolved will be the concrete type being operated over (NB. A completely unconstrained global definition as above is not recommended as it triggers many problems similar to a general override of the global operators).

These type aware operators can be declared as either free functions or in class, and can be specified with or without the other implicit parameters, with overload resolution performed according to the existing standard parameter prioritisation, only with type parameterised operators having higher precedence than non-type aware operators. The only exception is destroying_delete which for reasons discussed in the paper we do not support type-aware variants by default.

In order to ensure interchangability with existing operators, this implementation does extend the definition of a usual deallocation function as suggested in the proposal to support templated operators, as long as the only dependent parameter is the type_identity tag type, and the tag is explicitly a specialization of std::type_identity

The implementation adds a new -fcxx-type-aware-allocators flag to enable the feature. This is detectable in source via the cxx_type_aware_allocators feature check.

This also adds a separate flag to support the application of the proposal to the destroying delete, -fcxx-type-aware-destroying-delete. The proposal currently does not support destroying delete as it adds some significant hazards, but we've received sufficient requests for the behavior that we included it so people can see if it is actually useful. This can be detected in source via the cxx_type_aware_destroying_delete feature flag.

The implementation includes some error checking and warnings to try to detect basic errors, but the problem of detecting mismatch type-polymorphic new and delete is a problem to be explored in future.

ojhunt avatar Oct 24 '24 00:10 ojhunt