libstdc++
|
00001 // Allocators -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 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 /* 00026 * Copyright (c) 1996-1997 00027 * Silicon Graphics Computer Systems, Inc. 00028 * 00029 * Permission to use, copy, modify, distribute and sell this software 00030 * and its documentation for any purpose is hereby granted without fee, 00031 * provided that the above copyright notice appear in all copies and 00032 * that both that copyright notice and this permission notice appear 00033 * in supporting documentation. Silicon Graphics makes no 00034 * representations about the suitability of this software for any 00035 * purpose. It is provided "as is" without express or implied warranty. 00036 */ 00037 00038 /** @file ext/debug_allocator.h 00039 * This file is a GNU extension to the Standard C++ Library. 00040 */ 00041 00042 #ifndef _DEBUG_ALLOCATOR_H 00043 #define _DEBUG_ALLOCATOR_H 1 00044 00045 #include <stdexcept> 00046 #include <bits/functexcept.h> 00047 #include <ext/alloc_traits.h> 00048 00049 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00050 { 00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00052 00053 using std::size_t; 00054 00055 /** 00056 * @brief A meta-allocator with debugging bits. 00057 * @ingroup allocators 00058 * 00059 * This is precisely the allocator defined in the C++03 Standard. 00060 */ 00061 template<typename _Alloc> 00062 class debug_allocator 00063 { 00064 template<typename> friend class debug_allocator; 00065 00066 typedef __alloc_traits<_Alloc> _Traits; 00067 00068 public: 00069 typedef typename _Traits::size_type size_type; 00070 typedef typename _Traits::difference_type difference_type; 00071 typedef typename _Traits::pointer pointer; 00072 typedef typename _Traits::const_pointer const_pointer; 00073 typedef typename _Traits::reference reference; 00074 typedef typename _Traits::const_reference const_reference; 00075 typedef typename _Traits::value_type value_type; 00076 00077 template<typename _Up> 00078 class rebind 00079 { 00080 typedef typename _Traits::template rebind<_Up>::other __other; 00081 00082 public: 00083 typedef debug_allocator<__other> other; 00084 }; 00085 00086 private: 00087 // _M_extra is the number of objects that correspond to the 00088 // extra space where debug information is stored. 00089 size_type _M_extra; 00090 00091 _Alloc _M_allocator; 00092 00093 template<typename _Alloc2, 00094 typename = typename _Alloc2::template rebind<value_type>::other> 00095 struct __convertible 00096 { }; 00097 00098 template<typename _Alloc2> 00099 struct __convertible<_Alloc2, _Alloc> 00100 { 00101 typedef void* __type; 00102 }; 00103 00104 size_type _S_extra() 00105 { 00106 const size_t __obj_size = sizeof(value_type); 00107 return (sizeof(size_type) + __obj_size - 1) / __obj_size; 00108 } 00109 00110 public: 00111 debug_allocator() : _M_extra(_S_extra()) { } 00112 00113 template<typename _Alloc2> 00114 debug_allocator(const debug_allocator<_Alloc2>& __a2, 00115 typename __convertible<_Alloc2>::__type = 0) 00116 : _M_allocator(__a2._M_allocator), _M_extra(_S_extra()) { } 00117 00118 debug_allocator(const _Alloc& __a) 00119 : _M_allocator(__a), _M_extra(_S_extra()) { } 00120 00121 pointer 00122 allocate(size_type __n) 00123 { 00124 pointer __res = _M_allocator.allocate(__n + _M_extra); 00125 size_type* __ps = reinterpret_cast<size_type*>(__res); 00126 *__ps = __n; 00127 return __res + _M_extra; 00128 } 00129 00130 pointer 00131 allocate(size_type __n, const void* __hint) 00132 { 00133 pointer __res = _M_allocator.allocate(__n + _M_extra, __hint); 00134 size_type* __ps = reinterpret_cast<size_type*>(__res); 00135 *__ps = __n; 00136 return __res + _M_extra; 00137 } 00138 00139 void 00140 deallocate(pointer __p, size_type __n) 00141 { 00142 using std::__throw_runtime_error; 00143 if (__p) 00144 { 00145 pointer __real_p = __p - _M_extra; 00146 if (*reinterpret_cast<size_type*>(__real_p) != __n) 00147 __throw_runtime_error("debug_allocator::deallocate wrong size"); 00148 _M_allocator.deallocate(__real_p, __n + _M_extra); 00149 } 00150 else 00151 __throw_runtime_error("debug_allocator::deallocate null pointer"); 00152 } 00153 00154 void 00155 construct(pointer __p, const value_type& __val) 00156 { _Traits::construct(_M_allocator, __p, __val); } 00157 00158 #if __cplusplus >= 201103L 00159 template<typename _Tp, typename... _Args> 00160 void 00161 construct(_Tp* __p, _Args&&... __args) 00162 { 00163 _Traits::construct(_M_allocator, __p, 00164 std::forward<_Args>(__args)...); 00165 } 00166 #endif 00167 00168 template<typename _Tp> 00169 void 00170 destroy(_Tp* __p) 00171 { _Traits::destroy(_M_allocator, __p); } 00172 00173 size_type 00174 max_size() const throw() 00175 { return _Traits::max_size(_M_allocator) - _M_extra; } 00176 00177 friend bool 00178 operator==(const debug_allocator& __lhs, const debug_allocator& __rhs) 00179 { return __lhs._M_allocator == __rhs._M_allocator; } 00180 }; 00181 00182 template<typename _Alloc> 00183 inline bool 00184 operator!=(const debug_allocator<_Alloc>& __lhs, 00185 const debug_allocator<_Alloc>& __rhs) 00186 { return !(__lhs == __rhs); } 00187 00188 _GLIBCXX_END_NAMESPACE_VERSION 00189 } // namespace 00190 00191 #endif