Created
August 17, 2014 17:34
-
-
Save Ation/4a24b94bddc86edc2c4f to your computer and use it in GitHub Desktop.
Custom allocator for unordered_set (use malloc-free) instead of new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class _Ty> | |
class MyAllocator | |
: public _Allocator_base<_Ty> | |
{ // generic MyAllocator for objects of class _Ty | |
public: | |
typedef MyAllocator<_Ty> other; | |
typedef _Allocator_base<_Ty> _Mybase; | |
typedef typename _Mybase::value_type value_type; | |
typedef value_type *pointer; | |
typedef const value_type *const_pointer; | |
typedef void *void_pointer; | |
typedef const void *const_void_pointer; | |
typedef value_type& reference; | |
typedef const value_type& const_reference; | |
typedef size_t size_type; | |
MyAllocator<_Ty> select_on_container_copy_construction() const | |
{ // return this MyAllocator | |
return (*this); | |
} | |
template<class _Other> | |
struct rebind | |
{ // convert this type to MyAllocator<_Other> | |
typedef MyAllocator<_Other> other; | |
}; | |
pointer address(reference _Val) const _NOEXCEPT | |
{ // return address of mutable _Val | |
return (_STD addressof(_Val)); | |
} | |
const_pointer address(const_reference _Val) const _NOEXCEPT | |
{ // return address of nonmutable _Val | |
return (_STD addressof(_Val)); | |
} | |
MyAllocator() _THROW0() | |
{ // construct default MyAllocator (do nothing) | |
} | |
MyAllocator(const MyAllocator<_Ty>&) _THROW0() | |
{ // construct by copying (do nothing) | |
} | |
template<class _Other> | |
MyAllocator(const MyAllocator<_Other>&) _THROW0() | |
{ // construct from a related MyAllocator (do nothing) | |
} | |
template<class _Other> | |
MyAllocator<_Ty>& operator=(const MyAllocator<_Other>&) | |
{ // assign from a related MyAllocator (do nothing) | |
return (*this); | |
} | |
void deallocate(pointer _Ptr, size_type) | |
{ // deallocate object at _Ptr, ignore size | |
free( (void*)_Ptr ); | |
} | |
pointer allocate(size_type _Count) | |
{ // allocate array of _Count elements | |
return (pointer)malloc(_Count * sizeof(value_type)); | |
} | |
pointer allocate(size_type _Count, const void *) | |
{ // allocate array of _Count elements, ignore hint | |
return (allocate(_Count)); | |
} | |
void construct(_Ty *_Ptr) | |
{ // default construct object at _Ptr | |
::new ((void *)_Ptr) _Ty(); | |
} | |
void construct(_Ty *_Ptr, const _Ty& _Val) | |
{ // construct object at _Ptr with value _Val | |
::new ((void *)_Ptr) _Ty(_Val); | |
} | |
template<class _Objty, | |
class... _Types> | |
void construct(_Objty *_Ptr, _Types&&... _Args) | |
{ // construct _Objty(_Types...) at _Ptr | |
::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...); | |
} | |
template<class _Uty> | |
void destroy(_Uty *_Ptr) | |
{ // destroy object at _Ptr | |
_Ptr->~_Uty(); | |
} | |
size_t max_size() const _THROW0() | |
{ // estimate maximum array size | |
return ((size_t)(-1) / sizeof (_Ty)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment