allocate_sys_buffer
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 20 Jul 2016 22:07:31 +0000 (15:07 -0700)
committerFacebook Github Bot 7 <facebook-github-bot-7-bot@fb.com>
Wed, 20 Jul 2016 22:08:28 +0000 (15:08 -0700)
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
folly/test/MemoryTest.cpp

index a64ca4c83db5e2ac732aacb4f87165ff1dcaa188..81ac176cb11a3494923ff5f378844472f0f1f926 100644 (file)
@@ -114,6 +114,12 @@ std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T, D>&& ptr) {
   return std::shared_ptr<T>(std::move(ptr));
 }
 
   return std::shared_ptr<T>(std::move(ptr));
 }
 
+using SysBufferDeleter = static_function_deleter<void, ::free>;
+using SysBufferUniquePtr = std::unique_ptr<void, SysBufferDeleter>;
+inline SysBufferUniquePtr allocate_sys_buffer(size_t size) {
+  return SysBufferUniquePtr(::malloc(size));
+}
+
 /**
  * A SimpleAllocator must provide two methods:
  *
 /**
  * A SimpleAllocator must provide two methods:
  *
index d61934024738f0fa1391ebedee4ad171f5b7bf42..c35e40b9d95bbd4b7f4f5991340a82cdf773ebfa 100644 (file)
@@ -33,6 +33,11 @@ TEST(make_unique, compatible_with_std_make_unique) {
   make_unique<string>("hello, world");
 }
 
   make_unique<string>("hello, world");
 }
 
+TEST(allocate_sys_buffer, compiles) {
+  auto buf = allocate_sys_buffer(256);
+  //  Freed at the end of the scope.
+}
+
 template <std::size_t> struct T {};
 template <std::size_t> struct S {};
 template <std::size_t> struct P {};
 template <std::size_t> struct T {};
 template <std::size_t> struct S {};
 template <std::size_t> struct P {};