diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/fud_allocator.hpp | 70 | ||||
-rw-r--r-- | include/fud_directory.hpp | 11 | ||||
-rw-r--r-- | include/fud_memory.hpp | 10 | ||||
-rw-r--r-- | include/fud_result.hpp | 1 | ||||
-rw-r--r-- | include/fud_status.hpp | 3 | ||||
-rw-r--r-- | include/fud_string.hpp | 43 |
6 files changed, 109 insertions, 29 deletions
diff --git a/include/fud_allocator.hpp b/include/fud_allocator.hpp new file mode 100644 index 0000000..8955fae --- /dev/null +++ b/include/fud_allocator.hpp @@ -0,0 +1,70 @@ +/* + * libfud + * Copyright 2024 Dominick Allen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FUD_ALLOCATOR_HPP +#define FUD_ALLOCATOR_HPP + +#include "fud_result.hpp" +#include "fud_status.hpp" + +#include <cstddef> +#include <limits> + +namespace fud { + +class Allocator { + public: + virtual ~Allocator() = default; + + virtual Result<void*, FudStatus> allocate(size_t bytes, size_t alignment = alignof(std::max_align_t)) = 0; + + /* ...should this be void? */ + virtual void deallocate(void* pointer, size_t bytes, size_t alignment = alignof(std::max_align_t)) = 0; + + virtual bool isEqual(const Allocator& rhs) const = 0; +}; + +constexpr bool operator==(const Allocator& lhs, const Allocator& rhs) { + return &lhs == &rhs; +} + +class FudAllocator : public Allocator { + public: + virtual ~FudAllocator() override = default; + + virtual Result<void*, FudStatus> allocate(size_t bytes, size_t alignment = alignof(std::max_align_t)) override; + + /* ...should this be void? */ + virtual void deallocate(void* pointer, size_t bytes, size_t alignment = alignof(std::max_align_t)) override; + + virtual bool isEqual(const Allocator& rhs) const override; +}; + +extern FudAllocator globalFudAllocator; + +/** \brief The default allocation function for globalFudAllocator. */ +extern void* fudAlloc(size_t size); + +/** \brief The default allocation function for globalFudAllocator. */ +extern void* fudRealloc(void* ptr, size_t size); + +extern void fudFree(void* ptr); + + +} // namespace fud + +#endif diff --git a/include/fud_directory.hpp b/include/fud_directory.hpp index ca94528..d2bd53d 100644 --- a/include/fud_directory.hpp +++ b/include/fud_directory.hpp @@ -91,7 +91,8 @@ struct DirectoryEntry { class Directory { public: - explicit Directory(const String& name); + // explicit Directory(const String& name); + static Result<Directory, FudStatus> make(const String& name); Directory(const Directory& rhs) = delete; Directory(Directory&& rhs) noexcept; ~Directory(); @@ -102,11 +103,6 @@ class Directory { return m_name; } - constexpr FudStatus status() const - { - return m_status; - } - constexpr int errorCode() const { return m_errorCode; @@ -119,11 +115,10 @@ class Directory { FudStatus reset(); private: - bool valid() const; + Directory() = default; String m_name{}; DIR* m_directory{nullptr}; - FudStatus m_status{FudStatus::ObjectInvalid}; int m_errorCode{-1}; int m_dirFd{-1}; }; diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index 62ff81a..97328a9 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -27,16 +27,6 @@ namespace fud { -extern void* fudAlloc(size_t size); -extern void* fudRealloc(void* ptr, size_t size); -extern void fudFree(void* ptr); - -// An allocating function which returns null on failure. -using FudAllocOne = void(*)(size_t); - -// An allocating function which returns null on failure. -using FudAllocMany = void(*)(size_t, size_t); - /** \brief Copies from source to destination count bytes. * * \retcode FudStatus::Success diff --git a/include/fud_result.hpp b/include/fud_result.hpp index 3acf776..5dabaf5 100644 --- a/include/fud_result.hpp +++ b/include/fud_result.hpp @@ -27,6 +27,7 @@ template <typename T, typename E> class [[nodiscard]] Result { public: using ResultType = Result<T, E>; + static ResultType okay(const T& okay) { return ResultType{okay}; diff --git a/include/fud_status.hpp b/include/fud_status.hpp index f8f7687..5ebf229 100644 --- a/include/fud_status.hpp +++ b/include/fud_status.hpp @@ -38,6 +38,7 @@ enum class [[nodiscard]] FudStatus Full, RangeError, VariantInvalid, + BadArrayLength, NotImplemented, NotSupported }; @@ -77,6 +78,8 @@ constexpr const char* FudStatusToString(FudStatus status) return "RangeError"; case FudStatus::VariantInvalid: return "VariantInvalid"; + case FudStatus::BadArrayLength: + return "BadArrayLength"; case FudStatus::NotImplemented: return "NotImplemented"; case FudStatus::NotSupported: diff --git a/include/fud_string.hpp b/include/fud_string.hpp index 6880cb7..4367aae 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -18,6 +18,7 @@ #ifndef FUD_STRING_HPP #define FUD_STRING_HPP +#include "fud_allocator.hpp" #include "fud_assert.hpp" #include "fud_result.hpp" #include "fud_status.hpp" @@ -40,18 +41,25 @@ class String { public: static StringResult makeFromCString(const char* cString); - static StringResult makeFromUtf8(const utf8* utf8String); + static StringResult makeFromCString(const char* cString, Allocator* allocator); - template <typename... Strings> - static StringResult makeFromCString(Strings... cStrings) + template <typename... CStrings> + static StringResult makeFromCStrings(CStrings... cStrings) { + return makeFromCStringsAlloc(&globalFudAllocator, cStrings...); + } + + template <typename... CStrings> + static StringResult makeFromCStringsAlloc(Allocator* allocator, CStrings... cStrings) + { + if (allocator == nullptr) { + return StringResult::error(FudStatus::NullPointer); + } size_t totalLength = 0; Array<size_t, sizeof...(cStrings)> lengths{}; Array<const char*, sizeof...(cStrings)> strPointers{}; size_t index = 0; for (const auto* cStringItem: {cStrings...}) { - // for (size_t index = 0; index < strPointers.size(); ++index) { - // const auto* cString = strPointers[index]; const char* cString = nullptr; if constexpr (std::is_same_v<decltype(cStringItem), const char*>) { cString = cStringItem; @@ -78,13 +86,14 @@ class String { String output{}; auto* data = output.m_buffer.data(); output.m_length = totalLength; + output.m_allocator = allocator; if (output.m_length >= output.m_capacity) { output.m_capacity = output.m_length + 1; - data = static_cast<utf8*>(fudAlloc(output.m_capacity)); - if (data == nullptr) { - return StringResult::error(FudStatus::AllocFailure); + auto dataResult = output.m_allocator->allocate(output.m_capacity); + if (dataResult.isError()) { + return StringResult::error(dataResult.getError()); } - output.m_data = data; + output.m_data = static_cast<utf8*>(dataResult.getOkay()); } size_t cumulativeLength = 0; @@ -102,14 +111,21 @@ class String { } String() noexcept = default; - String(const String& rhs); + + String(const String& rhs) = delete; + String(String&& rhs) noexcept; ~String(); - String& operator=(const String& rhs); + String& operator=(const String& rhs) = delete; + String& operator=(String&& rhs) noexcept; + static StringResult from(const String& rhs); + + FudStatus copy(const String& rhs); + [[nodiscard]] constexpr size_t length() const { return m_length; @@ -206,6 +222,7 @@ class String { private: void cleanup(); + FudStatus resize(size_t newCapacity); using BufType = Array<utf8, SSO_BUF_SIZE>; @@ -213,13 +230,17 @@ class String { BufType m_buffer{BufType::constFill(0)}; utf8* m_data; }; + size_t m_length{0}; + size_t m_capacity{SSO_BUF_SIZE}; [[nodiscard]] constexpr bool isLarge() const { return m_capacity > SSO_BUF_SIZE; } + + Allocator* m_allocator{&globalFudAllocator}; }; } // namespace fud |