1 | #ifndef nsCppSharedAllocator_h__
|
---|
2 | #define nsCppSharedAllocator_h__
|
---|
3 |
|
---|
4 | #include "nsMemory.h" // for |nsMemory|
|
---|
5 | #include "nscore.h" // for |NS_XXX_CAST|
|
---|
6 | #include NEW_H // to allow placement |new|
|
---|
7 |
|
---|
8 |
|
---|
9 | // under Metrowerks (Mac), we don't have autoconf yet
|
---|
10 | #ifdef __MWERKS__
|
---|
11 | #define HAVE_CPP_MEMBER_TEMPLATES
|
---|
12 | #define HAVE_CPP_NUMERIC_LIMITS
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | // under MSVC shut off copious warnings about unused in-lines
|
---|
16 | #ifdef _MSC_VER
|
---|
17 | #pragma warning( disable: 4514 )
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #ifdef HAVE_CPP_NUMERIC_LIMITS
|
---|
21 | #include <limits>
|
---|
22 | #else
|
---|
23 | #include <limits.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 |
|
---|
27 | template <class T>
|
---|
28 | class nsCppSharedAllocator
|
---|
29 | /*
|
---|
30 | ...allows Standard Library containers, et al, to use our global shared
|
---|
31 | (XP)COM-aware allocator.
|
---|
32 | */
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | typedef T value_type;
|
---|
36 | typedef size_t size_type;
|
---|
37 | typedef ptrdiff_t difference_type;
|
---|
38 |
|
---|
39 | typedef T* pointer;
|
---|
40 | typedef const T* const_pointer;
|
---|
41 |
|
---|
42 | typedef T& reference;
|
---|
43 | typedef const T& const_reference;
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | nsCppSharedAllocator() { }
|
---|
48 |
|
---|
49 | #ifdef HAVE_CPP_MEMBER_TEMPLATES
|
---|
50 | template <class U>
|
---|
51 | nsCppSharedAllocator( const nsCppSharedAllocator<U>& ) { }
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | ~nsCppSharedAllocator() { }
|
---|
55 |
|
---|
56 |
|
---|
57 | pointer
|
---|
58 | address( reference r ) const
|
---|
59 | {
|
---|
60 | return &r;
|
---|
61 | }
|
---|
62 |
|
---|
63 | const_pointer
|
---|
64 | address( const_reference r ) const
|
---|
65 | {
|
---|
66 | return &r;
|
---|
67 | }
|
---|
68 |
|
---|
69 | pointer
|
---|
70 | allocate( size_type n, const void* /*hint*/=0 )
|
---|
71 | {
|
---|
72 | return NS_REINTERPRET_CAST(pointer, nsMemory::Alloc(NS_STATIC_CAST(PRUint32, n*sizeof(T))));
|
---|
73 | }
|
---|
74 |
|
---|
75 | void
|
---|
76 | deallocate( pointer p, size_type /*n*/ )
|
---|
77 | {
|
---|
78 | nsMemory::Free(p);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void
|
---|
82 | construct( pointer p, const T& val )
|
---|
83 | {
|
---|
84 | new (p) T(val);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void
|
---|
88 | destroy( pointer p )
|
---|
89 | {
|
---|
90 | p->~T();
|
---|
91 | }
|
---|
92 |
|
---|
93 | size_type
|
---|
94 | max_size() const
|
---|
95 | {
|
---|
96 | #ifdef HAVE_CPP_NUMERIC_LIMITS
|
---|
97 | return numeric_limits<size_type>::max() / sizeof(T);
|
---|
98 | #else
|
---|
99 | return ULONG_MAX / sizeof(T);
|
---|
100 | #endif
|
---|
101 | }
|
---|
102 |
|
---|
103 | #ifdef HAVE_CPP_MEMBER_TEMPLATES
|
---|
104 | template <class U>
|
---|
105 | struct rebind
|
---|
106 | {
|
---|
107 | typedef nsCppSharedAllocator<U> other;
|
---|
108 | };
|
---|
109 | #endif
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | template <class T>
|
---|
114 | PRBool
|
---|
115 | operator==( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
|
---|
116 | {
|
---|
117 | return PR_TRUE;
|
---|
118 | }
|
---|
119 |
|
---|
120 | template <class T>
|
---|
121 | PRBool
|
---|
122 | operator!=( const nsCppSharedAllocator<T>&, const nsCppSharedAllocator<T>& )
|
---|
123 | {
|
---|
124 | return PR_FALSE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | #endif /* !defined(nsCppSharedAllocator_h__) */
|
---|