STL内存分配器详解之__default_alloc_template

接下来看另一个模板:

typedef __default_alloc_template<__NODE_ALLOCATOR_THREADS, 0> alloc;

__default_alloc_template的实现。

template 
class __default_alloc_template {

    private:
      // Really we should use static const int x = N
      // instead of enum { x = N }, but few compilers accept the former.
# ifndef __SUNPRO_CC
    enum {__ALIGN = 8};
    enum {__MAX_BYTES = 128};
    enum {__NFREELISTS = __MAX_BYTES/__ALIGN};
# endif
    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];    /* The client sees this.        */
    };
private:
# ifdef __SUNPRO_CC
    static obj * __VOLATILE free_list[]; 
        // Specifying a size results in duplicate def for 4.1
# else
    static obj * __VOLATILE free_list[__NFREELISTS]; 
# endif
    static  size_t FREELIST_INDEX(size_t bytes) {
        return (((bytes) + __ALIGN-1)/__ALIGN - 1);
    }

    // Returns an object of size n, and optionally adds to size n free list.
    static void *refill(size_t n);
    // Allocates a chunk for nobjs of size "size".  nobjs may be reduced
    // if it is inconvenient to allocate the requested number.
    static char *chunk_alloc(size_t size, int &nobjs);

    // Chunk allocation state.
    static char *start_free;
    static char *end_free;
    static size_t heap_size;

# ifdef __STL_SGI_THREADS
    static volatile unsigned long __node_allocator_lock;
    static void __lock(volatile unsigned long *); 
    static inline void __unlock(volatile unsigned long *);
# endif

# ifdef __STL_PTHREADS
    static pthread_mutex_t __node_allocator_lock;
# endif

# ifdef __STL_WIN32THREADS
    static CRITICAL_SECTION __node_allocator_lock;
    static bool __node_allocator_lock_initialized;

  public:
    __default_alloc_template() {
    // This assumes the first constructor is called before threads
    // are started.
        if (!__node_allocator_lock_initialized) {
            InitializeCriticalSection(&__node_allocator_lock);
            __node_allocator_lock_initialized = true;
        }
    }
  private:
# endif

    class lock {
        public:
            lock() { __NODE_ALLOCATOR_LOCK; }
            ~lock() { __NODE_ALLOCATOR_UNLOCK; }
    };
    friend class lock;

public:

  /* n must be > 0      */
  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);
    // Acquire the lock here with a constructor call.
    // This ensures that it is released in exit or during stack
    // unwinding.
#       ifndef _NOTHREADS
        /*REFERENCED*/
        lock lock_instance;
#       endif
    result = *my_free_list;
    if (result == 0) {
        void *r = refill(ROUND_UP(n));
        return r;
    }
    *my_free_list = result -> free_list_link;
        return (result);
    };

  /* p may not be 0 */
    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);
    // acquire lock
#       ifndef _NOTHREADS
        /*REFERENCED*/
        lock lock_instance;
#       endif /* _NOTHREADS */
    q -> free_list_link = *my_free_list;
    *my_free_list = q;
    // lock is released here
  }

  static void * reallocate(void *p, size_t old_sz, size_t new_sz);

} ;

很多宏判断,我们暂且不管,只关注内存是怎么分配的。先看allocte函数

  /* n must be > 0      */
  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);
    // Acquire the lock here with a constructor call.
    // This ensures that it is released in exit or during stack
    // unwinding.
#       ifndef _NOTHREADS
        /*REFERENCED*/
        lock lock_instance;
#       endif
    result = *my_free_list;
    if (result == 0) {
        void *r = refill(ROUND_UP(n));
        return r;
    }
    *my_free_list = result -> free_list_link;
        return (result);
    };

当分配的内存比较大的时候,直接调用了malloc_alloc::allocate(n)方法,也就是直接调用了底层的 malloc 函数(详细见:STL内存分配器详解之__malloc_alloc_template

在讲解后续的代码前,先介绍下FREELIST_INDEX

    static  size_t FREELIST_INDEX(size_t bytes) {
        return (((bytes) + __ALIGN-1)/__ALIGN - 1);
    }

这个函数的目的是求得bytes是__ALIGN的多少倍。为的是,需要取N个字节时,会先确定N是8的多少倍,然后在对应的free_list中取空间进行分配给客户端。
free_list是个指针数组,其中每一个元素挂载了一个链表,第0条链表每一个元素的大小为8个字节,第1条链表,每个元素大小为16字节,.....第N条,每个元素大小为(N+1)*8。
//allocate 步骤
//1.如果需要的内存大于128,直接调用malloc_alloc::allocate
//2.如果是小于128的区块, 则执行下面的流程。
//2.1获得链表。
//2.2取第一号元素。如果取不到就调用refill函数,进行填充。
取到,则将该链表指向下一个元素。

refill 函数

template 
void* __default_alloc_template::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);
}

直接就先调用了chunk_alloc函数,暂时不看chunk_alloc的动作,假设内部已经调整好了内存。返回一个chunk。
如果nobjs的大小为1,就直接返回chunk,也就是说,如果只分配了一个小块,就直接返回这个块。
否则的话,找到n对应的那个链表。然后将获得的chunk切分为n的大小,并连接起来。注意:这里的n已经调整为8的倍数了。

下面看chunk_alloc是怎么进行分配的。

char* __default_alloc_template::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) {//空间不是足够,但是够一个以上的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);
        // Try to make use of the left-over piece.
        //回收空闲池中的碎片。
        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;
            // Try to make do with what we have.  That can't
            // hurt.  We do not try smaller requests, since that tends
            // to result in disaster on multi-process machines.
            //如果从系统中请求不到,这个时候从 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) {
                    *my_free_list = p -> free_list_link;
                    start_free = (char *)p;
                    end_free = start_free + i;
                    return(chunk_alloc(size, nobjs));
                    // Any leftover piece will eventually make it to the
                    // right free list.
                }
            }
        //如果临近的更大区块,获取不到。再次尝试malloc_alloc 的 allocate调用,这个函数会会调用用户定义的内存不足处理函数,
        //用户可能会尝试去施放一些暂时可以施放的空间。
        end_free = 0;   // In case of exception.
            start_free = (char *)malloc_alloc::allocate(bytes_to_get);
            // This should either throw an
            // exception or remedy the situation.  Thus we assume it
            // succeeded.
        }
        heap_size += bytes_to_get;
        end_free = start_free + bytes_to_get;
        return(chunk_alloc(size, nobjs));
    }
}

flow:这里的start_free, end_free。这里称为:reserve_space 区间就是空闲空间,尚未被切分的空间。与free_list中挂载的链表但未使用的节点不是同一个东西。
1.如果reserve_space空间足够大于 total_bytes,20个n 的大小。

你可能感兴趣的:(STL内存分配器详解之__default_alloc_template)