ReactOS 0.4.15-dev-7961-gdcf9eb0
stack_allocator.h
Go to the documentation of this file.
1#ifndef STLPORT_UNIT_TEST_STACK_ALLOCATOR_H
2#define STLPORT_UNIT_TEST_STACK_ALLOCATOR_H
3
4#include <algorithm>
5
6#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
7//For bad_alloc:
8# include <new>
9#endif
10
11#undef __STD
12#if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
13# define __STD std::
14#else
15# define __STD
16#endif
17
18struct State {
19 char *m_beg, *m_end, *m_cur;
22
23 //The following members are shared among all StackAllocator instance created from
24 //a reference StackAllocator instance:
28
29#if defined (__DMC__)
30 State(){}
31#endif
32
33 State(char *beg, char *end)
36
42};
43
44/* This allocator is not thread safe:
45 */
46template <class _Tp>
48#if defined (STLPORT) && \
49 defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
50 //Special Borland workaround that have problem with function
51 //overloading when one of the overloaded version is a template
52 //one. This is the case for the std::swap function.
54#endif
55{
56 typedef _Tp value_type;
58 typedef const _Tp* const_pointer;
59 typedef _Tp& reference;
60 typedef const _Tp& const_reference;
61 typedef size_t size_type;
63
64#if defined (__DMC__)
66#endif
67
68 StackAllocator(char *beg, char *end)
69 : m_state(beg, end) {}
70
71 const State& getState() const { return m_state; }
72#if !defined (STLPORT) || defined (_STLP_MEMBER_TEMPLATES)
73 template <class _OtherTp>
75 : m_state(other.getState()) {}
76#else
78 : m_state(state) {}
79#endif
80
81#if !defined (STLPORT) || defined (_STLP_MEMBER_TEMPLATE_CLASSES)
82 template <class _Other>
83 struct rebind {
85 };
86#endif
87
88 _Tp* allocate(size_type n, void* = 0) {
89 if (n == 0)
90 return 0;
91
92 ++(*m_state.m_sharedNbAlloc);
93
94 if (*m_state.m_sharedCur + (n * sizeof(_Tp)) < m_state.m_end) {
95 char *ret = *m_state.m_sharedCur;
96 *m_state.m_sharedCur += n * sizeof(_Tp);
97 return reinterpret_cast<_Tp*>(ret);
98 }
99#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
100 throw __STD bad_alloc();
101# if defined (__DMC__)
102 return 0;
103# endif
104#else
105 return 0;
106#endif
107 }
108
109#if defined (STLPORT) && \
110 defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
111 //Necessary extension to make StackAllocator a real STLport allocator
112 //implementation:
113 _Tp* _M_allocate(size_type n, size_type &new_n) {
114 new_n = n;
115 return allocate(n);
116 }
117#endif
118
120 if (p == 0)
121 return;
122
123 --(*m_state.m_sharedNbAlloc);
124
125 if ((char*)p == (*m_state.m_sharedCur - n * sizeof(_Tp))) {
126 *m_state.m_sharedCur -= n * sizeof(_Tp);
127 }
128
129 if ((char*)p < m_state.m_beg || (char*)p >= m_state.m_end) {
130 //An object has been returned to the bad allocator instance:
131 *m_state.m_sharedOk = false;
132 }
133 }
134
135 pointer address(reference __x) const {return &__x;}
136 const_pointer address(const_reference __x) const { return &__x; }
137 size_type max_size() const { return m_state.m_end - *m_state.m_sharedCur; }
138 void construct(pointer __p, const_reference __val) { new(__p) _Tp(__val); }
139 void destroy(pointer __p) { __p->~_Tp(); }
140
141 bool ok() const { return m_state.m_isOk && (m_state.m_nbAlloc == 0); }
142 void reset () {
143 m_state.m_cur = m_state.m_beg;
144 m_state.m_isOk = true;
145 m_state.m_swaped = false;
146 }
147 bool swaped() const { return m_state.m_swaped; }
149 __STD swap(m_state, other.m_state);
150 m_state.m_swaped = true;
151 other.m_state.m_swaped = true;
152 }
153#if defined (STLPORT) && \
154 defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
155 void _M_swap_workaround(StackAllocator& __x) { swap(__x); }
156#endif
157
158 //2 StackAllocator instance are identical if they are built on top
159 //of the same buffer.
161 { return m_state.m_beg == other.m_state.m_beg; }
162
164 { return !(*this == other); }
165
166private:
168};
169
170#if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
171namespace std {
172#endif
173
174# if defined (STLPORT) && (defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || !defined (_STLP_MEMBER_TEMPLATES))
175template <class _Tp1, class _Tp2>
177__stl_alloc_rebind(StackAllocator<_Tp1>& __a, const _Tp2*) { return (StackAllocator<_Tp2>&)(__a); }
178template <class _Tp1, class _Tp2>
180__stl_alloc_create(const StackAllocator<_Tp1>& __a, const _Tp2*) { return StackAllocator<_Tp2>(__a.getState()); }
181# endif
182
183# if !defined (STLPORT) || defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
184 template <class _Tp>
186 { __a.swap(__b); }
187# elif !defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
188//The following overloads depends on instanciation, if new unit tests are written
189//with new StackAllocator instanciations associated swap overload should also be
190//written
191inline void swap(StackAllocator<int>& __a, StackAllocator<int>& __b)
192{ __a.swap(__b); }
193inline void swap(StackAllocator<char>& __a, StackAllocator<char>& __b)
194{ __a.swap(__b); }
195inline void swap(StackAllocator<pair<const int, int> >& __a,
197{ __a.swap(__b); }
198# endif
199
200#if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
201}
202#endif
203
204#undef __STD
205
206#endif //STLPORT_UNIT_TEST_STACK_ALLOCATOR_H
_STLP_INLINE_LOOP _InputIter const _Tp & __val
Definition: _algobase.h:656
allocator< _Tp2 > &_STLP_CALL __stl_alloc_rebind(allocator< _Tp1 > &__a, const _Tp2 *)
Definition: _alloc.h:462
bool _STLP_CALL operator!=(const allocator< _T1 > &, const allocator< _T2 > &) _STLP_NOTHROW
Definition: _alloc.h:384
allocator< _Tp2 > _STLP_CALL __stl_alloc_create(const allocator< _Tp1 > &, const _Tp2 *)
Definition: _alloc.h:465
static int state
Definition: maze.c:121
__kernel_ptrdiff_t ptrdiff_t
Definition: linux.h:247
#define STLPORT
Definition: features.h:569
#define _STLP_FUNCTION_TMPL_PARTIAL_ORDER
Definition: features.h:124
GLuint GLuint end
Definition: gl.h:1545
GLdouble n
Definition: glext.h:7729
GLfloat GLfloat p
Definition: glext.h:8902
if(dx< 0)
Definition: linetemp.h:194
int other
Definition: msacm.c:1376
Definition: features.h:417
#define swap(a, b)
Definition: qsort.c:63
#define __STD
#define true
Definition: stdbool.h:36
#define false
Definition: stdbool.h:37
StackAllocator< _Other > other
StackAllocator(char *beg, char *end)
_Tp * allocate(size_type n, void *=0)
const _Tp * const_pointer
void destroy(pointer __p)
const_pointer address(const_reference __x) const
const _Tp & const_reference
ptrdiff_t difference_type
void construct(pointer __p, const_reference __val)
const State & getState() const
void swap(StackAllocator &other)
void deallocate(pointer p, size_type n)
size_type max_size() const
value_type * pointer
StackAllocator(StackAllocator< _OtherTp > const &other)
pointer address(reference __x) const
bool ok() const
bool swaped() const
int m_nbAlloc
bool m_isOk
char * m_cur
bool m_swaped
State(const State &other)
int * m_sharedNbAlloc
char * m_end
bool * m_sharedOk
char * m_beg
State(char *beg, char *end)
char ** m_sharedCur
Definition: _pair.h:47
bool operator==(const TKeyDef &t1, const TKeyDef &t2)
Definition: tkeydef.cpp:122
int ret