diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/fud_allocator.cpp | 29 | ||||
-rw-r--r-- | source/fud_assert.cpp | 4 | ||||
-rw-r--r-- | source/fud_file.cpp | 20 | ||||
-rw-r--r-- | source/fud_string.cpp | 95 |
4 files changed, 75 insertions, 73 deletions
diff --git a/source/fud_allocator.cpp b/source/fud_allocator.cpp index 8daf969..d5127fa 100644 --- a/source/fud_allocator.cpp +++ b/source/fud_allocator.cpp @@ -19,18 +19,18 @@ namespace fud { -Result<void*, FudStatus> FudAllocator::allocate(size_t bytes, size_t alignment) +Result<std::byte*, FudStatus> FudAllocator::allocate(size_t bytes, size_t alignment) { - using RetType = Result<void*, FudStatus>; + using RetType = Result<std::byte*, FudStatus>; static_cast<void>(alignment); - auto* pointer = static_cast<std::byte*>(fudAlloc(bytes)); + auto* pointer = fudAlloc(bytes); if (pointer == nullptr) { return RetType::error(FudStatus::AllocFailure); } return RetType::okay(pointer); } -FudStatus FudAllocator::deallocate(void* pointer, size_t bytes) +FudStatus FudAllocator::deallocate(std::byte* pointer, size_t bytes) { if (pointer == nullptr || bytes == 0) { return FudStatus::ArgumentInvalid; @@ -46,4 +46,25 @@ bool FudAllocator::isEqual(const Allocator& rhs) const FudAllocator globalFudAllocator{}; +Result<std::byte*, FudStatus> NullAllocator::allocate(size_t bytes, size_t alignment) +{ + static_cast<void>(bytes); + static_cast<void>(alignment); + return FudError{FudStatus::Failure}; +} + +FudStatus 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 +{ + return &rhs == static_cast<const Allocator*>(this); +} + +NullAllocator globalNullAllocator{}; + } // namespace fud diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index 7838e22..966f44c 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -42,8 +42,8 @@ DrainResult BufferSink::drain(StringView source) return result; } /* TODO: give users control over this functionality */ - result.bytesWritten = fwrite(reinterpret_cast<const char*>(source.m_data), 1, source.m_length, stderr); - if (result.bytesWritten != source.m_length) { + result.bytesDrained = fwrite(reinterpret_cast<const char*>(source.m_data), 1, source.m_length, stderr); + if (result.bytesDrained != source.m_length) { result.status = FudStatus::Full; } return result; diff --git a/source/fud_file.cpp b/source/fud_file.cpp index 8f84648..94f1f27 100644 --- a/source/fud_file.cpp +++ b/source/fud_file.cpp @@ -30,13 +30,8 @@ FileResult RegularFile::open( StringView filename, FileAccessMode mode, OpenFlags flags, - Option<int> dirFdOption, - Allocator* allocator) + Option<int> dirFdOption) { - if (allocator == nullptr) { - return FileResult::error(FudStatus::NullPointer); - } - if (!filename.nullTerminated()) { return FileResult::error(FudStatus::ArgumentInvalid); } @@ -115,6 +110,8 @@ FileResult RegularFile::open( return FileResult::error(FudStatus::ObjectInvalid); } + file.m_openFlags = flags; + return FileResult::okay(std::move(file)); } @@ -124,13 +121,8 @@ FileResult RegularFile::create( OpenFlags flags, Permissions permissions, bool exclusive, - Option<int> dirFdOption, - Allocator* allocator) + Option<int> dirFdOption) { - if (allocator == nullptr) { - return FileResult::error(FudStatus::NullPointer); - } - if (!filename.nullTerminated()) { return FileResult::error(FudStatus::ArgumentInvalid); } @@ -218,7 +210,7 @@ RegularFile::~RegularFile() static_cast<void>(this->close()); } -RegularFile::RegularFile(RegularFile&& rhs) noexcept : m_fd{rhs.m_fd}, m_modeFlags{rhs.m_modeFlags} +RegularFile::RegularFile(RegularFile&& rhs) noexcept : m_fd{rhs.m_fd}, m_openFlags{rhs.m_openFlags}, m_modeFlags{rhs.m_modeFlags} { rhs.m_fd = -1; } @@ -232,6 +224,7 @@ RegularFile& RegularFile::operator=(RegularFile&& rhs) noexcept static_cast<void>(this->close()); m_fd = rhs.m_fd; + m_openFlags = rhs.m_openFlags; m_modeFlags = rhs.m_modeFlags; rhs.m_fd = -1; @@ -250,6 +243,7 @@ FudStatus RegularFile::take(RegularFile& rhs) } m_fd = rhs.m_fd; + m_openFlags = rhs.m_openFlags; m_modeFlags = rhs.m_modeFlags; rhs.m_fd = -1; diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 058c813..4a20630 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -52,9 +52,9 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) return StringResult::error(FudStatus::ArgumentInvalid); } - auto length = static_cast<size_t>(lenResult); + auto outputLength = static_cast<size_t>(lenResult); if constexpr (static_cast<size_t>(std::numeric_limits<ssize_t>::max()) > maxStringLength) { - if (length > maxStringLength) { + if (outputLength > maxStringLength) { return StringResult::error(FudStatus::ArgumentInvalid); } } @@ -62,30 +62,21 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) String output{}; output.m_allocator = reinterpret_cast<uintptr_t>(allocator); - utf8* data{nullptr}; - size_t capacity = length + 1; - bool isLarge = capacity > SsoBufSize; + utf8* outputData{nullptr}; + size_t outputCapacity = outputLength + 1; + bool isLarge = outputCapacity > SsoBufSize; if (isLarge) { - output.m_repr.large.capacity = capacity; - output.m_repr.large.length = length; - auto dataResult = output.allocator()->allocate(capacity); - if (dataResult.isError()) { - return StringResult::error(dataResult.getError()); + auto status = output.makeLarge(outputCapacity, outputLength, outputData); + if (status != FudStatus::Success) { + return StringResult::error(status); } - output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay()); - data = output.m_repr.large.data; - output.setLarge(); } else { - capacity = SsoBufSize; - static_assert(SsoBufSize < std::numeric_limits<int8_t>::max()); - output.m_repr.small.length = length & smallStringLengthMask; - data = output.m_repr.small.buffer.data(); - output.setSmall(); + output.makeSmall(outputCapacity, outputLength, outputData); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyStatus = copyMem(data, capacity, cString, length); + auto copyStatus = copyMem(outputData, outputCapacity, cString, outputLength); fudAssert(copyStatus == FudStatus::Success); auto terminateStatus = output.nullTerminate(); @@ -102,25 +93,20 @@ StringResult String::from(const String& rhs) String output{}; output.m_allocator = rhs.m_allocator; - utf8* data{nullptr}; - size_t capacity{0}; - size_t length{0}; + utf8* outputData{nullptr}; + size_t outputCapacity{0}; + size_t outputLength{0}; if (rhs.isLarge()) { - output.m_repr.large = rhs.m_repr.large; - output.m_repr.large.data = static_cast<utf8*>( - M_TakeOrReturn(StringResult, output.allocator()->allocate(output.m_repr.large.capacity))); - data = output.m_repr.large.data; - capacity = output.m_repr.large.capacity; - length = output.m_repr.large.length; + auto status = output.makeLarge(outputCapacity, outputLength, outputData); + if (status != FudStatus::Success) { + return StringResult::error(status); + } } else { - output.m_repr.small = rhs.m_repr.small; - data = output.m_repr.small.buffer.data(); - capacity = SsoBufSize; - length = output.m_repr.small.length; + output.makeSmall(outputCapacity, outputLength, outputData); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyResult = copyMem(output.dataMut(), capacity, rhs.data(), length); + auto copyResult = copyMem(output.dataMut(), outputCapacity, rhs.data(), outputLength); fudAssert(copyResult == FudStatus::Success); auto nullTerminateStatus = output.nullTerminate(); @@ -193,28 +179,29 @@ FudStatus String::copy(const String& rhs) m_allocator = rhs.m_allocator; m_repr = rhs.m_repr; - utf8* data{nullptr}; - size_t capacity{}; - size_t length{}; + utf8* outputData{nullptr}; + size_t outputCapacity{}; + size_t outputLength{}; if (isLarge()) { auto allocResult = allocator()->allocate(m_repr.large.capacity); if (allocResult.isError()) { return allocResult.takeError(); } - m_repr.large.data = static_cast<utf8*>(allocResult.takeOkay()); - capacity = m_repr.large.capacity; - length = m_repr.large.length; - data = m_repr.large.data; + m_repr.large.data = reinterpret_cast<utf8*>(allocResult.takeOkay()); + outputCapacity = m_repr.large.capacity; + outputLength = m_repr.large.length; + outputData = m_repr.large.data; + } else { - capacity = SsoBufSize; - length = m_repr.small.length; - data = m_repr.small.buffer.data(); + outputCapacity = SsoBufSize; + outputLength = m_repr.small.length; + outputData = m_repr.small.buffer.data(); } - fudAssert(data != nullptr); + fudAssert(outputData != nullptr); - auto copyResult = copyMem(dataMut(), capacity, rhs.data(), length); + auto copyResult = copyMem(dataMut(), outputCapacity, rhs.data(), outputLength); fudAssert(copyResult == FudStatus::Success); auto nullTerminateStatus = nullTerminate(); fudAssert(nullTerminateStatus == FudStatus::Success); @@ -243,7 +230,7 @@ void String::cleanup() { const auto* allocPtr = allocator(); if (isLarge() && m_repr.large.data != nullptr && allocPtr != nullptr) { - auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity); + auto deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); static_cast<void>(deallocStatus); m_repr.large.data = nullptr; } @@ -271,7 +258,7 @@ FudStatus String::resize(size_t newCapacity) auto copyResult = copyMem(dataMut(), temp.size(), temp.data(), len); fudAssert(copyResult == FudStatus::Success); - auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity); + auto deallocStatus = 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); @@ -284,7 +271,7 @@ FudStatus String::resize(size_t newCapacity) if (allocResult.isError()) { return allocResult.takeError(); } - auto* newData = static_cast<utf8*>(allocResult.takeOkay()); + auto* newData = reinterpret_cast<utf8*>(allocResult.takeOkay()); fudAssert(newData != nullptr); auto copyResult = copyMem(newData, newCapacity, data(), length()); @@ -292,7 +279,7 @@ FudStatus String::resize(size_t newCapacity) auto deallocStatus = FudStatus::Success; if (isLarge()) { - deallocStatus = allocator()->deallocate(dataMut(), m_repr.large.capacity); + deallocStatus = allocator()->deallocate(reinterpret_cast<std::byte*>(m_repr.large.data), m_repr.large.capacity); } size_t len = length(); @@ -569,7 +556,7 @@ DrainResult String::drain(StringView source) auto status = copyMem(destPtr, remainingLength(), firstPart.m_data, firstPart.m_length); fudAssert(status == FudStatus::Success); addToLength(firstPart.m_length); - result.bytesWritten += firstPart.m_length; + result.bytesDrained += firstPart.m_length; status = nullTerminate(); source.advanceUnsafe(firstPart.m_length); fudAssert(status == FudStatus::Success); @@ -603,7 +590,7 @@ DrainResult String::drain(StringView source) auto* destPtr = dataMut() + length(); result.status = copyMem(destPtr, remainingLength(), source.data(), source.length()); fudAssert(result.status == FudStatus::Success); - result.bytesWritten += source.length(); + result.bytesDrained += source.length(); setLength(newLength); result.status = nullTerminate(); @@ -767,7 +754,7 @@ FudStatus String::makeLarge(size_t cap, size_t len, utf8*& outputData) if (dataResult.isError()) { return dataResult.getError(); } - m_repr.large.data = static_cast<utf8*>(dataResult.getOkay()); + m_repr.large.data = reinterpret_cast<utf8*>(dataResult.getOkay()); outputData = m_repr.large.data; setLarge(); return FudStatus::Success; |