File: memory_pool.h

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (272 lines) | stat: -rw-r--r-- 10,004 bytes parent folder | download | duplicates (4)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
    Copyright (c) 2005-2024 Intel Corporation

    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 __TBB_memory_pool_H
#define __TBB_memory_pool_H

#if !TBB_PREVIEW_MEMORY_POOL
#error Set TBB_PREVIEW_MEMORY_POOL to include memory_pool.h
#endif
/** @file */

#include "scalable_allocator.h"

#include <new> // std::bad_alloc
#include <stdexcept> // std::runtime_error, std::invalid_argument
#include <utility> // std::forward


#if __TBB_EXTRA_DEBUG
#define __TBBMALLOC_ASSERT ASSERT
#else
#define __TBBMALLOC_ASSERT(a,b) ((void)0)
#endif

namespace tbb {
namespace detail {
namespace d1 {

//! Base of thread-safe pool allocator for variable-size requests
class pool_base : no_copy {
    // Pool interface is separate from standard allocator classes because it has
    // to maintain internal state, no copy or assignment. Move and swap are possible.
public:
    //! Reset pool to reuse its memory (free all objects at once)
    void recycle() { rml::pool_reset(my_pool); }

    //! The "malloc" analogue to allocate block of memory of size bytes
    void *malloc(size_t size) { return rml::pool_malloc(my_pool, size); }

    //! The "free" analogue to discard a previously allocated piece of memory.
    void free(void* ptr) { rml::pool_free(my_pool, ptr); }

    //! The "realloc" analogue complementing pool_malloc.
    // Enables some low-level optimization possibilities
    void *realloc(void* ptr, size_t size) {
        return rml::pool_realloc(my_pool, ptr, size);
    }

protected:
    //! destroy pool - must be called in a child class
    void destroy() { rml::pool_destroy(my_pool); }

    rml::MemoryPool *my_pool;
};

#if _MSC_VER && !defined(__INTEL_COMPILER)
    // Workaround for erroneous "unreferenced parameter" warning in method destroy.
    #pragma warning (push)
    #pragma warning (disable: 4100)
#endif

//! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
/** @ingroup memory_allocation */
template<typename T, typename P = pool_base>
class memory_pool_allocator {
protected:
    typedef P pool_type;
    pool_type *my_pool;
    template<typename U, typename R>
    friend class memory_pool_allocator;
    template<typename V, typename U, typename R>
    friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
    template<typename V, typename U, typename R>
    friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
public:
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    template<typename U> struct rebind {
        typedef memory_pool_allocator<U, P> other;
    };

    explicit memory_pool_allocator(pool_type &pool) noexcept : my_pool(&pool) {}
    memory_pool_allocator(const memory_pool_allocator& src) noexcept : my_pool(src.my_pool) {}
    template<typename U>
    memory_pool_allocator(const memory_pool_allocator<U,P>& src) noexcept : my_pool(src.my_pool) {}

    pointer address(reference x) const { return &x; }
    const_pointer address(const_reference x) const { return &x; }

    //! Allocate space for n objects.
    pointer allocate( size_type n, const void* /*hint*/ = nullptr) {
        pointer p = static_cast<pointer>( my_pool->malloc( n*sizeof(value_type) ) );
        if (!p)
            throw_exception(std::bad_alloc());
        return p;
    }
    //! Free previously allocated block of memory.
    void deallocate( pointer p, size_type ) {
        my_pool->free(p);
    }
    //! Largest value for which method allocate might succeed.
    size_type max_size() const noexcept {
        size_type max = static_cast<size_type>(-1) / sizeof (value_type);
        return (max > 0 ? max : 1);
    }
    //! Copy-construct value at location pointed to by p.

    template<typename U, typename... Args>
    void construct(U *p, Args&&... args)
        { ::new((void *)p) U(std::forward<Args>(args)...); }

    //! Destroy value at location pointed to by p.
    void destroy( pointer p ) { p->~value_type(); }

};

#if _MSC_VER && !defined(__INTEL_COMPILER)
    #pragma warning (pop)
#endif // warning 4100 is back

//! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
/** @ingroup memory_allocation */
template<typename P>
class memory_pool_allocator<void, P> {
public:
    typedef P pool_type;
    typedef void* pointer;
    typedef const void* const_pointer;
    typedef void value_type;
    template<typename U> struct rebind {
        typedef memory_pool_allocator<U, P> other;
    };

    explicit memory_pool_allocator( pool_type &pool) noexcept : my_pool(&pool) {}
    memory_pool_allocator( const memory_pool_allocator& src) noexcept : my_pool(src.my_pool) {}
    template<typename U>
    memory_pool_allocator(const memory_pool_allocator<U,P>& src) noexcept : my_pool(src.my_pool) {}

protected:
    pool_type *my_pool;
    template<typename U, typename R>
    friend class memory_pool_allocator;
    template<typename V, typename U, typename R>
    friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
    template<typename V, typename U, typename R>
    friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
};

template<typename T, typename U, typename P>
inline bool operator==( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool==b.my_pool;}

template<typename T, typename U, typename P>
inline bool operator!=( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool!=b.my_pool;}

//! Thread-safe growable pool allocator for variable-size requests
template <typename Alloc>
class memory_pool : public pool_base {
    Alloc my_alloc; // TODO: base-class optimization
    static void *allocate_request(intptr_t pool_id, size_t & bytes);
    static int deallocate_request(intptr_t pool_id, void*, size_t raw_bytes);

public:
    //! construct pool with underlying allocator
    explicit memory_pool(const Alloc &src = Alloc());

    //! destroy pool
    ~memory_pool() { destroy(); } // call the callbacks first and destroy my_alloc latter
};

class fixed_pool : public pool_base {
    void *my_buffer;
    size_t my_size;
    inline static void *allocate_request(intptr_t pool_id, size_t & bytes);

public:
    //! construct pool with underlying allocator
    inline fixed_pool(void *buf, size_t size);
    //! destroy pool
    ~fixed_pool() { destroy(); }
};

//////////////// Implementation ///////////////

template <typename Alloc>
memory_pool<Alloc>::memory_pool(const Alloc &src) : my_alloc(src) {
    rml::MemPoolPolicy args(allocate_request, deallocate_request,
                            sizeof(typename Alloc::value_type));
    rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
    if (res!=rml::POOL_OK)
        throw_exception(std::runtime_error("Can't create pool"));
}
template <typename Alloc>
void *memory_pool<Alloc>::allocate_request(intptr_t pool_id, size_t & bytes) {
    memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
    const size_t unit_size = sizeof(typename Alloc::value_type);
    __TBBMALLOC_ASSERT( 0 == bytes%unit_size, nullptr);
    void *ptr;
#if TBB_USE_EXCEPTIONS
    try {
#endif
        ptr = self.my_alloc.allocate( bytes/unit_size );
#if TBB_USE_EXCEPTIONS
    } catch(...) {
        return nullptr;
    }
#endif
    return ptr;
}
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
    // Workaround for erroneous "unreachable code" warning in the template below.
    // Specific for VC++ 17-18 compiler
    #pragma warning (push)
    #pragma warning (disable: 4702)
#endif
template <typename Alloc>
int memory_pool<Alloc>::deallocate_request(intptr_t pool_id, void* raw_ptr, size_t raw_bytes) {
    memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
    const size_t unit_size = sizeof(typename Alloc::value_type);
    __TBBMALLOC_ASSERT( 0 == raw_bytes%unit_size, nullptr);
    self.my_alloc.deallocate( static_cast<typename Alloc::value_type*>(raw_ptr), raw_bytes/unit_size );
    return 0;
}
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
    #pragma warning (pop)
#endif
inline fixed_pool::fixed_pool(void *buf, size_t size) : my_buffer(buf), my_size(size) {
    if (!buf || !size)
        // TODO: improve support for mode with exceptions disabled
        throw_exception(std::invalid_argument("Zero in parameter is invalid"));
    rml::MemPoolPolicy args(allocate_request, nullptr, size, /*fixedPool=*/true);
    rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
    if (res!=rml::POOL_OK)
        throw_exception(std::runtime_error("Can't create pool"));
}
inline void *fixed_pool::allocate_request(intptr_t pool_id, size_t & bytes) {
    fixed_pool &self = *reinterpret_cast<fixed_pool*>(pool_id);
    __TBBMALLOC_ASSERT(0 != self.my_size, "The buffer must not be used twice.");
    bytes = self.my_size;
    self.my_size = 0; // remember that buffer has been used
    return self.my_buffer;
}

} // namespace d1
} // namespace detail

inline namespace v1 {
using detail::d1::memory_pool_allocator;
using detail::d1::memory_pool;
using detail::d1::fixed_pool;
} // inline namepspace v1
} // namespace tbb

#undef __TBBMALLOC_ASSERT
#endif// __TBB_memory_pool_H