1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_WIN_SCOPED_LOCALALLOC_H_ 6 #define BASE_WIN_SCOPED_LOCALALLOC_H_ 7 8 #include <memory> 9 #include <utility> 10 11 #include "base/win/windows_types.h" 12 13 namespace base { 14 namespace win { 15 16 // unique_ptr deleter for LocalAlloc memory. 17 struct LocalAllocDeleter { operatorLocalAllocDeleter18 void operator()(void* ptr) const { ::LocalFree(ptr); } 19 }; 20 21 template <typename T> 22 using ScopedLocalAllocTyped = std::unique_ptr<T, LocalAllocDeleter>; 23 24 using ScopedLocalAlloc = ScopedLocalAllocTyped<void>; 25 26 // Make a typed ScopedLocalAlloc class and clear the original pointer. 27 template <typename T> TakeLocalAlloc(T * & ptr)28ScopedLocalAllocTyped<T> TakeLocalAlloc(T*& ptr) { 29 return ScopedLocalAllocTyped<T>(std::exchange(ptr, nullptr)); 30 } 31 32 } // namespace win 33 } // namespace base 34 35 #endif // BASE_WIN_SCOPED_LOCALALLOC_H_ 36