2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
22 #include <folly/Memory.h>
23 #include <folly/Synchronized.h>
27 template <typename T
, T
* (*Creator
)(), void (*Deleter
)(T
*)>
28 class CompressionContextPool
{
31 std::unique_ptr
<T
, folly::static_function_deleter
<T
, Deleter
>>;
33 class ReturnToPoolDeleter
{
35 using Pool
= CompressionContextPool
<T
, Creator
, Deleter
>;
37 // lets us default-construct non-attached deleters, so we can
38 // default-construct empty Refs.
39 ReturnToPoolDeleter() : pool_(nullptr) {}
41 explicit ReturnToPoolDeleter(Pool
* pool
) : pool_(pool
) {}
43 void operator()(T
* t
) {
45 if (pool_
!= nullptr) {
46 pool_
->add(std::move(ptr
));
56 using Ref
= std::unique_ptr
<T
, ReturnToPoolDeleter
>;
58 explicit CompressionContextPool() {}
61 auto stack
= stack_
.wlock();
65 throw std::runtime_error("Failed to allocate new pool object!");
67 return Ref(t
, ReturnToPoolDeleter(this));
69 auto ptr
= std::move(stack
->back());
71 return Ref(ptr
.release(), ReturnToPoolDeleter(this));
75 void add(InternalRef ptr
) {
77 stack_
->push_back(std::move(ptr
));
80 folly::Synchronized
<std::vector
<InternalRef
>> stack_
;