scoped_localalloc.h 926 B

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