DART 6.13.2
Loading...
Searching...
No Matches
MemoryAllocator.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_MEMORYALLOCATOR_HPP_
34#define DART_COMMON_MEMORYALLOCATOR_HPP_
35
36#include <cstddef>
37#include <iostream>
38#include <string>
39
41
42namespace dart::common {
43
45class MemoryAllocator : public Castable<MemoryAllocator>
46{
47public:
50
52 MemoryAllocator() noexcept = default;
53
55 virtual ~MemoryAllocator() = default;
56
58 [[nodiscard]] virtual const std::string& getType() const = 0;
59
66 [[nodiscard]] virtual void* allocate(size_t bytes) noexcept = 0;
67 // TODO(JS): Make this constexpr once migrated to C++20
68
74 template <typename T>
75 [[nodiscard]] T* allocateAs(size_t n = 1) noexcept;
76
82 virtual void deallocate(void* pointer, size_t bytes) = 0;
83 // TODO(JS): Make this constexpr once migrated to C++20
84
92 template <typename T, typename... Args>
93 [[nodiscard]] T* construct(Args&&... args) noexcept;
94
95 template <typename T, typename... Args>
96 [[nodiscard]] T* constructAt(void* pointer, Args&&... args);
97
98 template <typename T, typename... Args>
99 [[nodiscard]] T* constructAt(T* pointer, Args&&... args);
100
102 template <typename T>
103 void destroy(T* object) noexcept;
104
106 virtual void print(std::ostream& os = std::cout, int indent = 0) const;
107
109 friend std::ostream& operator<<(
110 std::ostream& os, const MemoryAllocator& allocator);
111};
112
113} // namespace dart::common
114
115#include "dart/common/detail/MemoryAllocator-impl.hpp"
116
117#endif // DART_COMMON_MEMORYALLOCATOR_HPP_
A CRTP base class that provides an interface for easily casting to the derived types.
Definition Castable.hpp:58
Base class for std::allocator compatible allocators.
Definition MemoryAllocator.hpp:46
virtual void * allocate(size_t bytes) noexcept=0
Allocates size bytes of uninitialized storage.
virtual void print(std::ostream &os=std::cout, int indent=0) const
Prints state of the memory allocator.
Definition MemoryAllocator.cpp:48
void destroy(T *object) noexcept
Calls the destructor of the object and deallocate the storage.
Definition MemoryAllocator-impl.hpp:90
virtual void deallocate(void *pointer, size_t bytes)=0
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
T * constructAt(void *pointer, Args &&... args)
Definition MemoryAllocator-impl.hpp:74
MemoryAllocator() noexcept=default
Default constructor.
T * construct(Args &&... args) noexcept
Allocates uninitialized storage and constructs an object of type T to the allocated storage.
Definition MemoryAllocator-impl.hpp:49
static MemoryAllocator & GetDefault()
Returns the default memory allocator.
Definition MemoryAllocator.cpp:41
virtual const std::string & getType() const =0
Returns type string.
T * allocateAs(size_t n=1) noexcept
Allocates object(s) without calling the constructor.
Definition MemoryAllocator-impl.hpp:42
Definition Aspect.cpp:42