diff --git a/src/core/lib/support/memory.h b/src/core/lib/support/memory.h index 3e00fb2011db9b423feb719b46747a730a8bbecc..6eff94eff78326f5dd0c81af89ec1ac43cd336ad 100644 --- a/src/core/lib/support/memory.h +++ b/src/core/lib/support/memory.h @@ -31,8 +31,8 @@ * */ -#ifndef GRPC_SUPPORT_MEMORY_H -#define GRPC_SUPPORT_MEMORY_H +#ifndef GRPC_CORE_LIB_SUPPORT_MEMORY_H +#define GRPC_CORE_LIB_SUPPORT_MEMORY_H #include <grpc/support/alloc.h> @@ -61,8 +61,8 @@ class DefaultDelete { void operator()(T* p) { Delete(p); } }; -template <typename T> -using UniquePtr = std::unique_ptr<T, DefaultDelete<T>>; +template <typename T, typename Deleter = DefaultDelete<T>> +using UniquePtr = std::unique_ptr<T, Deleter>; template <typename T, typename... Args> inline UniquePtr<T> MakeUnique(Args&&... args) { @@ -71,4 +71,4 @@ inline UniquePtr<T> MakeUnique(Args&&... args) { } // namespace grpc_core -#endif /* GRPC_SUPPORT_NEW_H */ +#endif /* GRPC_CORE_LIB_SUPPORT_MEMORY_H */ diff --git a/test/core/support/memory_test.cc b/test/core/support/memory_test.cc index 9e20e48b0ccf1e9c297db670ace66872832b8f7b..8db316a423870c0e110154931a4ea8818e4262c7 100644 --- a/test/core/support/memory_test.cc +++ b/test/core/support/memory_test.cc @@ -66,6 +66,19 @@ TEST(MemoryTest, MakeUniqueWithArgTest) { EXPECT_EQ(42, *i); } +TEST(MemoryTest, UniquePtrWithCustomDeleter) { + int n = 0; + class IncrementingDeleter { + public: + void operator()(int* p) { ++*p; } + }; + { + UniquePtr<int, IncrementingDeleter> p(&n); + EXPECT_EQ(0, n); + } + EXPECT_EQ(1, n); +} + } // namespace testing } // namespace grpc_core