From: Yedidya Feldblum Date: Wed, 20 Jul 2016 22:07:31 +0000 (-0700) Subject: allocate_sys_buffer X-Git-Tag: 2016.07.26~22 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=e28213bf2c5aa1c400cfd2ab363abfeadee81fa2 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 --- 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 {};