1 : #ifndef nsCppSharedAllocator_h__
2 : #define nsCppSharedAllocator_h__
3 :
4 : #include "nsMemory.h" // for |nsMemory|
5 : #include NEW_H // to allow placement |new|
6 :
7 :
8 : // under MSVC shut off copious warnings about unused in-lines
9 : #ifdef _MSC_VER
10 : #pragma warning( disable: 4514 )
11 : #endif
12 :
13 : #include <limits.h>
14 :
15 :
16 : template <class T>
17 : class nsCppSharedAllocator
18 : /*
19 : ...allows Standard Library containers, et al, to use our global shared
20 : (XP)COM-aware allocator.
21 : */
22 : {
23 : public:
24 : typedef T value_type;
25 : typedef size_t size_type;
26 : typedef ptrdiff_t difference_type;
27 :
28 : typedef T* pointer;
29 : typedef const T* const_pointer;
30 :
31 : typedef T& reference;
32 : typedef const T& const_reference;
33 :
34 :
35 :
36 0 : nsCppSharedAllocator() { }
37 :
38 0 : ~nsCppSharedAllocator() { }
39 :
40 :
41 : pointer
42 : address( reference r ) const
43 : {
44 : return &r;
45 : }
46 :
47 : const_pointer
48 : address( const_reference r ) const
49 : {
50 : return &r;
51 : }
52 :
53 : pointer
54 0 : allocate( size_type n, const void* /*hint*/=0 )
55 : {
56 0 : return reinterpret_cast<pointer>(nsMemory::Alloc(static_cast<PRUint32>(n*sizeof(T))));
57 : }
58 :
59 : void
60 : deallocate( pointer p, size_type /*n*/ )
61 : {
62 : nsMemory::Free(p);
63 : }
64 :
65 : void
66 : construct( pointer p, const T& val )
67 : {
68 : new (p) T(val);
69 : }
70 :
71 : void
72 : destroy( pointer p )
73 : {
74 : p->~T();
75 : }
76 :
77 : size_type
78 : max_size() const
79 : {
80 : return ULONG_MAX / sizeof(T);
81 : }
82 :
83 : };
84 :
85 :
86 : template <class T>
87 : bool
88 : operator==( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
89 : {
90 : return true;
91 : }
92 :
93 : template <class T>
94 : bool
95 : operator!=( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
96 : {
97 : return false;
98 : }
99 :
100 : #endif /* !defined(nsCppSharedAllocator_h__) */
|