Commit 6e466116 authored by Dominik Charousset's avatar Dominik Charousset

fixed memory leak in memory_cache

parent 0b62812c
...@@ -122,6 +122,9 @@ class memory { ...@@ -122,6 +122,9 @@ class memory {
template<typename T> template<typename T>
class basic_memory_cache : public memory_cache { class basic_memory_cache : public memory_cache {
static constexpr size_t ne = s_alloc_size / sizeof(T);
static constexpr size_t dsize = ne > s_min_elements ? ne : s_min_elements;
struct wrapper : instance_wrapper { struct wrapper : instance_wrapper {
ref_counted* parent; ref_counted* parent;
union { T instance; }; union { T instance; };
...@@ -133,8 +136,6 @@ class basic_memory_cache : public memory_cache { ...@@ -133,8 +136,6 @@ class basic_memory_cache : public memory_cache {
class storage : public ref_counted { class storage : public ref_counted {
static constexpr size_t ne = s_alloc_size / sizeof(T);
static constexpr size_t dsize = ne > s_min_elements ? ne : s_min_elements;
public: public:
...@@ -163,7 +164,7 @@ class basic_memory_cache : public memory_cache { ...@@ -163,7 +164,7 @@ class basic_memory_cache : public memory_cache {
std::vector<wrapper*> cached_elements; std::vector<wrapper*> cached_elements;
basic_memory_cache() { basic_memory_cache() {
cached_elements.reserve(s_cache_size / sizeof(T)); cached_elements.reserve(dsize);
} }
~basic_memory_cache() { ~basic_memory_cache() {
...@@ -174,18 +175,16 @@ class basic_memory_cache : public memory_cache { ...@@ -174,18 +175,16 @@ class basic_memory_cache : public memory_cache {
return static_cast<T*>(ptr); return static_cast<T*>(ptr);
} }
void release_instance(void* vptr) override {
virtual void release_instance(void* vptr) {
CPPA_REQUIRE(vptr != nullptr); CPPA_REQUIRE(vptr != nullptr);
auto ptr = reinterpret_cast<T*>(vptr); auto ptr = reinterpret_cast<T*>(vptr);
CPPA_REQUIRE(ptr->outer_memory != nullptr); CPPA_REQUIRE(ptr->outer_memory != nullptr);
auto wptr = static_cast<wrapper*>(ptr->outer_memory); auto wptr = static_cast<wrapper*>(ptr->outer_memory);
wptr->destroy(); wptr->destroy();
if (cached_elements.capacity() > 0) cached_elements.push_back(wptr); wptr->deallocate();
else wptr->deallocate();
} }
virtual std::pair<instance_wrapper*, void*> new_instance() { std::pair<instance_wrapper*, void*> new_instance() override {
if (cached_elements.empty()) { if (cached_elements.empty()) {
auto elements = new storage; auto elements = new storage;
for (auto i = elements->begin(); i != elements->end(); ++i) { for (auto i = elements->begin(); i != elements->end(); ++i) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment