C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
memory.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../compat.hpp"
26
27#include <cstddef>
28#include <limits>
29#include <memory>
30#include <new>
31
32extern "C" {
33ES_API(CPPESSENCE) void* es_alloc(std::size_t size) noexcept;
34ES_API(CPPESSENCE) void* es_aligned_alloc(std::size_t size, std::size_t alignment) noexcept;
35ES_API(CPPESSENCE) void es_dealloc(void* ptr, std::size_t size) noexcept;
36ES_API(CPPESSENCE) void es_aligned_dealloc(void* ptr, std::size_t size, std::size_t alignment) noexcept;
37}
38
39namespace essence::abi {
41 ES_API(CPPESSENCE) uniform_allocator_base() noexcept;
42 ES_API(CPPESSENCE) uniform_allocator_base(const uniform_allocator_base&) noexcept;
43 ES_API(CPPESSENCE) uniform_allocator_base(uniform_allocator_base&&) noexcept;
44 ES_API(CPPESSENCE) ~uniform_allocator_base();
45 ES_API(CPPESSENCE) uniform_allocator_base& operator=(const uniform_allocator_base&) noexcept;
46 ES_API(CPPESSENCE) uniform_allocator_base& operator=(uniform_allocator_base&&) noexcept;
47 [[nodiscard]] ES_API(CPPESSENCE) static void* allocate(std::size_t size, std::size_t alignment) noexcept;
48 ES_API(CPPESSENCE) static void deallocate(void* ptr, std::size_t size, std::size_t alignment) noexcept;
49 };
50
51 template <typename T>
53 using value_type = T;
54 using size_type = std::size_t;
55 using difference_type = std::ptrdiff_t;
56 using propagate_on_container_move_assignment = std::true_type;
57
58 constexpr uniform_allocator() noexcept = default;
59
60 constexpr uniform_allocator(const uniform_allocator&) noexcept = default;
61
62 constexpr uniform_allocator(uniform_allocator&&) noexcept = default;
63
64 template <typename U>
65 constexpr uniform_allocator(const uniform_allocator<U>&) noexcept {}
66
67 ~uniform_allocator() = default;
68
69 uniform_allocator& operator=(const uniform_allocator&) noexcept = default;
70
71 uniform_allocator& operator=(uniform_allocator&&) noexcept = default;
72
73 [[nodiscard]] static T* allocate(std::size_t size) {
74 if (size > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
75 throw std::bad_array_new_length{};
76 }
77
78 auto uninitialized_ptr = uniform_allocator_base::allocate(size * sizeof(T), alignof(T));
79
80 if (uninitialized_ptr == nullptr) {
81 throw std::bad_alloc{};
82 }
83
84 return static_cast<T*>(uninitialized_ptr);
85 }
86
87 static void deallocate(T* ptr, std::size_t size) noexcept {
88 uniform_allocator_base::deallocate(ptr, size * sizeof(T), alignof(T));
89 }
90
91 constexpr bool operator==(const uniform_allocator& right) const noexcept {
92 return true;
93 }
94
95 constexpr bool operator!=(const uniform_allocator& right) const noexcept {
96 return false;
97 }
98 };
99} // namespace essence::abi
Definition memory.hpp:40
Definition memory.hpp:52