Sign in to follow this  
schupf

Custom Allocator

Recommended Posts

schupf    221
Hi,

I have written a little custom allocator for std::map:
[code]
template<typename T>
class MemManagerMapAllocator {
public :

typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

public :
template<typename U>
struct rebind {
typedef MemManagerMapAllocator<U> other;
};

public :
inline explicit MemManagerMapAllocator() {}
inline ~MemManagerMapAllocator() {}
inline explicit MemManagerMapAllocator(MemManagerMapAllocator const&) {}
template<typename U>
inline explicit MemManagerMapAllocator(MemManagerMapAllocator<U> const&) {}


inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }


inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) {
return reinterpret_cast<pointer>(malloc(cnt * sizeof(T)));
}

inline void deallocate(pointer p, size_type) {
free(p);
}

inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}

inline void construct(pointer p, const T& t) { new(p) T(t); }
inline void destroy(pointer p) { p->~T(); }

inline bool operator==(MemManagerMapAllocator const&) { return true; }
inline bool operator!=(MemManagerMapAllocator const& a) { return !operator==(a); }
}; // [/code]

But when I try to declare a private member variable of a map with my allocator:
[code]typedef MemManagerMapAllocator< std::pair<const void*, AllocInfo> > MapAllocator;
std::map<void*, AllocInfo, std::less<void*>, MapAllocator> mAllocMap;[/code]
I get this cryptic error:
[quote]>D:\Microsoft Visual Studio 10.0\VC\include\xtree(698): error C2664: 'std::_Tree_val<_Traits>::_Tree_val(const std::less<_Ty> &,ple::MemManagerMapAllocator<T>)' : cannot convert parameter 2 from 'const ple::MemManagerMapAllocator<T>' to 'ple::MemManagerMapAllocator<T>'
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>,
1> _Ty=void *,
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> and
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> and
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> Constructor for class 'ple::MemManagerMapAllocator<T>' is declared 'explicit'
1> with
1> [
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> D:\Microsoft Visual Studio 10.0\VC\include\xtree(695) : while compiling class template member function 'std::_Tree<_Traits>::_Tree(const std::less<_Ty> &,const ple::MemManagerMapAllocator<T> &)'
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>,
1> _Ty=void *,
1> T=std::pair<void *const ,ple::MemManager::AllocInfo>
1> ]
1> D:\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<void *,ple::MemManager::AllocInfo,std::less<void *>,ple::MemManager::MapAllocator,false>
1> ]
1> E:\Programmieren\C++\MemManager.h(185) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1> with
1> [
1> _Kty=void *,
1> _Ty=ple::MemManager::AllocInfo,
1> _Pr=std::less<void *>,
1> _Alloc=ple::MemManager::MapAllocator
1> ][/quote]
Does anyone have an idea what could be the problem?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this