00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef WINSTL_INCL_H_WINSTL
00060 # include "winstl.h"
00061 #endif
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"
00064 #endif
00065 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00066 # include "stlsoft_sap_cast.h"
00067 #endif
00068
00069
00070
00071
00072
00073 #ifndef _WINSTL_NO_NAMESPACE
00074 # if defined(_STLSOFT_NO_NAMESPACE) || \
00075 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00076
00077 namespace winstl
00078 {
00079 # else
00080
00081
00082 namespace stlsoft
00083 {
00084
00085 namespace winstl_project
00086 {
00087
00088 # endif
00089 #endif
00090
00091
00092
00095
00099
00104
00105
00106
00107
00108
00109 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00110
00111 template <ss_typename_param_k T>
00112 class processheap_allocator;
00113
00114
00115 STLSOFT_TEMPLATE_SPECIALISATION
00116 class processheap_allocator<void>
00117 {
00118 public:
00119 typedef void value_type;
00120 typedef processheap_allocator<void> class_type;
00121 typedef void *pointer;
00122 typedef void const *const_pointer;
00123 typedef ptrdiff_t difference_type;
00124 typedef ws_size_t size_type;
00125
00126 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00127
00128 template <ss_typename_param_k U>
00129 struct rebind
00130 {
00131 typedef processheap_allocator<U> other;
00132 };
00133 #endif
00134 };
00135
00136 #endif
00137
00141 template <ss_typename_param_k T>
00142 class processheap_allocator
00143 {
00144 public:
00146 typedef T value_type;
00148 typedef processheap_allocator<value_type> class_type;
00150 typedef value_type *pointer;
00152 typedef value_type const *const_pointer;
00154 typedef value_type &reference;
00156 typedef value_type const &const_reference;
00158 typedef ptrdiff_t difference_type;
00160 typedef ws_size_t size_type;
00162 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00163 typedef pointer deallocate_pointer;
00164 #else
00165 typedef void *deallocate_pointer;
00166 #endif
00167
00168 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00169
00170 template <ss_typename_param_k U>
00171 struct rebind
00172 {
00173 typedef processheap_allocator<U> other;
00174 };
00175 #endif
00176
00177
00178 public:
00180 processheap_allocator() winstl_throw_0()
00181 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00182 : m_processheap(::GetProcessHeap())
00183 #endif
00184 {}
00186 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00187 processheap_allocator(processheap_allocator const &) winstl_throw_0()
00188 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00189 : m_processheap(::GetProcessHeap())
00190 #endif
00191 {}
00192 #else
00193 template <ss_typename_param_k U>
00194 processheap_allocator(processheap_allocator<U> const &) winstl_throw_0()
00195 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00196 : m_processheap(::GetProcessHeap())
00197 #endif
00198 {}
00199 #endif
00200
00201 ~processheap_allocator() winstl_throw_0()
00202 {}
00203
00204
00205 public:
00207 size_type max_size() const winstl_throw_0()
00208 {
00209 return static_cast<size_type>(-1) / sizeof(value_type);
00210 }
00211
00212
00213 public:
00217 pointer address(reference x) const winstl_throw_0()
00218 {
00219 return &x;
00220 }
00224 const_pointer address(const_reference x) const winstl_throw_0()
00225 {
00226 return &x;
00227 }
00228
00229
00230 public:
00236 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00237 static
00238 #endif
00239 pointer allocate(size_type n, processheap_allocator<void>::const_pointer pv = NULL)
00240 {
00241 STLSOFT_SUPPRESS_UNUSED(pv);
00242
00243 processheap_allocator<void>::pointer p = static_cast<processheap_allocator<void>::pointer>(::HeapAlloc(get_process_(), 0, n * sizeof(value_type)));
00244
00245 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00246 if(p == NULL)
00247 {
00248 throw ::std::bad_alloc();
00249 }
00250 #endif
00251
00252 return static_cast<pointer>(p);
00253 }
00254
00255 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00256
00257 char *_Charalloc(size_type n)
00258 {
00259 return sap_cast<char*>(allocate(n, NULL));
00260 }
00261 #endif
00262
00267 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00268 static
00269 #endif
00270 void deallocate(pointer p, size_type n)
00271 {
00272 STLSOFT_SUPPRESS_UNUSED(n);
00273
00274 ::HeapFree(get_process_(), 0, p);
00275 }
00276
00280 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00281 static
00282 #endif
00283 void deallocate(pointer p)
00284 {
00285 ::HeapFree(get_process_(), 0, p);
00286 }
00287
00288
00289 public:
00294 void construct(pointer p, value_type const &x)
00295 {
00296 winstl_assert(p != NULL);
00297
00298 new(p) value_type(x);
00299 }
00300
00304 void construct(pointer p)
00305 {
00306 winstl_assert(p != NULL);
00307
00308 new(p) value_type();
00309 }
00310
00314 void destroy(pointer p) winstl_throw_0()
00315 {
00316 winstl_assert(p != NULL);
00317
00318 winstl_destroy_instance(T, value_type, p);
00319 }
00320
00321
00322 protected:
00323 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00324 static HANDLE get_process_()
00325 {
00326 static HANDLE s_processheap = ::GetProcessHeap();
00327
00328 return s_processheap;
00329 }
00330 #else
00331 HANDLE get_process_()
00332 {
00333 return m_processheap;
00334 }
00335 #endif
00336
00337
00338 protected:
00339 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00340 HANDLE m_processheap;
00341 #endif
00342
00343
00344 private:
00345
00346 };
00347
00348 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00349
00350 template <ss_typename_param_k T>
00351 inline ws_bool_t operator ==(processheap_allocator<T> const &, processheap_allocator<T> const &)
00352 {
00353 return ws_true_v;
00354 }
00355
00356 template <ss_typename_param_k T>
00357 inline ws_bool_t operator !=(processheap_allocator<T> const &, processheap_allocator<T> const &)
00358 {
00359 return ws_false_v;
00360 }
00361
00362 #endif
00363
00365
00366
00367 #ifdef STLSOFT_UNITTEST
00368
00369 namespace unittest
00370 {
00371 ss_bool_t test_winstl_processheap_allocator(unittest_reporter *r)
00372 {
00373 using stlsoft::unittest::unittest_initialiser;
00374
00375 ss_bool_t bSuccess = true;
00376
00377 unittest_initialiser init(r, "WinSTL", "processheap_allocator", __FILE__);
00378
00379 typedef processheap_allocator<int> int_allocator_t;
00380
00381 int_allocator_t ator1;
00382
00383 int *pi1 = ator1.allocate(100);
00384
00385 if(NULL != pi1)
00386 {
00387 ator1.construct(pi1, 1968);
00388
00389 if(1968 != *pi1)
00390 {
00391 r->report("construct() failed ", __LINE__);
00392 bSuccess = false;
00393 }
00394 }
00395
00396 ator1.deallocate(pi1);
00397
00398 return bSuccess;
00399 }
00400
00401 unittest_registrar unittest_winstl_processheap_allocator(test_winstl_processheap_allocator);
00402
00403 }
00404
00405 #endif
00406
00407
00408
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 #endif
00422
00423
00424
00425
00426
00427