あなたはこのような何かを行うことができます:
は
#include <hbwmalloc.h> // hbw_check_available, hbw_malloc, hbw_free
#include <cstddef> // std::size_t
#include <cstdint> // std::intptr_t
#include <stdexcept> // std::logic_error
#include <new> // std::bad_alloc
#include <memory> // std::addressof
#define do_hbw_check_available
template <class T>
struct hbw_allocator
{
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::intptr_t;
static pointer allocate(size_type n)
{
#if defined(do_hbw_check_available)
if (0 != hbw_check_available())
{
throw std::logic_error("HBW not available");
}
#endif
// Calculate size of storage with alignment and padding
const size_type size = sizeof(value_type) * n;
// Allocate storage
pointer p = hbw_malloc(size);
// Ensure that memory was in fact allocated
if (nullptr == p)
{
throw std::bad_alloc();
}
return p;
}
static void deallocate(const_pointer p, size_type /* n */)
{
hbw_free(p);
}
void construct(pointer p, const_reference t)
{
// Call copy-constructor of the element pointed to by p
new ((void*) p) value_type(t);
}
void destroy(pointer p)
{
// Call destructor of the element pointed to by p
p->~value_type();
}
size_type max_size() const
{
return size_type(-1)/sizeof(value_type);
}
bool operator!=(const hbw_allocator<value_type>& arg) const
{
return !(*this == arg);
}
// Returns true if and only if storage allocated from *this can be deallocated from other, and vice versa.
// Always returns true for stateless allocators.
bool operator==(const hbw_allocator<value_type>& /* arg */) const
{
return true;
}
pointer adress(reference arg)
{
return std::addressof(arg);
}
const_pointer adress(const_reference arg) const
{
return std::addressof(arg);
}
template <class U>
struct rebind
{
using other = hbw_allocator<U>;
};
};
例:は
あなたはそれが好きで使用します。
template <class T>
using hbw_vector = std::vector<T, hbw_allocator<T>>;
hbw_vector<int> vec(5);
が見る、ベクトルのための 'のstd :: allocator'を実装し、この例のように使用量は、非常に簡単ですです:http://en.cppreference.com/w/cpp/memory /アロケータ –