williamr@2: /*
williamr@2:  *
williamr@2:  * Copyright (c) 1996,1997
williamr@2:  * Silicon Graphics Computer Systems, Inc.
williamr@2:  *
williamr@2:  * Copyright (c) 1997
williamr@2:  * Moscow Center for SPARC Technology
williamr@2:  *
williamr@2:  * Copyright (c) 1999 
williamr@2:  * Boris Fomitchev
williamr@2:  *
williamr@2:  * This material is provided "as is", with absolutely no warranty expressed
williamr@2:  * or implied. Any use is at your own risk.
williamr@2:  *
williamr@2:  * Permission to use or copy this software for any purpose is hereby granted 
williamr@2:  * without fee, provided the above notices are retained on all copies.
williamr@2:  * Permission to modify the code and to distribute modified code is granted,
williamr@2:  * provided the above notices are retained, and a notice that the code was
williamr@2:  * modified is included with the above copyright notice.
williamr@2:  *
williamr@2:  */
williamr@2: 
williamr@2: /* NOTE: This is an internal header file, included by other STL headers.
williamr@2:  *   You should not attempt to use it directly.
williamr@2:  */
williamr@2: 
williamr@2: #ifndef _STLP_INTERNAL_ALLOC_H
williamr@2: #define _STLP_INTERNAL_ALLOC_H
williamr@2: 
williamr@2: # ifndef _STLP_CSTDDEF
williamr@2: #  include <cstddef>
williamr@2: # endif
williamr@2: 
williamr@2: #if !defined (_STLP_DEBUG_H) && (defined  (_STLP_DEBUG) || defined (_STLP_ASSERTIONS))
williamr@2: # include <stl/debug/_debug.h>
williamr@2: #endif
williamr@2: 
williamr@2: # ifndef _STLP_CSTDLIB
williamr@2: #  include <cstdlib>
williamr@2: # endif
williamr@2: # ifndef _STLP_CSTRING
williamr@2: #  include <cstring>
williamr@2: # endif
williamr@2: 
williamr@2: # ifndef __THROW_BAD_ALLOC
williamr@2: #  if !defined(_STLP_USE_EXCEPTIONS)
williamr@2: #   if !defined (_STLP_CSTDIO)
williamr@2: #    include <cstdio>
williamr@2: #   endif
williamr@2: #   if !defined (_STLP_CSTDLIB)
williamr@2: #    include <cstdlib>
williamr@2: #   endif
williamr@2: #   define __THROW_BAD_ALLOC puts("out of memory\n"); exit(1)
williamr@2: #  else /* !defined(_STLP_USE_EXCEPTIONS) */
williamr@2: #   define __THROW_BAD_ALLOC throw _STLP_STD::bad_alloc()
williamr@2: #  endif /* !defined(_STLP_USE_EXCEPTIONS) */
williamr@2: # endif   /* __THROW_BAD_ALLOC */
williamr@2: 
williamr@2: # ifndef _STLP_INTERNAL_NEW_HEADER
williamr@2: #  include <stl/_new.h>
williamr@2: # endif
williamr@2: 
williamr@2: // #if ! defined (__SYMBIAN32__)
williamr@2: #if /* defined (_STLP_THREADS) && */ ! defined (_STLP_INTERNAL_THREADS_H)
williamr@2: # include <stl/_threads.h>
williamr@2: // #endif
williamr@2: #endif
williamr@2: 
williamr@2: #ifndef _STLP_INTERNAL_CONSTRUCT_H
williamr@2: # include <stl/_construct.h>
williamr@2: #endif
williamr@2: 
williamr@2: #ifndef __ALLOC
williamr@2: #   define __ALLOC __sgi_alloc
williamr@2: #endif
williamr@2: 
williamr@2: # ifndef __RESTRICT
williamr@2: #  define __RESTRICT
williamr@2: # endif
williamr@2: 
williamr@2: #if defined (_STLP_THREADS) || (defined(_STLP_OWN_IOSTREAMS) && ! defined (_STLP_NO_THREADS) && ! defined (_NOTHREADS) )
williamr@2: # define _STLP_NODE_ALLOCATOR_THREADS true
williamr@2: #else
williamr@2: # define _STLP_NODE_ALLOCATOR_THREADS false
williamr@2: #endif
williamr@2: 
williamr@2: _STLP_BEGIN_NAMESPACE
williamr@2: 
williamr@2: # if defined (_STLP_USE_RAW_SGI_ALLOCATORS)
williamr@2: template <class _Tp, class _Alloc> struct __allocator;
williamr@2: # endif
williamr@2: 
williamr@2: #ifndef _STLP_NO_NODE_ALLOC
williamr@2: 
williamr@2: // Malloc-based allocator.  Typically slower than default alloc below.
williamr@2: // Typically thread-safe and more storage efficient.
williamr@2: 
williamr@2: typedef void (* __oom_handler_type)();
williamr@2: 
williamr@2: template <int __inst>
williamr@2: class __malloc_alloc {
williamr@2: private:
williamr@2:   static void* _STLP_CALL _S_oom_malloc(size_t);
williamr@2:   static __oom_handler_type __oom_handler;
williamr@2: public:
williamr@2:   // this one is needed for proper simple_alloc wrapping
williamr@2:   typedef char value_type;
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef __allocator<_Tp1, __malloc_alloc<__inst> > other;
williamr@2:   };
williamr@2: # endif
williamr@2:   static void* _STLP_CALL allocate(size_t __n)    {
williamr@2:     void* __result = malloc(__n);
williamr@2:     if (0 == __result) __result = _S_oom_malloc(__n);
williamr@2:     return __result;
williamr@2:   }
williamr@2:   static void _STLP_CALL deallocate(void* __p, size_t /* __n */) { free((char*)__p); }
williamr@2:   static __oom_handler_type _STLP_CALL set_malloc_handler(__oom_handler_type __f) {
williamr@2:     __oom_handler_type __old = __oom_handler;
williamr@2:     __oom_handler = __f;
williamr@2:     return(__old);
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: # endif
williamr@2: 
williamr@2: // New-based allocator.  Typically slower than default alloc below.
williamr@2: // Typically thread-safe and more storage efficient.
williamr@2: class _STLP_CLASS_DECLSPEC __new_alloc {
williamr@2: public:
williamr@2:   // this one is needed for proper simple_alloc wrapping
williamr@2:   typedef char value_type;
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) &&  defined(_STLP_USE_RAW_SGI_ALLOCATORS)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef __allocator<_Tp1, __new_alloc > other;
williamr@2:   };
williamr@2: # endif
williamr@2:   static void* _STLP_CALL  allocate(size_t __n) {  
williamr@2:     return __stl_new(__n); 
williamr@2:   }
williamr@2:   static void _STLP_CALL deallocate(void* __p, size_t) { __stl_delete(__p); }
williamr@2: };
williamr@2: 
williamr@2: 
williamr@2: // Allocator adaptor to check size arguments for debugging.
williamr@2: // Reports errors using assert.  Checking can be disabled with
williamr@2: // NDEBUG, but it's far better to just use the underlying allocator
williamr@2: // instead when no checking is desired.
williamr@2: // There is some evidence that this can confuse Purify.
williamr@2: // This adaptor can only be applied to raw allocators
williamr@2: 
williamr@2: template <class _Alloc>
williamr@2: class __debug_alloc : public _Alloc {
williamr@2: public:
williamr@2:   typedef _Alloc __allocator_type;
williamr@2:   typedef typename _Alloc::value_type value_type;
williamr@2: private:
williamr@2:   struct __alloc_header {
williamr@2:     size_t __magic: 16;
williamr@2:     size_t __type_size:16;
williamr@2:     _STLP_UINT32_T _M_size;
williamr@2:   }; // that is 8 bytes for sure
williamr@2:   // Sunpro CC has bug on enums, so extra_before/after set explicitly
williamr@2:   enum { __pad=8, __magic=0xdeba, __deleted_magic = 0xdebd,
williamr@2: 	 __shred_byte= _STLP_SHRED_BYTE
williamr@2:   };
williamr@2: 
williamr@2:   enum { __extra_before = 16, __extra_after = 8 };
williamr@2:   // Size of space used to store size.  Note
williamr@2:   // that this must be large enough to preserve
williamr@2:   // alignment.
williamr@2:   static size_t _STLP_CALL __extra_before_chunk() {
williamr@2:     return (long)__extra_before/sizeof(value_type)+
williamr@2:       (size_t)((long)__extra_before%sizeof(value_type)>0);
williamr@2:   }
williamr@2:   static size_t _STLP_CALL __extra_after_chunk() {
williamr@2:     return (long)__extra_after/sizeof(value_type)+
williamr@2:       (size_t)((long)__extra_after%sizeof(value_type)>0);
williamr@2:   }
williamr@2: public:
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef __allocator< _Tp1, __debug_alloc<_Alloc> > other;
williamr@2:   };
williamr@2: # endif
williamr@2:   __debug_alloc() {}
williamr@2:   ~__debug_alloc() {}
williamr@2:   static void * _STLP_CALL allocate(size_t);
williamr@2:   static void _STLP_CALL deallocate(void *, size_t);
williamr@2: };
williamr@2: 
williamr@2: # if defined(__OS400__)
williamr@2: enum {_ALIGN = 16, _ALIGN_SHIFT=4, _MAX_BYTES = 256};
williamr@2: #  define  _STLP_NFREELISTS 16
williamr@2: # else
williamr@2: enum {_ALIGN = 8, _ALIGN_SHIFT=3, _MAX_BYTES = 128};
williamr@2: #  define  _STLP_NFREELISTS 16
williamr@2: # endif /* __OS400__ */
williamr@2: 
williamr@2: #ifndef _STLP_NO_NODE_ALLOC
williamr@2: 
williamr@2: // Default node allocator.
williamr@2: // With a reasonable compiler, this should be roughly as fast as the
williamr@2: // original STL class-specific allocators, but with less fragmentation.
williamr@2: // Default_alloc_template parameters are experimental and MAY
williamr@2: // DISAPPEAR in the future.  Clients should just use alloc for now.
williamr@2: //
williamr@2: // Important implementation properties:
williamr@2: // 1. If the client request an object of size > _MAX_BYTES, the resulting
williamr@2: //    object will be obtained directly from malloc.
williamr@2: // 2. In all other cases, we allocate an object of size exactly
williamr@2: //    _S_round_up(requested_size).  Thus the client has enough size
williamr@2: //    information that we can return the object to the proper free list
williamr@2: //    without permanently losing part of the object.
williamr@2: //
williamr@2: 
williamr@2: // The first template parameter specifies whether more than one thread
williamr@2: // may use this allocator.  It is safe to allocate an object from
williamr@2: // one instance of a default_alloc and deallocate it with another
williamr@2: // one.  This effectively transfers its ownership to the second one.
williamr@2: // This may have undesirable effects on reference locality.
williamr@2: // The second parameter is unreferenced and serves only to allow the
williamr@2: // creation of multiple default_alloc instances.
williamr@2: 
williamr@2: class _STLP_CLASS_DECLSPEC _Node_alloc_obj {
williamr@2: public:
williamr@2:     _Node_alloc_obj * _M_free_list_link;
williamr@2: };
williamr@2: 
williamr@2: template <bool __threads, int __inst>
williamr@2: class __node_alloc {
williamr@2:   _STLP_PRIVATE:
williamr@2:   static inline size_t _STLP_CALL _S_round_up(size_t __bytes) { return (((__bytes) + (size_t)_ALIGN-1) & ~((size_t)_ALIGN - 1)); }
williamr@2:   typedef _Node_alloc_obj _Obj;
williamr@2: private:
williamr@2:   // Returns an object of size __n, and optionally adds to size __n free list.
williamr@2:   static void*  _STLP_CALL _S_refill(size_t __n);
williamr@2:   // Allocates a chunk for nobjs of size size.  nobjs may be reduced
williamr@2:   // if it is inconvenient to allocate the requested number.
williamr@2:   static char*  _STLP_CALL _S_chunk_alloc(size_t __p_size, int& __nobjs);
williamr@2:   // Chunk allocation state.
williamr@2:   static _Node_alloc_obj * _STLP_VOLATILE _S_free_list[_STLP_NFREELISTS]; 
williamr@2:   static char* _S_start_free;
williamr@2:   static char* _S_end_free;
williamr@2:   static size_t _S_heap_size;
williamr@2:   static void * _STLP_CALL _M_allocate(size_t __n);
williamr@2:   /* __p may not be 0 */
williamr@2:   static void _STLP_CALL _M_deallocate(void *__p, size_t __n);
williamr@2: public:
williamr@2:   // this one is needed for proper simple_alloc wrapping
williamr@2:   typedef char value_type;
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef __allocator<_Tp1, __node_alloc<__threads, __inst> > other;
williamr@2:   };
williamr@2: # endif
williamr@2:   /* __n must be > 0      */
williamr@2:   static void * _STLP_CALL allocate(size_t __n) { 
williamr@2: return (__n > (size_t)_MAX_BYTES) ?  __stl_new(__n) : _M_allocate(__n); }
williamr@2:   /* __p may not be 0 */
williamr@2:   static void _STLP_CALL deallocate(void *__p, size_t __n) { 
williamr@2: if (__n > (size_t)_MAX_BYTES) __stl_delete(__p); else _M_deallocate(__p, __n); }
williamr@2: };
williamr@2: 
williamr@2: # if defined (_STLP_USE_TEMPLATE_EXPORT)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS __malloc_alloc<0>;
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0>;
williamr@2: # endif /* _STLP_USE_TEMPLATE_EXPORT */
williamr@2: typedef __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0> _Node_alloc;
williamr@2: # if defined (_STLP_USE_TEMPLATE_EXPORT)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<_Node_alloc>;
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__new_alloc>;
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__malloc_alloc<0> >;
williamr@2: # endif
williamr@2: 
williamr@2: #endif
williamr@2: 
williamr@2: # if defined (_STLP_USE_PERTHREAD_ALLOC)
williamr@2: 
williamr@2: _STLP_END_NAMESPACE
williamr@2: // include additional header here
williamr@2: # include <stl/_pthread_alloc.h>
williamr@2: _STLP_BEGIN_NAMESPACE
williamr@2: 
williamr@2: #  if defined ( _STLP_DEBUG_ALLOC )
williamr@2: typedef __debug_alloc<__pthread_alloc> __sgi_alloc;
williamr@2: #  else
williamr@2: typedef __pthread_alloc __sgi_alloc;
williamr@2: #  endif /* _STLP_DEBUG_ALLOC */
williamr@2: 
williamr@2: typedef __pthread_alloc __single_client_alloc;
williamr@2: typedef __pthread_alloc __multithreaded_alloc;
williamr@2: 
williamr@2: # else
williamr@2: 
williamr@2: # if defined ( _STLP_USE_NEWALLOC )
williamr@2: 
williamr@2: #  if defined ( _STLP_DEBUG_ALLOC )
williamr@2: typedef __debug_alloc<__new_alloc> __sgi_alloc;
williamr@2: #  else
williamr@2: typedef __new_alloc __sgi_alloc;
williamr@2: #  endif /* _STLP_DEBUG_ALLOC */
williamr@2: 
williamr@2: typedef __new_alloc __single_client_alloc;
williamr@2: typedef __new_alloc __multithreaded_alloc;
williamr@2: 
williamr@2: #  elif defined (_STLP_USE_MALLOC)
williamr@2: 
williamr@2: #   if defined ( _STLP_DEBUG_ALLOC )
williamr@2: typedef __debug_alloc<__malloc_alloc<0> > __sgi_alloc;
williamr@2: #   else
williamr@2: typedef __malloc_alloc<0> __sgi_alloc;
williamr@2: #   endif /* _STLP_DEBUG_ALLOC */
williamr@2: 
williamr@2: typedef __malloc_alloc<0> __single_client_alloc;
williamr@2: typedef __malloc_alloc<0> __multithreaded_alloc;
williamr@2: 
williamr@2: # else
williamr@2: 
williamr@2: #   if defined ( _STLP_DEBUG_ALLOC )
williamr@2: typedef __debug_alloc<_Node_alloc> __sgi_alloc;
williamr@2: #   else
williamr@2: typedef _Node_alloc __sgi_alloc;
williamr@2: #   endif
williamr@2: 
williamr@2: typedef __node_alloc<false, 0> __single_client_alloc;
williamr@2: typedef __node_alloc<true, 0>  __multithreaded_alloc;
williamr@2: 
williamr@2: #  endif /* _STLP_USE_NEWALLOC */
williamr@2: # endif /* PTHREAD_ALLOC */
williamr@2: 
williamr@2: // This implements allocators as specified in the C++ standard.  
williamr@2: //
williamr@2: // Note that standard-conforming allocators use many language features
williamr@2: // that are not yet widely implemented.  In particular, they rely on
williamr@2: // member templates, partial specialization, partial ordering of function
williamr@2: // templates, the typename keyword, and the use of the template keyword
williamr@2: // to refer to a template member of a dependent type.
williamr@2: 
williamr@2: template <class _Tp>
williamr@2: class allocator {
williamr@2: public:
williamr@2: 
williamr@2:   typedef _Tp        value_type;
williamr@2:   typedef value_type *       pointer;
williamr@2:   typedef const _Tp* const_pointer;
williamr@2:   typedef _Tp&       reference;
williamr@2:   typedef const _Tp& const_reference;
williamr@2:   typedef size_t     size_type;
williamr@2:   typedef ptrdiff_t  difference_type;
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef allocator<_Tp1> other;
williamr@2:   };
williamr@2: # endif
williamr@2:   allocator() _STLP_NOTHROW {}
williamr@2:  # if defined (_STLP_MEMBER_TEMPLATES)
williamr@2:   template <class _Tp1> allocator(const allocator<_Tp1>&) _STLP_NOTHROW {}
williamr@2:  # endif    
williamr@2:   allocator(const allocator<_Tp>&) _STLP_NOTHROW {}
williamr@2:   ~allocator() _STLP_NOTHROW {}
williamr@2:   pointer address(reference __x) const { return &__x; }
williamr@2:   const_pointer address(const_reference __x) const { return &__x; }
williamr@2:   // __n is permitted to be 0.  The C++ standard says nothing about what the return value is when __n == 0.
williamr@2:   _Tp* allocate(size_type __n, const void* = 0) { 
williamr@2:     return __n != 0 ? __REINTERPRET_CAST(value_type*,__sgi_alloc::allocate(__n * sizeof(value_type))) : 0;
williamr@2:   }
williamr@2:   // __p is permitted to be a null pointer, only if n==0.
williamr@2:   void deallocate(pointer __p, size_type __n) {
williamr@2:     _STLP_ASSERT( (__p == 0) == (__n == 0) )
williamr@2:       if (__p != 0) __sgi_alloc::deallocate((void*)__p, __n * sizeof(value_type));
williamr@2:   }
williamr@2:   // backwards compatibility
williamr@2:   void deallocate(pointer __p) const {  if (__p != 0) __sgi_alloc::deallocate((void*)__p, sizeof(value_type)); }
williamr@2:   size_type max_size() const _STLP_NOTHROW  { return size_t(-1) / sizeof(value_type); }
williamr@2:   void construct(pointer __p, const _Tp& __val) { _STLP_STD::_Construct(__p, __val); }
williamr@2:   void destroy(pointer __p) { _STLP_STD::_Destroy(__p); }
williamr@2: # if defined(__MRC__)||(defined(__SC__) && !defined(__DMC__))
williamr@2:   template <class _T2> bool operator==(const allocator<_T2>&) const  _STLP_NOTHROW { return true; }
williamr@2:   template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
williamr@2: # endif
williamr@2: };
williamr@2: 
williamr@2: _STLP_TEMPLATE_NULL
williamr@2: class _STLP_CLASS_DECLSPEC allocator<void> {
williamr@2: public:
williamr@2:   typedef size_t      size_type;
williamr@2:   typedef ptrdiff_t   difference_type;
williamr@2:   typedef void*       pointer;
williamr@2:   typedef const void* const_pointer;
williamr@2: # if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
williamr@2:   typedef void        value_type;
williamr@2: # endif
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
williamr@2:   template <class _Tp1> struct rebind {
williamr@2:     typedef allocator<_Tp1> other;
williamr@2:   };
williamr@2: # endif
williamr@2: # if defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__))		//*ty 03/24/2001 - MPW compilers get confused on these operator definitions
williamr@2:   template <class _T2> bool operator==(const allocator<_T2>&) const _STLP_NOTHROW { return true; }
williamr@2:   template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
williamr@2: # endif
williamr@2: };
williamr@2: 
williamr@2: #if !(defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__)))		//*ty 03/24/2001 - MPW compilers get confused on these operator definitions
williamr@2: template <class _T1, class _T2> inline bool  _STLP_CALL operator==(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return true; }
williamr@2: template <class _T1, class _T2> inline bool  _STLP_CALL operator!=(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return false; }
williamr@2: #endif
williamr@2: 
williamr@2: # if defined (_STLP_USE_TEMPLATE_EXPORT)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS allocator<char>;
williamr@2: #  if defined (_STLP_HAS_WCHAR_T)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS allocator<wchar_t>;
williamr@2: #  endif
williamr@2: # endif /* _STLP_USE_TEMPLATE_EXPORT */
williamr@2: 
williamr@2: // Another allocator adaptor: _Alloc_traits.  This serves two
williamr@2: // purposes.  First, make it possible to write containers that can use
williamr@2: // either SGI-style allocators or standard-conforming allocator.
williamr@2: 
williamr@2: // The fully general version.
williamr@2: template <class _Tp, class _Allocator>
williamr@2: struct _Alloc_traits
williamr@2: {
williamr@2:   typedef _Allocator _Orig;
williamr@2: # if defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM) 
williamr@2:   typedef typename _Allocator::_STLP_TEMPLATE rebind<_Tp> _Rebind_type;
williamr@2:   typedef typename _Rebind_type::other  allocator_type;
williamr@2:   static allocator_type create_allocator(const _Orig& __a) { return allocator_type(__a); }
williamr@2: # else
williamr@2:   // this is not actually true, used only to pass this type through
williamr@2:   // to dynamic overload selection in _STLP_alloc_proxy methods
williamr@2:   typedef _Allocator allocator_type;
williamr@2: # endif /* _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
williamr@2: };
williamr@2: 
williamr@2: #ifndef _STLP_FORCE_ALLOCATORS
williamr@2: #define _STLP_FORCE_ALLOCATORS(a,y) 
williamr@2: #endif
williamr@2: 
williamr@2: #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && ! defined (_STLP_MEMBER_TEMPLATE_CLASSES)
williamr@2: // The version for the default allocator, for rare occasion when we have partial spec w/o member template classes
williamr@2: template <class _Tp, class _Tp1>
williamr@2: struct _Alloc_traits<_Tp, allocator<_Tp1> > {
williamr@2:   typedef allocator<_Tp1> _Orig;
williamr@2:   typedef allocator<_Tp> allocator_type;
williamr@2:   static allocator_type create_allocator(const allocator<_Tp1 >& __a) { return allocator_type(__a); }
williamr@2: };
williamr@2: #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
williamr@2: 
williamr@2: /* macro to convert the allocator for initialization
williamr@2:  * not using MEMBER_TEMPLATE_CLASSES as it should work given template constructor  */
williamr@2: #if defined (_STLP_MEMBER_TEMPLATES) || ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
williamr@2: /* if _STLP_NO_TEMPLATE_CONVERSIONS is set, the member template constructor is
williamr@2:  * not used implicitly to convert allocator parameter, so let us do it explicitly */
williamr@2: # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_NO_TEMPLATE_CONVERSIONS)
williamr@2: #  define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
williamr@2: # else
williamr@2: #  define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __a
williamr@2: # endif
williamr@2: /* else convert, but only if partial specialization works, since else
williamr@2:  * Container::allocator_type won't be different */
williamr@2: #else 
williamr@2: #  define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
williamr@2: #endif
williamr@2: 
williamr@2: # if defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM) 
williamr@2: template <class _Tp, class _Alloc>
williamr@2: inline _STLP_TYPENAME_ON_RETURN_TYPE _Alloc_traits<_Tp, _Alloc>::allocator_type  _STLP_CALL
williamr@2: __stl_alloc_create(const _Alloc& __a, const _Tp*) {
williamr@2:   typedef typename _Alloc::_STLP_TEMPLATE rebind<_Tp>::other _Rebound_type;
williamr@2:   return _Rebound_type(__a);
williamr@2: }
williamr@2: #else
williamr@2: // If custom allocators are being used without member template classes support :
williamr@2: // user (on purpose) is forced to define rebind/get operations !!!
williamr@2: template <class _Tp1, class _Tp2>
williamr@2: inline allocator<_Tp2>& _STLP_CALL
williamr@2: __stl_alloc_rebind(allocator<_Tp1>& __a, const _Tp2*) {  return (allocator<_Tp2>&)(__a); }
williamr@2: template <class _Tp1, class _Tp2>
williamr@2: inline allocator<_Tp2> _STLP_CALL
williamr@2: __stl_alloc_create(const allocator<_Tp1>&, const _Tp2*) { return allocator<_Tp2>(); }
williamr@2: #endif /* _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
williamr@2: 
williamr@2: # ifdef _STLP_USE_RAW_SGI_ALLOCATORS
williamr@2: // move obsolete stuff out of the way
williamr@2: # include <stl/_alloc_old.h>
williamr@2: # endif
williamr@2: 
williamr@2: // inheritance is being used for EBO optimization
williamr@2: template <class _Value, class _Tp, class _MaybeReboundAlloc>
williamr@2: class _STLP_alloc_proxy : public _MaybeReboundAlloc {
williamr@2: private:
williamr@2:   typedef _MaybeReboundAlloc _Base;
williamr@2:   typedef _STLP_alloc_proxy<_Value, _Tp, _MaybeReboundAlloc> _Self;
williamr@2: public:
williamr@2:   _Value _M_data;
williamr@2:   inline _STLP_alloc_proxy(const _MaybeReboundAlloc& __a, _Value __p) : _MaybeReboundAlloc(__a), _M_data(__p) {}
williamr@2: 
williamr@2: # if 0
williamr@2:   inline _STLP_alloc_proxy(const _Self& __x) : _MaybeReboundAlloc(__x), _M_data(__x._M_data) {} 
williamr@2:   // construction/destruction
williamr@2:   inline _Self& operator = (const _Self& __x) { 
williamr@2:     *(_MaybeReboundAlloc*)this = *(_MaybeReboundAlloc*)__x;
williamr@2:     _M_data = __x._M_data; return *this; 
williamr@2:   } 
williamr@2:   inline _Self& operator = (const _Base& __x) { ((_Base&)*this) = __x; return *this; } 
williamr@2: # endif
williamr@2:   // Unified interface to perform allocate()/deallocate() with limited
williamr@2:   // language support
williamr@2: #if ! defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM)
williamr@2:   // else it is rebound already, and allocate() member is accessible
williamr@2:   inline _Tp* allocate(size_t __n) { 
williamr@2:     return __stl_alloc_rebind(__STATIC_CAST(_Base&,*this),(_Tp*)0).allocate(__n,0); 
williamr@2:   }
williamr@2:   inline void deallocate(_Tp* __p, size_t __n) { 
williamr@2:     __stl_alloc_rebind(__STATIC_CAST(_Base&, *this),(_Tp*)0).deallocate(__p, __n); 
williamr@2:   }
williamr@2: #endif /* !_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
williamr@2: };
williamr@2: 
williamr@2: # if defined (_STLP_USE_TEMPLATE_EXPORT)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<char *,char,allocator<char> >;
williamr@2: #  if defined (_STLP_HAS_WCHAR_T)
williamr@2: _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<wchar_t *,wchar_t,allocator<wchar_t> >;
williamr@2: #  endif
williamr@2: # endif /* _STLP_USE_TEMPLATE_EXPORT */
williamr@2: 
williamr@2: # undef _STLP_NODE_ALLOCATOR_THREADS
williamr@2: 
williamr@2: _STLP_END_NAMESPACE
williamr@2: 
williamr@2: # if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
williamr@2: #  include <stl/_alloc.c>
williamr@2: # endif
williamr@2: 
williamr@2: #endif /* _STLP_INTERNAL_ALLOC_H */
williamr@2: 
williamr@2: // Local Variables:
williamr@2: // mode:C++
williamr@2: // End:
williamr@2: