STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

stlsoft_malloc_allocator.h

Go to the documentation of this file.
00001 /* 
00002  * File:        stlsoft_malloc_allocator.h (originally MRAtors.h; ::SynesisCrt)
00003  *
00004  * Purpose:     stlsoft_malloc_allocator class - uses malloc()/free().
00005  *
00006  * Created:     2nd January 2001
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2001-2004, 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 
00044 
00045 #ifndef STLSOFT_INCL_H_STLSOFT_MALLOC_ALLOCATOR
00046 #define STLSOFT_INCL_H_STLSOFT_MALLOC_ALLOCATOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define STLSOFT_VER_H_STLSOFT_MALLOC_ALLOCATOR_MAJOR       2
00050 # define STLSOFT_VER_H_STLSOFT_MALLOC_ALLOCATOR_MINOR       0
00051 # define STLSOFT_VER_H_STLSOFT_MALLOC_ALLOCATOR_REVISION    1
00052 # define STLSOFT_VER_H_STLSOFT_MALLOC_ALLOCATOR_EDIT        47
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef STLSOFT_INCL_H_STLSOFT
00060 # include "stlsoft.h"                   // Include the STLSoft root header
00061 #endif /* !STLSOFT_INCL_H_STLSOFT */
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"    // Include the STLSoft root header
00064 #endif /* !STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE */
00065 #include <stdlib.h>                     // malloc, free
00066 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00067 # include "stlsoft_sap_cast.h"
00068 #endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00069 
00070 /* 
00071  * Namespace
00072  */
00073 
00074 #ifndef _STLSOFT_NO_NAMESPACE
00075 namespace stlsoft
00076 {
00077 #endif /* _STLSOFT_NO_NAMESPACE */
00078 
00079 /* 
00080 
00083 
00087 
00092 
00093 /* 
00094  * Classes
00095  */
00096 
00097 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00098 
00099 template <ss_typename_param_k T>
00100 class malloc_allocator;
00101 
00102 // Specialisation for void
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 /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00122 };
00123 
00124 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
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 /* __STLSOFT_COMPILER_IS_MSVC && _MSC_VER == 1200 */
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 /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00164 
00165 // Construction
00166 public:
00168     malloc_allocator() stlsoft_throw_0()
00169     {}
00171 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00172     malloc_allocator(const malloc_allocator &/* rhs */) stlsoft_throw_0()
00173     {}
00174 #else
00175     template <ss_typename_param_k U>
00176     malloc_allocator(const malloc_allocator<U> &/* rhs */) stlsoft_throw_0()
00177     {}
00178 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
00179 
00180     ~malloc_allocator() stlsoft_throw_0()
00181     {}
00182 
00183 // Attributes
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 // Conversion
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 // Allocation
00209 public:
00215 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00216     static
00217 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
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 /* __STLSOFT_CF_THROW_BAD_ALLOC */
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 /* __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD */
00241 
00242 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00243     static
00244 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
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 /* __STLSOFT_CF_THROW_BAD_ALLOC */
00255 
00256         return static_cast<pointer>(pNew);
00257     }
00258 
00263 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00264     static
00265 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
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 // Operations
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 // Not to be implemented
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> &/* lhs */, const malloc_allocator<T> &/* rhs */)
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> &/* lhs */, const malloc_allocator<T> &/* rhs */)
00329 {
00330     return ss_false_v;
00331 }
00332 
00333 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00334 
00336 // Unit-testing
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 } // namespace unittest
00373 
00374 #endif /* STLSOFT_UNITTEST */
00375 
00376 /* 
00377 
00379 
00380 /* 
00381 
00382 #ifndef _STLSOFT_NO_NAMESPACE
00383 } // namespace stlsoft
00384 #endif /* _STLSOFT_NO_NAMESPACE */
00385 
00386 /* 
00387 
00388 #endif /* !STLSOFT_INCL_H_STLSOFT_MALLOC_ALLOCATOR */
00389 
00390 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004