DART 6.13.2
Loading...
Searching...
No Matches
MemoryAllocator-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * 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
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_DETAIL_MEMORYALLOCATOR_HPP_
34#define DART_COMMON_DETAIL_MEMORYALLOCATOR_HPP_
35
37
38namespace dart::common {
39
40//==============================================================================
41template <typename T>
42T* MemoryAllocator::allocateAs(size_t n) noexcept
43{
44 return static_cast<T*>(allocate(n * sizeof(T)));
45}
46
47//==============================================================================
48template <typename T, typename... Args>
49T* MemoryAllocator::construct(Args&&... args) noexcept
50{
51 // Allocate new memory for a new object (without calling the constructor)
52 void* object = allocate(sizeof(T));
53 if (!object)
54 {
55 return nullptr;
56 }
57
58 // Call constructor. Return nullptr if failed.
59 try
60 {
61 new (object) T(std::forward<Args>(args)...);
62 }
63 catch (...)
64 {
65 deallocate(object, sizeof(T));
66 return nullptr;
67 }
68
69 return reinterpret_cast<T*>(object);
70}
71
72//==============================================================================
73template <typename T, typename... Args>
74T* MemoryAllocator::constructAt(void* pointer, Args&&... args)
75{
76 return ::new (const_cast<void*>(static_cast<const volatile void*>(pointer)))
77 T(std::forward<Args>(args)...);
78}
79
80//==============================================================================
81template <typename T, typename... Args>
82T* MemoryAllocator::constructAt(T* pointer, Args&&... args)
83{
84 return ::new (const_cast<void*>(static_cast<const volatile void*>(pointer)))
85 T(std::forward<Args>(args)...);
86}
87
88//==============================================================================
89template <typename T>
90void MemoryAllocator::destroy(T* object) noexcept
91{
92 if (!object)
93 {
94 return;
95 }
96 object->~T();
97 deallocate(object, sizeof(T));
98}
99
100} // namespace dart::common
101
102#endif // DART_COMMON_DETAIL_MEMORYALLOCATOR_HPP_
void destroy(T *object) noexcept
Calls the destructor of the object and deallocate the storage.
Definition MemoryAllocator-impl.hpp:90
T * constructAt(void *pointer, Args &&... args)
Definition MemoryAllocator-impl.hpp:74
T * construct(Args &&... args) noexcept
Allocates uninitialized storage and constructs an object of type T to the allocated storage.
Definition MemoryAllocator-impl.hpp:49
T * allocateAs(size_t n=1) noexcept
Allocates object(s) without calling the constructor.
Definition MemoryAllocator-impl.hpp:42
Definition Aspect.cpp:42