-
Notifications
You must be signed in to change notification settings - Fork 194
adding support to object construction with arguments. #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,10 @@ class Pool { | |
MARL_NO_EXPORT inline T* get(); | ||
|
||
// construct() calls the constructor on the item's data. | ||
MARL_NO_EXPORT inline void construct(); | ||
template <typename... Args> | ||
MARL_NO_EXPORT inline void construct(Args&&... args) { | ||
new (&data) T(std::forward<Args>(args)...); | ||
} | ||
|
||
// destruct() calls the destructor on the item's data. | ||
MARL_NO_EXPORT inline void destruct(); | ||
|
@@ -110,11 +113,6 @@ T* Pool<T>::Item::get() { | |
return reinterpret_cast<T*>(&data); | ||
} | ||
|
||
template <typename T> | ||
void Pool<T>::Item::construct() { | ||
new (&data) T; | ||
} | ||
|
||
template <typename T> | ||
void Pool<T>::Item::destruct() { | ||
get()->~T(); | ||
|
@@ -214,32 +212,64 @@ class BoundedPool : public Pool<T> { | |
|
||
// borrow() borrows a single item from the pool, blocking until an item is | ||
// returned if the pool is empty. | ||
MARL_NO_EXPORT inline Loan borrow() const; | ||
template <typename... Args> | ||
MARL_NO_EXPORT inline Loan borrow(Args&&... args) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you've inlined these methods into the class? Marl tries to keep the implementations out of the class, so the public interface is easily human parsable. I'd prefer it if these were put back where they were. |
||
marl::lock lock(storage->mutex); | ||
storage->returned.wait(lock, [&] { return storage->free != nullptr; }); | ||
auto item = storage->free; | ||
storage->free = storage->free->next; | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(std::forward<Args>(args)...); | ||
} | ||
return Loan(item, storage); | ||
} | ||
|
||
// borrow() borrows count items from the pool, blocking until there are at | ||
// borrowList() borrows count items from the pool, blocking until there are at | ||
// least count items in the pool. The function f() is called with each | ||
// borrowed item. | ||
// F must be a function with the signature: void(T&&) | ||
template <typename F> | ||
MARL_NO_EXPORT inline void borrow(size_t count, const F& f) const; | ||
template <typename F, typename... Args> | ||
MARL_NO_EXPORT inline void borrowList(size_t count, const F& f, Args&&... args) const { | ||
marl::lock lock(storage->mutex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add the |
||
for (size_t i = 0; i < count; i++) { | ||
storage->returned.wait(lock, [&] { return storage->free != nullptr; }); | ||
auto item = storage->free; | ||
storage->free = storage->free->next; | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(std::forward<Args>(args)...); | ||
} | ||
f(std::move(Loan(item, storage))); | ||
} | ||
} | ||
|
||
// tryBorrow() attempts to borrow a single item from the pool without | ||
// blocking. | ||
// The boolean of the returned pair is true on success, or false if the pool | ||
// is empty. | ||
MARL_NO_EXPORT inline std::pair<Loan, bool> tryBorrow() const; | ||
template <typename... Args> | ||
MARL_NO_EXPORT inline std::pair<Loan, bool> tryBorrow(Args&&... args) const { | ||
Item* item = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here |
||
{ | ||
marl::lock lock(storage->mutex); | ||
if (storage->free == nullptr) { | ||
return std::make_pair(Loan(), false); | ||
} | ||
item = storage->free; | ||
storage->free = storage->free->next; | ||
item->pool = this; | ||
} | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(std::forward<Args>(args)...); | ||
} | ||
return std::make_pair(Loan(item, storage), true); | ||
} | ||
|
||
private: | ||
class Storage : public Pool<T>::Storage { | ||
public: | ||
MARL_NO_EXPORT inline Storage(Allocator* allocator); | ||
MARL_NO_EXPORT inline ~Storage(); | ||
MARL_NO_EXPORT inline void return_(Item*) override; | ||
// We cannot copy this as the Item pointers would be shared and | ||
// deleted at a wrong point. We cannot move this because we return | ||
// pointers into items[N]. | ||
MARL_NO_EXPORT inline Storage(const Storage&) = delete; | ||
MARL_NO_EXPORT inline Storage& operator=(const Storage&) = delete; | ||
|
||
Item items[N]; | ||
marl::mutex mutex; | ||
|
@@ -253,7 +283,7 @@ template <typename T, int N, PoolPolicy POLICY> | |
BoundedPool<T, N, POLICY>::Storage::Storage(Allocator* allocator) | ||
: returned(allocator) { | ||
for (int i = 0; i < N; i++) { | ||
if (POLICY == PoolPolicy::Preserve) { | ||
if constexpr (POLICY == PoolPolicy::Preserve) { | ||
items[i].construct(); | ||
} | ||
items[i].next = this->free; | ||
|
@@ -263,7 +293,7 @@ BoundedPool<T, N, POLICY>::Storage::Storage(Allocator* allocator) | |
|
||
template <typename T, int N, PoolPolicy POLICY> | ||
BoundedPool<T, N, POLICY>::Storage::~Storage() { | ||
if (POLICY == PoolPolicy::Preserve) { | ||
if constexpr (POLICY == PoolPolicy::Preserve) { | ||
for (int i = 0; i < N; i++) { | ||
items[i].destruct(); | ||
} | ||
|
@@ -275,51 +305,9 @@ BoundedPool<T, N, POLICY>::BoundedPool( | |
Allocator* allocator /* = Allocator::Default */) | ||
: storage(allocator->make_shared<Storage>(allocator)) {} | ||
|
||
template <typename T, int N, PoolPolicy POLICY> | ||
typename BoundedPool<T, N, POLICY>::Loan BoundedPool<T, N, POLICY>::borrow() | ||
const { | ||
Loan out; | ||
borrow(1, [&](Loan&& loan) { out = std::move(loan); }); | ||
return out; | ||
} | ||
|
||
template <typename T, int N, PoolPolicy POLICY> | ||
template <typename F> | ||
void BoundedPool<T, N, POLICY>::borrow(size_t n, const F& f) const { | ||
marl::lock lock(storage->mutex); | ||
for (size_t i = 0; i < n; i++) { | ||
storage->returned.wait(lock, [&] { return storage->free != nullptr; }); | ||
auto item = storage->free; | ||
storage->free = storage->free->next; | ||
if (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(); | ||
} | ||
f(std::move(Loan(item, storage))); | ||
} | ||
} | ||
|
||
template <typename T, int N, PoolPolicy POLICY> | ||
std::pair<typename BoundedPool<T, N, POLICY>::Loan, bool> | ||
BoundedPool<T, N, POLICY>::tryBorrow() const { | ||
Item* item = nullptr; | ||
{ | ||
marl::lock lock(storage->mutex); | ||
if (storage->free == nullptr) { | ||
return std::make_pair(Loan(), false); | ||
} | ||
item = storage->free; | ||
storage->free = storage->free->next; | ||
item->pool = this; | ||
} | ||
if (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(); | ||
} | ||
return std::make_pair(Loan(item, storage), true); | ||
} | ||
|
||
template <typename T, int N, PoolPolicy POLICY> | ||
void BoundedPool<T, N, POLICY>::Storage::return_(Item* item) { | ||
if (POLICY == PoolPolicy::Reconstruct) { | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
tinebp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
item->destruct(); | ||
} | ||
{ | ||
|
@@ -366,11 +354,6 @@ class UnboundedPool : public Pool<T> { | |
MARL_NO_EXPORT inline Storage(Allocator* allocator); | ||
MARL_NO_EXPORT inline ~Storage(); | ||
MARL_NO_EXPORT inline void return_(Item*) override; | ||
// We cannot copy this as the Item pointers would be shared and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I see you've moved this up to |
||
// deleted at a wrong point. We could move this but would have to take | ||
// extra care no Item pointers are left in the moved-out object. | ||
MARL_NO_EXPORT inline Storage(const Storage&) = delete; | ||
MARL_NO_EXPORT inline Storage& operator=(const Storage&) = delete; | ||
|
||
Allocator* allocator; | ||
marl::mutex mutex; | ||
|
@@ -389,7 +372,7 @@ UnboundedPool<T, POLICY>::Storage::Storage(Allocator* allocator) | |
template <typename T, PoolPolicy POLICY> | ||
UnboundedPool<T, POLICY>::Storage::~Storage() { | ||
for (auto item : items) { | ||
if (POLICY == PoolPolicy::Preserve) { | ||
if constexpr (POLICY == PoolPolicy::Preserve) { | ||
item->destruct(); | ||
} | ||
allocator->destroy(item); | ||
|
@@ -418,7 +401,7 @@ inline void UnboundedPool<T, POLICY>::borrow(size_t n, const F& f) const { | |
auto count = std::max<size_t>(storage->items.size(), 32); | ||
for (size_t j = 0; j < count; j++) { | ||
auto item = allocator->create<Item>(); | ||
if (POLICY == PoolPolicy::Preserve) { | ||
if constexpr (POLICY == PoolPolicy::Preserve) { | ||
item->construct(); | ||
} | ||
storage->items.push_back(item); | ||
|
@@ -429,7 +412,7 @@ inline void UnboundedPool<T, POLICY>::borrow(size_t n, const F& f) const { | |
|
||
auto item = storage->free; | ||
storage->free = storage->free->next; | ||
if (POLICY == PoolPolicy::Reconstruct) { | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
item->construct(); | ||
} | ||
f(std::move(Loan(item, storage))); | ||
|
@@ -438,7 +421,7 @@ inline void UnboundedPool<T, POLICY>::borrow(size_t n, const F& f) const { | |
|
||
template <typename T, PoolPolicy POLICY> | ||
void UnboundedPool<T, POLICY>::Storage::return_(Item* item) { | ||
if (POLICY == PoolPolicy::Reconstruct) { | ||
if constexpr (POLICY == PoolPolicy::Reconstruct) { | ||
item->destruct(); | ||
} | ||
marl::lock lock(mutex); | ||
|
Uh oh!
There was an error while loading. Please reload this page.