Back to index

lightning-sunbird  0.9+nobinonly
nsCppSharedAllocator.h
Go to the documentation of this file.
00001 #ifndef nsCppSharedAllocator_h__
00002 #define nsCppSharedAllocator_h__
00003 
00004 #include "nsMemory.h"     // for |nsMemory|
00005 #include "nscore.h"       // for |NS_XXX_CAST|
00006 #include NEW_H                                   // to allow placement |new|
00007 
00008 
00009   // under Metrowerks (Mac), we don't have autoconf yet
00010 #ifdef __MWERKS__
00011   #define HAVE_CPP_MEMBER_TEMPLATES
00012        #define HAVE_CPP_NUMERIC_LIMITS
00013 #endif
00014 
00015   // under MSVC shut off copious warnings about unused in-lines
00016 #ifdef _MSC_VER
00017   #pragma warning( disable: 4514 )
00018 #endif
00019 
00020 #ifdef HAVE_CPP_NUMERIC_LIMITS
00021 #include <limits>
00022 #else
00023 #include <limits.h>
00024 #endif
00025 
00026 
00027 template <class T>
00028 class nsCppSharedAllocator
00029     /*
00030       ...allows Standard Library containers, et al, to use our global shared
00031       (XP)COM-aware allocator.
00032     */
00033   {
00034     public:
00035       typedef T          value_type;
00036       typedef size_t     size_type;
00037       typedef ptrdiff_t  difference_type;
00038 
00039       typedef T*         pointer;
00040       typedef const T*   const_pointer;
00041 
00042       typedef T&         reference;
00043       typedef const T&   const_reference;
00044 
00045 
00046 
00047       nsCppSharedAllocator() { }
00048 
00049 #ifdef HAVE_CPP_MEMBER_TEMPLATES
00050       template <class U>
00051       nsCppSharedAllocator( const nsCppSharedAllocator<U>& ) { }
00052 #endif
00053 
00054      ~nsCppSharedAllocator() { }
00055 
00056 
00057       pointer
00058       address( reference r ) const
00059         {
00060           return &r;
00061         }
00062 
00063       const_pointer
00064       address( const_reference r ) const
00065         {
00066           return &r;
00067         }
00068 
00069       pointer
00070       allocate( size_type n, const void* /*hint*/=0 )
00071         {
00072           return NS_REINTERPRET_CAST(pointer, nsMemory::Alloc(NS_STATIC_CAST(PRUint32, n*sizeof(T))));
00073         }
00074 
00075       void
00076       deallocate( pointer p, size_type /*n*/ )
00077         {
00078           nsMemory::Free(p);
00079         }
00080 
00081       void
00082       construct( pointer p, const T& val )
00083         {
00084           new (p) T(val);
00085         }
00086       
00087       void
00088       destroy( pointer p )
00089         {
00090           p->~T();
00091         }
00092 
00093       size_type
00094       max_size() const
00095         {
00096 #ifdef HAVE_CPP_NUMERIC_LIMITS
00097           return numeric_limits<size_type>::max() / sizeof(T);
00098 #else
00099           return ULONG_MAX / sizeof(T);
00100 #endif
00101         }
00102 
00103 #ifdef HAVE_CPP_MEMBER_TEMPLATES
00104       template <class U>
00105       struct rebind
00106         {
00107           typedef nsCppSharedAllocator<U> other;
00108         };
00109 #endif
00110   };
00111 
00112 
00113 template <class T>
00114 PRBool
00115 operator==( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
00116   {
00117     return PR_TRUE;
00118   }
00119 
00120 template <class T>
00121 PRBool
00122 operator!=( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
00123   {
00124     return PR_FALSE;
00125   }
00126 
00127 #endif /* !defined(nsCppSharedAllocator_h__) */