00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: stlsoft/memory/new_allocator.hpp 00003 * 00004 * Purpose: stlsoft_new_allocator class - use new & delete operators. 00005 * 00006 * Created: 2nd January 2001 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2001-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00047 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR 00048 #define STLSOFT_INCL_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR 00049 00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00051 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR_MAJOR 4 00052 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR_MINOR 0 00053 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR_REVISION 2 00054 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR_EDIT 81 00055 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00056 00057 /* ///////////////////////////////////////////////////////////////////////// 00058 * Compatibility 00059 */ 00060 00061 /* 00062 [DocumentationStatus:Ready] 00063 */ 00064 00065 /* ///////////////////////////////////////////////////////////////////////// 00066 * Includes 00067 */ 00068 00069 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT 00070 # include <stlsoft/stlsoft.h> 00071 #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */ 00072 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE 00073 # include <stlsoft/memory/allocator_base.hpp> 00074 #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */ 00075 00076 /* ///////////////////////////////////////////////////////////////////////// 00077 * Namespace 00078 */ 00079 00080 #ifndef _STLSOFT_NO_NAMESPACE 00081 namespace stlsoft 00082 { 00083 #endif /* _STLSOFT_NO_NAMESPACE */ 00084 00085 /* ///////////////////////////////////////////////////////////////////////// 00086 * Classes 00087 */ 00088 00095 template <ss_typename_param_k T> 00096 class new_allocator 00097 : public allocator_base<T, new_allocator<T> > 00098 { 00099 private: 00100 typedef allocator_base<T, new_allocator<T> > parent_class_type; 00101 public: 00103 typedef new_allocator<T> class_type; 00105 typedef ss_typename_type_k parent_class_type::value_type value_type; 00107 typedef ss_typename_type_k parent_class_type::pointer pointer; 00109 typedef ss_typename_type_k parent_class_type::const_pointer const_pointer; 00111 typedef ss_typename_type_k parent_class_type::reference reference; 00113 typedef ss_typename_type_k parent_class_type::const_reference const_reference; 00115 typedef ss_typename_type_k parent_class_type::difference_type difference_type; 00117 typedef ss_typename_type_k parent_class_type::size_type size_type; 00118 00119 public: 00120 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00122 template <ss_typename_param_k U> 00123 struct rebind 00124 { 00125 typedef new_allocator<U> other; 00126 }; 00127 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00128 00131 public: 00133 new_allocator() stlsoft_throw_0() 00134 {} 00136 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00137 template <ss_typename_param_k U> 00138 new_allocator(new_allocator<U> const&) 00139 {} 00140 #else /* ? STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00141 new_allocator(class_type const&) 00142 {} 00143 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00145 00146 private: 00147 friend class allocator_base<T, new_allocator<T> >; 00148 00149 void *do_allocate(size_type n, void const* hint) 00150 { 00151 STLSOFT_SUPPRESS_UNUSED(hint); 00152 00153 #if defined(new) || \ 00154 defined(delete) 00155 return new ss_byte_t[n * sizeof(value_type)]; 00156 #else /* ? new || delete */ 00157 return ::operator new (n * sizeof(value_type)); 00158 #endif /* new || delete */ 00159 } 00160 void do_deallocate(void *pv, size_type n) 00161 { 00162 STLSOFT_SUPPRESS_UNUSED(n); 00163 00164 #if defined(new) || \ 00165 defined(delete) 00166 delete [] static_cast<ss_byte_t*>(pv); 00167 #else /* ? new || delete */ 00168 ::operator delete (pv); 00169 #endif /* new || delete */ 00170 } 00171 void do_deallocate(void *pv) 00172 { 00173 #if defined(new) || \ 00174 defined(delete) 00175 delete [] static_cast<ss_byte_t*>(pv); 00176 #else /* ? new || delete */ 00177 ::operator delete (pv); 00178 #endif /* new || delete */ 00179 } 00180 00181 private: 00182 // class_type& operator =(class_type const&); 00183 }; 00184 00185 00186 00187 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00188 00189 // Specialisation for void 00190 STLSOFT_TEMPLATE_SPECIALISATION 00191 class new_allocator<void> 00192 { 00193 public: 00194 typedef void value_type; 00195 typedef new_allocator<void> class_type; 00196 typedef void* pointer; 00197 typedef void const* const_pointer; 00198 typedef ss_ptrdiff_t difference_type; 00199 typedef ss_size_t size_type; 00200 00201 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00203 template <ss_typename_param_k U> 00204 struct rebind 00205 { 00206 typedef new_allocator<U> other; 00207 }; 00208 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00209 }; 00210 00211 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00212 00213 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00214 00215 template <ss_typename_param_k T> 00216 inline ss_bool_t operator ==(const new_allocator<T> &/* lhs */, const new_allocator<T> &/* rhs */) 00217 { 00218 return ss_true_v; 00219 } 00220 00221 template <ss_typename_param_k T> 00222 inline ss_bool_t operator !=(const new_allocator<T> &/* lhs */, const new_allocator<T> &/* rhs */) 00223 { 00224 return ss_false_v; 00225 } 00226 00227 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00228 00230 // Unit-testing 00231 00232 #ifdef STLSOFT_UNITTEST 00233 # include "./unittest/new_allocator_unittest_.h" 00234 #endif /* STLSOFT_UNITTEST */ 00235 00236 /* ////////////////////////////////////////////////////////////////////// */ 00237 00238 #ifndef _STLSOFT_NO_NAMESPACE 00239 } // namespace stlsoft 00240 #endif /* _STLSOFT_NO_NAMESPACE */ 00241 00242 /* ////////////////////////////////////////////////////////////////////// */ 00243 00244 #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_NEW_ALLOCATOR */ 00245 00246 /* ///////////////////////////// end of file //////////////////////////// */