free_deleter.h 683 B

12345678910111213141516171819202122232425
  1. // Copyright 2016 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_MEMORY_FREE_DELETER_H_
  5. #define BASE_MEMORY_FREE_DELETER_H_
  6. #include <stdlib.h>
  7. namespace base {
  8. // Function object which invokes 'free' on its parameter, which must be
  9. // a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
  10. //
  11. // std::unique_ptr<int, base::FreeDeleter> foo_ptr(
  12. // static_cast<int*>(malloc(sizeof(int))));
  13. struct FreeDeleter {
  14. inline void operator()(void* ptr) const {
  15. free(ptr);
  16. }
  17. };
  18. } // namespace base
  19. #endif // BASE_MEMORY_FREE_DELETER_H_