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

📄 stl_alloc.h

📁 STL完整源码,实现STL文件的读写和三维体的重建及其分析
💻 H
📖 第 1 页 / 共 2 页
字号:
/* * 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#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#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#endiftemplate <int inst>class __malloc_alloc_template {private:static void *oom_malloc(size_t);static void *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 = 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);}};// 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>::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)); }};// 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: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;}};# 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//    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 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;

⌨️ 快捷键说明

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