File: scalable_allocator.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 (335 lines) | stat: -rw-r--r-- 11,707 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
    Copyright (c) 2005-2023 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_scalable_allocator_H
#define __TBB_scalable_allocator_H

#ifdef __cplusplus
#include "oneapi/tbb/detail/_config.h"
#include "oneapi/tbb/detail/_utils.h"
#include "oneapi/tbb/detail/_namespace_injection.h"
#include <cstdlib>
#include <utility>
#include <new> /* std::bad_alloc() */
#else
#include "oneapi/tbb/detail/_export.h"
#include <stddef.h> /* Need ptrdiff_t and size_t from here. */
#if !defined(_MSC_VER) || defined(__clang__)
#include <stdint.h> /* Need intptr_t from here. */
#endif
#endif

#if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
#include <memory_resource>
#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#if _MSC_VER
    #define __TBB_EXPORTED_FUNC __cdecl
#else
    #define __TBB_EXPORTED_FUNC
#endif

/** The "malloc" analogue to allocate block of memory of size bytes.
  * @ingroup memory_allocation */
TBBMALLOC_EXPORT void* __TBB_EXPORTED_FUNC scalable_malloc(size_t size);

/** The "free" analogue to discard a previously allocated piece of memory.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void   __TBB_EXPORTED_FUNC scalable_free(void* ptr);

/** The "realloc" analogue complementing scalable_malloc.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void* __TBB_EXPORTED_FUNC scalable_realloc(void* ptr, size_t size);

/** The "calloc" analogue complementing scalable_malloc.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void* __TBB_EXPORTED_FUNC scalable_calloc(size_t nobj, size_t size);

/** The "posix_memalign" analogue.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT int __TBB_EXPORTED_FUNC scalable_posix_memalign(void** memptr, size_t alignment, size_t size);

/** The "_aligned_malloc" analogue.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void* __TBB_EXPORTED_FUNC scalable_aligned_malloc(size_t size, size_t alignment);

/** The "_aligned_realloc" analogue.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void* __TBB_EXPORTED_FUNC scalable_aligned_realloc(void* ptr, size_t size, size_t alignment);

/** The "_aligned_free" analogue.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT void __TBB_EXPORTED_FUNC scalable_aligned_free(void* ptr);

/** The analogue of _msize/malloc_size/malloc_usable_size.
    Returns the usable size of a memory block previously allocated by scalable_*,
    or 0 (zero) if ptr does not point to such a block.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT size_t __TBB_EXPORTED_FUNC scalable_msize(void* ptr);

/* Results for scalable_allocation_* functions */
typedef enum {
    TBBMALLOC_OK,
    TBBMALLOC_INVALID_PARAM,
    TBBMALLOC_UNSUPPORTED,
    TBBMALLOC_NO_MEMORY,
    TBBMALLOC_NO_EFFECT
} ScalableAllocationResult;

/* Setting TBB_MALLOC_USE_HUGE_PAGES environment variable to 1 enables huge pages.
   scalable_allocation_mode call has priority over environment variable. */
typedef enum {
    TBBMALLOC_USE_HUGE_PAGES,  /* value turns using huge pages on and off */
    /* deprecated, kept for backward compatibility only */
    USE_HUGE_PAGES = TBBMALLOC_USE_HUGE_PAGES,
    /* try to limit memory consumption value (Bytes), clean internal buffers
       if limit is exceeded, but not prevents from requesting memory from OS */
    TBBMALLOC_SET_SOFT_HEAP_LIMIT,
    /* Lower bound for the size (Bytes), that is interpreted as huge
     * and not released during regular cleanup operations. */
    TBBMALLOC_SET_HUGE_SIZE_THRESHOLD
} AllocationModeParam;

/** Set TBB allocator-specific allocation modes.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value);

typedef enum {
    /* Clean internal allocator buffers for all threads.
       Returns TBBMALLOC_NO_EFFECT if no buffers cleaned,
       TBBMALLOC_OK if some memory released from buffers. */
    TBBMALLOC_CLEAN_ALL_BUFFERS,
    /* Clean internal allocator buffer for current thread only.
       Return values same as for TBBMALLOC_CLEAN_ALL_BUFFERS. */
    TBBMALLOC_CLEAN_THREAD_BUFFERS
} ScalableAllocationCmd;

/** Call TBB allocator-specific commands.
    @ingroup memory_allocation */
TBBMALLOC_EXPORT int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#ifdef __cplusplus

//! The namespace rml contains components of low-level memory pool interface.
namespace rml {
class MemoryPool;

typedef void *(*rawAllocType)(std::intptr_t pool_id, std::size_t &bytes);
// returns non-zero in case of error
typedef int   (*rawFreeType)(std::intptr_t pool_id, void* raw_ptr, std::size_t raw_bytes);

struct MemPoolPolicy {
    enum {
        TBBMALLOC_POOL_VERSION = 1
    };

    rawAllocType pAlloc;
    rawFreeType  pFree;
                 // granularity of pAlloc allocations. 0 means default used.
    std::size_t  granularity;
    int          version;
                 // all memory consumed at 1st pAlloc call and never returned,
                 // no more pAlloc calls after 1st
    unsigned     fixedPool : 1,
                 // memory consumed but returned only at pool termination
                 keepAllMemory : 1,
                 reserved : 30;

    MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
                  std::size_t granularity_ = 0, bool fixedPool_ = false,
                  bool keepAllMemory_ = false) :
        pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(TBBMALLOC_POOL_VERSION),
        fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
        reserved(0) {}
};

// enums have same values as appropriate enums from ScalableAllocationResult
// TODO: use ScalableAllocationResult in pool_create directly
enum MemPoolError {
    // pool created successfully
    POOL_OK = TBBMALLOC_OK,
    // invalid policy parameters found
    INVALID_POLICY = TBBMALLOC_INVALID_PARAM,
     // requested pool policy is not supported by allocator library
    UNSUPPORTED_POLICY = TBBMALLOC_UNSUPPORTED,
    // lack of memory during pool creation
    NO_MEMORY = TBBMALLOC_NO_MEMORY,
    // action takes no effect
    NO_EFFECT = TBBMALLOC_NO_EFFECT
};

TBBMALLOC_EXPORT MemPoolError pool_create_v1(std::intptr_t pool_id, const MemPoolPolicy *policy,
                            rml::MemoryPool **pool);

TBBMALLOC_EXPORT bool  pool_destroy(MemoryPool* memPool);
TBBMALLOC_EXPORT void *pool_malloc(MemoryPool* memPool, std::size_t size);
TBBMALLOC_EXPORT void *pool_realloc(MemoryPool* memPool, void *object, std::size_t size);
TBBMALLOC_EXPORT void *pool_aligned_malloc(MemoryPool* mPool, std::size_t size, std::size_t alignment);
TBBMALLOC_EXPORT void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, std::size_t size, std::size_t alignment);
TBBMALLOC_EXPORT bool  pool_reset(MemoryPool* memPool);
TBBMALLOC_EXPORT bool  pool_free(MemoryPool *memPool, void *object);
TBBMALLOC_EXPORT MemoryPool *pool_identify(void *object);
TBBMALLOC_EXPORT std::size_t pool_msize(MemoryPool *memPool, void *object);

} // namespace rml

namespace tbb {
namespace detail {
namespace d1 {

// keep throw in a separate function to prevent code bloat
template<typename E>
void throw_exception(const E &e) {
#if TBB_USE_EXCEPTIONS
    throw e;
#else
    suppress_unused_warning(e);
#endif
}

template<typename T>
class scalable_allocator {
public:
    using value_type = T;
    using propagate_on_container_move_assignment = std::true_type;

    //! Always defined for TBB containers
    using is_always_equal = std::true_type;

    scalable_allocator() = default;
    template<typename U> scalable_allocator(const scalable_allocator<U>&) noexcept {}

    //! Allocate space for n objects.
    __TBB_nodiscard T* allocate(std::size_t n) {
        T* p = static_cast<T*>(scalable_malloc(n * sizeof(value_type)));
        if (!p) {
            throw_exception(std::bad_alloc());
        }
        return p;
    }

    //! Free previously allocated block of memory
    void deallocate(T* p, std::size_t) {
        scalable_free(p);
    }

#if TBB_ALLOCATOR_TRAITS_BROKEN
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using reference = value_type&;
    using const_reference = const value_type&;
    using difference_type = std::ptrdiff_t;
    using size_type = std::size_t;
    template<typename U> struct rebind {
        using other = scalable_allocator<U>;
    };
    //! Largest value for which method allocate might succeed.
    size_type max_size() const noexcept {
        size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
        return (absolutemax > 0 ? absolutemax : 1);
    }
    template<typename U, typename... Args>
    void construct(U *p, Args&&... args)
        { ::new((void *)p) U(std::forward<Args>(args)...); }
    void destroy(pointer p) { p->~value_type(); }
    pointer address(reference x) const { return &x; }
    const_pointer address(const_reference x) const { return &x; }
#endif // TBB_ALLOCATOR_TRAITS_BROKEN

};

#if TBB_ALLOCATOR_TRAITS_BROKEN
    template<>
    class scalable_allocator<void> {
    public:
        using pointer = void*;
        using const_pointer = const void*;
        using value_type = void;
        template<typename U> struct rebind {
            using other = scalable_allocator<U>;
        };
    };
#endif

template<typename T, typename U>
inline bool operator==(const scalable_allocator<T>&, const scalable_allocator<U>&) noexcept { return true; }

#if !__TBB_CPP20_COMPARISONS_PRESENT
template<typename T, typename U>
inline bool operator!=(const scalable_allocator<T>&, const scalable_allocator<U>&) noexcept { return false; }
#endif

#if __TBB_CPP17_MEMORY_RESOURCE_PRESENT

//! C++17 memory resource implementation for scalable allocator
//! ISO C++ Section 23.12.2
class scalable_resource_impl : public std::pmr::memory_resource {
private:
    void* do_allocate(std::size_t bytes, std::size_t alignment) override {
        void* p = scalable_aligned_malloc(bytes, alignment);
        if (!p) {
            throw_exception(std::bad_alloc());
        }
        return p;
    }

    void do_deallocate(void* ptr, std::size_t /*bytes*/, std::size_t /*alignment*/) override {
        scalable_free(ptr);
    }

    //! Memory allocated by one instance of scalable_resource_impl could be deallocated by any
    //! other instance of this class
    bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
        return this == &other ||
#if __TBB_USE_OPTIONAL_RTTI
            dynamic_cast<const scalable_resource_impl*>(&other) != nullptr;
#else
            false;
#endif
    }
};

//! Global scalable allocator memory resource provider
inline std::pmr::memory_resource* scalable_memory_resource() noexcept {
    static tbb::detail::d1::scalable_resource_impl scalable_res;
    return &scalable_res;
}

#endif // __TBB_CPP17_MEMORY_RESOURCE_PRESENT

} // namespace d1
} // namespace detail

inline namespace v1 {
using detail::d1::scalable_allocator;
#if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
using detail::d1::scalable_memory_resource;
#endif
} // namespace v1

} // namespace tbb

#endif /* __cplusplus */

#endif /* __TBB_scalable_allocator_H */