libstdc++
malloc_allocator.h
Go to the documentation of this file.
00001 // Allocator that wraps "C" malloc -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/malloc_allocator.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _MALLOC_ALLOCATOR_H
00030 #define _MALLOC_ALLOCATOR_H 1
00031 
00032 #include <cstdlib>
00033 #include <new>
00034 #include <bits/functexcept.h>
00035 #include <bits/move.h>
00036 #if __cplusplus >= 201103L
00037 #include <type_traits>
00038 #endif
00039 
00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00044   using std::size_t;
00045   using std::ptrdiff_t;
00046 
00047   /**
00048    *  @brief  An allocator that uses malloc.
00049    *  @ingroup allocators
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 #if __cplusplus >= 201103L
00072       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00073       // 2103. propagate_on_container_move_assignment
00074       typedef std::true_type propagate_on_container_move_assignment;
00075 #endif
00076 
00077       malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
00078 
00079       malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
00080 
00081       template<typename _Tp1>
00082         malloc_allocator(const malloc_allocator<_Tp1>&)
00083     _GLIBCXX_USE_NOEXCEPT { }
00084 
00085       ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
00086 
00087       pointer
00088       address(reference __x) const _GLIBCXX_NOEXCEPT
00089       { return std::__addressof(__x); }
00090 
00091       const_pointer
00092       address(const_reference __x) const _GLIBCXX_NOEXCEPT
00093       { return std::__addressof(__x); }
00094 
00095       // NB: __n is permitted to be 0.  The C++ standard says nothing
00096       // about what the return value is when __n == 0.
00097       pointer
00098       allocate(size_type __n, const void* = 0)
00099       {
00100     if (__n > this->max_size())
00101       std::__throw_bad_alloc();
00102 
00103     pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
00104     if (!__ret)
00105       std::__throw_bad_alloc();
00106     return __ret;
00107       }
00108 
00109       // __p is not permitted to be a null pointer.
00110       void
00111       deallocate(pointer __p, size_type)
00112       { std::free(static_cast<void*>(__p)); }
00113 
00114       size_type
00115       max_size() const _GLIBCXX_USE_NOEXCEPT 
00116       { return size_t(-1) / sizeof(_Tp); }
00117 
00118 #if __cplusplus >= 201103L
00119       template<typename _Up, typename... _Args>
00120         void
00121         construct(_Up* __p, _Args&&... __args)
00122     { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
00123 
00124       template<typename _Up>
00125         void 
00126         destroy(_Up* __p) { __p->~_Up(); }
00127 #else
00128       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00129       // 402. wrong new expression in [some_] allocator::construct
00130       void 
00131       construct(pointer __p, const _Tp& __val) 
00132       { ::new((void *)__p) value_type(__val); }
00133 
00134       void 
00135       destroy(pointer __p) { __p->~_Tp(); }
00136 #endif
00137     };
00138 
00139   template<typename _Tp>
00140     inline bool
00141     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00142     { return true; }
00143   
00144   template<typename _Tp>
00145     inline bool
00146     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00147     { return false; }
00148 
00149 _GLIBCXX_END_NAMESPACE_VERSION
00150 } // namespace
00151 
00152 #endif