diff options
author | babenko <babenko@yandex-team.ru> | 2022-02-10 16:49:19 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:19 +0300 |
commit | cec37806d8847aa3db53bafc9e251d4aaf325c12 (patch) | |
tree | 4a61c191e93e31d9ab423e258c71ab43550ee3d2 /library/cpp/yt/memory/new.h | |
parent | 58cd0b86ed99a72df22479e26a20bc1c1e57e65e (diff) | |
download | ydb-cec37806d8847aa3db53bafc9e251d4aaf325c12.tar.gz |
Restoring authorship annotation for <babenko@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/yt/memory/new.h')
-rw-r--r-- | library/cpp/yt/memory/new.h | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/library/cpp/yt/memory/new.h b/library/cpp/yt/memory/new.h index 2db45e0465..d8a1db0df4 100644 --- a/library/cpp/yt/memory/new.h +++ b/library/cpp/yt/memory/new.h @@ -1,70 +1,70 @@ -#pragma once - +#pragma once + #include "intrusive_ptr.h" #include "ref_tracked.h" - -#include <library/cpp/yt/misc/source_location.h> - -#include <util/system/defaults.h> - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -/*! - * \defgroup yt_new New<T> safe smart pointer constructors - * \ingroup yt_new - * - * This is collection of safe smart pointer constructors. - * - * \page yt_new_rationale Rationale - * New<T> function family was designed to prevent the following problem. - * Consider the following piece of code. - * - * \code - * class TFoo - * : public virtual TRefCounted - * { - * public: - * TFoo(); - * }; - * - * typedef TIntrusivePtr<TFoo> TFooPtr; - * - * void RegisterObject(TFooPtr foo) - * { - * ... - * } - * - * TFoo::TFoo() - * { - * // ... do something before - * RegisterObject(this); - * // ... do something after - * } - * \endcode - * - * What will happen on <tt>new TFoo()</tt> construction? After memory allocation - * the reference counter for newly created instance would be initialized to zero. - * Afterwards, the control goes to TFoo constructor. To invoke - * <tt>RegisterObject</tt> a new temporary smart pointer to the current instance - * have to be created effectively incrementing the reference counter (now one). - * After <tt>RegisterObject</tt> returns the control to the constructor - * the temporary pointer is destroyed effectively decrementing the reference - * counter to zero hence triggering object destruction during its initialization. - * - * To avoid this undefined behavior <tt>New<T></tt> was introduced. - * <tt>New<T></tt> holds a fake - * reference to the object during its construction effectively preventing - * premature destruction. - * - * \note An initialization like <tt>TIntrusivePtr<T> p = new T()</tt> - * would result in a dangling reference due to internals of #New<T> and - * #TRefCountedBase. - */ - -//////////////////////////////////////////////////////////////////////////////// - + +#include <library/cpp/yt/misc/source_location.h> + +#include <util/system/defaults.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +/*! + * \defgroup yt_new New<T> safe smart pointer constructors + * \ingroup yt_new + * + * This is collection of safe smart pointer constructors. + * + * \page yt_new_rationale Rationale + * New<T> function family was designed to prevent the following problem. + * Consider the following piece of code. + * + * \code + * class TFoo + * : public virtual TRefCounted + * { + * public: + * TFoo(); + * }; + * + * typedef TIntrusivePtr<TFoo> TFooPtr; + * + * void RegisterObject(TFooPtr foo) + * { + * ... + * } + * + * TFoo::TFoo() + * { + * // ... do something before + * RegisterObject(this); + * // ... do something after + * } + * \endcode + * + * What will happen on <tt>new TFoo()</tt> construction? After memory allocation + * the reference counter for newly created instance would be initialized to zero. + * Afterwards, the control goes to TFoo constructor. To invoke + * <tt>RegisterObject</tt> a new temporary smart pointer to the current instance + * have to be created effectively incrementing the reference counter (now one). + * After <tt>RegisterObject</tt> returns the control to the constructor + * the temporary pointer is destroyed effectively decrementing the reference + * counter to zero hence triggering object destruction during its initialization. + * + * To avoid this undefined behavior <tt>New<T></tt> was introduced. + * <tt>New<T></tt> holds a fake + * reference to the object during its construction effectively preventing + * premature destruction. + * + * \note An initialization like <tt>TIntrusivePtr<T> p = new T()</tt> + * would result in a dangling reference due to internals of #New<T> and + * #TRefCountedBase. + */ + +//////////////////////////////////////////////////////////////////////////////// + template <class T, class = void> struct THasAllocator { @@ -72,7 +72,7 @@ struct THasAllocator }; template <class T> -struct THasAllocator<T, std::void_t<typename T::TAllocator>> +struct THasAllocator<T, std::void_t<typename T::TAllocator>> { using TTrue = void; }; @@ -82,7 +82,7 @@ struct THasAllocator<T, std::void_t<typename T::TAllocator>> //! Allocates a new instance of |T|. template <class T, class... As, class = typename THasAllocator<T>::TFalse> TIntrusivePtr<T> New(As&&... args); - + template <class T, class... As, class = typename THasAllocator<T>::TTrue> TIntrusivePtr<T> New(typename T::TAllocator* allocator, As&&... args); @@ -101,26 +101,26 @@ TIntrusivePtr<T> NewWithDelete(const TDeleter& deleter, As&&... args); //! The allocation is additionally marked with #location. template <class T, class TTag, int Counter, class... As> TIntrusivePtr<T> NewWithLocation(const TSourceLocation& location, As&&... args); - + //! Enables calling #New and co for types with private ctors. #define DECLARE_NEW_FRIEND() \ template <class DECLARE_NEW_FRIEND_T> \ friend struct NYT::TRefCountedWrapper; - + //////////////////////////////////////////////////////////////////////////////// - + //! CRTP mixin enabling access to instance's extra space. template <class T> class TWithExtraSpace -{ +{ protected: const void* GetExtraSpacePtr() const; void* GetExtraSpacePtr(); }; - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT #define NEW_INL_H_ #include "new-inl.h" |