libstdc++
|
00001 // Allocator that wraps "C" malloc -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /** @file ext/malloc_allocator.h 00027 * This file is a GNU extension to the Standard C++ Library. 00028 */ 00029 00030 #ifndef _MALLOC_ALLOCATOR_H 00031 #define _MALLOC_ALLOCATOR_H 1 00032 00033 #include <cstdlib> 00034 #include <new> 00035 #include <bits/functexcept.h> 00036 #include <bits/move.h> 00037 00038 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 00039 00040 using std::size_t; 00041 using std::ptrdiff_t; 00042 00043 /** 00044 * @brief An allocator that uses malloc. 00045 * @ingroup allocators 00046 * 00047 * This is precisely the allocator defined in the C++ Standard. 00048 * - all allocation calls malloc 00049 * - all deallocation calls free 00050 */ 00051 template<typename _Tp> 00052 class malloc_allocator 00053 { 00054 public: 00055 typedef size_t size_type; 00056 typedef ptrdiff_t difference_type; 00057 typedef _Tp* pointer; 00058 typedef const _Tp* const_pointer; 00059 typedef _Tp& reference; 00060 typedef const _Tp& const_reference; 00061 typedef _Tp value_type; 00062 00063 template<typename _Tp1> 00064 struct rebind 00065 { typedef malloc_allocator<_Tp1> other; }; 00066 00067 malloc_allocator() throw() { } 00068 00069 malloc_allocator(const malloc_allocator&) throw() { } 00070 00071 template<typename _Tp1> 00072 malloc_allocator(const malloc_allocator<_Tp1>&) throw() { } 00073 00074 ~malloc_allocator() throw() { } 00075 00076 pointer 00077 address(reference __x) const { return &__x; } 00078 00079 const_pointer 00080 address(const_reference __x) const { return &__x; } 00081 00082 // NB: __n is permitted to be 0. The C++ standard says nothing 00083 // about what the return value is when __n == 0. 00084 pointer 00085 allocate(size_type __n, const void* = 0) 00086 { 00087 if (__builtin_expect(__n > this->max_size(), false)) 00088 std::__throw_bad_alloc(); 00089 00090 pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp))); 00091 if (!__ret) 00092 std::__throw_bad_alloc(); 00093 return __ret; 00094 } 00095 00096 // __p is not permitted to be a null pointer. 00097 void 00098 deallocate(pointer __p, size_type) 00099 { std::free(static_cast<void*>(__p)); } 00100 00101 size_type 00102 max_size() const throw() 00103 { return size_t(-1) / sizeof(_Tp); } 00104 00105 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00106 // 402. wrong new expression in [some_] allocator::construct 00107 void 00108 construct(pointer __p, const _Tp& __val) 00109 { ::new((void *)__p) value_type(__val); } 00110 00111 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00112 template<typename... _Args> 00113 void 00114 construct(pointer __p, _Args&&... __args) 00115 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); } 00116 #endif 00117 00118 void 00119 destroy(pointer __p) { __p->~_Tp(); } 00120 }; 00121 00122 template<typename _Tp> 00123 inline bool 00124 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&) 00125 { return true; } 00126 00127 template<typename _Tp> 00128 inline bool 00129 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&) 00130 { return false; } 00131 00132 _GLIBCXX_END_NAMESPACE 00133 00134 #endif