Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private variable rename #4

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions src/Core/DataStructures/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,23 @@ namespace Kraid
{

template<typename T>
struct ArrayView
class ArrayView
{
T* data;
uint64 size;

ArrayView(const std::vector<T>& vector)
{
this->data = vector.data();
this->size = vector.size();
}

ArrayView(const Buffer& buffer)
{
this->data = buffer.data;
this->size = buffer.size;
}

ArrayView(const T* buffer, uint64 size)
{
this->data = buffer;
this->size = size;
}

inline uint64 GetSize()
{
return this->size;
}

inline T* GetArray()
{
return this->data;
}
public:
const T* const data;
const uint64 size;
public:
ArrayView(const std::vector<T>& vector):
data(vector.data()),
size(vector.size()) {}

ArrayView(const Buffer& buffer):
data(buffer.data),
size(buffer.size) {}

ArrayView(const T* buffer, uint64 size):
data(buffer),
size(size) {}

inline T& operator[](const uint64 index)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Core/DataStructures/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Kraid
memcpy(this->data, other.data, other.size);
this->size = other.size;
};

Buffer::Buffer(Buffer&& other)
{
if (this->data == other.data) return;

this->data = other.data;
other.data = nullptr;
}

Buffer::Buffer(uint8* data, uint64 size) :data(data), size(size) {}

Buffer& Buffer::operator=(const Buffer& other)
Expand All @@ -39,15 +39,14 @@ namespace Kraid

return *this;
}

Buffer::~Buffer()
{
if (data == nullptr) return;

free(data);
data = nullptr;
size = 0;
LINFO("Freed");
}

}
5 changes: 3 additions & 2 deletions src/Core/DataStructures/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
namespace Kraid
{

struct Buffer
class Buffer
{
public:
uint8* data = nullptr;
uint64 size = 0;

public:
Buffer() = default;
Buffer(uint64 size);
Buffer(const Buffer& other);
Expand Down
38 changes: 19 additions & 19 deletions src/Core/DataStructures/RefCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,60 @@ namespace Kraid

ReferenceCounter::ReferenceCounter()
{
this->refcounter = new ReferenceCounterInternal();
this->refcounter_mutex = new Mutex();
this->m_refcounter = new uint64(0);
this->m_refcounter_mutex = new Mutex();
}

ReferenceCounter::ReferenceCounter(const ReferenceCounter& other)
{
this->refcounter = other.refcounter;
this->refcounter_mutex = other.refcounter_mutex;
this->m_refcounter = other.m_refcounter;
this->m_refcounter_mutex = other.m_refcounter_mutex;
this->Increment();
}

ReferenceCounter& ReferenceCounter::operator=(const ReferenceCounter& other)
{
if(this == &other) return *this;

this->refcounter = other.refcounter;
this->refcounter_mutex = other.refcounter_mutex;
this->m_refcounter = other.m_refcounter;
this->m_refcounter_mutex = other.m_refcounter_mutex;
this->Increment();

return *this;
}

ReferenceCounterInternal* ReferenceCounter::operator->() const
uint64* ReferenceCounter::operator->() const
{
return this->refcounter;
return this->m_refcounter;
}

void ReferenceCounter::Increment() const
{
this->refcounter_mutex->Lock();
this->refcounter->refcount++;
this->refcounter_mutex->Lock();
this->m_refcounter_mutex->Lock();
(*this->m_refcounter)++;
this->m_refcounter_mutex->Lock();
}

void ReferenceCounter::Decrement() const
{
bool should_free_mutex = false;

this->refcounter_mutex->Lock();
this->m_refcounter_mutex->Lock();

this->refcounter->refcount--;
if(this->refcounter_mutex == 0)
(*this->m_refcounter)--;
if(*this->m_refcounter == 0)
{
free(this->refcounter);
this->refcounter = nullptr;
free(this->m_refcounter);
this->m_refcounter = nullptr;
should_free_mutex = true;
}

this->refcounter_mutex->Unlock();
this->m_refcounter_mutex->Unlock();

if(should_free_mutex)
{
free(this->refcounter_mutex);
this->refcounter_mutex = nullptr;
free(this->m_refcounter_mutex);
this->m_refcounter_mutex = nullptr;
}
}

Expand Down
24 changes: 8 additions & 16 deletions src/Core/DataStructures/RefCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,26 @@
namespace Kraid
{

struct ReferenceCounterInternal
class ReferenceCounter
{
mutable uint64 refcount = 0;

ReferenceCounterInternal() = default;
ReferenceCounterInternal(const ReferenceCounterInternal& other) = delete;
ReferenceCounterInternal& operator=(const ReferenceCounterInternal& other) = delete;
};

struct ReferenceCounter
{
mutable ReferenceCounterInternal* refcounter = nullptr;
mutable Mutex* refcounter_mutex = nullptr;

private:
mutable uint64* m_refcounter = nullptr;
mutable Mutex* m_refcounter_mutex = nullptr;
public:
ReferenceCounter();
ReferenceCounter(const ReferenceCounter& other);
ReferenceCounter& operator=(const ReferenceCounter& other);
ReferenceCounterInternal* operator->() const;
uint64* operator->() const;

void Increment() const;
void Decrement() const;

//NOTE(Tiago):when the reference counted object decrements the object, only the last one to decrement will have a null pointer,
//which means that only that threads copy of the refcounter will return on this, ensuring that only one thread tries to release
//their data.
bool inline ShouldFree() const
bool inline ShouldFree() const
{
return this->refcounter==nullptr && this->refcounter_mutex == nullptr;
return this->m_refcounter==nullptr;
}
};

Expand Down
34 changes: 25 additions & 9 deletions src/Core/DataStructures/RingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,42 @@

namespace Kraid
{
template<typename T>
template<typename T>
class CircularBuffer
{
private:
T* m_buffer = nullptr;
T* m_backbuffer = nullptr;
public:
T* buffer = nullptr;
T* backbuffer = nullptr;
uint64 size = 0;
public:
CircularBuffer(uint64 size):size(size)
{
buffer = (T*)malloc(size * sizeof(T));
backbuffer = (T*)malloc(size * sizeof(T));
memset(buffer, 0, size * sizeof(T));
m_buffer = (T*)malloc(size * sizeof(T));
m_backbuffer = (T*)malloc(size * sizeof(T));
memset(m_buffer, 0, size * sizeof(T));
}

T& operator[](const uint64 index)
{
return this->m_buffer[index];
}

T at(const uint64 index) const
{
return this->m_buffer[index];
}

T* data() const
{
return this->m_buffer;
}

void push(T value)
{
memcpy(backbuffer, buffer + 1, (size - 1) * sizeof(T));
backbuffer[size - 1] = value;
memcpy(buffer, backbuffer, size * sizeof(T));
memcpy(m_backbuffer, m_buffer + 1, (size - 1) * sizeof(T));
m_backbuffer[size - 1] = value;
memcpy(m_buffer, m_backbuffer, size * sizeof(T));
}
};

Expand Down
8 changes: 0 additions & 8 deletions src/Core/DataStructures/RingeBuffer.cpp

This file was deleted.

6 changes: 4 additions & 2 deletions src/Core/DataStructures/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

namespace Kraid
{
class StringView:public ArrayView<const char>

class StringView: public ArrayView<const char>
{
public:
StringView(const char* string):ArrayView(string, strlen(string)){};
StringView(const char* string, uint64 length): ArrayView(string, length){};
StringView(const std::string& string): ArrayView(string.data(), string.length()){};

const char* Get() const {return this->data;};
operator const char*() const {return this->data;};
};

class WideStringView:public ArrayView<const wchar_t>
Expand All @@ -25,6 +26,7 @@ namespace Kraid
WideStringView(const std::wstring& string): ArrayView(string.data(), string.length()){};

const wchar_t* Get() const { return this->data; };
operator const wchar_t*() const { return this->data; };
};

}
Loading