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