📄 _alloc.h
字号:
/* * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1997 * Moscow Center for SPARC Technology * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * *//* NOTE: This is an internal header file, included by other STL headers. * You should not attempt to use it directly. */#ifndef _STLP_INTERNAL_ALLOC_H#define _STLP_INTERNAL_ALLOC_H# ifndef _STLP_CSTDDEF///# include <cstddef># endif#if !defined (_STLP_DEBUG_H) && (defined (_STLP_DEBUG) || defined (_STLP_ASSERTIONS))# include <stl/debug/_debug.h>#endif# ifndef _STLP_CSTDLIB///# include <cstdlib># endif# ifndef _STLP_CSTRING///# include <cstring># endif# ifndef __THROW_BAD_ALLOC# if !defined(_STLP_USE_EXCEPTIONS)# if !defined (_STLP_CSTDIO)///# include <cstdio># endif# if !defined (_STLP_CSTDLIB)///# include <cstdlib># endif# define __THROW_BAD_ALLOC puts("out of memory\n"); exit(1)# else /* !defined(_STLP_USE_EXCEPTIONS) */# define __THROW_BAD_ALLOC throw _STLP_STD::bad_alloc()# endif /* !defined(_STLP_USE_EXCEPTIONS) */# endif /* __THROW_BAD_ALLOC */# ifndef _STLP_INTERNAL_NEW_HEADER# include <stl/_new.h># endif#if /* defined (_STLP_THREADS) && */ ! defined (_STLP_INTERNAL_THREADS_H)# include <stl/_threads.h>#endif#ifndef _STLP_INTERNAL_CONSTRUCT_H# include <stl/_construct.h>#endif#ifndef __ALLOC# define __ALLOC __sgi_alloc#endif# ifndef __RESTRICT# define __RESTRICT# endif#if defined (_STLP_THREADS) || (defined(_STLP_OWN_IOSTREAMS) && ! defined (_STLP_NO_THREADS) && ! defined (_NOTHREADS) )# define _STLP_NODE_ALLOCATOR_THREADS true#else# define _STLP_NODE_ALLOCATOR_THREADS false#endif_STLP_BEGIN_NAMESPACE# if defined (_STLP_USE_RAW_SGI_ALLOCATORS)template <class _Tp, class _Alloc> struct __allocator;# endif// Malloc-based allocator. Typically slower than default alloc below.// Typically thread-safe and more storage efficient.typedef void (* __oom_handler_type)();template <int __inst>class __malloc_alloc {private: static void* _STLP_CALL _S_oom_malloc(size_t); static __oom_handler_type __oom_handler;public: // this one is needed for proper simple_alloc wrapping typedef char value_type;# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS) template <class _Tp1> struct rebind { typedef __allocator<_Tp1, __malloc_alloc<__inst> > other; };# endif static void* _STLP_CALL allocate(size_t __n) { void* __result = malloc(__n); if (0 == __result) __result = _S_oom_malloc(__n); return __result; } static void _STLP_CALL deallocate(void* __p, size_t /* __n */) { free((char*)__p); } static __oom_handler_type _STLP_CALL set_malloc_handler(__oom_handler_type __f) { __oom_handler_type __old = __oom_handler; __oom_handler = __f; return(__old); }};// New-based allocator. Typically slower than default alloc below.// Typically thread-safe and more storage efficient.class _STLP_CLASS_DECLSPEC __new_alloc {public: // this one is needed for proper simple_alloc wrapping typedef char value_type;# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_USE_RAW_SGI_ALLOCATORS) template <class _Tp1> struct rebind { typedef __allocator<_Tp1, __new_alloc > other; };# endif static void* _STLP_CALL allocate(size_t __n) { return __stl_new(__n); } static void _STLP_CALL deallocate(void* __p, size_t) { __stl_delete(__p); }};// 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.// This adaptor can only be applied to raw allocatorstemplate <class _Alloc>class __debug_alloc : public _Alloc {public: typedef _Alloc __allocator_type; typedef typename _Alloc::value_type value_type;private: struct __alloc_header { size_t __magic: 16; size_t __type_size:16; _STLP_UINT32_T _M_size; }; // that is 8 bytes for sure // Sunpro CC has bug on enums, so extra_before/after set explicitly enum { __pad=8, __magic=0xdeba, __deleted_magic = 0xdebd, __shred_byte= _STLP_SHRED_BYTE }; enum { __extra_before = 16, __extra_after = 8 }; // Size of space used to store size. Note // that this must be large enough to preserve // alignment. static size_t _STLP_CALL __extra_before_chunk() { return (long)__extra_before/sizeof(value_type)+ (size_t)((long)__extra_before%sizeof(value_type)>0); } static size_t _STLP_CALL __extra_after_chunk() { return (long)__extra_after/sizeof(value_type)+ (size_t)((long)__extra_after%sizeof(value_type)>0); }public:# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS) template <class _Tp1> struct rebind { typedef __allocator< _Tp1, __debug_alloc<_Alloc> > other; };# endif __debug_alloc() {} ~__debug_alloc() {} static void * _STLP_CALL allocate(size_t); static void _STLP_CALL deallocate(void *, size_t);};// 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.# if defined(__OS400__)enum {_ALIGN = 16, _ALIGN_SHIFT=4, _MAX_BYTES = 256};# define _STLP_NFREELISTS 16# elseenum {_ALIGN = 8, _ALIGN_SHIFT=3, _MAX_BYTES = 128};# define _STLP_NFREELISTS 16# endif /* __OS400__ */class _STLP_CLASS_DECLSPEC _Node_alloc_obj {public: _Node_alloc_obj * _M_free_list_link;};template <bool __threads, int __inst>class __node_alloc { _STLP_PRIVATE: static inline size_t _STLP_CALL _S_round_up(size_t __bytes) { return (((__bytes) + (size_t)_ALIGN-1) & ~((size_t)_ALIGN - 1)); } typedef _Node_alloc_obj _Obj;private: // Returns an object of size __n, and optionally adds to size __n free list. static void* _STLP_CALL _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* _STLP_CALL _S_chunk_alloc(size_t __p_size, int& __nobjs); // Chunk allocation state. static _Node_alloc_obj * _STLP_VOLATILE _S_free_list[_STLP_NFREELISTS]; static char* _S_start_free; static char* _S_end_free; static size_t _S_heap_size; static void * _STLP_CALL _M_allocate(size_t __n); /* __p may not be 0 */ static void _STLP_CALL _M_deallocate(void *__p, size_t __n);public: // this one is needed for proper simple_alloc wrapping typedef char value_type;# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS) template <class _Tp1> struct rebind { typedef __allocator<_Tp1, __node_alloc<__threads, __inst> > other; };# endif /* __n must be > 0 */ static void * _STLP_CALL allocate(size_t __n) { return (__n > (size_t)_MAX_BYTES) ? __stl_new(__n) : _M_allocate(__n); } /* __p may not be 0 */ static void _STLP_CALL deallocate(void *__p, size_t __n) { if (__n > (size_t)_MAX_BYTES) __stl_delete(__p); else _M_deallocate(__p, __n); }};# if defined (_STLP_USE_TEMPLATE_EXPORT)_STLP_EXPORT_TEMPLATE_CLASS __malloc_alloc<0>;_STLP_EXPORT_TEMPLATE_CLASS __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0>;# endif /* _STLP_USE_TEMPLATE_EXPORT */typedef __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0> _Node_alloc;# if defined (_STLP_USE_TEMPLATE_EXPORT)_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<_Node_alloc>;_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__new_alloc>;_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__malloc_alloc<0> >;# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -