Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_SUPPORT_MEMORYSUPPORT |
| 25 | #define ARM_COMPUTE_SUPPORT_MEMORYSUPPORT |
| 26 | |
| 27 | #include <memory> |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace support |
| 32 | { |
| 33 | namespace cpp11 |
| 34 | { |
| 35 | // std::align is missing in GCC 4.9 |
| 36 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350 |
| 37 | inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space) |
| 38 | { |
| 39 | std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr); |
| 40 | std::uintptr_t aligned = (pn + alignment - 1) & -alignment; |
| 41 | std::size_t padding = aligned - pn; |
| 42 | if(space < size + padding) |
| 43 | { |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | space -= padding; |
| 48 | |
| 49 | return ptr = reinterpret_cast<void *>(aligned); |
| 50 | } |
| 51 | } //namespace cpp11 |
| 52 | namespace cpp14 |
| 53 | { |
| 54 | /** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */ |
| 55 | |
| 56 | /**<Template for single object */ |
| 57 | template <class T> |
| 58 | struct _Unique_if |
| 59 | { |
| 60 | typedef std::unique_ptr<T> _Single_object; /**< Single object type */ |
| 61 | }; |
| 62 | |
| 63 | /** Template for array */ |
| 64 | template <class T> |
| 65 | struct _Unique_if<T[]> |
| 66 | { |
| 67 | typedef std::unique_ptr<T[]> _Unknown_bound; /**< Array type */ |
| 68 | }; |
| 69 | |
| 70 | /** Template for array with known bounds (to throw an error). |
| 71 | * |
| 72 | * @note this is intended to never be hit. |
| 73 | */ |
| 74 | template <class T, size_t N> |
| 75 | struct _Unique_if<T[N]> |
| 76 | { |
| 77 | typedef void _Known_bound; /**< Should never be used */ |
| 78 | }; |
| 79 | |
| 80 | /** Construct a single object and return a unique pointer to it. |
| 81 | * |
| 82 | * @param[in] args Constructor arguments. |
| 83 | * |
| 84 | * @return a unique pointer to the new object. |
| 85 | */ |
| 86 | template <class T, class... Args> |
| 87 | typename _Unique_if<T>::_Single_object |
| 88 | make_unique(Args &&... args) |
| 89 | { |
| 90 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 91 | } |
| 92 | |
| 93 | /** Construct an array of objects and return a unique pointer to it. |
| 94 | * |
| 95 | * @param[in] n Array size |
| 96 | * |
| 97 | * @return a unique pointer to the new array. |
| 98 | */ |
| 99 | template <class T> |
| 100 | typename _Unique_if<T>::_Unknown_bound |
| 101 | make_unique(size_t n) |
| 102 | { |
| 103 | typedef typename std::remove_extent<T>::type U; |
| 104 | return std::unique_ptr<T>(new U[n]()); |
| 105 | } |
| 106 | |
| 107 | /** It is invalid to attempt to make_unique an array with known bounds. */ |
| 108 | template <class T, class... Args> |
| 109 | typename _Unique_if<T>::_Known_bound |
| 110 | make_unique(Args &&...) = delete; |
| 111 | } // namespace cpp14 |
| 112 | } // namespace support |
| 113 | } // namespace arm_compute |
| 114 | #endif /* ARM_COMPUTE_SUPPORT_MEMORYSUPPORT */ |