malloc_allocator.h

Go to the documentation of this file.
00001 // Allocator that wraps "C" malloc -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file ext/malloc_allocator.h
00032  *  This file is a GNU extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _MALLOC_ALLOCATOR_H
00036 #define _MALLOC_ALLOCATOR_H 1
00037 
00038 #include <cstdlib>
00039 #include <new>
00040 #include <bits/functexcept.h>
00041 #include <bits/stl_move.h>
00042 
00043 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00044 
00045   using std::size_t;
00046   using std::ptrdiff_t;
00047 
00048   /**
00049    *  @brief  An allocator that uses malloc.
00050    *
00051    *  This is precisely the allocator defined in the C++ Standard. 
00052    *    - all allocation calls malloc
00053    *    - all deallocation calls free
00054    */
00055   template<typename _Tp>
00056     class malloc_allocator
00057     {
00058     public:
00059       typedef size_t     size_type;
00060       typedef ptrdiff_t  difference_type;
00061       typedef _Tp*       pointer;
00062       typedef const _Tp* const_pointer;
00063       typedef _Tp&       reference;
00064       typedef const _Tp& const_reference;
00065       typedef _Tp        value_type;
00066 
00067       template<typename _Tp1>
00068         struct rebind
00069         { typedef malloc_allocator<_Tp1> other; };
00070 
00071       malloc_allocator() throw() { }
00072 
00073       malloc_allocator(const malloc_allocator&) throw() { }
00074 
00075       template<typename _Tp1>
00076         malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
00077 
00078       ~malloc_allocator() throw() { }
00079 
00080       pointer
00081       address(reference __x) const { return &__x; }
00082 
00083       const_pointer
00084       address(const_reference __x) const { return &__x; }
00085 
00086       // NB: __n is permitted to be 0.  The C++ standard says nothing
00087       // about what the return value is when __n == 0.
00088       pointer
00089       allocate(size_type __n, const void* = 0)
00090       {
00091     if (__builtin_expect(__n > this->max_size(), false))
00092       std::__throw_bad_alloc();
00093 
00094     pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
00095     if (!__ret)
00096       std::__throw_bad_alloc();
00097     return __ret;
00098       }
00099 
00100       // __p is not permitted to be a null pointer.
00101       void
00102       deallocate(pointer __p, size_type)
00103       { std::free(static_cast<void*>(__p)); }
00104 
00105       size_type
00106       max_size() const throw() 
00107       { return size_t(-1) / sizeof(_Tp); }
00108 
00109       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00110       // 402. wrong new expression in [some_] allocator::construct
00111       void 
00112       construct(pointer __p, const _Tp& __val) 
00113       { ::new((void *)__p) value_type(__val); }
00114 
00115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00116       template<typename... _Args>
00117         void
00118         construct(pointer __p, _Args&&... __args) 
00119     { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
00120 #endif
00121 
00122       void 
00123       destroy(pointer __p) { __p->~_Tp(); }
00124     };
00125 
00126   template<typename _Tp>
00127     inline bool
00128     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00129     { return true; }
00130   
00131   template<typename _Tp>
00132     inline bool
00133     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00134     { return false; }
00135 
00136 _GLIBCXX_END_NAMESPACE
00137 
00138 #endif

Generated on Fri Jan 23 20:12:14 2009 for libstdc++ by  doxygen 1.5.6