diff options
Diffstat (limited to 'include/fud_allocator.hpp')
-rw-r--r-- | include/fud_allocator.hpp | 70 |
1 files changed, 70 insertions, 0 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 |