STL空间配置器的剖析

来源:互联网 发布:dns 多域名 同一ip 编辑:程序博客网 时间:2024/06/16 16:07

本来昨天就能写好,晚上编辑器出了点问题,所以拖到今天重新写,呜呜呜呜~~~

空间配置器的剖析是从《STL源码剖析》上看的,然后把它总结发到博客上,希望对大家有些帮助


STL六大组件:

空间配置器  容器  算法  迭代器  仿函数  容器适配器

 



关于SGI的标准空间配置器,std::allocator,不建议使用,主要原因是效率不佳,只是把::operator new 和operator delete 做一层薄薄的包装而已,这里主要讨论SGI特殊的空间配置器:std::alloc

C++的内存配置与释放的操作是这样的:

Class A {}

A *p = new A;    //配置内存,构造对象

delete p;        //析构对象,释放内存

 

其中:new 算式内进行两阶段操作:

(1)调用::operator new 配置内存

(2)调用A::A()构造对象

   Delete也进行两阶段操作:

(1)调用A::~A()析构对象

(2)调用::operator delete 释放内存


为了精密分工,STL allocator 决定将这两阶段操作分开

内存配置由alloc::allocate()负责

内存释放由 alloc::deallocate() 负责

对象的构造由::construct()负责

对象的析构由::destroy()负责


配置器定义于<memory>中,<memory>中包含两个文件

#include<stl_alloc.h>//负责内存空间的配置与释放

#include<stl_construct.h>//负责对象的构造与析构




下面是construct的源码:#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H#define __SGI_STL_INTERNAL_CONSTRUCT_H#include <new.h>     //使用定位new 必须引入的头文件__STL_BEGIN_NAMESPACEtemplate <class T>inline void destroy(T* pointer) {    pointer->~T();}template <class T1, class T2>inline void construct(T1* p, const T2& value) {  new (p) T1(value);                   //定位new}template <class ForwardIterator>inline void__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) {  for ( ; first < last; ++first)    destroy(&*first);}template <class ForwardIterator> inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}template <class ForwardIterator, class T>inline void __destroy(ForwardIterator first, ForwardIterator last, T*) {  typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;  __destroy_aux(first, last, trivial_destructor());}template <class ForwardIterator>inline void destroy(ForwardIterator first, ForwardIterator last) {  __destroy(first, last, value_type(first));}inline void destroy(char*, char*) {}inline void destroy(wchar_t*, wchar_t*) {}__STL_END_NAMESPACE#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */// Local Variables:// mode:C++// End:接下来将他们一块一块拿出来分析。剖析~~~~对象的构造与析构-------------------------------------------------------------------------------------------template <class T1, class T2>inline void construct(T1* p, const T2& value) {  new (p) T1(value);                   //定位new}--------------------------------------------------------------------------------------Construct( )  接受一个指针p,还有一个初值value  该函数的功能就是将初始值设定到指针所指的空间上  
———————————————————————————————————————————template <class T>inline void destroy(T* pointer) {    pointer->~T();}———————————————————————————————————————————这是destroy的第一个版本,准备将 该指针所指的对象析构掉,直接调用对象的析构函数---------------------------------------------------------------------------------------------------template <class ForwardIterator>inline void destroy(ForwardIterator first, ForwardIterator last) {  __destroy(first, last, value_type(first));}----------------------------------------------------------------------------------------------------template <class ForwardIterator, class T>inline void __destroy(ForwardIterator first, ForwardIterator last, T*) {  typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;  __destroy_aux(first, last, trivial_destructor());------------------------------------------------------------------------------------------template <class ForwardIterator> inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}template <class ForwardIterator>inline void__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) {  for ( ; first < last; ++first)    destroy(&*first);}这是destroy 的第二个版本,接受first 和last的两个迭代器,准备将[first ,last)范围内的所有对象析构,我们不知道这个范围有多大,万一很大,但是每个对象的析构函数
都无关痛痒,那么一次又一次调用这些无关痛痒的析构函数,对效率来说是一种伤害,所以最后一个参数用value_type( )来获取迭代器所指对象的型别,再利用_type_traits<T>
(萃取技术)来判断该型别的析构函数是否无关痛痒,若是,就什么也不做,若不是,用循环的方式遍历整个范围,每经历一个对象就调用第一个版本的destroy()



空间的配置与释放

Construct( )   和 destroy( )

<stl_alloc.h>负责


向system heap 要求空间

考虑多线程状态

考虑内存不足的应变措施

考虑过多“小型区块”可能造成的内存碎片问题



SGI 是以malloc( )和 free( ) 完成内存的配置与释放

考虑到小型区块可能造成的内存碎片问题,SGI 设计了双层的配置器,第一级配置器直接用malloc()和free(),第二级则视情况而定采用不同的策略:

当配置的区块超过128字节时,视为足够大,调用一级空间配置器

当配置的区块小于128字节时,视为过小,为了降低额外负担,采用内存池的方式,而不再求助于第一级空间配置器。

源码如下:源码比较多,为了方便剖析,我把它删了一部分#include <stddef.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <pthread.h>template <int inst>class __malloc_alloc_template {private:static void *oom_malloc(size_t);static void *oom_realloc(void *, size_t);static void (* __malloc_alloc_oom_handler)();public:static void * allocate(size_t n){    void *result = malloc(n);    if (0 == result) result = oom_malloc(n);    return result;}static void deallocate(void *p, size_t /* n */){    free(p);}static void * reallocate(void *p, size_t /* old_sz */, size_t new_sz){    void * result = realloc(p, new_sz);    if (0 == result) result = oom_realloc(p, new_sz);    return result;}static void (* set_malloc_handler(void (*f)()))(){    void (* old)() = __malloc_alloc_oom_handler;    __malloc_alloc_oom_handler = f;    return(old);}};template <int inst>void (* __malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;template <int inst>void * __malloc_alloc_template<inst>::oom_malloc(size_t n){    void (* my_malloc_handler)();    void *result;    for (;;) {        my_malloc_handler = __malloc_alloc_oom_handler;        if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }        (*my_malloc_handler)();        result = malloc(n);        if (result) return(result);    }}template <int inst>void * __malloc_alloc_template<inst>::oom_realloc(void *p, size_t n){    void (* my_malloc_handler)();    void *result;    for (;;) {        my_malloc_handler = __malloc_alloc_oom_handler;        if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }        (*my_malloc_handler)();        result = realloc(p, n);        if (result) return(result);    }}typedef __malloc_alloc_template<0> malloc_alloc;template<class T, class Alloc>class simple_alloc {public:    static T *allocate(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 (0 != n) Alloc::deallocate(p, n * sizeof (T)); }    static void deallocate(T *p)                { Alloc::deallocate(p, sizeof (T)); }};template <class Alloc>class debug_alloc {private:enum {extra = 8};       public:static void * allocate(size_t n){    char *result = (char *)Alloc::allocate(n + extra);    *(size_t *)result = n;    return result + extra;}static void deallocate(void *p, size_t n){    char * real_p = (char *)p - extra;    assert(*(size_t *)real_p == n);    Alloc::deallocate(real_p, n + extra);}static void * reallocate(void *p, size_t old_sz, size_t new_sz){    char * real_p = (char *)p - extra;    assert(*(size_t *)real_p == old_sz);    char * result = (char *)                  Alloc::reallocate(real_p, old_sz + extra, new_sz + extra);    *(size_t *)result = new_sz;    return result + extra;}};二级空间配置器typedef malloc_alloc alloc;  enum {__ALIGN = 8};  enum {__MAX_BYTES = 128};  enum {__NFREELISTS = __MAX_BYTES/__ALIGN};template <bool threads, int inst>class __default_alloc_template {private:     enum {__ALIGN = 8};    enum {__MAX_BYTES = 128};enum {__NFREELISTS = __MAX_BYTES/__ALIGN};  static size_t ROUND_UP(size_t bytes) {        return (((bytes) + __ALIGN-1) & ~(__ALIGN - 1));  }__PRIVATE:  union obj {        union obj * free_list_link;        char client_data[1];     };private:    static obj * __VOLATILE free_list[];            static obj * __VOLATILE free_list[__NFREELISTS];   static  size_t FREELIST_INDEX(size_t bytes) {        return (((bytes) + __ALIGN-1)/__ALIGN - 1);  }   static void *refill(size_t n);  static char *chunk_alloc(size_t size, int &nobjs);  static char *start_free;  static char *end_free;  static size_t heap_size;  public:  static void * allocate(size_t n)  {    obj * __VOLATILE * my_free_list;    obj * __RESTRICT result;    if (n > (size_t) __MAX_BYTES) {        return(malloc_alloc::allocate(n));    }    my_free_list = free_list + FREELIST_INDEX(n);    result = *my_free_list;    if (result == 0) {        void *r = refill(ROUND_UP(n));        return r;    }    *my_free_list = result -> free_list_link;    return (result);  };   static void deallocate(void *p, size_t n)  {    obj *q = (obj *)p;    obj * __VOLATILE * my_free_list;    if (n > (size_t) __MAX_BYTES) {        malloc_alloc::deallocate(p, n);        return;    }    my_free_list = free_list + FREELIST_INDEX(n);     q -> free_list_link = *my_free_list;    *my_free_list = q;   }  static void * reallocate(void *p, size_t old_sz, size_t new_sz);} ;typedef __default_alloc_template<__NODE_ALLOCATOR_THREADS, 0> alloc;typedef __default_alloc_template<false, 0> single_client_alloc;                                    template <bool threads, int inst>char*__default_alloc_template<threads, inst>::chunk_alloc(size_t size, int& nobjs){    char * result;    size_t total_bytes = size * nobjs;    size_t bytes_left = end_free - start_free;    if (bytes_left >= total_bytes) {        result = start_free;        start_free += total_bytes;        return(result);    } else if (bytes_left >= size) {        nobjs = bytes_left/size;        total_bytes = size * nobjs;        result = start_free;        start_free += total_bytes;        return(result);    } else {        size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4);              if (bytes_left > 0) {            obj * __VOLATILE * my_free_list =                        free_list + FREELIST_INDEX(bytes_left);            ((obj *)start_free) -> free_list_link = *my_free_list;            *my_free_list = (obj *)start_free;        }        start_free = (char *)malloc(bytes_to_get);        if (0 == start_free) {            int i;            obj * __VOLATILE * my_free_list, *p;                    for (i = size; i <= __MAX_BYTES; i += __ALIGN) {                my_free_list = free_list + FREELIST_INDEX(i);                p = *my_free_list;                if (0 != p) {                    *my_free_list = p -> free_list_link;                    start_free = (char *)p;                    end_free = start_free + i;                    return(chunk_alloc(size, nobjs));                                 }            }    end_free = 0;            start_free = (char *)malloc_alloc::allocate(bytes_to_get);                }        heap_size += bytes_to_get;        end_free = start_free + bytes_to_get;        return(chunk_alloc(size, nobjs));    }}template <bool threads, int inst>void* __default_alloc_template<threads, inst>::refill(size_t n){    int nobjs = 20;    char * chunk = chunk_alloc(n, nobjs);    obj * __VOLATILE * my_free_list;    obj * result;    obj * current_obj, * next_obj;    int i;    if (1 == nobjs) return(chunk);    my_free_list = free_list + FREELIST_INDEX(n);    /* Build free list in chunk */      result = (obj *)chunk;      *my_free_list = next_obj = (obj *)(chunk + n);      for (i = 1; ; i++) {        current_obj = next_obj;        next_obj = (obj *)((char *)next_obj + n);        if (nobjs - 1 == i) {            current_obj -> free_list_link = 0;            break;        } else {            current_obj -> free_list_link = next_obj;        }      }    return(result);}template <bool threads, int inst>void*__default_alloc_template<threads, inst>::reallocate(void *p,                                                    size_t old_sz,                                                    size_t new_sz){    void * result;    size_t copy_sz;    if (old_sz > (size_t) __MAX_BYTES && new_sz > (size_t) __MAX_BYTES) {        return(realloc(p, new_sz));    }    if (ROUND_UP(old_sz) == ROUND_UP(new_sz)) return(p);    result = allocate(new_sz);    copy_sz = new_sz > old_sz? old_sz : new_sz;    memcpy(result, p, copy_sz);    deallocate(p, old_sz);    return(result);}  template <bool threads, int inst>char *__default_alloc_template<threads, inst>::start_free = 0;template <bool threads, int inst>char *__default_alloc_template<threads, inst>::end_free = 0;template <bool threads, int inst>size_t __default_alloc_template<threads, inst>::heap_size = 0;template <bool threads, int inst>__default_alloc_template<threads, inst>::obj * __VOLATILE__default_alloc_template<threads, inst> ::free_list[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };---------------------------------------------------------------------------------------template<class T, class Alloc>class simple_alloc {public:    static T *allocate(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 (0 != n) Alloc::deallocate(p, n * sizeof (T)); }    static void deallocate(T *p)                { Alloc::deallocate(p, sizeof (T)); }};-----------------------------------------------------------------------------------------------------这是SGI 为一级空间配置器和二级空间配置器提供的接口,使配置器的接口能够符合STL 规则




----------------------------------------------------------------------------------------------------------static void *oom_malloc(size_t); static void *oom_realloc(void *, size_t); static void (* __malloc_alloc_oom_handler)();----------------------------------------------------------------------------------------------------------用这三个函数来处理内存不足的情况----------------------------------------------------------------------------------------------------------static void * allocate(size_t n){    void *result = malloc(n);    if (0 == result) result = oom_malloc(n);    return result;}   template <int inst>void * __malloc_alloc_template<inst>::oom_malloc(size_t n){    void (* my_malloc_handler)();    void *result;     for (;;) {        my_malloc_handler = __malloc_alloc_oom_handler;        if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }        (*my_malloc_handler)();        result = malloc(n);        if (result) return(result);    }}    ---------------------------------------------------------------------------------------------------------一级空间配置器直接用 malloc(),  在调用malloc()不成功后,改用oom_malloc(),里面有内循环,不断调用“内存不足处理”,期望在某次调用之后,能获得足够的内存
而圆满完成任务,但如果内存不足处理函数都不能解决,就会调用_THROW_BAD_ALLOC,丢出异常信息。下面的relloc()原理相同:    static void * reallocate(void *p, size_t /* old_sz */, size_t new_sz){    void * result = realloc(p, new_sz);    if (0 == result) result = oom_realloc(p, new_sz);    return result;}   template <int inst>void * __malloc_alloc_template<inst>::oom_realloc(void *p, size_t n){    void (* my_malloc_handler)();    void *result;     for (;;) {        my_malloc_handler = __malloc_alloc_oom_handler;        if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }        (*my_malloc_handler)();        result = realloc(p, n);        if (result) return(result);    }}--------------------------------------------------------------------------------------- static void (* set_malloc_handler(void (*f)()))(){    void (* old)() = __malloc_alloc_oom_handler;    __malloc_alloc_oom_handler = f;    return(old);}----------------------------------------------------------------------------------------------------------内存不足处理函数         接下来的部分就是二级空间配置器 二级配置器多了一些机制,避免太多小额区块造成内存碎片的问题     二级空间配置器的做法是:如果区块足够大,超过128字节时,就移交第一级配置器处理,当区块小于128字节时,以内存池的方式管理,成为次级配置,
每配置一大块内存时,并维护对应之自由链表,下次若有相同大小的内存需求,就直接从freelist中拨出,如果客端释还小额区块,就由配置器回收到freelist中, 
 为了方便管理,二级空间配置器会自动将任何小额区块的内存调至8的倍数,并维护16个freelist,各自管理的大小为  8,16,24,32,    40,48,56,64,    72,80,88,96,    104,112,120,128  -------------------------------------------------------------------------------------------------------free list的节点结构union obj {        union obj * free_list_link;        char client_data[1];     };----------------------------------------------------------------------------------------------------------  static size_t ROUND_UP(size_t bytes) {        return (((bytes) + __ALIGN-1) & ~(__ALIGN - 1));  }将byte调至8的倍数 ---------------------------------------------------------------------------------------------------------空间配置函数allocate() static void * allocate(size_t n)  {    obj * __VOLATILE * my_free_list;    obj * __RESTRICT result;     if (n > (size_t) __MAX_BYTES) {        return(malloc_alloc::allocate(n));    }    my_free_list = free_list + FREELIST_INDEX(n);    result = *my_free_list;    if (result == 0) {        void *r = refill(ROUND_UP(n));        return r;    }    *my_free_list = result -> free_list_link;    return (result);  };-------------------------------------------------------------------------------------- static  size_t FREELIST_INDEX(size_t bytes) {        return (((bytes) + __ALIGN-1)/__ALIGN - 1);    }


-------------------------------------------------------------------------------------template <bool threads, int inst>void* __default_alloc_template<threads, inst>::refill(size_t n){    int nobjs = 20;    char * chunk = chunk_alloc(n, nobjs);    obj * __VOLATILE * my_free_list;    obj * result;    obj * current_obj, * next_obj;    int i;    if (1 == nobjs) return(chunk);//如果只获得一个区块,这个区块就分配给调用者使用,free_list无新节点,否则,准备调整free_list  纳入新节点my_free_list = free_list + FREELIST_INDEX(n);//在chunk空间内建立free_list      result = (obj *)chunk;    //使free_list指向新配置的空间(取自内存池)      *my_free_list = next_obj = (obj *)(chunk + n);    //将free_list的各个节点串联起来      for (i = 1; ; i++) {        current_obj = next_obj;        next_obj = (obj *)((char *)next_obj + n);        if (nobjs - 1 == i) {            current_obj -> free_list_link = 0;            break;        } else {            current_obj -> free_list_link = next_obj;        }      }    return(result);}首先判断区块大小,如果大于128就用一级空间配置器,小于一级空间配置器就检查对应的free_list,如果free_list内有可用区块,就直接拿来用,
如果没有可用区块,就将可用区块上调至8的倍数,然后调用refill()重新为free_list()填充空间,新的空间将取自内存池,缺省取得20个内存空间,
但是万一内存池的空间不够,获得的节点可能小于20---------------------------------------------------------------------------------------------------------__default_alloc_template<threads, inst>::chunk_alloc(size_t size, int& nobjs){    char * result;    size_t total_bytes = size * nobjs;    size_t bytes_left = end_free - start_free;//如果内存池空间完全满足需求量的话,直接从内存池里面取    if (bytes_left >= total_bytes) {        result = start_free;        start_free += total_bytes;        return(result);}  //内存池的剩余空间不能满足需求量,但是能够提供一个以上的区块else if (bytes_left >= size) {        nobjs = bytes_left/size;        total_bytes = size * nobjs;        result = start_free;        start_free += total_bytes;        return(result);} //内存池连一个区块也无法提供else {        size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4);      //内存池中还有一些零头,先配给适当的free_list        if (bytes_left > 0) {            obj * __VOLATILE * my_free_list =                        free_list + FREELIST_INDEX(bytes_left);//调整free_list,将内存池中的残余空间编入            ((obj *)start_free) -> free_list_link = *my_free_list;            *my_free_list = (obj *)start_free;        }//动态开辟空间,用来补充内存池        start_free = (char *)malloc(bytes_to_get);        if (0 == start_free) {            int i;            obj * __VOLATILE * my_free_list, *p;        //以下搜索适当的free_list,尚有未用区块,而且足够大            for (i = size; i <= __MAX_BYTES; i += __ALIGN) {                my_free_list = free_list + FREELIST_INDEX(i);                p = *my_free_list;                if (0 != p) //free_list里面尚有未用区块,调整free_list 以释放出未用区块             {                    *my_free_list = p -> free_list_link;                    start_free = (char *)p;                    end_free = start_free + i;              //递归调用自己,为了修正nobjs                    return(chunk_alloc(size, nobjs));//任何残余零头将会被编入适当的free_list  备用                                 }            }    end_free = 0;   //如果出现意外,到处没内存,就调用一级空间配置器             start_free = (char *)malloc_alloc::allocate(bytes_to_get);                }        heap_size += bytes_to_get;        end_free = start_free + bytes_to_get;     //递归调用自己,为了修正自己的nobjs        return(chunk_alloc(size, nobjs));    }}-------------------------------------------------------------------------------------从内存池中取空间给free_list使用是上面chunk_alloc()的工作以end_free-start_free来检查内存池中的水量是否充足,如果水量充足,就直接调用20个区块给free_list  如果水量不足,但是还可以提供一个以上的区块,
就拨出这不足20个区块的空间出去,这时nobjs 参数将会被修改为实际能够供应的区块数,如果空间连一个区块都无法提供,就利用malloc()函数从heap上申请内存,
为内存池注入水,新水量的大小等于需求量的两倍,加上一个随着配置次数越来越大的附加量      size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4);

下图是具体实例来说明内存池是怎么工作的~~~~~~

 

一开始调用chunk_alloc(32, 20);

于是malloc()就个配置40个32bytes区块,其中1个交出,另外19个交给free_list[3]维护

剩余20个留给内存池,接下来客端调用chunk_alloc(64, 20),此时free_list必须向内存池请求支持,但是内存池只能供应(32*20)/64 = 10个区块,第一个交给客端,其余九个交给free_list[7]维护,此时内存池全是空的,接下来再调用chunk_alloc(96, 20),此时free_list[11]是空的,必须向内存池请求支援,但是内存池也是空的,然后就直接调用malloc()配置40+n(附加量),其中第一个交出,另外19个交给free_list[11]维护,剩下20+n个区块留给内存池

万一整个堆空间都不够了,malloc()失败,chunk_alloc()就寻找“尚有未用区块,而且区块足够大”

的自由链表,找到了就交出,找不到就调用一级空间配置器,一级空间配置器也是由malloc()来配置内存,但是它有内存不足管理机制,或许有机会释放其他内存拿来使用,如果可以就拿来使用,如果不行,就发出bad_alloc异常







-----------------------------------------------------------------------------------------------------

最后一个空间释放函数忘记了~~~~~~

static void deallocate(void *p, size_t n)

  {

    obj *q = (obj *)p;

    obj * __VOLATILE * my_free_list;

 

// 大于128就调用一级空间配置器  

if (n > (size_t) __MAX_BYTES) {

        malloc_alloc::deallocate(p, n);

        return;

}

//小于就寻找相应的free_list的位置

    my_free_list = free_list + FREELIST_INDEX(n);

   //调整free_list ,回收区块

    q -> free_list_link = *my_free_list;

    *my_free_list = q;

 

  }

 

---------------------------------------------------------------------------------------------------

该函数首先判断区块的大小,大于128bytes就调用一级空间配置器,小于128bytes 就找出对应的free_list ,将区块回收















下面介绍内存基本处理工具uninitilized_copy( )Uninitilized_fill( ) Uninitilized_fill_n( )源码如下所示,为了容易理解,去掉了一些注释之类的东西template <class InputIterator, class ForwardIterator>inline ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __true_type) {  return copy(first, last, result);}template <class InputIterator, class ForwardIterator>ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __false_type) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; first != last; ++first, ++cur)      construct(&*cur, *first);    return cur;  }  __STL_UNWIND(destroy(result, cur));}template <class InputIterator, class ForwardIterator, class T>inline ForwardIterator__uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result, T*) {  typedef typename __type_traits<T>::is_POD_type is_POD;  return __uninitialized_copy_aux(first, last, result, is_POD());}template <class InputIterator, class ForwardIterator>inline ForwardIterator  uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result) {  return __uninitialized_copy(first, last, result, value_type(result));}inline char* uninitialized_copy(const char* first, const char* last,                                char* result) {  memmove(result, first, last - first);  return result + (last - first);}inline wchar_t* uninitialized_copy(const wchar_t* first, const wchar_t* last,                                   wchar_t* result) {  memmove(result, first, sizeof(wchar_t) * (last - first));  return result + (last - first);}template <class InputIterator, class Size, class ForwardIterator>pair<InputIterator, ForwardIterator>__uninitialized_copy_n(InputIterator first, Size count,                       ForwardIterator result,                       input_iterator_tag) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; count > 0 ; --count, ++first, ++cur)       construct(&*cur, *first);    return pair<InputIterator, ForwardIterator>(first, cur);  }  __STL_UNWIND(destroy(result, cur));}template <class RandomAccessIterator, class Size, class ForwardIterator>inline pair<RandomAccessIterator, ForwardIterator>__uninitialized_copy_n(RandomAccessIterator first, Size count,                       ForwardIterator result,                       random_access_iterator_tag) {  RandomAccessIterator last = first + count;  return make_pair(last, uninitialized_copy(first, last, result));}template <class InputIterator, class Size, class ForwardIterator>inline pair<InputIterator, ForwardIterator>uninitialized_copy_n(InputIterator first, Size count,                     ForwardIterator result) {  return __uninitialized_copy_n(first, count, result,                                iterator_category(first));}template <class ForwardIterator, class T>inline void__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,                          const T& x, __true_type){  fill(first, last, x);}template <class ForwardIterator, class T>void__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,                          const T& x, __false_type){  ForwardIterator cur = first;  __STL_TRY {    for ( ; cur != last; ++cur)      construct(&*cur, x);  }  __STL_UNWIND(destroy(first, cur));}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());                   }template <class ForwardIterator, class T>inline void uninitialized_fill(ForwardIterator first, ForwardIterator last,                                const T& x) {  __uninitialized_fill(first, last, x, value_type(first));}template <class ForwardIterator, class Size, class T>inline ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __true_type) {  return fill_n(first, n, x);}template <class ForwardIterator, class Size, class T>ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __false_type) {  ForwardIterator cur = first;  __STL_TRY {    for ( ; n > 0; --n, ++cur)      construct(&*cur, x);    return cur;  }  __STL_UNWIND(destroy(first, cur));}template <class ForwardIterator, class Size, class T, class T1>inline ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,                                              const T& x, T1*) {  typedef typename __type_traits<T1>::is_POD_type is_POD;  return __uninitialized_fill_n_aux(first, n, x, is_POD());                                    }template <class ForwardIterator, class Size, class T>inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,                                            const T& x) {  return __uninitialized_fill_n(first, n, x, value_type(first));}template <class InputIterator1, class InputIterator2, class ForwardIterator>inline ForwardIterator__uninitialized_copy_copy(InputIterator1 first1, InputIterator1 last1,                          InputIterator2 first2, InputIterator2 last2,                          ForwardIterator result) {  ForwardIterator mid = uninitialized_copy(first1, last1, result);  __STL_TRY {    return uninitialized_copy(first2, last2, mid);  }  __STL_UNWIND(destroy(result, mid));}template <class ForwardIterator, class T, class InputIterator>inline ForwardIterator __uninitialized_fill_copy(ForwardIterator result, ForwardIterator mid,                          const T& x,                          InputIterator first, InputIterator last) {  uninitialized_fill(result, mid, x);  __STL_TRY {    return uninitialized_copy(first, last, mid);  }  __STL_UNWIND(destroy(result, mid));}template <class InputIterator, class ForwardIterator, class T>inline void__uninitialized_copy_fill(InputIterator first1, InputIterator last1,                          ForwardIterator first2, ForwardIterator last2,                          const T& x) {  ForwardIterator mid2 = uninitialized_copy(first1, last1, first2);  __STL_TRY {    uninitialized_fill(mid2, last2, x);  }  __STL_UNWIND(destroy(first2, mid2));}__STL_END_NAMESPACE#endif接下来一段一段进行剖析~~~~~~~---------------------------------------------------------------------------------------emplate <class InputIterator, class ForwardIterator>inline ForwardIterator  uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result) {  return __uninitialized_copy(first, last, result, value_type(result));}---------------------------------------------------------------------------------------能够将内存的配置与对象的构造行为分开,如果作为输出目的地的[result, result+(last-first))范围内的每一个迭代器都指向未初始化的区域,
则这个函数会使用copy constructor,给范围内的每一个对象产生一个复制品。要么构造出所有必要元素,要么一个也不构造本函数接收三个参数迭代器first 指向输入端的起始位置迭代器last指向输入端的结束位置迭代器result指向输出端的起始处---------------------------------------------------------------------------------------template <class InputIterator, class ForwardIterator, class T>inline ForwardIterator__uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result, T*) {  typedef typename __type_traits<T>::is_POD_type is_POD;  return __uninitialized_copy_aux(first, last, result, is_POD());}template <class InputIterator, class ForwardIterator>inline ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __true_type) {  return copy(first, last, result);}template <class InputIterator, class ForwardIterator>ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __false_type) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; first != last; ++first, ++cur)      construct(&*cur, *first);    return cur;  }  __STL_UNWIND(destroy(result, cur));}首先萃取出迭代器的value_type(),然后判断该型别是否是POD型

如果是就调用STL中的copy()template <class InputIterator, class ForwardIterator>inline ForwardIterator__uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __true_type) {  return copy(first, last, result);}  如果不是,就必须一个一个构造,无法批量进行   template <class InputIterator, class ForwardIterator>ForwardIterator__uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __false_type) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; first != last; ++first, ++cur)      construct(&*cur, *first);    return cur;  }  __STL_UNWIND(destroy(result, cur));}      ----------------------------------------------------------------------------------------------------------针对char*  和 wchar_t*  两种类别,可以采用最具有效率的做法memmove(直接移动内存)来执行复制行为,如下源码所示:inline char* uninitialized_copy(const char* first, const char* last,                                char* result) {  memmove(result, first, last - first);  return result + (last - first);} inline wchar_t* uninitialized_copy(const wchar_t* first, const wchar_t* last,                                   wchar_t* result) {  memmove(result, first, sizeof(wchar_t) * (last - first));  return result + (last - first);} ----------------------------------------------------------------------------------------------------------Uninitialized_filltemplate <class ForwardIterator, class T>inline void uninitialized_fill(ForwardIterator first, ForwardIterator last,                               const T& x) {  __uninitialized_fill(first, last, x, value_type(first));}--------------------------------------------------------------------------------------------------- 迭代器first 指向输出端的起始处迭代器last指向输出端的结束处X 表示初值 跟上面的原理相同------------------------------------------------------------------------------------------------------ 
Uninitilized_fill_n ---------------------------------------------------------------------------------------template <class ForwardIterator, class Size, class T, class T1>inline ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,                                              const T& x, T1*) {  typedef typename __type_traits<T1>::is_POD_type is_POD;  return __uninitialized_fill_n_aux(first, n, x, is_POD());                                    }template <class ForwardIterator, class Size, class T>ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __false_type) {  ForwardIterator cur = first;  __STL_TRY {    for ( ; n > 0; --n, ++cur)      construct(&*cur, x);    return cur;  }  __STL_UNWIND(destroy(first, cur));}template <class ForwardIterator, class Size, class T>inline ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __true_type) {  return fill_n(first, n, 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());                   }template <class InputIterator, class ForwardIterator>inline ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __true_type) {  return copy(first, last, result);}template <class InputIterator, class ForwardIterator>ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __false_type) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; first != last; ++first, ++cur)      construct(&*cur, *first);    return cur;  }  __STL_UNWIND(destroy(result, cur));}