Skip to content

Commit

Permalink
RingBuffer and SortedRingBuffer allocate data on the heap now
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Jan 22, 2024
1 parent abacfb5 commit 10fa381
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions Emulator/Utilities/RingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace util {
template <class T, isize capacity> struct Array
{
// Element storage
T elements[capacity];
T *elements = new T[capacity];

// Write pointer
isize w;
Expand All @@ -44,7 +44,8 @@ template <class T, isize capacity> struct Array
//

Array() { clear(); }

~Array() { delete[] elements; }

void clear() { w = 0; }
void clear(T t) { for (isize i = 0; i < capacity; i++) elements[i] = t; clear(); }
void align(isize offset) { w = offset; }
Expand All @@ -57,10 +58,11 @@ template <class T, isize capacity> struct Array
template <class W>
void operator<<(W& worker)
{
worker << this->elements << this->w;
for (isize i = 0; i < capacity; i++) worker << elements[i];
worker << w;
}


//
// Querying the fill status
//
Expand Down Expand Up @@ -100,8 +102,32 @@ template <class T, isize capacity>
struct SortedArray : public Array<T, capacity>
{
// Key storage
i64 keys[capacity];

i64 *keys = new i64[capacity];


//
// Initializing
//

~SortedArray() { delete[] keys; }


//
// Serializing
//

template <class W>
void operator<<(W& worker)
{
Array<T, capacity>::operator<<(worker);
for (isize i = 0; i < capacity; i++) worker << keys[i];
}


//
// Inserting
//

// Inserts an element at the end
void write(i64 key, T element)
{
Expand Down Expand Up @@ -141,7 +167,7 @@ struct SortedArray : public Array<T, capacity>
template <class T, isize capacity> struct RingBuffer
{
// Element storage
T elements[capacity];
T *elements = new T[capacity];

// Read and write pointers
isize r, w;
Expand All @@ -152,7 +178,8 @@ template <class T, isize capacity> struct RingBuffer
//

RingBuffer() { clear(); }

~RingBuffer() { delete[] elements; }

void clear() { r = w = 0; }
void clear(T t) { for (isize i = 0; i < capacity; i++) elements[i] = t; clear(); }
void align(isize offset) { w = (r + offset) % capacity; }
Expand All @@ -165,9 +192,10 @@ template <class T, isize capacity> struct RingBuffer
template <class W>
void operator<<(W& worker)
{
worker << this->elements << this->r << this->w;
for (isize i = 0; i < capacity; i++) worker << elements[i];
worker << this->r << this->w;
}


//
// Querying the fill status
Expand Down Expand Up @@ -253,8 +281,8 @@ template <class T, isize capacity>
struct SortedRingBuffer : public RingBuffer<T, capacity>
{
// Key storage
i64 keys[capacity];
i64 *keys = new i64[capacity];

// Inserts an element at the proper position
void insert(i64 key, T element)
{
Expand Down

0 comments on commit 10fa381

Please sign in to comment.