|
| 1 | +/** |
| 2 | +Copyright (c) 2022, Philip Deegan. |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are |
| 7 | +met: |
| 8 | +
|
| 9 | + * Redistributions of source code must retain the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer. |
| 11 | + * Redistributions in binary form must reproduce the above |
| 12 | +copyright notice, this list of conditions and the following disclaimer |
| 13 | +in the documentation and/or other materials provided with the |
| 14 | +distribution. |
| 15 | + * Neither the name of Philip Deegan nor the names of its |
| 16 | +contributors may be used to endorse or promote products derived from |
| 17 | +this software without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +*/ |
| 31 | +#ifndef _MKN_KUL_ALLOC_BASE_HPP_ |
| 32 | +#define _MKN_KUL_ALLOC_BASE_HPP_ |
| 33 | + |
| 34 | +#include <new> |
| 35 | +#include <cstdint> |
| 36 | +#include <cstddef> |
| 37 | +#include <cstdlib> |
| 38 | +#include <type_traits> |
| 39 | + |
| 40 | +namespace mkn::kul { |
| 41 | + |
| 42 | +template <typename T> |
| 43 | +class Allocator { |
| 44 | + using This = Allocator<T>; |
| 45 | + |
| 46 | + public: |
| 47 | + using pointer = T*; |
| 48 | + using reference = T&; |
| 49 | + using value_type = T; |
| 50 | + using size_type = std::size_t; |
| 51 | + using difference_type = std::ptrdiff_t; |
| 52 | + |
| 53 | + template <typename U> |
| 54 | + struct rebind { |
| 55 | + using other = Allocator<U>; |
| 56 | + }; |
| 57 | + |
| 58 | + T* allocate(std::size_t const n) const { return static_cast<T*>(::operator new(n * sizeof(T))); } |
| 59 | + |
| 60 | + void deallocate(T* const p) noexcept { |
| 61 | + if (p) ::operator delete(p); |
| 62 | + } |
| 63 | + void deallocate(T* const p, std::size_t /*n*/) noexcept { // needed from std:: |
| 64 | + if (p) ::operator delete(p); |
| 65 | + } |
| 66 | + bool operator!=(This const& that) const { return !(*this == that); } |
| 67 | + bool operator==(This const& /*that*/) const { |
| 68 | + return true; // stateless |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +template <typename T> |
| 73 | +class NonConstructingAllocator : public Allocator<T> { |
| 74 | + using This = NonConstructingAllocator<T>; |
| 75 | + |
| 76 | + public: |
| 77 | + template <typename U> |
| 78 | + struct rebind { |
| 79 | + using other = NonConstructingAllocator<U>; |
| 80 | + }; |
| 81 | + |
| 82 | + T* allocate(std::size_t const n) const { |
| 83 | + // if (n == 0) return nullptr; |
| 84 | + return static_cast<T*>(malloc(n * sizeof(T))); |
| 85 | + } |
| 86 | + |
| 87 | + template <typename U, typename... Args> |
| 88 | + void construct(U* ptr, Args&&... args) { |
| 89 | + ::new ((void*)ptr) U(std::forward<Args>(args)...); |
| 90 | + } |
| 91 | + |
| 92 | + template <typename U> |
| 93 | + void construct(U* /*ptr*/) noexcept(std::is_nothrow_default_constructible<U>::value) {} |
| 94 | + |
| 95 | + bool operator!=(This const& that) const { return !(*this == that); } |
| 96 | + bool operator==(This const& /*that*/) const { |
| 97 | + return true; // stateless |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace mkn::kul |
| 102 | + |
| 103 | +#endif /*_MKN_KUL_ALLOC_BASE_HPP_*/ |
0 commit comments