File: test_memory_utils.h

package info (click to toggle)
stdgpu 1.3.0%2Bgit20220507.32e0517-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,528 kB
  • sloc: cpp: 7,818; pascal: 1,893; xml: 214; sh: 181; makefile: 50
file content (126 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  Copyright 2021 Patrick Stotko
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef TEST_MEMORY_UTILS_H
#define TEST_MEMORY_UTILS_H

#include <stdgpu/cstddef.h>
#include <stdgpu/memory.h>
#include <stdgpu/platform.h>

namespace test_utils
{
/**
 * \brief A statistics class for allocators
 */
struct allocator_statistics
{
    /**
     * \brief Resets the statistics
     */
    void
    reset();

    // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
    stdgpu::index_t default_constructions = 0; /**< The number of default constructions */
    // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
    stdgpu::index_t copy_constructions = 0; /**< The number of copy constructions */
    // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
    stdgpu::index_t destructions = 0; /**< The number of destructions */
};

/**
 * \brief Returns the global allocator statistics
 */
allocator_statistics&
get_allocator_statistics();

/**
 * \brief A test allocator for device memory
 * \tparam T A type
 */
template <typename T>
class test_device_allocator
{
public:
    using base_type = stdgpu::safe_device_allocator<T>; /**< stdgpu::safe_device_allocator<T> */
    using value_type = typename base_type::value_type;  /**< base_type::value_type */

    /**
     * \brief Default constructor
     */
    STDGPU_HOST_DEVICE
    test_device_allocator() noexcept;

    /**
     * \brief Destructor
     */
    STDGPU_HOST_DEVICE
    ~test_device_allocator() noexcept;

    /**
     * \brief Copy constructor
     * \param[in] other The allocator to be copied from
     */
    STDGPU_HOST_DEVICE
    test_device_allocator(const test_device_allocator& other) noexcept;

    /**
     * \brief Deleted copy assignment operator
     */
    test_device_allocator&
    operator=(const test_device_allocator&) = delete;

    /**
     * \brief Copy constructor
     * \tparam U Another type
     * \param[in] other The allocator to be copied from
     */
    template <typename U>
    explicit STDGPU_HOST_DEVICE
    test_device_allocator(const test_device_allocator<U>& other) noexcept;

    /**
     * \brief Deleted move constructor
     */
    test_device_allocator(test_device_allocator&&) = delete;

    /**
     * \brief Deleted move assignment operator
     */
    test_device_allocator&
    operator=(test_device_allocator&&) = delete;

    /**
     * \brief Allocates a memory block of the given size
     * \param[in] n The size of the memory block in bytes
     * \return A pointer to the allocated memory block
     */
    [[nodiscard]] T*
    allocate(stdgpu::index64_t n);

    /**
     * \brief Deallocates the given memory block
     * \param[in] p A pointer to the memory block
     * \param[in] n The size of the memory block in bytes (must match the size during allocation)
     */
    void
    deallocate(T* p, stdgpu::index64_t n);
};
} // namespace test_utils

#include <test_memory_utils_detail.h>

#endif // TEST_MEMORY_UTILS_H