2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2006-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2006-02-05 18:56:30 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
2021-12-14 18:35:31 +11:00
|
|
|
* \ingroup intern_memutil
|
2011-02-25 11:47:18 +00:00
|
|
|
*/
|
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#ifndef __MEM_ALLOCATOR_H__
|
|
|
|
|
#define __MEM_ALLOCATOR_H__
|
2006-02-05 18:56:30 +00:00
|
|
|
|
|
|
|
|
#include "guardedalloc/MEM_guardedalloc.h"
|
2025-01-26 20:08:09 +01:00
|
|
|
#include <cstddef>
|
2006-02-05 18:56:30 +00:00
|
|
|
|
|
|
|
|
template<typename _Tp> struct MEM_Allocator {
|
2025-01-26 20:08:09 +01:00
|
|
|
using size_type = size_t;
|
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
using pointer = _Tp *;
|
|
|
|
|
using const_pointer = const _Tp *;
|
|
|
|
|
using reference = _Tp &;
|
|
|
|
|
using const_reference = const _Tp &;
|
|
|
|
|
using value_type = _Tp;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
template<typename _Tp1> struct rebind {
|
2025-01-26 20:08:09 +01:00
|
|
|
using other = MEM_Allocator<_Tp1>;
|
2006-02-05 18:56:30 +00:00
|
|
|
};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:09 +01:00
|
|
|
MEM_Allocator() noexcept = default;
|
|
|
|
|
MEM_Allocator(const MEM_Allocator & /*other*/) noexcept = default;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:09 +01:00
|
|
|
template<typename _Tp1> MEM_Allocator(const MEM_Allocator<_Tp1> /*other*/) noexcept {}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:09 +01:00
|
|
|
~MEM_Allocator() noexcept = default;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
pointer address(reference __x) const
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2006-02-05 18:56:30 +00:00
|
|
|
return &__x;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
const_pointer address(const_reference __x) const
|
|
|
|
|
{
|
|
|
|
|
return &__x;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 13:05:39 +10:00
|
|
|
/* NOTE: `__n` is permitted to be 0.
|
|
|
|
|
* The C++ standard says nothing about what the return value is when `__n == 0`. */
|
2025-01-26 20:08:09 +01:00
|
|
|
_Tp *allocate(size_type __n, const void * /*unused*/ = nullptr)
|
2006-02-05 18:56:30 +00:00
|
|
|
{
|
2013-03-08 06:32:00 +00:00
|
|
|
_Tp *__ret = NULL;
|
2023-09-24 14:52:38 +10:00
|
|
|
if (__n) {
|
2006-02-05 18:56:30 +00:00
|
|
|
__ret = static_cast<_Tp *>(MEM_mallocN(__n * sizeof(_Tp), "STL MEM_Allocator"));
|
2023-09-24 14:52:38 +10:00
|
|
|
}
|
2006-02-05 18:56:30 +00:00
|
|
|
return __ret;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
// __p is not permitted to be a null pointer.
|
2025-01-26 20:08:09 +01:00
|
|
|
void deallocate(pointer __p, size_type /*unused*/)
|
2012-06-04 20:11:09 +00:00
|
|
|
{
|
2025-02-20 10:37:10 +01:00
|
|
|
MEM_freeN(static_cast<void *>(__p));
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:09 +01:00
|
|
|
size_type max_size() const noexcept
|
2019-01-24 16:20:16 +11:00
|
|
|
{
|
|
|
|
|
return size_t(-1) / sizeof(_Tp);
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-24 16:20:16 +11:00
|
|
|
void construct(pointer __p, const _Tp &__val)
|
|
|
|
|
{
|
|
|
|
|
new (__p) _Tp(__val);
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-24 16:20:16 +11:00
|
|
|
void destroy(pointer __p)
|
|
|
|
|
{
|
|
|
|
|
__p->~_Tp();
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#endif // __MEM_ALLOCATOR_H__
|