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
00047 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR
00048 #define STLSOFT_INCL_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR
00049
00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR_MAJOR 4
00052 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR_MINOR 0
00053 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR_REVISION 6
00054 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR_EDIT 86
00055 #endif
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00070 # include <stlsoft/stlsoft.h>
00071 #endif
00072 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE
00073 # include <stlsoft/memory/allocator_base.hpp>
00074 #endif
00075 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
00076 # include <stlsoft/util/constraints.hpp>
00077 #endif
00078
00079 #ifndef STLSOFT_INCL_H_STDLIB
00080 # define STLSOFT_INCL_H_STDLIB
00081 # include <stdlib.h>
00082 #endif
00083
00084
00085
00086
00087
00088 #ifndef _STLSOFT_NO_NAMESPACE
00089 namespace stlsoft
00090 {
00091 #endif
00092
00093
00094
00095
00096
00103 template <ss_typename_param_k T>
00104 class malloc_allocator
00105 : public allocator_base<T, malloc_allocator<T> >
00106 {
00107 private:
00108 typedef allocator_base<T, malloc_allocator<T> > parent_class_type;
00109 public:
00111 typedef malloc_allocator<T> class_type;
00113 typedef ss_typename_type_k parent_class_type::value_type value_type;
00115 typedef ss_typename_type_k parent_class_type::pointer pointer;
00117 typedef ss_typename_type_k parent_class_type::const_pointer const_pointer;
00119 typedef ss_typename_type_k parent_class_type::reference reference;
00121 typedef ss_typename_type_k parent_class_type::const_reference const_reference;
00123 typedef ss_typename_type_k parent_class_type::difference_type difference_type;
00125 typedef ss_typename_type_k parent_class_type::size_type size_type;
00126
00127 public:
00128 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00130 template <ss_typename_param_k U>
00131 struct rebind
00132 {
00133 typedef malloc_allocator<U> other;
00134 };
00135 #endif
00136
00139 public:
00141 malloc_allocator() stlsoft_throw_0()
00142 {}
00144 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00145 template <ss_typename_param_k U>
00146 malloc_allocator(malloc_allocator<U> const&)
00147 {}
00148 #else
00149 malloc_allocator(class_type const&)
00150 {}
00151 #endif
00153
00156 public:
00157 pointer reallocate(pointer p, size_type n, void const* hint = NULL)
00158 {
00159 STLSOFT_SUPPRESS_UNUSED(hint);
00160
00161 stlsoft_constraint_must_be_pod_or_void(value_type);
00162
00163 void *pNew = ::realloc(p, n * sizeof(value_type));
00164
00165 #ifdef STLSOFT_CF_THROW_BAD_ALLOC
00166 if(pNew == NULL)
00167 {
00168 STLSOFT_THROW_X(stlsoft_ns_qual_std(bad_alloc)());
00169 }
00170 #endif
00171
00172 return static_cast<pointer>(pNew);
00173 }
00175
00176 private:
00177 friend class allocator_base<T, malloc_allocator<T> >;
00178
00179 void *do_allocate(size_type n, void const* hint)
00180 {
00181 STLSOFT_SUPPRESS_UNUSED(hint);
00182
00183 return ::malloc(n * sizeof(value_type));
00184 }
00185 void do_deallocate(void *pv, size_type n)
00186 {
00187 STLSOFT_SUPPRESS_UNUSED(n);
00188
00189 ::free(pv);
00190 }
00191 void do_deallocate(void *pv)
00192 {
00193 ::free(pv);
00194 }
00195 };
00196
00197
00198
00199 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00200
00201
00202 STLSOFT_TEMPLATE_SPECIALISATION
00203 class malloc_allocator<void>
00204 {
00205 public:
00206 typedef void value_type;
00207 typedef malloc_allocator<void> class_type;
00208 typedef void* pointer;
00209 typedef void const* const_pointer;
00210 typedef ss_ptrdiff_t difference_type;
00211 typedef ss_size_t size_type;
00212
00213 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00215 template <ss_typename_param_k U>
00216 struct rebind
00217 {
00218 typedef malloc_allocator<U> other;
00219 };
00220 #endif
00221 };
00222
00223 #endif
00224
00225 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00226
00227 template <ss_typename_param_k T>
00228 inline ss_bool_t operator ==(const malloc_allocator<T> &, const malloc_allocator<T> &)
00229 {
00230 return ss_true_v;
00231 }
00232
00233 template <ss_typename_param_k T>
00234 inline ss_bool_t operator !=(const malloc_allocator<T> &, const malloc_allocator<T> &)
00235 {
00236 return ss_false_v;
00237 }
00238
00239 #endif
00240
00242
00243
00244 #ifdef STLSOFT_UNITTEST
00245 # include "./unittest/malloc_allocator_unittest_.h"
00246 #endif
00247
00248
00249
00250 #ifndef _STLSOFT_NO_NAMESPACE
00251 }
00252 #endif
00253
00254
00255
00256 #endif
00257
00258