From 880ccc91201c19caa80ce4bc3a956c63d743d28a Mon Sep 17 00:00:00 2001 From: Roberto Martin Fantini Date: Sat, 22 Feb 2025 17:35:36 +0100 Subject: [PATCH 1/2] Correct typo and double dealocator --- src/mfast/coder/common/template_repo.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/mfast/coder/common/template_repo.h b/src/mfast/coder/common/template_repo.h index d949d56f..85f58eb5 100644 --- a/src/mfast/coder/common/template_repo.h +++ b/src/mfast/coder/common/template_repo.h @@ -14,10 +14,14 @@ class template_repo_base { template_repo_base(mfast::allocator *dictionary_alloc) : dictionary_alloc_(dictionary_alloc) {} virtual ~template_repo_base() { - for (auto &elem : vector_enties_) { + for (auto &elem : vector_entries_) { if (elem->of_array.capacity_in_bytes_) + { dictionary_alloc_->deallocate(elem->of_array.content_, elem->of_array.capacity_in_bytes_); + elem->of_array.capacity_in_bytes_ = 0; + elem->of_array.content_ = nullptr; + } } } @@ -35,7 +39,7 @@ class template_repo_base { } void add_vector_entry(value_storage *entry) { if (dictionary_alloc_) - vector_enties_.push_back(entry); + vector_entries_.push_back(entry); } protected: @@ -43,7 +47,7 @@ class template_repo_base { typedef std::vector value_entries_t; value_entries_t reset_entries_; - value_entries_t vector_enties_; // for string and byteVector + value_entries_t vector_entries_; // for string and byteVector arena_allocator instruction_alloc_; mfast::allocator *dictionary_alloc_; }; From 84f85be10e58f51b670d22b08d9085f9fedc8239 Mon Sep 17 00:00:00 2001 From: Roberto Martin Fantini Date: Sat, 22 Feb 2025 17:39:08 +0100 Subject: [PATCH 2/2] Add unit tests for the template repo base --- src/mfast/coder/common/template_repo.h | 3 ++ tests/CMakeLists.txt | 1 + tests/template_repo_base.cpp | 41 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/template_repo_base.cpp diff --git a/src/mfast/coder/common/template_repo.h b/src/mfast/coder/common/template_repo.h index 85f58eb5..cc8c5102 100644 --- a/src/mfast/coder/common/template_repo.h +++ b/src/mfast/coder/common/template_repo.h @@ -37,6 +37,9 @@ class template_repo_base { void add_reset_entry(value_storage *entry) { reset_entries_.push_back(entry); } +#ifdef TESTING_TEMPLATE_REPO_BASE +public: +#endif void add_vector_entry(value_storage *entry) { if (dictionary_alloc_) vector_entries_.push_back(entry); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2859a552..116488ce 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -61,6 +61,7 @@ add_executable (mfast_test aggregate_view_test.cpp simple_coder_test.cpp scp_reset_test.cpp + template_repo_base.cpp ) target_link_libraries (mfast_test diff --git a/tests/template_repo_base.cpp b/tests/template_repo_base.cpp new file mode 100644 index 00000000..4dfcb464 --- /dev/null +++ b/tests/template_repo_base.cpp @@ -0,0 +1,41 @@ +#include "catch.hpp" + +#define TESTING_TEMPLATE_REPO_BASE +#include +#include +#include +#include +#include + +class template_repo_base_impl : public mfast::template_repo_base +{ +public: + template_repo_base_impl(mfast::allocator *dictionary_alloc) + : mfast::template_repo_base(dictionary_alloc) {} + + virtual mfast::template_instruction *get_template(uint32_t) + { + return nullptr; + } +}; + +TEST_CASE("Test template repo base with repeated pointers","[test_template_repo_base]") +{ + using namespace mfast; + + value_storage* storage_1_ = new value_storage(); + { + const char * test_data = "Test_Data"; + storage_1_->of_array.len_ = static_cast(std::strlen(test_data) + 1); + storage_1_->of_array.content_ = malloc_allocator::instance()->allocate(storage_1_->of_array.len_); + storage_1_->of_array.capacity_in_bytes_ = 9; + } + + { + template_repo_base_impl templ_repo(malloc_allocator::instance()); + templ_repo.add_vector_entry(storage_1_); + templ_repo.add_vector_entry(storage_1_); + } + + delete storage_1_; +}