From e28213bf2c5aa1c400cfd2ab363abfeadee81fa2 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 20 Jul 2016 15:07:31 -0700 Subject: [PATCH] allocate_sys_buffer Summary: [Folly] `allocate_sys_buffer`. For when a `malloc`'d buffer is required, with an associated deleter that calls `free`. Reviewed By: JonCoens Differential Revision: D3590516 fbshipit-source-id: 644f4b5d5e8f19dbc8f29efe3e93517fba0ad72f --- folly/Memory.h | 6 ++++++ folly/test/MemoryTest.cpp | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/folly/Memory.h b/folly/Memory.h index a64ca4c8..81ac176c 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -114,6 +114,12 @@ std::shared_ptr to_shared_ptr(std::unique_ptr&& ptr) { return std::shared_ptr(std::move(ptr)); } +using SysBufferDeleter = static_function_deleter; +using SysBufferUniquePtr = std::unique_ptr; +inline SysBufferUniquePtr allocate_sys_buffer(size_t size) { + return SysBufferUniquePtr(::malloc(size)); +} + /** * A SimpleAllocator must provide two methods: * diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp index d6193402..c35e40b9 100644 --- a/folly/test/MemoryTest.cpp +++ b/folly/test/MemoryTest.cpp @@ -33,6 +33,11 @@ TEST(make_unique, compatible_with_std_make_unique) { make_unique("hello, world"); } +TEST(allocate_sys_buffer, compiles) { + auto buf = allocate_sys_buffer(256); + // Freed at the end of the scope. +} + template struct T {}; template struct S {}; template struct P {}; -- 2.34.1