⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stl_alloc.h

📁 xprobe package sources code
💻 H
📖 第 1 页 / 共 3 页
字号:
/* * 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   // Extra access restrictions prevent us from really making some things   // private.#else#  define __PRIVATE private#endif#ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG#  define __USE_MALLOC#endif// This implements some standard node allocators.  These are// NOT the same as the allocators in the C++ draft standard or in// in the original STL.  They do not encapsulate different pointer// types; indeed we assume that there is only one pointer type.// The allocation primitives are intended to allocate individual objects,// not larger arenas as with the original STL allocators.#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#ifdef __STL_WIN32THREADS#   include <windows.h>#endif#include <stddef.h>#include <stdlib.h>#include <string.h>#include <assert.h>#ifndef __RESTRICT#  define __RESTRICT#endif#if !defined(__STL_PTHREADS) && !defined(__STL_SOLTHREADS) \ && !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(&_S_node_allocator_lock)#   define __NODE_ALLOCATOR_UNLOCK \        if (threads) pthread_mutex_unlock(&_S_node_allocator_lock)#   define __NODE_ALLOCATOR_THREADS true#   define __VOLATILE volatile  // Needed at -O3 on SGI# endif# ifdef __STL_SOLTHREADS#   include <thread.h>#   define __NODE_ALLOCATOR_LOCK \	if (threads) mutex_lock(&_S_node_allocator_lock)#   define __NODE_ALLOCATOR_UNLOCK \        if (threads) mutex_unlock(&_S_node_allocator_lock)#   define __NODE_ALLOCATOR_THREADS true#   define __VOLATILE# 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(&_S_node_allocator_lock)#   define __NODE_ALLOCATOR_UNLOCK \        LeaveCriticalSection(&_S_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) \                { _S_lock(&_S_node_allocator_lock); }#   define __NODE_ALLOCATOR_UNLOCK if (threads && __us_rsthread_malloc) \                { _S_unlock(&_S_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#endiftemplate <int __inst>class __malloc_alloc_template {private:  static void* _S_oom_malloc(size_t);  static void* _S_oom_realloc(void*, size_t);#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG  static void (* __malloc_alloc_oom_handler)();#endifpublic:  static void* allocate(size_t __n)  {    void* __result = malloc(__n);    if (0 == __result) __result = _S_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 = _S_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);  }};// malloc_alloc out-of-memory handling#ifndef __STL_STATIC_TEMPLATE_MEMBER_BUGtemplate <int __inst>void (* __malloc_alloc_template<__inst>::__malloc_alloc_oom_handler)() = 0;#endiftemplate <int __inst>void*__malloc_alloc_template<__inst>::_S_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>::_S_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 _Tp, class _Alloc>class simple_alloc {public:    static _Tp* allocate(size_t __n)      { return 0 == __n ? 0 : (_Tp*) _Alloc::allocate(__n * sizeof (_Tp)); }    static _Tp* allocate(void)      { return (_Tp*) _Alloc::allocate(sizeof (_Tp)); }    static void deallocate(_Tp* __p, size_t __n)      { if (0 != __n) _Alloc::deallocate(__p, __n * sizeof (_Tp)); }    static void deallocate(_Tp* __p)      { _Alloc::deallocate(__p, sizeof (_Tp)); }};// 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 {_S_extra = 8};  // Size of space used to store size.  Note                        // that this must be large enough to preserve                        // alignment.public:  static void* allocate(size_t __n)  {    char* __result = (char*)_Alloc::allocate(__n + _S_extra);    *(size_t*)__result = __n;    return __result + _S_extra;  }  static void deallocate(void* __p, size_t __n)  {    char* __real_p = (char*)__p - _S_extra;    assert(*(size_t*)__real_p == __n);    _Alloc::deallocate(__real_p, __n + _S_extra);  }  static void* reallocate(void* __p, size_t __old_sz, size_t __new_sz)  {    char* __real_p = (char*)__p - _S_extra;    assert(*(size_t*)__real_p == __old_sz);    char* __result = (char*)      _Alloc::reallocate(__real_p, __old_sz + _S_extra, __new_sz + _S_extra);    *(size_t*)__result = __new_sz;    return __result + _S_extra;  }};# ifdef __USE_MALLOCtypedef malloc_alloc alloc;typedef malloc_alloc single_client_alloc;# else// Default node allocator.// With a reasonable compiler, this should be roughly as fast as the// original STL class-specific allocators, but with less fragmentation.// Default_alloc_template parameters are experimental and MAY// DISAPPEAR in the future.  Clients should just use alloc for now.//// Important implementation properties:// 1. If the client request an object of size > _MAX_BYTES, the resulting//    object will be obtained directly from malloc.// 2. In all other cases, we allocate an object of size exactly//    _S_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.//// The first template parameter specifies whether more than one thread// may use this allocator.  It is safe to allocate an object from// one instance of a default_alloc and deallocate it with another// one.  This effectively transfers its ownership to the second one.// This may have undesirable effects on reference locality.// The second parameter is unreferenced and serves only to allow the// creation of multiple default_alloc instances.// Node that containers built on different allocator instances have// different types, limiting the utility of this approach.#ifdef __SUNPRO_CC// breaks if we make these template class members:  enum {_ALIGN = 8};  enum {_MAX_BYTES = 128};  enum {_NFREELISTS = _MAX_BYTES/_ALIGN};#endiftemplate <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  static size_t  _S_round_up(size_t __bytes)    { return (((__bytes) + _ALIGN-1) & ~(_ALIGN - 1)); }__PRIVATE:  union _Obj {        union _Obj* _M_free_list_link;        char _M_client_data[1];    /* The client sees this.        */  };private:# ifdef __SUNPRO_CC    static _Obj* __VOLATILE _S_free_list[];        // Specifying a size results in duplicate def for 4.1# else    static _Obj* __VOLATILE _S_free_list[_NFREELISTS];# endif  static  size_t _S_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* _S_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* _S_chunk_alloc(size_t __size, int& __nobjs);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -