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 STLSOFT_INCL_H_STLSOFT
00060 # include "stlsoft.h"
00061 #endif
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"
00064 #endif
00065 #include <stdlib.h>
00066 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00067 # include "stlsoft_sap_cast.h"
00068 #endif
00069
00070
00071
00072
00073
00074 #ifndef _STLSOFT_NO_NAMESPACE
00075 namespace stlsoft
00076 {
00077 #endif
00078
00079
00080
00083
00087
00092
00093
00094
00095
00096
00097 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00098
00099 template <ss_typename_param_k T>
00100 class malloc_allocator;
00101
00102
00103 STLSOFT_TEMPLATE_SPECIALISATION
00104 class malloc_allocator<void>
00105 {
00106 public:
00107 typedef void value_type;
00108 typedef malloc_allocator<void> class_type;
00109 typedef void *pointer;
00110 typedef void const *const_pointer;
00111 typedef ptrdiff_t difference_type;
00112 typedef ss_size_t size_type;
00113
00114 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00115
00116 template <ss_typename_param_k U>
00117 struct rebind
00118 {
00119 typedef malloc_allocator<U> other;
00120 };
00121 #endif
00122 };
00123
00124 #endif
00125
00129 template <ss_typename_param_k T>
00130 class malloc_allocator
00131 {
00132 public:
00134 typedef T value_type;
00136 typedef malloc_allocator<value_type> class_type;
00138 typedef value_type *pointer;
00140 typedef value_type const *const_pointer;
00142 typedef value_type &reference;
00144 typedef value_type const &const_reference;
00146 typedef ptrdiff_t difference_type;
00148 typedef ss_size_t size_type;
00150 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00151 typedef pointer deallocate_pointer;
00152 #else
00153 typedef void *deallocate_pointer;
00154 #endif
00155
00156 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00157
00158 template <ss_typename_param_k U>
00159 struct rebind
00160 {
00161 typedef malloc_allocator<U> other;
00162 };
00163 #endif
00164
00165
00166 public:
00168 malloc_allocator() stlsoft_throw_0()
00169 {}
00171 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00172 malloc_allocator(const malloc_allocator &) stlsoft_throw_0()
00173 {}
00174 #else
00175 template <ss_typename_param_k U>
00176 malloc_allocator(const malloc_allocator<U> &) stlsoft_throw_0()
00177 {}
00178 #endif
00179
00180 ~malloc_allocator() stlsoft_throw_0()
00181 {}
00182
00183
00184 public:
00186 size_type max_size() const stlsoft_throw_0()
00187 {
00188 return static_cast<size_type>(-1) / sizeof(value_type);
00189 }
00190
00191
00192 public:
00196 pointer address(reference x) const stlsoft_throw_0()
00197 {
00198 return &x;
00199 }
00203 const_pointer address(const_reference x) const stlsoft_throw_0()
00204 {
00205 return &x;
00206 }
00207
00208
00209 public:
00215 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00216 static
00217 #endif
00218 pointer allocate(size_type n, malloc_allocator<void>::const_pointer pv = NULL)
00219 {
00220 STLSOFT_SUPPRESS_UNUSED(pv);
00221
00222 malloc_allocator<void>::pointer p = malloc(n * sizeof(value_type));
00223
00224 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00225 if(p == NULL)
00226 {
00227 throw stlsoft_ns_qual_std(bad_alloc)();
00228 }
00229 #endif
00230
00231 return static_cast<pointer>(p);
00232 }
00233
00234 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00235
00236 char *_Charalloc(size_type n)
00237 {
00238 return sap_cast<char*>(allocate(n, NULL));
00239 }
00240 #endif
00241
00242 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00243 static
00244 #endif
00245 pointer reallocate(pointer p, size_type n, malloc_allocator<void>::const_pointer = NULL)
00246 {
00247 malloc_allocator<void>::pointer pNew = realloc(p, n * sizeof(value_type));
00248
00249 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00250 if(pNew == NULL)
00251 {
00252 throw stlsoft_ns_qual_std(bad_alloc)();
00253 }
00254 #endif
00255
00256 return static_cast<pointer>(pNew);
00257 }
00258
00263 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00264 static
00265 #endif
00266 void deallocate(deallocate_pointer p, size_type n)
00267 {
00268 STLSOFT_SUPPRESS_UNUSED(n);
00269
00270 free(p);
00271 }
00272
00276 void deallocate(deallocate_pointer p)
00277 {
00278 free(p);
00279 }
00280
00281
00282 public:
00287 void construct(pointer p, value_type const &x)
00288 {
00289 stlsoft_assert(p != NULL);
00290
00291 new(p) value_type(x);
00292 }
00293
00297 void construct(pointer p)
00298 {
00299 stlsoft_assert(p != NULL);
00300
00301 new(p) value_type();
00302 }
00303
00307 void destroy(pointer p) stlsoft_throw_0()
00308 {
00309 stlsoft_assert(p != NULL);
00310
00311 stlsoft_destroy_instance(T, value_type, p);
00312 }
00313
00314
00315 private:
00316 class_type const &operator =(class_type const &rhs);
00317 };
00318
00319 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00320
00321 template <ss_typename_param_k T>
00322 inline ss_bool_t operator ==(const malloc_allocator<T> &, const malloc_allocator<T> &)
00323 {
00324 return ss_true_v;
00325 }
00326
00327 template <ss_typename_param_k T>
00328 inline ss_bool_t operator !=(const malloc_allocator<T> &, const malloc_allocator<T> &)
00329 {
00330 return ss_false_v;
00331 }
00332
00333 #endif
00334
00336
00337
00338 #ifdef STLSOFT_UNITTEST
00339
00340 namespace unittest
00341 {
00342 ss_bool_t test_stlsoft_malloc_allocator(unittest_reporter *r)
00343 {
00344 ss_bool_t bSuccess = true;
00345
00346 unittest_initialiser init(r, "STLSoft", "malloc_allocator", __FILE__);
00347
00348 typedef malloc_allocator<int> int_allocator_t;
00349
00350 int_allocator_t ator1;
00351
00352 int *pi1 = ator1.allocate(100);
00353
00354 if(NULL != pi1)
00355 {
00356 ator1.construct(pi1, 1968);
00357
00358 if(1968 != *pi1)
00359 {
00360 r->report("malloc_allocator failed ", __LINE__);
00361 bSuccess = false;
00362 }
00363 }
00364
00365 ator1.deallocate(pi1);
00366
00367 return bSuccess;
00368 }
00369
00370 unittest_registrar unittest_stlsoft_malloc_allocator(test_stlsoft_malloc_allocator);
00371
00372 }
00373
00374 #endif
00375
00376
00377
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390