summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/fud_allocator.cpp8
-rw-r--r--source/fud_assert.cpp21
-rw-r--r--source/fud_c_file.cpp8
-rw-r--r--source/fud_directory.cpp2
-rw-r--r--source/fud_memory.cpp10
-rw-r--r--source/fud_sqlite.cpp4
-rw-r--r--source/fud_string.cpp131
-rw-r--r--source/fud_utf8.cpp2
-rw-r--r--source/libfud.cpp9
9 files changed, 93 insertions, 102 deletions
diff --git a/source/fud_allocator.cpp b/source/fud_allocator.cpp
index b5af593..1e4117a 100644
--- a/source/fud_allocator.cpp
+++ b/source/fud_allocator.cpp
@@ -30,16 +30,18 @@ Result<void*, FudStatus> FudAllocator::allocate(size_t bytes, size_t alignment)
return RetType::okay(pointer);
}
-void FudAllocator::deallocate(void* pointer, size_t bytes, size_t alignment)
+FudStatus FudAllocator::deallocate(void* pointer, size_t bytes)
{
+ if (pointer == nullptr || bytes == 0) {
+ return FudStatus::ArgumentInvalid;
+ }
static_cast<void>(bytes);
- static_cast<void>(alignment);
fudFree(pointer);
+ return FudStatus::Success;
}
bool FudAllocator::isEqual(const Allocator& rhs) const
{
- /* Is this legit? Or is this a bogus check? */
return &rhs == static_cast<const Allocator*>(this);
}
diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp
index 749aa6b..14bf5eb 100644
--- a/source/fud_assert.cpp
+++ b/source/fud_assert.cpp
@@ -1,24 +1,20 @@
#include "fud_assert.hpp"
#include "fud_array.hpp"
+#include "fud_span.hpp"
#include "fud_string_view.hpp"
#include <climits>
#include <cstdio>
#include <format>
-#include <stdexcept>
namespace fud {
-[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation)
+void assertFormat(
+ const char* assertion,
+ const std::source_location sourceLocation,
+ Span<char, ASSERT_MSG_SIZE> buffer)
{
- constexpr size_t MAX_FILE_CHARS = 256;
- constexpr size_t MAX_FUNCTION_CHARS = 256;
- constexpr size_t BITS_PER_OCTAL = 3;
- constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(sourceLocation.line())) + 3;
- constexpr size_t MAX_ASSERT_CHARS = 512 - MAX_LINE_CHARS;
- constexpr size_t ASSERT_MSG_SIZE = MAX_FILE_CHARS + MAX_LINE_CHARS + MAX_FUNCTION_CHARS + MAX_ASSERT_CHARS;
-
auto lengthResult = cStringLength(sourceLocation.file_name());
size_t filenameLength = 0;
auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX;
@@ -36,7 +32,6 @@ namespace fud {
filenameLength = MAX_FILE_CHARS;
}
- Array<char, ASSERT_MSG_SIZE> buffer{};
// clang-format off
static_cast<void>(std::format_to_n(
buffer.data(), buffer.size() - 1U,
@@ -46,6 +41,12 @@ namespace fud {
sourceLocation.function_name(), MAX_FUNCTION_CHARS,
assertion, MAX_ASSERT_CHARS));
// clang-format on
+}
+
+[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation)
+{
+ Array<char, ASSERT_MSG_SIZE> buffer{};
+ assertFormat(assertion, sourceLocation, Span<char, ASSERT_MSG_SIZE>::make(buffer));
buffer[buffer.size() - 1] = '\0';
fputs(buffer.data(), stderr);
std::abort();
diff --git a/source/fud_c_file.cpp b/source/fud_c_file.cpp
index 15c57ab..9fc7734 100644
--- a/source/fud_c_file.cpp
+++ b/source/fud_c_file.cpp
@@ -22,7 +22,7 @@ namespace fud {
CBinaryFileResult CBinaryFile::make(const String& filename, CFileMode mode)
{
if (!filename.valid()) {
- return CBinaryFileResult::error(FudStatus::InvalidInput);
+ return CBinaryFileResult::error(FudStatus::ArgumentInvalid);
}
auto modeResult{String::makeFromCString(CBinaryFileModeFromFlags(mode))};
if (modeResult.isError()) {
@@ -42,7 +42,7 @@ CBinaryFileResult CBinaryFile::make(const String& filename, CFileMode mode)
CBinaryFileResult CBinaryFile::make(const String& filename, CFileMode mode, const char* extraFlags)
{
if (!filename.valid()) {
- return CBinaryFileResult::error(FudStatus::InvalidInput);
+ return CBinaryFileResult::error(FudStatus::ArgumentInvalid);
}
auto modeResult{String::makeFromCStrings(CBinaryFileModeFromFlags(mode), extraFlags)};
if (modeResult.isError()) {
@@ -90,7 +90,7 @@ CBinaryFile& CBinaryFile::operator=(CBinaryFile&& rhs) noexcept
CTextFileResult CTextFile::make(const String& filename, CFileMode mode)
{
if (!filename.valid()) {
- return CTextFileResult::error(FudStatus::InvalidInput);
+ return CTextFileResult::error(FudStatus::ArgumentInvalid);
}
auto modeResult{String::makeFromCString(CBinaryFileModeFromFlags(mode))};
if (modeResult.isError()) {
@@ -110,7 +110,7 @@ CTextFileResult CTextFile::make(const String& filename, CFileMode mode)
CTextFileResult CTextFile::make(const String& filename, CFileMode mode, const char* extraFlags)
{
if (!filename.valid()) {
- return CTextFileResult::error(FudStatus::InvalidInput);
+ return CTextFileResult::error(FudStatus::ArgumentInvalid);
}
auto modeResult{String::makeFromCStrings(CTextFileModeFromFlags(mode), extraFlags)};
if (modeResult.isError()) {
diff --git a/source/fud_directory.cpp b/source/fud_directory.cpp
index 318d7e6..f46d530 100644
--- a/source/fud_directory.cpp
+++ b/source/fud_directory.cpp
@@ -90,7 +90,7 @@ Result<Directory, FudStatus> Directory::make(const String& name)
using RetType = Result<Directory, FudStatus>;
Directory directory{};
if (!name.valid()) {
- return RetType::error(FudStatus::InvalidInput);
+ return RetType::error(FudStatus::ArgumentInvalid);
}
if (!name.utf8Valid()) {
diff --git a/source/fud_memory.cpp b/source/fud_memory.cpp
index c4a4aa8..56701a6 100644
--- a/source/fud_memory.cpp
+++ b/source/fud_memory.cpp
@@ -26,7 +26,7 @@ FudStatus copyMem(void* destination, size_t destination_size, const void* source
}
if (destination_size < count) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
auto* destPtr = static_cast<char*>(destination);
@@ -45,7 +45,7 @@ FudStatus compareMem(const void* lhs, size_t destination_size, const void* rhs,
}
if (destination_size < count) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
int localDifference = 0;
@@ -83,7 +83,7 @@ FudStatus setMemory(void* data, size_t dataSize, uint8_t pattern, size_t count)
if (count > dataSize)
{
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
for (size_t idx = 0; idx < count; ++idx)
@@ -104,12 +104,12 @@ FudStatus setMemory(
{
if (eltOffset >= collectionCount)
{
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
if (eltOffset + eltCount > collectionCount)
{
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
auto dataSize = collectionCount * eltSize;
diff --git a/source/fud_sqlite.cpp b/source/fud_sqlite.cpp
index e13feee..735389f 100644
--- a/source/fud_sqlite.cpp
+++ b/source/fud_sqlite.cpp
@@ -140,7 +140,7 @@ FudStatus SqliteDb::initialize()
m_nameValid = m_name.utf8Valid();
if (!m_nameValid) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
m_errorCode = open();
@@ -178,7 +178,7 @@ SqliteStatement::SqliteStatement(const SqliteDb& sqliteDb, const String& input)
}
if (!input.utf8Valid() || input.length() > INT_MAX) {
- m_status = FudStatus::InvalidInput;
+ m_status = FudStatus::ArgumentInvalid;
return;
}
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index 31ba5ba..d354fe7 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -19,6 +19,8 @@
#include "fud_assert.hpp"
+#include <type_traits>
+
namespace fud {
ssize_t cStringLength(const char* str)
@@ -59,31 +61,23 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator)
auto lenResult = cStringLength(cString);
if (lenResult < 0 || lenResult >= SSIZE_MAX) {
- return StringResult::error(FudStatus::InvalidInput);
+ return StringResult::error(FudStatus::ArgumentInvalid);
}
String output{};
output.m_allocator = allocator;
- auto* data = output.m_buffer.data();
output.m_length = static_cast<size_t>(lenResult);
if (output.m_length >= output.m_capacity) {
output.m_capacity = output.m_length + 1;
- auto dataResult{output.m_allocator->allocate(output.m_capacity)};
- if (dataResult.isError()) {
- return StringResult::error(dataResult);
- }
-
- data = static_cast<utf8*>(dataResult.takeOkay());
- if (data == nullptr) {
- return StringResult::error(FudStatus::AllocFailure);
- }
- output.m_data = data;
+ output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity)));
+ fudAssert(output.m_data != nullptr);
}
- auto copyStatus = copyMem(data, output.m_capacity, cString, output.m_length);
+ auto copyStatus = copyMem(output.data(), output.m_capacity, cString, output.m_length);
fudAssert(copyStatus == FudStatus::Success);
+
auto terminateStatus = output.nullTerminate();
fudAssert(terminateStatus == FudStatus::Success);
@@ -93,7 +87,7 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator)
StringResult String::from(const String& rhs)
{
if (!rhs.valid()) {
- return StringResult::error(FudStatus::InvalidInput);
+ return StringResult::error(FudStatus::ArgumentInvalid);
}
String output{};
@@ -102,18 +96,16 @@ StringResult String::from(const String& rhs)
output.m_allocator = rhs.m_allocator;
if (rhs.isLarge()) {
- auto dataResult = output.m_allocator->allocate(output.m_capacity);
- if (dataResult.isError()) {
- return StringResult::error(dataResult.getError());
- }
- output.m_data = static_cast<utf8*>(dataResult.getOkay());
+ output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity)));
fudAssert(output.m_data != nullptr);
}
auto copyResult = copyMem(output.data(), output.m_capacity, rhs.data(), output.m_length);
fudAssert(copyResult == FudStatus::Success);
+
auto nullTerminateStatus = output.nullTerminate();
fudAssert(nullTerminateStatus == FudStatus::Success);
+
return StringResult::okay(std::move(output));
}
@@ -141,7 +133,7 @@ FudStatus String::copy(const String& rhs)
}
if (!rhs.valid()) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
cleanup();
@@ -151,12 +143,7 @@ FudStatus String::copy(const String& rhs)
m_allocator = rhs.m_allocator;
if (isLarge()) {
- auto dataResult = m_allocator->allocate(m_capacity);
- if (dataResult.isError()) {
- return dataResult.getError();
- }
- m_data = static_cast<utf8*>(dataResult.getOkay());
- fudAssert(m_data != nullptr);
+ m_data = static_cast<utf8*>(M_TakeOrReturn(m_allocator->allocate(m_capacity)));
}
auto copyResult = copyMem(data(), m_capacity, rhs.data(), m_length);
@@ -187,7 +174,8 @@ String& String::operator=(String&& rhs) noexcept
void String::cleanup()
{
if (isLarge() && m_data != nullptr && m_allocator != nullptr) {
- m_allocator->deallocate(m_data, m_capacity);
+ auto deallocStatus = m_allocator->deallocate(m_data, m_capacity);
+ static_cast<void>(deallocStatus);
m_data = nullptr;
}
}
@@ -209,37 +197,36 @@ FudStatus String::resize(size_t newCapacity)
if (newCapacity <= SSO_BUF_SIZE) {
BufType temp{BufType::constFill(0)};
- static_cast<void>(copyMem(data(), temp.size(), temp.data(), length()));
- m_allocator->deallocate(m_data, m_capacity);
+ auto copyResult = copyMem(data(), temp.size(), temp.data(), length());
+ fudAssert(copyResult == FudStatus::Success);
+
+ auto deallocStatus = m_allocator->deallocate(m_data, m_capacity);
m_capacity = SSO_BUF_SIZE;
m_data = nullptr;
+
copyMem(m_buffer, temp);
data()[m_length] = '\0';
- return FudStatus::Success;
- }
- utf8* newData = nullptr;
- auto newDataResult = m_allocator->allocate(newCapacity);
- if (newDataResult.isError()) {
- return newDataResult.getError();
- }
- newData = static_cast<utf8*>(newDataResult.getOkay());
- if (newData == nullptr) {
- return FudStatus::AllocFailure;
+ return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
}
+ auto* newData = static_cast<utf8*>(M_TakeOrReturn(m_allocator->allocate(newCapacity)));
+ fudAssert(newData != nullptr);
+
auto copyResult = copyMem(newData, newCapacity, data(), length());
fudAssert(copyResult == FudStatus::Success);
+ auto deallocStatus = FudStatus::Success;
if (isLarge()) {
- m_allocator->deallocate(data(), m_capacity);
+ deallocStatus = m_allocator->deallocate(data(), m_capacity);
}
m_capacity = newCapacity;
m_data = newData;
data()[m_length] = '\0';
fudAssert(valid());
- return FudStatus::Success;
+
+ return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success;
}
bool String::nullTerminated() const
@@ -348,12 +335,12 @@ FudStatus String::pushBack(const FudUtf8& letter)
}
if (!letter.valid()) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
const auto* letterData = letter.data();
if (letterData == nullptr) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
auto letterSize = letter.size();
@@ -385,7 +372,7 @@ FudStatus String::append(const char* source)
{
auto lenResult = cStringLength(source);
if (lenResult < 0 || lenResult >= SSIZE_MAX) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
return this->append(StringView{static_cast<size_t>(lenResult), source});
@@ -436,41 +423,48 @@ FudStatus String::append(StringView source)
StringResult String::catenate(const char* rhs) const
{
if (!valid()) {
- return StringResult::error(FudStatus::InvalidInput);
+ return StringResult::error(FudStatus::ArgumentInvalid);
}
auto lenResult = cStringLength(rhs);
if (lenResult < 0 || lenResult >= SSIZE_MAX) {
- return StringResult::error(FudStatus::InvalidInput);
+ return StringResult::error(FudStatus::ArgumentInvalid);
}
size_t rhsLength = static_cast<size_t>(lenResult);
String output{};
- if (SIZE_MAX - m_length < rhsLength)
- {
+ if (SIZE_MAX - m_length < rhsLength) {
return StringResult::error(FudStatus::Failure);
}
+ output.m_allocator = m_allocator;
+
output.m_length = m_length + rhsLength;
- auto* destPtr = output.m_buffer.data();
- if (output.m_length >= output.m_capacity) {
+ if (output.m_length > output.m_capacity) {
output.m_capacity = output.m_length + 1;
- auto ptrResult = m_allocator->allocate(output.m_capacity);
- if (ptrResult.isError()) {
- return StringResult::error(ptrResult.getError());
- }
- destPtr = static_cast<utf8*>(ptrResult.getOkay());
- if (destPtr == nullptr) {
- return StringResult::error(FudStatus::AllocFailure);
- }
- output.m_data = destPtr;
}
- auto status = copyMem(destPtr, m_capacity, data(), length());
+ if (!output.isLarge()) {
+ auto status = copyMem(output.m_buffer.data(), output.m_capacity, data(), length());
+ fudAssert(status == FudStatus::Success);
+
+ status = copyMem(output.m_buffer.data() + length(), output.m_capacity - length(), rhs, rhsLength);
+ fudAssert(status == FudStatus::Success);
+
+ auto terminateStatus = output.nullTerminate();
+ fudAssert(terminateStatus == FudStatus::Success);
+
+ return StringResult::okay(std::move(output));
+ }
+
+ output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity)));
+ fudAssert(output.m_data != nullptr);
+
+ auto status = copyMem(output.m_data, m_capacity, data(), length());
fudAssert(status == FudStatus::Success);
- status = copyMem(destPtr + length(), output.m_capacity - length(), rhs, rhsLength);
+ status = copyMem(output.m_data + length(), output.m_capacity - length(), rhs, rhsLength);
fudAssert(status == FudStatus::Success);
auto terminateStatus = output.nullTerminate();
@@ -482,26 +476,25 @@ StringResult String::catenate(const char* rhs) const
StringResult String::catenate(const String& rhs) const
{
if (!valid() || !rhs.valid()) {
- return StringResult::error(FudStatus::InvalidInput);
+ return StringResult::error(FudStatus::ArgumentInvalid);
}
- if (SIZE_MAX - m_length < rhs.length())
- {
+ if (SIZE_MAX - m_length < rhs.length()) {
return StringResult::error(FudStatus::Failure);
}
String output{};
+ output.m_allocator = m_allocator;
output.m_length = m_length + rhs.length();
output.m_capacity = output.m_length + 1;
+
if (output.m_capacity < SSO_BUF_SIZE) {
output.m_capacity = SSO_BUF_SIZE;
}
+
if (output.isLarge()) {
- auto allocResult{output.m_allocator->allocate(output.m_capacity)};
- if (allocResult.isError()) {
- return StringResult::error(allocResult.getError());
- }
- output.m_data = static_cast<utf8*>(allocResult.takeOkay());
+ output.m_data = static_cast<utf8*>(M_TakeOrReturn(output.m_allocator->allocate(output.m_capacity)));
+ fudAssert(output.m_data != nullptr);
}
auto* destPtr = output.data();
diff --git a/source/fud_utf8.cpp b/source/fud_utf8.cpp
index 0804531..ee8137a 100644
--- a/source/fud_utf8.cpp
+++ b/source/fud_utf8.cpp
@@ -104,7 +104,7 @@ FudStatus isAsciiPredicate(FudUtf8* character, bool* pred, Predicate&& predicate
auto maybeAscii = character->getAscii();
if (!maybeAscii.has_value()) {
- return FudStatus::InvalidInput;
+ return FudStatus::ArgumentInvalid;
}
auto asciiChar = *maybeAscii;
diff --git a/source/libfud.cpp b/source/libfud.cpp
index 538d3a9..802b2c7 100644
--- a/source/libfud.cpp
+++ b/source/libfud.cpp
@@ -37,7 +37,7 @@ FUD fud()
return fudInfo;
}
-Result<String, FudStatus> getEnv(const char* name)
+Result<String, FudStatus> getEnv(const char* name, Allocator* allocator)
{
using RetType = Result<String, FudStatus>;
@@ -50,12 +50,7 @@ Result<String, FudStatus> getEnv(const char* name)
return RetType::error(FudStatus::NotFound);
}
- auto envVarResult = String::makeFromCString(resultString);
- if (envVarResult.isError()) {
- return RetType::error(envVarResult);
- }
-
- return RetType::okay(std::move(envVarResult));
+ return String::makeFromCString(resultString, allocator);
}
} // namespace fud