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 COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"
00061 #endif
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"
00064 #endif
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #ifndef _COMSTL_NO_NAMESPACE
00084 # if defined(_STLSOFT_NO_NAMESPACE) || \
00085 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00086
00087 namespace comstl
00088 {
00089 # else
00090
00091
00092 namespace stlsoft
00093 {
00094
00095 namespace comstl_project
00096 {
00097
00098 # endif
00099 #endif
00100
00101
00102
00105
00109
00114
00115
00116
00117
00118
00119 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00120
00121 template <ss_typename_param_k T>
00122 class task_allocator;
00123
00124
00125 STLSOFT_TEMPLATE_SPECIALISATION
00126 class task_allocator<void>
00127 {
00128 public:
00129 typedef void value_type;
00130 typedef task_allocator<void> class_type;
00131 typedef void *pointer;
00132 typedef void const *const_pointer;
00133 typedef ptrdiff_t difference_type;
00134 typedef cs_size_t size_type;
00135
00136 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00137
00138 template <ss_typename_param_k U>
00139 struct rebind
00140 {
00141 typedef task_allocator<U> other;
00142 };
00143 #endif
00144 };
00145
00146 #endif
00147
00151 template <ss_typename_param_k T>
00152 class task_allocator
00153 {
00154 public:
00156 typedef T value_type;
00158 typedef task_allocator<value_type> class_type;
00160 typedef value_type *pointer;
00162 typedef value_type const *const_pointer;
00164 typedef value_type &reference;
00166 typedef value_type const &const_reference;
00168 typedef ptrdiff_t difference_type;
00170 typedef cs_size_t size_type;
00172 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00173 typedef pointer deallocate_pointer;
00174 #else
00175 typedef void *deallocate_pointer;
00176 #endif
00177
00178 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00179
00180 template <ss_typename_param_k U>
00181 struct rebind
00182 {
00183 typedef task_allocator<U> other;
00184 };
00185 #endif
00186
00187
00188 public:
00190 task_allocator() comstl_throw_0()
00191 {}
00193 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00194 task_allocator(const task_allocator &) comstl_throw_0()
00195 {}
00196 #else
00197 template <ss_typename_param_k U>
00198 task_allocator(const task_allocator<U> &) comstl_throw_0()
00199 {}
00200 #endif
00201
00202 ~task_allocator() comstl_throw_0()
00203 {}
00204
00205
00206 public:
00208 size_type max_size() const comstl_throw_0()
00209 {
00210 return static_cast<size_type>(-1) / sizeof(value_type);
00211 }
00212
00213
00214 public:
00218 pointer address(reference x) const comstl_throw_0()
00219 {
00220 return &x;
00221 }
00225 const_pointer address(const_reference x) const comstl_throw_0()
00226 {
00227 return &x;
00228 }
00229
00230
00231 public:
00237 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00238 static
00239 #endif
00240 pointer allocate(size_type n, task_allocator<void>::const_pointer pv = NULL)
00241 {
00242 STLSOFT_SUPPRESS_UNUSED(pv);
00243
00244 task_allocator<void>::pointer p = static_cast<task_allocator<void>::pointer>(::CoTaskMemAlloc(n * sizeof(value_type)));
00245
00246 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00247 if(p == NULL)
00248 {
00249 throw stlsoft_ns_qual_std(bad_alloc)();
00250 }
00251 #endif
00252
00253 return static_cast<pointer>(p);
00254 }
00255
00256 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00257
00258 char *_Charalloc(size_type n)
00259 {
00260 return reinterpret_cast<char*>(allocate(n, NULL));
00261 }
00262 #endif
00263
00268 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00269 static
00270 #endif
00271 void deallocate(pointer p, size_type n)
00272 {
00273 STLSOFT_SUPPRESS_UNUSED(n);
00274
00275 ::CoTaskMemFree(static_cast<HGLOBAL>(p));
00276 }
00277
00281 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00282 static
00283 #endif
00284 void deallocate(pointer p)
00285 {
00286 ::CoTaskMemFree(static_cast<HGLOBAL>(p));
00287 }
00288
00289
00290 public:
00295 void construct(pointer p, value_type const &x)
00296 {
00297 new(p) value_type(x);
00298 }
00299
00303 void construct(pointer p)
00304 {
00305 new(p) value_type();
00306 }
00307
00311 void destroy(pointer p) comstl_throw_0()
00312 {
00313 comstl_destroy_instance(T, value_type, p);
00314 }
00315
00316
00317 private:
00318 class_type const &operator =(class_type const &rhs);
00319 };
00320
00321 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00322
00323 template <ss_typename_param_k T>
00324 inline cs_bool_t operator ==(const task_allocator<T> &, const task_allocator<T> &)
00325 {
00326 return cs_true_v;
00327 }
00328
00329 template <ss_typename_param_k T>
00330 inline cs_bool_t operator !=(const task_allocator<T> &, const task_allocator<T> &)
00331 {
00332 return cs_false_v;
00333 }
00334
00335 #endif
00336
00338
00339
00340 #ifdef STLSOFT_UNITTEST
00341
00342 namespace unittest
00343 {
00344 ss_bool_t test_comstl_task_allocator(unittest_reporter *r)
00345 {
00346 using stlsoft::unittest::unittest_initialiser;
00347
00348 ss_bool_t bSuccess = true;
00349
00350 unittest_initialiser init(r, "COMSTL", "task_allocator", __FILE__);
00351
00352 typedef task_allocator<int> int_allocator_t;
00353
00354 int_allocator_t ator1;
00355
00356 int *pi1 = ator1.allocate(100);
00357
00358 if(NULL != pi1)
00359 {
00360 ator1.construct(pi1, 1968);
00361
00362 if(1968 != *pi1)
00363 {
00364 r->report("new_allocator failed ", __LINE__);
00365 bSuccess = false;
00366 }
00367 }
00368
00369 ator1.deallocate(pi1);
00370
00371 return bSuccess;
00372 }
00373
00374 unittest_registrar unittest_comstl_task_allocator(test_comstl_task_allocator);
00375
00376 }
00377
00378 #endif
00379
00380
00381
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 #endif
00395
00396
00397
00398
00399
00400