Skip to content

Commit

Permalink
AK: Make it possible to store complex types in a CircularQueue
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Oct 23, 2019
1 parent 0c4c4c4 commit 0cea802
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
5 changes: 3 additions & 2 deletions AK/CircularDeque.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 28 additions & 8 deletions AK/CircularQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ class CircularQueue {
public:
CircularQueue()
{
for (int i = 0; i < Capacity; ++i)
m_elements[i] = T();
}

~CircularQueue()
{
clear();
}

void clear()
{
for (int i = 0; i < m_size; ++i)
elements()[(m_head + i) % Capacity].~T();

m_head = 0;
m_size = 0;
}
Expand All @@ -25,25 +31,36 @@ class CircularQueue {

int capacity() const { return Capacity; }

void enqueue(const T& t)
void enqueue(T&& value)
{
m_elements[(m_head + m_size) % Capacity] = t;
auto& slot = elements()[(m_head + m_size) % Capacity];
if (m_size == Capacity)
slot.~T();

new (&slot) T(value);
if (m_size == Capacity)
m_head = (m_head + 1) % Capacity;
else
++m_size;
}

void enqueue(const T& value)
{
enqueue(T(value));
}

T dequeue()
{
ASSERT(!is_empty());
T value = m_elements[m_head];
auto& slot = elements()[m_head];
T value = move(slot);
slot.~T();
m_head = (m_head + 1) % Capacity;
--m_size;
return value;
}

const T& at(int index) const { return m_elements[(m_head + index) % Capacity]; }
const T& at(int index) const { return elements()[(m_head + index) % Capacity]; }

const T& first() const { return at(0); }
const T& last() const { return at(size() - 1); }
Expand All @@ -59,7 +76,7 @@ class CircularQueue {
return *this;
}

const T& operator*() const { return m_queue.m_elements[m_index]; }
const T& operator*() const { return m_queue.elements()[m_index]; }

private:
friend class CircularQueue;
Expand All @@ -78,8 +95,11 @@ class CircularQueue {
int head_index() const { return m_head; }

protected:
T* elements() { return reinterpret_cast<T*>(m_storage); }
const T* elements() const { return reinterpret_cast<const T*>(m_storage); }

friend class ConstIterator;
T m_elements[Capacity];
alignas(T) u8 m_storage[sizeof(T) * Capacity];
int m_size { 0 };
int m_head { 0 };
};
Expand Down
6 changes: 5 additions & 1 deletion AK/Tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PROGRAMS = TestAtomic TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL TestStringView TestUtf8
PROGRAMS = TestAtomic TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL TestStringView TestUtf8 TestCircularQueue

CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../

Expand Down Expand Up @@ -70,6 +70,10 @@ TestStringView: TestStringView.o $(SHARED_TEST_OBJS)
TestUtf8: TestUtf8.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestUtf8.o $(SHARED_TEST_OBJS)

TestCircularQueue: TestCircularQueue.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestCircularQueue.o $(SHARED_TEST_OBJS)


clean:
rm -f $(SHARED_TEST_OBJS)
rm -f $(PROGRAMS)
51 changes: 51 additions & 0 deletions AK/Tests/TestCircularQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <AK/TestSuite.h>
#include <AK/String.h>
#include <AK/CircularQueue.h>

TEST_CASE(basic)
{
CircularQueue<int, 3> ints;
EXPECT(ints.is_empty());
ints.enqueue(1);
ints.enqueue(2);
ints.enqueue(3);
EXPECT_EQ(ints.size(), 3);

ints.enqueue(4);
EXPECT_EQ(ints.size(), 3);
EXPECT_EQ(ints.dequeue(), 2);
EXPECT_EQ(ints.dequeue(), 3);
EXPECT_EQ(ints.dequeue(), 4);
EXPECT_EQ(ints.size(), 0);
}

TEST_CASE(complex_type)
{
CircularQueue<String, 2> strings;

strings.enqueue("ABC");
strings.enqueue("DEF");

EXPECT_EQ(strings.size(), 2);

strings.enqueue("abc");
strings.enqueue("def");

EXPECT_EQ(strings.dequeue(), "abc");
EXPECT_EQ(strings.dequeue(), "def");
}

TEST_CASE(complex_type_clear)
{
CircularQueue<String, 5> strings;
strings.enqueue("xxx");
strings.enqueue("xxx");
strings.enqueue("xxx");
strings.enqueue("xxx");
strings.enqueue("xxx");
EXPECT_EQ(strings.size(), 5);
strings.clear();
EXPECT_EQ(strings.size(), 0);
}

TEST_MAIN(CircularQueue)

0 comments on commit 0cea802

Please sign in to comment.