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
|
#ifndef _MMAP_ALLOCATOR_H
#define _MMAP_ALLOCATOR_H
#include <cstddef>
#include <sstream>
class void_mmap_allocator
{
public:
typedef std::size_t size_type;
static void *allocate(size_type n, const void *hint = 0);
static void deallocate(void *p, size_type n);
static void destroy(void *p);
static void shutdown();
static void reportStoreSize(std::ostringstream &str);
static void openMmapFile(const std::string& mmapFilename);
};
template<typename T>
class mmap_allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef const T &const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef mmap_allocator<U> other;
};
mmap_allocator() = default;
template<typename OtherT>
mmap_allocator(OtherT &)
{ }
pointer allocate(size_type n, const void *hint = 0)
{
return reinterpret_cast<T *>(void_mmap_allocator::allocate(n * sizeof(T), hint));
}
void deallocate(pointer p, size_type n)
{
void_mmap_allocator::deallocate(p, n);
}
void construct(pointer p, const_reference val)
{
new((void *)p) T(val);
}
void destroy(pointer p) { void_mmap_allocator::destroy(p); }
};
template<typename T1, typename T2>
static inline bool operator==(mmap_allocator<T1> &, mmap_allocator<T2> &) { return true; }
template<typename T1, typename T2>
static inline bool operator!=(mmap_allocator<T1> &, mmap_allocator<T2> &) { return false; }
#endif
|