steinwurf / recycle

Simple resource pool for recycling resources in C++
BSD 3-Clause "New" or "Revised" License
63 stars 23 forks source link

How to allocate a fixed number of heavy objects? #25

Closed lattice0 closed 4 years ago

lattice0 commented 4 years ago

Sorry, it wasn't clear for me in the examples. If I call shared_pool.allocate() then wouldn't it do the heavy alocation?

What if I want to pre allocate 30 heavy objects and reuse them and put them back to resource pool to recycle?

mortenvp commented 4 years ago

Hi @lucaszanella, good question. I don't think we added anything in the API to explicitly pre-allocate objects. However, if you just allocate the 30 objects yourself after creating the pool you should get the behavior you want. A pull-request with a pre-allocation argument to the pool would be welcome :)

mortenvp commented 4 years ago

Yes, the pool will only allocated a new object if all objects are in use. So if you allocate 30 objects, this will grow the capacity of the pool to 30. If you simply release them e.g. let the shared/unique pointer go out of scope the objects return to the pool. The next 30 "allocations" will re-use those 30. Hope it makes sense?