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  

winstl_processheap_allocator.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_processheap_allocator.h
00003  *
00004  * Purpose:     processheap_allocator class.
00005  *
00006  * Created:     25th February 2002
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-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 WINSTL_INCL_H_WINSTL_PROCESSHEAP_ALLOCATOR
00046 #define WINSTL_INCL_H_WINSTL_PROCESSHEAP_ALLOCATOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define WINSTL_VER_H_WINSTL_PROCESSHEAP_ALLOCATOR_MAJOR     2
00050 # define WINSTL_VER_H_WINSTL_PROCESSHEAP_ALLOCATOR_MINOR     0
00051 # define WINSTL_VER_H_WINSTL_PROCESSHEAP_ALLOCATOR_REVISION  1
00052 # define WINSTL_VER_H_WINSTL_PROCESSHEAP_ALLOCATOR_EDIT      47
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef WINSTL_INCL_H_WINSTL
00060 # include "winstl.h"                    // Include the WinSTL root header
00061 #endif /* !WINSTL_INCL_H_WINSTL */
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 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00066 # include "stlsoft_sap_cast.h"
00067 #endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00068 
00069 /* 
00070  * Namespace
00071  */
00072 
00073 #ifndef _WINSTL_NO_NAMESPACE
00074 # if defined(_STLSOFT_NO_NAMESPACE) || \
00075      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00076 /* There is no stlsoft namespace, so must define ::winstl */
00077 namespace winstl
00078 {
00079 # else
00080 /* Define stlsoft::winstl_project */
00081 
00082 namespace stlsoft
00083 {
00084 
00085 namespace winstl_project
00086 {
00087 
00088 # endif /* _STLSOFT_NO_NAMESPACE */
00089 #endif /* !_WINSTL_NO_NAMESPACE */
00090 
00091 /* 
00092 
00095 
00099 
00104 
00105 /* 
00106  * Classes
00107  */
00108 
00109 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00110 
00111 template <ss_typename_param_k T>
00112 class processheap_allocator;
00113 
00114 // Specialisation for void
00115 STLSOFT_TEMPLATE_SPECIALISATION
00116 class processheap_allocator<void>
00117 {
00118 public:
00119     typedef void                                value_type;
00120     typedef processheap_allocator<void>         class_type;
00121     typedef void                                *pointer;
00122     typedef void const                          *const_pointer;
00123     typedef ptrdiff_t                           difference_type;
00124     typedef ws_size_t                           size_type;
00125 
00126 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00127 
00128     template <ss_typename_param_k U>
00129     struct rebind
00130     {
00131         typedef processheap_allocator<U>        other;
00132     };
00133 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00134 };
00135 
00136 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00137 
00141 template <ss_typename_param_k T>
00142 class processheap_allocator
00143 {
00144 public:
00146     typedef T                                   value_type;
00148     typedef processheap_allocator<value_type>   class_type;
00150     typedef value_type                          *pointer;
00152     typedef value_type const                    *const_pointer;
00154     typedef value_type                          &reference;
00156     typedef value_type const                    &const_reference;
00158     typedef ptrdiff_t                           difference_type;
00160     typedef ws_size_t                           size_type;
00162 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00163     typedef pointer                             deallocate_pointer;
00164 #else
00165     typedef void                                *deallocate_pointer;
00166 #endif /* __STLSOFT_COMPILER_IS_MSVC && _MSC_VER == 1200 */
00167 
00168 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00169 
00170     template <ss_typename_param_k U>
00171     struct rebind
00172     {
00173         typedef processheap_allocator<U>         other;
00174     };
00175 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00176 
00177 // Construction
00178 public:
00180     processheap_allocator() winstl_throw_0()
00181 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00182         : m_processheap(::GetProcessHeap())
00183 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00184     {}
00186 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00187     processheap_allocator(processheap_allocator const &/* rhs */) winstl_throw_0()
00188 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00189         : m_processheap(::GetProcessHeap())
00190 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00191     {}
00192 #else /* ? __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00193     template <ss_typename_param_k U>
00194     processheap_allocator(processheap_allocator<U> const &/* rhs */) winstl_throw_0()
00195 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00196         : m_processheap(::GetProcessHeap())
00197 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00198     {}
00199 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00200 
00201     ~processheap_allocator() winstl_throw_0()
00202     {}
00203 
00204 // Attributes
00205 public:
00207     size_type max_size() const winstl_throw_0()
00208     {
00209         return static_cast<size_type>(-1) / sizeof(value_type);
00210     }
00211 
00212 // Conversion
00213 public:
00217     pointer address(reference x) const winstl_throw_0()
00218     {
00219         return &x;
00220     }
00224     const_pointer address(const_reference x) const winstl_throw_0()
00225     {
00226         return &x;
00227     }
00228 
00229 // Allocation
00230 public:
00236 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00237     static
00238 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00239     pointer allocate(size_type n, processheap_allocator<void>::const_pointer pv = NULL)
00240     {
00241         STLSOFT_SUPPRESS_UNUSED(pv);
00242 
00243         processheap_allocator<void>::pointer p   =   static_cast<processheap_allocator<void>::pointer>(::HeapAlloc(get_process_(), 0, n * sizeof(value_type)));
00244 
00245 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00246         if(p == NULL)
00247         {
00248             throw ::std::bad_alloc();
00249         }
00250 #endif /* __STLSOFT_CF_THROW_BAD_ALLOC */
00251 
00252         return static_cast<pointer>(p);
00253     }
00254 
00255 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00256 
00257     char *_Charalloc(size_type n)
00258     {
00259         return sap_cast<char*>(allocate(n, NULL));
00260     }
00261 #endif /* __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD */
00262 
00267 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00268     static
00269 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00270     void deallocate(pointer p, size_type n)
00271     {
00272         STLSOFT_SUPPRESS_UNUSED(n);
00273 
00274         ::HeapFree(get_process_(), 0, p);
00275     }
00276 
00280 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00281     static
00282 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00283     void deallocate(pointer p)
00284     {
00285         ::HeapFree(get_process_(), 0, p);
00286     }
00287 
00288 // Operations
00289 public:
00294     void construct(pointer p, value_type const &x)
00295     {
00296         winstl_assert(p != NULL);
00297 
00298         new(p) value_type(x);
00299     }
00300 
00304     void construct(pointer p)
00305     {
00306         winstl_assert(p != NULL);
00307 
00308         new(p) value_type();
00309     }
00310 
00314     void destroy(pointer p) winstl_throw_0()
00315     {
00316         winstl_assert(p != NULL);
00317 
00318         winstl_destroy_instance(T, value_type, p);
00319     }
00320 
00321 // Members
00322 protected:
00323 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00324     static HANDLE get_process_()
00325     {
00326         static HANDLE   s_processheap   =   ::GetProcessHeap();
00327 
00328         return s_processheap;
00329     }
00330 #else
00331     HANDLE get_process_()
00332     {
00333         return m_processheap;
00334     }
00335 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00336 
00337 // Members
00338 protected:
00339 #ifndef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00340     HANDLE  m_processheap;
00341 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00342 
00343 // Not to be implemented
00344 private:
00345 //    class_type const &operator =(class_type const &rhs);
00346 };
00347 
00348 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00349 
00350 template <ss_typename_param_k T>
00351 inline ws_bool_t operator ==(processheap_allocator<T> const &/* lhs */, processheap_allocator<T> const &/* rhs */)
00352 {
00353     return ws_true_v;
00354 }
00355 
00356 template <ss_typename_param_k T>
00357 inline ws_bool_t operator !=(processheap_allocator<T> const &/* lhs */, processheap_allocator<T> const &/* rhs */)
00358 {
00359     return ws_false_v;
00360 }
00361 
00362 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00363 
00365 // Unit-testing
00366 
00367 #ifdef STLSOFT_UNITTEST
00368 
00369 namespace unittest
00370 {
00371     ss_bool_t test_winstl_processheap_allocator(unittest_reporter *r)
00372     {
00373         using stlsoft::unittest::unittest_initialiser;
00374 
00375         ss_bool_t               bSuccess    =   true;
00376 
00377         unittest_initialiser    init(r, "WinSTL", "processheap_allocator", __FILE__);
00378 
00379         typedef processheap_allocator<int>  int_allocator_t;
00380 
00381         int_allocator_t ator1;
00382 
00383         int     *pi1    =   ator1.allocate(100);
00384 
00385         if(NULL != pi1)
00386         {
00387             ator1.construct(pi1, 1968);
00388 
00389             if(1968 != *pi1)
00390             {
00391                 r->report("construct() failed ", __LINE__);
00392                 bSuccess = false;
00393             }
00394         }
00395 
00396         ator1.deallocate(pi1);
00397 
00398         return bSuccess;
00399     }
00400 
00401     unittest_registrar    unittest_winstl_processheap_allocator(test_winstl_processheap_allocator);
00402 
00403 } // namespace unittest
00404 
00405 #endif /* STLSOFT_UNITTEST */
00406 
00407 /* 
00408 
00410 
00411 /* 
00412 
00413 #ifndef _WINSTL_NO_NAMESPACE
00414 # if defined(_STLSOFT_NO_NAMESPACE) || \
00415      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00416 } // namespace winstl
00417 # else
00418 } // namespace winstl_project
00419 } // namespace stlsoft
00420 # endif /* _STLSOFT_NO_NAMESPACE */
00421 #endif /* !_WINSTL_NO_NAMESPACE */
00422 
00423 /* 
00424 
00425 #endif /* WINSTL_INCL_H_WINSTL_PROCESSHEAP_ALLOCATOR */
00426 
00427 /* 

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