1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:17:35 +00:00

AK: Make it possible to store complex types in a CircularQueue

Previously we would not run destructors for items in a CircularQueue,
which would lead to memory leaks.

This patch fixes that, and also adds a basic unit test for the class.
This commit is contained in:
Andreas Kling 2019-10-23 12:20:45 +02:00
parent 0c4c4c48f2
commit 0cea80218d
4 changed files with 87 additions and 11 deletions

View file

@ -8,12 +8,13 @@ namespace AK {
template<typename T, int Capacity>
class CircularDeque : public CircularQueue<T, Capacity> {
public:
T dequeue_end()
{
ASSERT(!this->is_empty());
T value = this->m_elements[(this->m_head + this->m_size - 1) % Capacity];
auto& slot = this->elements()[(this->m_head + this->m_size - 1) % Capacity];
T value = move(slot);
slot.~T();
this->m_size--;
return value;
}