summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-11-02 20:45:02 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-11-02 20:45:02 -0500
commite8422002f84dc4313894a5b3136c44a9005081fd (patch)
treeb41633d9f759306fe9c7c01e209780222b47f5df /source
parent6c7fd1db481ff10a16ecab958c6542784fa60b9c (diff)
Allocator deallocate is void rather than returning FudStatus.
Diffstat (limited to 'source')
-rw-r--r--source/fud_allocator.cpp10
-rw-r--r--source/fud_string.cpp38
2 files changed, 25 insertions, 23 deletions
diff --git a/source/fud_allocator.cpp b/source/fud_allocator.cpp
index d5127fa..3f58fb0 100644
--- a/source/fud_allocator.cpp
+++ b/source/fud_allocator.cpp
@@ -30,13 +30,12 @@ Result<std::byte*, FudStatus> FudAllocator::allocate(size_t bytes, size_t alignm
return RetType::okay(pointer);
}
-FudStatus FudAllocator::deallocate(std::byte* pointer, size_t bytes)
+void FudAllocator::deallocate(std::byte* pointer, size_t bytes)
{
if (pointer == nullptr || bytes == 0) {
- return FudStatus::ArgumentInvalid;
+ return;
}
fudFree(pointer);
- return FudStatus::Success;
}
bool FudAllocator::isEqual(const Allocator& rhs) const
@@ -50,14 +49,13 @@ Result<std::byte*, FudStatus> NullAllocator::allocate(size_t bytes, size_t align
{
static_cast<void>(bytes);
static_cast<void>(alignment);
- return FudError{FudStatus::Failure};
+ return FudError{FudStatus::AllocFailure};
}
-FudStatus NullAllocator::deallocate(std::byte* pointer, size_t bytes)
+void NullAllocator::deallocate(std::byte* pointer, size_t bytes)
{
static_cast<void>(pointer);
static_cast<void>(bytes);
- return FudStatus::Failure;
}
bool NullAllocator::isEqual(const Allocator& rhs) const
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index 4a20630..4dddedb 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -23,7 +23,8 @@
namespace fud {
-StringResult String::makeFromCString(const char8_t* cString) {
+StringResult String::makeFromCString(const char8_t* cString)
+{
return makeFromCString(reinterpret_cast<const char*>(cString));
}
@@ -85,14 +86,19 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator)
return StringResult::okay(std::move(output));
}
-StringResult String::from(const String& rhs)
+StringResult String::from(const String& rhs, Option<Allocator*> allocatorOption)
{
if (!rhs.valid()) {
return StringResult::error(FudStatus::ArgumentInvalid);
}
+ auto* allocator = allocatorOption.valueOr(rhs.allocator());
+ if (allocator == nullptr || !allocatorValid(allocator)) {
+ return StringResult::error(FudStatus::ArgumentInvalid);
+ }
+
String output{};
- output.m_allocator = rhs.m_allocator;
+ output.m_allocator = reinterpret_cast<uintptr_t>(allocator);
utf8* outputData{nullptr};
size_t outputCapacity{0};
size_t outputLength{0};
@@ -154,12 +160,11 @@ StringResult String::from(StringView view, Allocator* allocator)
String::String(String&& rhs) noexcept : m_allocator{rhs.m_allocator}, m_repr{std::move(rhs.m_repr)}
{
- if (isLarge()) {
- rhs.m_repr.large.data = nullptr;
- }
+ rhs.setSmall();
+ rhs.m_repr.small.length = 0;
}
-String::~String()
+String::~String() noexcept
{
cleanup();
}
@@ -184,7 +189,7 @@ FudStatus String::copy(const String& rhs)
size_t outputLength{};
if (isLarge()) {
- auto allocResult = allocator()->allocate(m_repr.large.capacity);
+ auto allocResult = allocator()->allocate(m_repr.large.capacity, 1);
if (allocResult.isError()) {
return allocResult.takeError();
}
@@ -223,6 +228,7 @@ String& String::operator=(String&& rhs) noexcept
if (isLarge()) {
rhs.m_repr.large.data = nullptr;
}
+
return *this;
}
@@ -230,8 +236,7 @@ void String::cleanup()
{
const auto* allocPtr = allocator();
if (isLarge() && m_repr.large.data != nullptr && allocPtr != nullptr) {
- auto deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
- static_cast<void>(deallocStatus);
+ allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
m_repr.large.data = nullptr;
}
}
@@ -258,16 +263,16 @@ FudStatus String::resize(size_t newCapacity)
auto copyResult = copyMem(dataMut(), temp.size(), temp.data(), len);
fudAssert(copyResult == FudStatus::Success);
- auto deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
+ allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
setSmall();
m_repr.small.length = len & smallStringLengthMask;
copyMem(m_repr.small.buffer, temp);
m_repr.small.buffer[len] = '\0';
- return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
+ return FudStatus::Success;
}
- auto allocResult = allocator()->allocate(newCapacity);
+ auto allocResult = allocator()->allocate(newCapacity, 1);
if (allocResult.isError()) {
return allocResult.takeError();
}
@@ -277,9 +282,8 @@ FudStatus String::resize(size_t newCapacity)
auto copyResult = copyMem(newData, newCapacity, data(), length());
fudAssert(copyResult == FudStatus::Success);
- auto deallocStatus = FudStatus::Success;
if (isLarge()) {
- deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
+ allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity);
}
size_t len = length();
@@ -291,7 +295,7 @@ FudStatus String::resize(size_t newCapacity)
m_repr.large.data[m_repr.large.length] = '\0';
fudAssert(valid());
- return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
+ return FudStatus::Success;
}
bool String::nullTerminated() const
@@ -750,7 +754,7 @@ FudStatus String::makeLarge(size_t cap, size_t len, utf8*& outputData)
{
m_repr.large.capacity = cap;
m_repr.large.length = len;
- auto dataResult = allocator()->allocate(cap);
+ auto dataResult = allocator()->allocate(cap, 1);
if (dataResult.isError()) {
return dataResult.getError();
}