blob: a904f34f97e8dba1f64cec91b313420ea9150ae9 [file] [log] [blame]
Matthew Bentham758b5ba2020-03-05 23:37:48 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Matthew Bentham758b5ba2020-03-05 23:37:48 +00003 *
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
29namespace arm_compute
30{
31namespace support
32{
33namespace cpp11
34{
35// std::align is missing in GCC 4.9
36// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
37inline 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
52namespace cpp14
53{
54/** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */
55
56/**<Template for single object */
57template <class T>
58struct _Unique_if
59{
60 typedef std::unique_ptr<T> _Single_object; /**< Single object type */
61};
62
63/** Template for array */
64template <class T>
65struct _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 */
74template <class T, size_t N>
75struct _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 */
86template <class T, class... Args>
87typename _Unique_if<T>::_Single_object
88make_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 */
99template <class T>
100typename _Unique_if<T>::_Unknown_bound
101make_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. */
108template <class T, class... Args>
109typename _Unique_if<T>::_Known_bound
110make_unique(Args &&...) = delete;
111} // namespace cpp14
112} // namespace support
113} // namespace arm_compute
114#endif /* ARM_COMPUTE_SUPPORT_MEMORYSUPPORT */