Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #71197
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Wading through template instantiation errors for allocator |
| Date | 2020-01-07 10:42 +0100 |
| Organization | albasani.net |
| Message-ID | <qv1jqa$593$1@news.albasani.net> (permalink) |
| References | <2925c440-1188-48ff-b931-243234719891@googlegroups.com> <qv1jfp$74e$1@news.albasani.net> |
Am 07.01.2020 um 10:36 schrieb Bonita Montero:
> Forget writing an allocator that doesn't have full heap-semantics.
> The C++-containers often rebind the allocator-type for orther
> allocators; f.e. an unordered_map might do a rebind for the
> bucket-nodes (it gets only a allocator<pair<const Key, T>,
> which isn't sufficient for everything a unordered_map needs).
I tried to write my own allocator that relies on VirtualAlloc() or
mmap(). But it doesn't work for the above reason; i.e. the allocator
might get rebound and the new allocator might be used for tiny amounts
of memory, rounded up to a page-size.
#ifdef _MSC_VER
#include <Windows.h>
#elif __unix__
#include <sys/mman.h>
#endif
#include <cstddef>
#include <exception>
#include <cassert>
#include <new>
#include <iostream>
template<class T>
class virtual_allocator
{
public:
typedef T value_type;
#if __cplusplus < 201703L
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
#endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
#if __cplusplus < 201703L
template<typename U>
struct rebind
{
typedef virtual_allocator<U> other;
};
#endif
explicit virtual_allocator() = default;
explicit virtual_allocator( virtual_allocator const
&other ) = default;
template<typename U>
virtual_allocator( virtual_allocator<U> const
&other );
#if __cplusplus < 201703L
static pointer address( reference value );
static const_pointer address( const_reference value );
static size_type max_size() throw();
#endif
#if __cplusplus >= 201703L
T *allocate( size_type n, void *hint = nullptr );
void deallocate( T *p, size_type num = 1 );
#else
pointer allocate( size_type n, void *hint = nullptr );
void deallocate( pointer p, size_type num = 1 );
#endif
#if __cplusplus < 201703L
static void construct( pointer p, const T&value );
template<typename U>
static void destroy( U *p );
#endif
};
template<typename T>
template<typename U>
inline
virtual_allocator<T>::virtual_allocator( virtual_allocator<U> const
&other ) :
virtual_allocator()
{
}
#if __cplusplus < 201703L
template<typename T>
inline
typename virtual_allocator<T>::pointer virtual_allocator<T>::address(
reference value )
{
return &value;
}
template<typename T>
inline
typename virtual_allocator<T>::const_pointer
virtual_allocator<T>::address( const_reference value )
{
return &value;
}
template<typename T>
inline
typename virtual_allocator<T>::size_type
virtual_allocator<T>::max_size() throw()
{
return (size_t)(ptrdiff_t)-1 / sizeof(T);
}
#endif
template<typename T>
#if __cplusplus >= 201703L
T *virtual_allocator<T>::allocate( size_type n, void *hint )
#else
typename virtual_allocator<T>::pointer virtual_allocator<T>::allocate(
size_type n, void *hint )
#endif
{
std::cout << "allocate" << n << std::endl;
#if defined(_MSC_VER)
T *p = (T *)VirtualAlloc( hint, n * sizeof(T), MEM_RESERVE |
MEM_COMMIT, PAGE_READWRITE );
if( !p )
throw std::bad_alloc();
return p;
#elif __unix__
void *p = mmap( hint, n * sizeof(T), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
if( p == MAP_FAILED )
throw std::bad_alloc();
return (pointer)p;
#else
#error no implementation for your OS
#endif
}
template<typename T>
#if __cplusplus >= 201703L
inline
void virtual_allocator<T>::deallocate( T *p, size_type n )
#else
inline
void virtual_allocator<T>::deallocate( pointer p, size_type n )
#endif
{
std::cout << "dellocate " << n << std::endl;
#if defined(_MSC_VER)
BOOL ret = VirtualFree( p, 0, MEM_RELEASE );
assert(ret);
#elif __unix__
int ret = munmap( p, n * sizeof(T) );
assert(ret == 0);
#else
#error no implementation for your OS
#endif
}
#if __cplusplus < 201703L
template<typename T>
inline
void virtual_allocator<T>::construct( pointer p, const T&value )
{
new( (void*)p ) T( value );
}
template<typename T>
template<typename U>
inline
void virtual_allocator<T>::destroy( U *p )
{
p->~T();
}
#endif
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-03 07:04 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-03 16:57 +0000
Re: Wading through template instantiation errors for allocator Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-03 21:32 +0200
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-04 06:12 -0800
Re: Wading through template instantiation errors for allocator Bo Persson <bo@bo-persson.se> - 2020-01-04 15:53 +0100
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-04 14:34 -0800
Re: Wading through template instantiation errors for allocator Manfred <noname@add.invalid> - 2020-01-05 22:31 +0100
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-06 02:12 -0800
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-06 23:52 -0800
Re: Wading through template instantiation errors for allocator Ian Collins <ian-news@hotmail.com> - 2020-01-07 21:15 +1300
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-07 01:01 -0800
Re: Wading through template instantiation errors for allocator Ian Collins <ian-news@hotmail.com> - 2020-01-08 07:31 +1300
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 19:50 +0100
Re: Wading through template instantiation errors for allocator Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-10 07:06 -0800
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-14 02:25 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:20 +0000
Re: Wading through template instantiation errors for allocator boltar@nowhere.org - 2020-01-08 10:12 +0000
Re: Wading through template instantiation errors for allocator Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-08 09:22 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-08 17:44 +0000
Re: Wading through template instantiation errors for allocator "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-08 23:00 +0100
Re: Wading through template instantiation errors for allocator Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-10 06:48 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:20 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:35 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:43 +0000
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:47 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:59 +0100
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:15 +0000
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 11:21 +0100
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:25 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:02 +0000
Re: Wading through template instantiation errors for allocator Bo Persson <bo@bo-persson.se> - 2020-01-07 15:27 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:36 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:42 +0100
csiph-web