Skip to content

Commit

Permalink
Fix bug in static pool
Browse files Browse the repository at this point in the history
Getting ALL events from pool caused SEGV
  • Loading branch information
dziegel committed Oct 13, 2024
1 parent 382758a commit 06f89d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions include/cpp_event_framework/StaticPool.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public:

auto* result = first_;
first_ = result->next;
if (first_ == nullptr)
{
last_ = nullptr;
}
fill_level_--;
return result;
}
Expand All @@ -109,6 +113,7 @@ public:
last_->next = ptr;
}
last_ = ptr;
ptr->next = nullptr;

fill_level_++;
assert(FillLevel() <= NumElements);
Expand Down
25 changes: 20 additions & 5 deletions test/ActiveObjectFrameworkEmbedded_unittest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

#include "../examples/activeobject_embedded/FsmImpl.hxx"

#include <cstddef>
#include <iostream>
#include <vector>

#include <cpp_active_objects_embedded/EventQueue.hxx>
#include <cpp_active_objects_embedded/SingleThreadActiveObjectDomain.hxx>
#include <cpp_event_framework/Signal.hxx>
#include <cpp_event_framework/StaticPool.hxx>

using namespace std::chrono_literals;
Expand All @@ -35,19 +38,31 @@ void ActiveObjectFrameworkEmbeddedMain()
{
malloc_called = false;
cpp_active_objects_embedded::EventQueue<10> queue;

cpp_event_framework::StaticPool<3, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool(
"EmbeddedEventPool");
// Tell EventPoolAllocator to use pool created above
example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool);
assert(!malloc_called);

// Test getting all events from queue and release them all afterwards
// Tests pool full -> empty -> full
for (int i = 0; i < 3; i++)
{
std::vector<cpp_event_framework::Signal::SPtr> events;
for (size_t j = 0; j < pool.Size(); j++)
{
events.emplace_back(example::activeobject_embedded::Go2::MakeShared());
}
events.clear();
}

// SingleThreadActiveObjectDomain uses jthread - thread creation uses heap.
// You need to write your own heapless SingleThreadActiveObjectDomain here if desired.
cpp_active_objects_embedded::SingleThreadActiveObjectDomain domain(&queue);

malloc_called = false;

cpp_event_framework::StaticPool<2, example::activeobject_embedded::EventPoolElementSizeCalculator::kSptrSize> pool(
"EmbeddedEventPool");
// Tell EventPoolAllocator to use pool created above
example::activeobject_embedded::EventPoolAllocator::SetAllocator(&pool);

example::activeobject_embedded::FsmImpl active_object;
domain.RegisterObject(&active_object);

Expand Down

0 comments on commit 06f89d0

Please sign in to comment.