Mobile API Reference  MicroStrategy 2019
Allocator.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : Allocator.h
3 // AUTHOR : Juan Pablo Muraira
4 // CREATION : 9/26/01
5 // Copyright (C) MicroStrategy Incorporated 2001
6 // All Rights Reserved
7 //==============================================================================================
8 #ifndef MBase_Allocator_h
9 #define MBase_Allocator_h
10 
11 // the allocator here is a standard allocator, not an RW alternative allocator interface, need to define _RWSTD_ALLOCATOR for sun
12 #if defined(sun) && !defined(_RWSTD_ALLOCATOR)
13 // if you get this error make sure that _RWSTD_ALLOCATOR is defined AT THE BEGINNING OF YOUR PROJECT
14 // it should be defined automatically for you in the Makefile, because it is on the local.param rules for Solaris
15 #error _RWSTD_ALLOCATOR not defined
16 _RWSTD_ALLOCATOR not defined
17 #endif
18 
19 
20 #ifdef _AIX
21 #include <new>
22 #endif
23 
24 #include "PDCHeader/PDClimits.h"
25 #include "PDCHeader/PDCcstdlib"
26 #include "PDCHeader/PDCutility"
27 
28 #include "Buffer.h"
29 
31 // IMPORTANT:
32 // Because allocator is a template and therefore expanded when the code is
33 // instantiated, it may not become part of a constellation interface.
34 // This means that it may not be referenced as parameter, return value, etc
35 // on a constellation interface. It may not be a part of another template
36 // on the interface either (such as MsiString)
37 // Templates may be used in another constellation if and only if they do not
38 // "escape" that constellation.
40 // HISTORY:
41 // This file was originally copied/pasted from Windows' <xmemory>
42 // for reasons unknown to us. After that some of the ancient macros were
43 // removed, such as _FARQ. The ultimate goal is to converge back to using
44 // std::allocator.
46 
47 namespace MBase
48 {
56  template<class _Ty>
57  class Allocator
58  {
59  public:
60  typedef size_t size_type;
61  typedef ptrdiff_t difference_type;
62  typedef _Ty* pointer;
63  typedef const _Ty* const_pointer;
64  typedef _Ty& reference;
65  typedef const _Ty& const_reference;
66  typedef _Ty value_type;
67 
69  mMemPtr(NULL)
70  {
71  // Default Constructor. We initialize the Buffer
72  // pointer to NULL. This means allocation will
73  // now be done from the process heap
74  }
75 
76  template<class _UT>
77  Allocator(const Allocator<_UT>& irAllocator):
78  mMemPtr(irAllocator.GetBuffer())
79  {
80  }
81 
82  Allocator(Buffer* iMan):
83  mMemPtr(iMan)
84  {
85  // Initialize the buffer pointer. This means allocation
86  // is now always done from the buffer
87  }
88 
89  template<class _UT>
90  struct rebind
91  {
93  };
94 
95  pointer address(reference irX) const throw()
96  {
97  return (&irX);
98  }
99 
101  {
102  return (&irX);
103  }
104 
105  pointer allocate(size_type inSize, const void *)
106  {
107  if (!mMemPtr)
108  {
109  // buffer pointer is NULL, do allocation
110  // from process heap
111  return reinterpret_cast<pointer>(malloc(sizeof(value_type) *inSize));
112  }
113 
114  // else get memory from the buffer
115  return reinterpret_cast<pointer>(mMemPtr->GetMem(inSize * sizeof(value_type)));
116  }
117 
119  {
120  return allocate(inSize, NULL);
121  }
122 
123  #ifdef WIN32
124  char*_Charalloc(size_type inSize)
125  {
126  if (!mMemPtr)
127  {
128  // buffer pointer is NULL, do allocation
129  // from process heap
130  return reinterpret_cast<char*>(new char[inSize]);
131  }
132 
133  // else get memory from the buffer.
134  return reinterpret_cast<char*>(mMemPtr->GetMem(inSize));
135  }
136 
137  void deallocate(void* ipPtr, size_type) throw()
138  {
139  // If allocation is being done from process heap, the
140  // deallocate
141  if (!mMemPtr)
142  {
143  delete[] ipPtr;
144  }
145  }
146  #endif
147 
149  {
150  // If allocation is being done from process heap, the
151  // deallocate
152  if (!mMemPtr)
153  {
154  free(ipPtr);
155  }
156  }
157 
158 
160  {
161  if (!ipPtr) return;
162 
163 #ifdef WIN32
164  std::_Construct(ipPtr, _V);
165 #else
166  new ((void *)ipPtr) (_Ty)(_V);
167 #endif
168  }
169 
170  void destroy(pointer ipPtr) throw()
171  {
172  // If allocation is being done from process heap, then
173  // destroy
174  if (!mMemPtr)
175  {
176 #ifdef WIN32
177  std::_Destroy(ipPtr);
178 #else
179  ipPtr->~_Ty();
180 #endif
181  }
182  }
183 
184 
185  size_type max_size() const throw()
186  {
187  size_type lnSize = static_cast<size_type>(UINT_MAX / sizeof(value_type));
188  return (0 < lnSize ? lnSize : 1);
189  }
190 
191  // this function is to be used internally by allocator only
192  // needs to be public to be accessed by another intantiation of the
193  // template
194  Buffer* GetBuffer() const
195  {
196  return mMemPtr;
197  }
198 
199  private:
200  Buffer* mMemPtr;
201  };
202 
203  template<class _Ty, class _UT> inline
204  bool operator==(const Allocator<_Ty>& left, const Allocator<_UT>& right)
205  {
206  //nprabhudessai , 02/16/06, it was always returning true which was wrong
207  return (left.GetBuffer() == right.GetBuffer());
208  }
209 
210  template<class _Ty, class _UT> inline
211  bool operator!=(const Allocator<_Ty>& left, const Allocator<_UT>& right)
212  {
213  //nprabhudessai , 02/16/06, it was always returning false which was wrong
214  return !(left == right);
215  }
216 
217 
218 // the following is only for Unix
219 #ifndef WIN32
220  // CLASS allocator<void>
221  template<> class Allocator<void>
222  {
223  public:
224  typedef void _Ty;
225  typedef _Ty *pointer;
226  typedef const _Ty *const_pointer;
227  typedef _Ty value_type;
228 
229  template<class _UT>
230  struct rebind
231  {
233  };
234 
236  {
237  }
238 
239  Allocator(const Allocator<_Ty>& /*irAlloc*/)
240  {
241  }
242 
243  template<class _UT>
244  Allocator(const Allocator<_UT>& /*irAlloc*/)
245  {
246  }
247 
248  template<class _UT>
250  {
251  return (*this);
252  }
253  };
254 #endif // WIN32
255 }
256 
257 #endif // MBase_Allocator_h
_Ty value_type
Definition: Allocator.h:227
size_t size_type
Definition: Allocator.h:60
Allocator(const Allocator< _UT > &irAllocator)
Definition: Allocator.h:77
bool operator==(const Allocator< _Ty > &left, const Allocator< _UT > &right)
Definition: Allocator.h:204
const_pointer address(const_reference irX) const
Definition: Allocator.h:100
const _Ty * const_pointer
Definition: Allocator.h:226
_Ty & reference
Definition: Allocator.h:64
Allocator(const Allocator< _Ty > &)
Definition: Allocator.h:239
void destroy(pointer ipPtr)
Definition: Allocator.h:170
Buffer * GetBuffer() const
Definition: Allocator.h:194
Definition: Allocator.h:90
Allocator()
Definition: Allocator.h:68
Definition: Buffer.h:58
Allocator(Buffer *iMan)
Definition: Allocator.h:82
Allocator(const Allocator< _UT > &)
Definition: Allocator.h:244
_Ty * pointer
Definition: Allocator.h:62
size_type max_size() const
Definition: Allocator.h:185
ptrdiff_t difference_type
Definition: Allocator.h:61
Definition: Allocator.h:47
bool operator!=(const Allocator< _Ty > &left, const Allocator< _UT > &right)
Definition: Allocator.h:211
Definition: Allocator.h:57
const _Ty * const_pointer
Definition: Allocator.h:63
Allocator< _UT > other
Definition: Allocator.h:92
_Ty * pointer
Definition: Allocator.h:225
Allocator< _Ty > & operator=(const Allocator< _UT > &)
Definition: Allocator.h:249
Allocator()
Definition: Allocator.h:235
void _Ty
Definition: Allocator.h:224
const _Ty & const_reference
Definition: Allocator.h:65
void construct(pointer ipPtr, const_reference _V)
Definition: Allocator.h:159
pointer allocate(size_type inSize, const void *)
Definition: Allocator.h:105
_Ty value_type
Definition: Allocator.h:66
void deallocate(pointer ipPtr, size_type)
Definition: Allocator.h:148
pointer address(reference irX) const
Definition: Allocator.h:95
#define NULL
Definition: Null.h:10
Allocator< _UT > other
Definition: Allocator.h:232
virtual void * GetMem(size_t iSize)=0
pointer allocate(size_type inSize)
Definition: Allocator.h:118