《STL原始碼剖析》-- stl_alloc.h

凝霜發表於2011-07-27
// Filename:    stl_alloc.h

// Comment By:  凝霜
// E-mail:      mdl2009@vip.qq.com
// Blog:        http://blog.csdn.net/mdl13412

// 特別說明: SGI STL的allocator在我的編譯環境下不使用記憶體池
//          而其記憶體池不進行記憶體釋放操作, 其釋放時機為程式退出或者stack unwinding
//          由作業系統保證記憶體的回收

/*
 * Copyright (c) 1996-1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/* NOTE: This is an internal header file, included by other STL headers.
 *   You should not attempt to use it directly.
 */

#ifndef __SGI_STL_INTERNAL_ALLOC_H
#define __SGI_STL_INTERNAL_ALLOC_H

#ifdef __SUNPRO_CC
#  define __PRIVATE public
// SUN編譯器對private限制過多, 需要開放許可權
#else
#  define __PRIVATE private
#endif

// 為了保證相容性, 對於不支援模板類靜態成員的情況, 使用malloc()進行記憶體分配
#ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG
#  define __USE_MALLOC
#endif

// 實現了一些標準的node allocator
// 但是不同於C++標準或者STL原始STL標準
// 這些allocator沒有封裝不同指標型別
// 事實上我們假定只有一種指標理性
// allocation primitives意在分配不大於原始STL allocator分配的獨立的物件

#if 0
#   include <new>
#   define __THROW_BAD_ALLOC throw bad_alloc
#elif !defined(__THROW_BAD_ALLOC)
#   include <iostream.h>
#   define __THROW_BAD_ALLOC cerr << "out of memory" << endl; exit(1)
#endif

#ifndef __ALLOC
#   define __ALLOC alloc
#endif
#ifdef __STL_WIN32THREADS
#   include <windows.h>
#endif

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifndef __RESTRICT
#  define __RESTRICT
#endif

// 多執行緒支援
// __STL_PTHREADS       // GCC編譯器
// _NOTHREADS           // 不支援多執行緒
// __STL_SGI_THREADS    // SGI機器專用
// __STL_WIN32THREADS   // MSVC編譯器
#if !defined(__STL_PTHREADS) && !defined(_NOTHREADS) \
 && !defined(__STL_SGI_THREADS) && !defined(__STL_WIN32THREADS)
#   define _NOTHREADS
#endif

# ifdef __STL_PTHREADS
    // POSIX Threads
    // This is dubious, since this is likely to be a high contention
    // lock.   Performance may not be adequate.
#   include <pthread.h>
#   define __NODE_ALLOCATOR_LOCK \
        if (threads) pthread_mutex_lock(&__node_allocator_lock)
#   define __NODE_ALLOCATOR_UNLOCK \
        if (threads) pthread_mutex_unlock(&__node_allocator_lock)
#   define __NODE_ALLOCATOR_THREADS true
#   define __VOLATILE volatile  // Needed at -O3 on SGI
# endif
# ifdef __STL_WIN32THREADS
    // The lock needs to be initialized by constructing an allocator
    // objects of the right type.  We do that here explicitly for alloc.
#   define __NODE_ALLOCATOR_LOCK \
        EnterCriticalSection(&__node_allocator_lock)
#   define __NODE_ALLOCATOR_UNLOCK \
        LeaveCriticalSection(&__node_allocator_lock)
#   define __NODE_ALLOCATOR_THREADS true
#   define __VOLATILE volatile  // may not be needed
# endif /* WIN32THREADS */
# ifdef __STL_SGI_THREADS
    // This should work without threads, with sproc threads, or with
    // pthreads.  It is suboptimal in all cases.
    // It is unlikely to even compile on nonSGI machines.

    extern "C" {
      extern int __us_rsthread_malloc;
    }
	// The above is copied from malloc.h.  Including <malloc.h>
	// would be cleaner but fails with certain levels of standard
	// conformance.
#   define __NODE_ALLOCATOR_LOCK if (threads && __us_rsthread_malloc) \
                { __lock(&__node_allocator_lock); }
#   define __NODE_ALLOCATOR_UNLOCK if (threads && __us_rsthread_malloc) \
                { __unlock(&__node_allocator_lock); }
#   define __NODE_ALLOCATOR_THREADS true
#   define __VOLATILE volatile  // Needed at -O3 on SGI
# endif
# ifdef _NOTHREADS
//  Thread-unsafe
#   define __NODE_ALLOCATOR_LOCK
#   define __NODE_ALLOCATOR_UNLOCK
#   define __NODE_ALLOCATOR_THREADS false
#   define __VOLATILE
# endif

__STL_BEGIN_NAMESPACE

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#endif

// Malloc-based allocator.  Typically slower than default alloc below.
// Typically thread-safe and more storage efficient.
#ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG
# ifdef __DECLARE_GLOBALS_HERE
    void (* __malloc_alloc_oom_handler)() = 0;
    // g++ 2.7.2 does not handle static template data members.
# else
    extern void (* __malloc_alloc_oom_handler)();
# endif
#endif

// 一級配置器
template <int inst>
class __malloc_alloc_template
{
private:
    // 用於在設定了__malloc_alloc_oom_handler情況下迴圈分配記憶體,
    // 直到成功分配
    static void *oom_malloc(size_t);
    static void *oom_realloc(void *, size_t);

    // 如果編譯器支援模板類靜態成員, 則使用錯誤處理函式, 類似C++的set_new_handler()
    // 預設值為0, 如果不設定, 則記憶體分配失敗時直接__THROW_BAD_ALLOC
#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
    static void (* __malloc_alloc_oom_handler)();
#endif

public:
    // 分配指定大小的記憶體(size_t n), 如果分配失敗, 則進入迴圈分配階段
    // 迴圈分配前提是要保證正確設定了__malloc_alloc_oom_handler
    static void * allocate(size_t n)
    {
        void *result = malloc(n);
        if (0 == result) result = oom_malloc(n);
        return result;
    }

    // 後面的size_t是為了相容operator delele
    static void deallocate(void *p, size_t /* n */)
    { free(p); }

    // 重新分配記憶體大小, 第二個引數是為了相容operator new
    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;
    }

    // 設定錯誤處理函式, 返回原來的函式指標
    // 不屬於C++標準規定的介面
    static void (* set_malloc_handler(void (*f)()))()
    {
        void (* old)() = __malloc_alloc_oom_handler;
        __malloc_alloc_oom_handler = f;
        return(old);
    }
};

// malloc_alloc out-of-memory handling

#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
template <int inst>
void (* __malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;
#endif

// 如果設定了__malloc_alloc_oom_handler, 則首先執行錯誤處理函式, 然後迴圈分配直到成功
// 如果未設定__malloc_alloc_oom_handler, __THROW_BAD_ALLOC
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);
    }
}

// 這個版本的STL並沒有使用non-type模板引數
typedef __malloc_alloc_template<0> malloc_alloc;

// 這個類中的介面其實就是STL標準中的allocator的介面
// 實際上所有的SGI STL都使用這個進行記憶體配置
// 例如: stl_vector.h中
// template <class T, class Alloc = alloc>
// class vector
// {
//      ...
// protected:
//      typedef simple_alloc<value_type, Alloc> data_allocator;
//      ...
//};
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)); }
};

// Allocator adaptor to check size arguments for debugging.
// Reports errors using assert.  Checking can be disabled with
// NDEBUG, but it's far better to just use the underlying allocator
// instead when no checking is desired.
// There is some evidence that this can confuse Purify.
template <class Alloc>
class debug_alloc
{
private:
    enum {extra = 8};       // Size of space used to store size.  Note
                            // that this must be large enough to preserve
                            // alignment.

public:

    // extra 保證不會分配為0的記憶體空間, 而且要保證記憶體對齊
    // 把分配記憶體的最前面設定成n的大小, 用於後面校驗
    // 記憶體對齊的作用就是保護前面extra大小的資料不被修改
    static void * allocate(size_t n)
    {
        char *result = (char *)Alloc::allocate(n + extra);
        *(size_t *)result = n;
        return result + extra;
    }

    // 如果*(size_t *)real_p != n則肯定發生向前越界
    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;
    }
};

# ifdef __USE_MALLOC

typedef malloc_alloc alloc;
typedef malloc_alloc single_client_alloc;

# else

// 預設的node allocator
// 如果有合適的編譯器, 速度上與原始的STL class-specific allocators大致等價
// 但是具有產生更少記憶體碎片的優點
// Default_alloc_template引數是用於實驗性質的, 在未來可能會消失
// 客戶只能在當下使用alloc
//
// 重要的實現屬性:
// 1. 如果客戶請求一個size > __MAX_BYTE的物件, 則直接使用malloc()分配
// 2. 對於其它情況下, 我們將請求物件的大小按照記憶體對齊向上舍入ROUND_UP(requested_size)
// TODO: 待翻譯
// 2. In all other cases, we allocate an object of size exactly
//    ROUND_UP(requested_size).  Thus the client has enough size
//    information that we can return the object to the proper free list
//    without permanently losing part of the object.
//

// 第一個模板引數指定是否有多於一個執行緒使用本allocator
// 在一個default_alloc例項中分配物件, 在另一個deallocate例項中釋放物件, 是安全的
// 這有效的轉換其所有權到另一個物件
// 這可能導致對我們引用的區域產生不良影響
// 第二個模板引數僅僅用於建立多個default_alloc例項
// 不同容器使用不同allocator例項建立的node擁有不同型別, 這限制了此方法的通用性

// Sun C++ compiler需要在類外定義這些列舉
#ifdef __SUNPRO_CC
// breaks if we make these template class members:
  enum {__ALIGN = 8};
  enum {__MAX_BYTES = 128};
  enum {__NFREELISTS = __MAX_BYTES/__ALIGN};
#endif

template <bool threads, int inst>
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
    // 向上舍入操作
    // 解釋一下, __ALIGN - 1指明的是實際記憶體對齊的粒度
    // 例如__ALIGN = 8時, 我們只需要7就可以實際表示8個數(0~7)
    // 那麼~(__ALIGN - 1)就是進行舍入的粒度
    // 我們將(bytes) + __ALIGN-1)就是先進行進位, 然後截斷
    // 這就保證了我是向上舍入的
    // 例如byte = 100, __ALIGN = 8的情況
    // ~(__ALIGN - 1) = (1 000)B
    // ((bytes) + __ALIGN-1) = (1 101 011)B
    // (((bytes) + __ALIGN-1) & ~(__ALIGN - 1)) = (1 101 000 )B = (104)D
    // 104 / 8 = 13, 這就實現了向上舍入
    // 對於byte剛好滿足記憶體對齊的情況下, 結果保持byte大小不變
    // 記得《Hacker's Delight》上面有相關的計算
    // 這個表示式與下面給出的等價
    // ((((bytes) + _ALIGN - 1) * _ALIGN) / _ALIGN)
    // 但是SGI STL使用的方法效率非常高
    static size_t ROUND_UP(size_t bytes)
    {
        return (((bytes) + __ALIGN-1) & ~(__ALIGN - 1));
    }
__PRIVATE:
    // 管理記憶體連結串列用
    // 為了盡最大可能減少記憶體的使用, 這裡使用一個union
    // 如果使用第一個成員, 則指向另一個相同的union obj
    // 而如果使用第二個成員, 則指向實際的記憶體區域
    // 這樣就實現了連結串列結點只使用一個指標的大小空間, 卻能同時做索引和指向記憶體區域
    // 這個技巧性非常強, 值得學習
    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
    // 這裡分配的free_list為16
    // 對應的記憶體鏈容量分別為8, 16, 32 ... 128
    static obj * __VOLATILE free_list[__NFREELISTS];
# endif
    // 根據待待分配的空間大小, 在free_list中選擇合適的大小
    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);

  // 記憶體池
  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;

    // 如果待分配物件大於__MAX_BYTES, 使用一級配置器分配
    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;

    // 對於大於__MAX_BYTES的物件, 因為採用的是一級配置器分配, 所以同樣使用一級配置器釋放
    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);
} ;

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;  // 計算記憶體池剩餘容量

    // 如果記憶體池中剩餘記憶體>=需要分配的內記憶體, 返回start_free指向的記憶體塊,
    // 並且重新設定記憶體池起始點
    if (bytes_left >= total_bytes) {
        result = start_free;
        start_free += total_bytes;
        return(result);
    }
    // 如果記憶體吃中剩餘的容量不夠分配, 但是能至少分配一個節點時,
    // 返回所能分配的最多的節點, 返回start_free指向的記憶體塊
    // 並且重新設定記憶體池起始點
    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[FREELIST_INDEX(bytes_left)]
        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.
            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.
                }
            }

            // 再次失敗, 直接呼叫一級配置器分配, 期待異常處理函式能提供幫助
            // 不過在我看來, 記憶體分配失敗進行其它嘗試已經沒什麼意義了,
            // 最好直接log, 然後讓程式崩潰
	    end_free = 0;	// In case of exception.
            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));
    }
}


// 返回一個大小為n的物件, 並且加入到free_list[FREELIST_INDEX(n)]
// 進行分配操作時, 根據具體環境決定是否加鎖
// 我們假定要分配的記憶體滿足記憶體對齊要求
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);

    // 在chunk的空間中建立free_list
      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;

    // 如果old_size和new_size均大於__MAX_BYTES, 則直接呼叫realloc()
    // 因為這部分記憶體不是經過記憶體池分配的
    if (old_sz > (size_t) __MAX_BYTES && new_sz > (size_t) __MAX_BYTES) {
        return(realloc(p, new_sz));
    }
    // 如果ROUND_UP(old_sz) == ROUND_UP(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);
}

#ifdef __STL_PTHREADS
    template <bool threads, int inst>
    pthread_mutex_t
    __default_alloc_template<threads, inst>::__node_allocator_lock
        = PTHREAD_MUTEX_INITIALIZER;
#endif

#ifdef __STL_WIN32THREADS
    template <bool threads, int inst> CRITICAL_SECTION
    __default_alloc_template<threads, inst>::__node_allocator_lock;

    template <bool threads, int inst> bool
    __default_alloc_template<threads, inst>::__node_allocator_lock_initialized
	= false;
#endif

#ifdef __STL_SGI_THREADS
__STL_END_NAMESPACE
#include <mutex.h>
#include <time.h>
__STL_BEGIN_NAMESPACE
// Somewhat generic lock implementations.  We need only test-and-set
// and some way to sleep.  These should work with both SGI pthreads
// and sproc threads.  They may be useful on other systems.
template <bool threads, int inst>
volatile unsigned long
__default_alloc_template<threads, inst>::__node_allocator_lock = 0;

#if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) || defined(__GNUC__)
#   define __test_and_set(l,v) test_and_set(l,v)
#endif

template <bool threads, int inst>
void
__default_alloc_template<threads, inst>::__lock(volatile unsigned long *lock)
{
    const unsigned low_spin_max = 30;  // spin cycles if we suspect uniprocessor
    const unsigned high_spin_max = 1000; // spin cycles for multiprocessor
    static unsigned spin_max = low_spin_max;
    unsigned my_spin_max;
    static unsigned last_spins = 0;
    unsigned my_last_spins;
    static struct timespec ts = {0, 1000};
    unsigned junk;
#   define __ALLOC_PAUSE junk *= junk; junk *= junk; junk *= junk; junk *= junk
    int i;

    if (!__test_and_set((unsigned long *)lock, 1)) {
        return;
    }
    my_spin_max = spin_max;
    my_last_spins = last_spins;
    for (i = 0; i < my_spin_max; i++) {
        if (i < my_last_spins/2 || *lock) {
            __ALLOC_PAUSE;
            continue;
        }
        if (!__test_and_set((unsigned long *)lock, 1)) {
            // got it!
            // Spinning worked.  Thus we're probably not being scheduled
            // against the other process with which we were contending.
            // Thus it makes sense to spin longer the next time.
            last_spins = i;
            spin_max = high_spin_max;
            return;
        }
    }
    // We are probably being scheduled against the other process.  Sleep.
    spin_max = low_spin_max;
    for (;;) {
        if (!__test_and_set((unsigned long *)lock, 1)) {
            return;
        }
        nanosleep(&ts, 0);
    }
}

template <bool threads, int inst>
inline void
__default_alloc_template<threads, inst>::__unlock(volatile unsigned long *lock)
{
#   if defined(__GNUC__) && __mips >= 3
        asm("sync");
        *lock = 0;
#   elif __mips >= 3 && (defined (_ABIN32) || defined(_ABI64))
        __lock_release(lock);
#   else
        *lock = 0;
        // This is not sufficient on many multiprocessors, since
        // writes to protected variables and the lock may be reordered.
#   endif
}
#endif

// 記憶體池起始位置
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[
# ifdef __SUNPRO_CC
    __NFREELISTS
# else
    __default_alloc_template<threads, inst>::__NFREELISTS
# endif
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
// The 16 zeros are necessary to make version 4.1 of the SunPro
// compiler happy.  Otherwise it appears to allocate too little
// space for the array.

# ifdef __STL_WIN32THREADS
  // Create one to get critical section initialized.
  // We do this onece per file, but only the first constructor
  // does anything.
  static alloc __node_allocator_dummy_instance;
# endif

#endif /* ! __USE_MALLOC */

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1174
#endif

__STL_END_NAMESPACE

#undef __PRIVATE

#endif /* __SGI_STL_INTERNAL_ALLOC_H */

// Local Variables:
// mode:C++
// End:


相關文章