template<class T,class Alloc>
class simple_alloc{
public:
static T* allocate(std::size_t n){
return 0==n?0:(T*)Alloc::allocate(n * sizeof(T));
}
static T* allocate(void){
return (T*)Alloc::allocate(sizeof (T));
}
static void deallocate(T* p,size_t n){
if (n!=0){
Alloc::deallocate(p,n * sizeof(T));
}
}
static void deallocate(T* p){
Alloc::deallocate(p,sizeof(T));
}
};
namespace Chy{
template <class T>
inline T* _allocate(ptrdiff_t size,T*){
std::set_new_handler(0);
T* tmp = (T*)(::operator new((std::size_t)(size * sizeof (T))));
if (tmp == 0){
std::cerr << "out of memory" << std::endl;
exit(1);
}
return tmp;
}
template<class T>
inline void _deallocate(T* buffer){
::operator delete (buffer);
}
template<class T1,class T2>
inline void _construct(T1 *p,const T2& value){
new(p) T1 (value); //没看懂
}
template <class T>
inline void _destroy(T* ptr){
ptr->~T();
}
template <class T>
class allocator{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef ptrdiff_t difference_type;
template<class U>
struct rebind{
typedef allocator<U>other;
};
pointer allocate(size_type n,const void * hint = 0){
return _allocate((difference_type)n,(pointer)0);
}
void deallocate(pointer p,size_type n){
_deallocate(p);
}
void construct(pointer p,const T& value){
_construct(p,value);
}
void destroy(pointer p){
_destroy(p);
}
pointer address(reference x){
return (pointer)&x;
}
const_pointer const_address(const_reference x){
return (const_pointer)&x;
}
size_type max_size()const{
return size_type(UINT_MAX/sizeof (T));
}
};
}
偏特化
//如果是copy construction 等同于assignment而且destructor 是 trivial以下就会有效
//如果是POD型别 执行的流程就会跳转到以下函数,这个是通过function template的参数推导机制得到的
template<class ForwardIterator,class Size,class T>
inline ForwardIterator __uninitizlized_fill_n_aux(ForwardIterator first,Size n,const T&x){
return fill_n(first,n,x); //交给高阶函数执行
}
struct __true_type{};
struct __false_type{};
template<class T>
struct __type_traits {
typedef __true_type this_dummy_member_must_be_first;
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_constructor;
typedef __false_type has_trivial_destructor;
typedef __false_type is_POD_type;
};
//函数的逻辑是
//首先萃取出 迭代器first的value type,然后判断这个型别是否是POD类型
template<class ForwardIterator,class Size,class T,class T1>
inline ForwardIterator __uninitizlized_fill_n(ForwardIterator first,Size n,const T&x,T1*){
//以下使用的是__type_traits<T1>::is_POD_type is _POD
typedef typename __type_traits<T1>::is_POD_type is_POD;
return __uninitizlized_fill_n_aux(first,n,x,is_POD());
}
//如果是copy construction 等同于assignment而且destructor 是 trivial以下就会有效
//如果是POD型别 执行的流程就会跳转到以下函数,这个是通过function template的参数推导机制得到的
template <class ForwardIterator,class T>
inline void __uninitialized_fill_aux(ForwardIterator first,ForwardIterator last,
const T& x,__true_type){
fill(first,last,x);//调用STL算法 fill()
}
//如果不是POD型别 执行的流程就会转向以下函数 这个是通过function template的参数推导机制得到的
template <class ForwardIterator,class T>
inline void __uninitialized_fill_aux(ForwardIterator first,ForwardIterator last,
const T& x,__false_type){
ForwardIterator cur = first;
//为了简化 省略了异常处理
for(;cur != last;++cur){
Chy::_construct(&*cur,x);
}
}
template<class ForwardIterator,class T,class T1>
inline void __uninitialized_fill(ForwardIterator first,ForwardIterator last,
const T&x,T1*){
typedef typename __type_traits<T1>::is_POD_type is_POD;
__uninitialized_fill_aux(first,last,x,is_POD());
}
# ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
# define __STL_NULL_TMPL_ARGS <>
# else
# define __STL_NULL_TMPL_ARGS
# endif
#ifdef __STL_USE_EXCEPTIONS
#define __STL_TRY try
#define __STL_UNWIND(action) catch(...) { action; throw; }
#else
#define __STL_TRY
#define __STL_UNWIND(action)
#endif
//注意:以下的identify定义于
template <class T>
struct identify : public std::unary_function<T,T>{
const T& operator()(const T& x) const {
return x;
}
};