_alloc.h
来自「stl的源码」· C头文件 代码 · 共 581 行 · 第 1/2 页
H
581 行
/* * * 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_INTERNAL_CSTDDEF# include <stl/_cstddef.h>#endif#ifndef _STLP_INTERNAL_CSTDLIB# include <stl/_cstdlib.h>#endif#ifndef _STLP_INTERNAL_CSTRING# include <stl/_cstring.h>#endif#ifndef _STLP_INTERNAL_ALGOBASE_H# include <stl/_algobase.h>#endif#ifndef _STLP_INTERNAL_NEW_HEADER# include <stl/_new.h>#endif#ifndef _STLP_INTERNAL_CONSTRUCT_H# include <stl/_construct.h>#endif_STLP_BEGIN_NAMESPACE// Malloc-based allocator. Typically slower than default alloc below.// Typically thread-safe and more storage efficient.#if !defined (_STLP_USE_NO_IOSTREAMS)typedef void (* __oom_handler_type)();#endifclass _STLP_CLASS_DECLSPEC __malloc_alloc {public: // this one is needed for proper simple_alloc wrapping typedef char value_type; static void* _STLP_CALL allocate(size_t __n)#if !defined (_STLP_USE_NO_IOSTREAMS) ;#else { void *__result = malloc(__n); if (__result == 0) { _STLP_THROW_BAD_ALLOC; } return __result; }#endif static void _STLP_CALL deallocate(void* __p, size_t /* __n */) { free((char*)__p); }#if !defined (_STLP_USE_NO_IOSTREAMS) static __oom_handler_type _STLP_CALL set_malloc_handler(__oom_handler_type __f);#endif};// 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; 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: __debug_alloc() {} ~__debug_alloc() {} static void* _STLP_CALL allocate(size_t); static void _STLP_CALL deallocate(void *, size_t);};# if defined (__OS400__)// dums 02/05/2007: is it really necessary ?enum { _MAX_BYTES = 256 };# elseenum { _MAX_BYTES = 32 * sizeof(void*) };# endif#if !defined (_STLP_USE_NO_IOSTREAMS)// Default node allocator.// With a reasonable compiler, this should be roughly as fast as the// original STL class-specific allocators, but with less fragmentation.class _STLP_CLASS_DECLSPEC __node_alloc { 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; /* __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 __debug_alloc<__node_alloc>;# endif#endif#if defined (_STLP_USE_TEMPLATE_EXPORT)_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__new_alloc>;_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__malloc_alloc>;#endif/* macro to convert the allocator for initialization * not using MEMBER_TEMPLATE_CLASSES as it should work given template constructor */#if defined (_STLP_MEMBER_TEMPLATES) || ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)/* if _STLP_NO_TEMPLATE_CONVERSIONS is set, the member template constructor is * not used implicitly to convert allocator parameter, so let us do it explicitly */# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_NO_TEMPLATE_CONVERSIONS)# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)# else# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __a# endif/* else convert, but only if partial specialization works, since else * Container::allocator_type won't be different */#else# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)#endif// Another allocator adaptor: _Alloc_traits. This serves two// purposes. First, make it possible to write containers that can use// either SGI-style allocators or standard-conforming allocator.// The fully general version.template <class _Tp, class _Allocator>struct _Alloc_traits { typedef _Allocator _Orig;#if !defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) typedef typename _Allocator::_STLP_TEMPLATE rebind<_Tp> _Rebind_type; typedef typename _Rebind_type::other allocator_type; static allocator_type create_allocator(const _Orig& __a) { return allocator_type(_STLP_CONVERT_ALLOCATOR(__a, _Tp)); }#else // this is not actually true, used only to pass this type through // to dynamic overload selection in _STLP_alloc_proxy methods typedef _Allocator allocator_type;#endif};#if defined (_STLP_USE_PERTHREAD_ALLOC)_STLP_END_NAMESPACE// include additional header here# include <stl/_pthread_alloc.h>_STLP_BEGIN_NAMESPACEtypedef __pthread_alloc __alloc_type;#elif defined (_STLP_USE_NEWALLOC)typedef __new_alloc __alloc_type;#elif defined (_STLP_USE_MALLOC)typedef __malloc_alloc __alloc_type;#elsetypedef __node_alloc __alloc_type;#endif#if defined (_STLP_DEBUG_ALLOC)typedef __debug_alloc<__alloc_type> __sgi_alloc;#elsetypedef __alloc_type __sgi_alloc;#endif#if !defined (_STLP_NO_ANACHRONISMS)typedef __sgi_alloc __single_client_alloc;typedef __sgi_alloc __multithreaded_alloc;#endif// This implements allocators as specified in the C++ standard.//// Note that standard-conforming allocators use many language features// that are not yet widely implemented. In particular, they rely on// member templates, partial specialization, partial ordering of function// templates, the typename keyword, and the use of the template keyword// to refer to a template member of a dependent type./*template <class _Tp>struct _AllocatorAux { typedef _Tp* pointer; typedef const _Tp* const_pointer; typedef _Tp& reference; typedef const _Tp& const_reference; pointer address(reference __x) const {return &__x;} const_pointer address(const_reference __x) const { return &__x; }};template <class _Tp>struct _AllocatorAux<const _Tp> { typedef _Tp* pointer; typedef const _Tp* const_pointer; typedef _Tp& reference; typedef const _Tp& const_reference; const_pointer address(const_reference __x) const { return &__x; }};*/template <class _Tp>class allocator //: public _AllocatorAux<_Tp>/* A small helper struct to recognize STLport allocator implementation * from any user specialization one. */ : public __stlport_class<allocator<_Tp> >{public: typedef _Tp value_type; typedef _Tp* pointer; typedef const _Tp* const_pointer; typedef _Tp& reference; typedef const _Tp& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type;#if defined (_STLP_MEMBER_TEMPLATE_CLASSES) template <class _Tp1> struct rebind { typedef allocator<_Tp1> other; };#endif allocator() _STLP_NOTHROW {}#if defined (_STLP_MEMBER_TEMPLATES) template <class _Tp1> allocator(const allocator<_Tp1>&) _STLP_NOTHROW {}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?