Skip to content

Commit

Permalink
[base] Add base::UncheckedFreeDeleter for use with std::unique_ptr
Browse files Browse the repository at this point in the history
Adds function object for use when storing an unchecked alloc pointer in
a std::unique_ptr. This mimics existing //base/memory/free_deleter.h's
FreeDeleter.

Bug: 1279371,1286810
Change-Id: Ied0319d496edd86274026526239097413839368e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4109349
Reviewed-by: Wez <wez@chromium.org>
Reviewed-by: Benoit Lize <lizeb@chromium.org>
Commit-Queue: Matthew Wolenetz <wolenetz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1083872}
  • Loading branch information
wolenetz authored and Chromium LUCI CQ committed Dec 15, 2022
1 parent f6071a4 commit dcb3201
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions base/process/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ using partition_alloc::win::kOomExceptionCode;
// TODO(crbug.com/1279371): Enforce it, when all callers are converted.
BASE_EXPORT void UncheckedFree(void* ptr);

// Function object which invokes 'UncheckedFree' on its parameter, which should
// be a pointer resulting from UncheckedMalloc or UncheckedCalloc. Can be used
// to store such pointers in std::unique_ptr:
//
// int* foo_ptr = nullptr;
// if (UncheckedMalloc(sizeof(*foo_ptr), reinterpret_cast<void**>(&foo_ptr))) {
// std::unique_ptr<int, base::UncheckedFreeDeleter> unique_foo_ptr(foo_ptr);
// ...
// }
struct UncheckedFreeDeleter {
inline void operator()(void* ptr) const { UncheckedFree(ptr); }
};

} // namespace base

#endif // BASE_PROCESS_MEMORY_H_

0 comments on commit dcb3201

Please sign in to comment.