ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

stack_allocator.h
Go to the documentation of this file.
00001 #ifndef STLPORT_UNIT_TEST_STACK_ALLOCATOR_H
00002 #define STLPORT_UNIT_TEST_STACK_ALLOCATOR_H
00003 
00004 #include <algorithm>
00005 
00006 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
00007 //For bad_alloc:
00008 #  include <new>
00009 #endif
00010 
00011 #undef __STD
00012 #if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
00013 #  define __STD std::
00014 #else
00015 #  define __STD
00016 #endif
00017 
00018 struct State {
00019   char *m_beg, *m_end, *m_cur;
00020   bool m_isOk, m_swaped;
00021   int m_nbAlloc;
00022 
00023   //The following members are shared among all StackAllocator instance created from
00024   //a reference StackAllocator instance:
00025   char **m_sharedCur;
00026   bool *m_sharedOk;
00027   int *m_sharedNbAlloc;
00028 
00029 #if defined (__DMC__)
00030   State(){}
00031 #endif
00032 
00033   State(char *beg, char *end)
00034     : m_beg(beg), m_end(end), m_cur(m_beg), m_isOk(true), m_swaped(false), m_nbAlloc(0),
00035       m_sharedCur(&m_cur), m_sharedOk(&m_isOk), m_sharedNbAlloc(&m_nbAlloc) {}
00036 
00037   State(const State& other)
00038   : m_beg(other.m_beg), m_end(other.m_end), m_cur(0),
00039     m_isOk(true), m_swaped(other.m_swaped), m_nbAlloc(0),
00040     m_sharedCur(other.m_sharedCur), m_sharedOk(other.m_sharedOk),
00041     m_sharedNbAlloc(other.m_sharedNbAlloc) {}
00042 };
00043 
00044 /* This allocator is not thread safe:
00045  */
00046 template <class _Tp>
00047 struct StackAllocator
00048 #if defined (STLPORT) && \
00049     defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
00050   //Special Borland workaround that have problem with function
00051   //overloading when one of the overloaded version is a template
00052   //one. This is the case for the std::swap function.
00053   : public __STD __stlport_class<StackAllocator<_Tp> >
00054 #endif
00055 {
00056   typedef _Tp        value_type;
00057   typedef value_type *       pointer;
00058   typedef const _Tp* const_pointer;
00059   typedef _Tp&       reference;
00060   typedef const _Tp& const_reference;
00061   typedef size_t     size_type;
00062   typedef ptrdiff_t  difference_type;
00063 
00064 #if defined (__DMC__)
00065   StackAllocator(){}
00066 #endif
00067 
00068   StackAllocator(char *beg, char *end)
00069     : m_state(beg, end) {}
00070 
00071   const State& getState() const { return m_state; }
00072 #if !defined (STLPORT) || defined (_STLP_MEMBER_TEMPLATES)
00073   template <class _OtherTp>
00074   StackAllocator(StackAllocator<_OtherTp> const& other)
00075     : m_state(other.getState()) {}
00076 #else
00077   StackAllocator(const State& state)
00078     : m_state(state) {}
00079 #endif
00080 
00081 #if !defined (STLPORT) || defined (_STLP_MEMBER_TEMPLATE_CLASSES)
00082   template <class _Other>
00083   struct rebind {
00084     typedef StackAllocator<_Other> other;
00085   };
00086 #endif
00087 
00088   _Tp* allocate(size_type n, void* = 0) {
00089     if (n == 0)
00090       return 0;
00091 
00092     ++(*m_state.m_sharedNbAlloc);
00093 
00094     if (*m_state.m_sharedCur + (n * sizeof(_Tp)) < m_state.m_end) {
00095       char *ret = *m_state.m_sharedCur;
00096       *m_state.m_sharedCur += n * sizeof(_Tp);
00097       return reinterpret_cast<_Tp*>(ret);
00098     }
00099 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
00100     throw __STD bad_alloc();
00101 #  if defined (__DMC__)
00102     return 0;
00103 #  endif
00104 #else
00105     return 0;
00106 #endif
00107   }
00108 
00109 #if defined (STLPORT) && \
00110     defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
00111   //Necessary extension to make StackAllocator a real STLport allocator
00112   //implementation:
00113   _Tp* _M_allocate(size_type n, size_type &new_n) {
00114     new_n = n;
00115     return allocate(n);
00116   }
00117 #endif
00118 
00119   void deallocate(pointer p, size_type n) {
00120     if (p == 0)
00121       return;
00122 
00123     --(*m_state.m_sharedNbAlloc);
00124 
00125     if ((char*)p == (*m_state.m_sharedCur - n * sizeof(_Tp))) {
00126       *m_state.m_sharedCur -= n * sizeof(_Tp);
00127     }
00128 
00129     if ((char*)p < m_state.m_beg || (char*)p >= m_state.m_end) {
00130       //An object has been returned to the bad allocator instance:
00131       *m_state.m_sharedOk = false;
00132     }
00133   }
00134 
00135   pointer address(reference __x) const {return &__x;}
00136   const_pointer address(const_reference __x) const { return &__x; }
00137   size_type max_size() const { return m_state.m_end - *m_state.m_sharedCur; }
00138   void construct(pointer __p, const_reference __val) { new(__p) _Tp(__val);  }
00139   void destroy(pointer __p) { __p->~_Tp(); }
00140 
00141   bool ok() const { return m_state.m_isOk && (m_state.m_nbAlloc == 0); }
00142   void reset () {
00143     m_state.m_cur = m_state.m_beg;
00144     m_state.m_isOk = true;
00145     m_state.m_swaped = false;
00146   }
00147   bool swaped() const { return m_state.m_swaped; }
00148   void swap(StackAllocator &other) {
00149     __STD swap(m_state, other.m_state);
00150     m_state.m_swaped = true;
00151     other.m_state.m_swaped = true;
00152   }
00153 #if defined (STLPORT) && \
00154     defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
00155   void _M_swap_workaround(StackAllocator& __x) { swap(__x); }
00156 #endif
00157 
00158   //2 StackAllocator instance are identical if they are built on top
00159   //of the same buffer.
00160   bool operator == (StackAllocator const& other) const
00161   { return m_state.m_beg == other.m_state.m_beg; }
00162 
00163   bool operator != (StackAllocator const& other) const
00164   { return !(*this == other); }
00165 
00166 private:
00167   State m_state;
00168 };
00169 
00170 #if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
00171 namespace std {
00172 #endif
00173 
00174 #  if defined (STLPORT) && (defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || !defined (_STLP_MEMBER_TEMPLATES))
00175 template <class _Tp1, class _Tp2>
00176 inline StackAllocator<_Tp2>&
00177 __stl_alloc_rebind(StackAllocator<_Tp1>& __a, const _Tp2*) {  return (StackAllocator<_Tp2>&)(__a); }
00178 template <class _Tp1, class _Tp2>
00179 inline StackAllocator<_Tp2>
00180 __stl_alloc_create(const StackAllocator<_Tp1>& __a, const _Tp2*) { return StackAllocator<_Tp2>(__a.getState()); }
00181 #  endif
00182 
00183 #  if !defined (STLPORT) || defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
00184   template <class _Tp>
00185   inline void swap(StackAllocator<_Tp>& __a, StackAllocator<_Tp>& __b)
00186   { __a.swap(__b); }
00187 #  elif !defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00188 //The following overloads depends on instanciation, if new unit tests are written
00189 //with new StackAllocator instanciations associated swap overload should also be
00190 //written
00191 inline void swap(StackAllocator<int>& __a, StackAllocator<int>& __b)
00192 { __a.swap(__b); }
00193 inline void swap(StackAllocator<char>& __a, StackAllocator<char>& __b)
00194 { __a.swap(__b); }
00195 inline void swap(StackAllocator<pair<const int, int> >& __a,
00196                  StackAllocator<pair<const int, int> >& __b)
00197 { __a.swap(__b); }
00198 #  endif
00199 
00200 #if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
00201 }
00202 #endif
00203 
00204 #undef __STD
00205 
00206 #endif //STLPORT_UNIT_TEST_STACK_ALLOCATOR_H

Generated on Sat May 26 2012 04:34:45 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.