libstdc++
|
00001 // Allocators -*- 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 /* 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 00047 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00048 { 00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00050 00051 using std::size_t; 00052 00053 /** 00054 * @brief A meta-allocator with debugging bits, as per [20.4]. 00055 * @ingroup allocators 00056 * 00057 * This is precisely the allocator defined in the C++ Standard. 00058 * - all allocation calls operator new 00059 * - all deallocation calls operator delete 00060 */ 00061 template<typename _Alloc> 00062 class debug_allocator 00063 { 00064 public: 00065 typedef typename _Alloc::size_type size_type; 00066 typedef typename _Alloc::difference_type difference_type; 00067 typedef typename _Alloc::pointer pointer; 00068 typedef typename _Alloc::const_pointer const_pointer; 00069 typedef typename _Alloc::reference reference; 00070 typedef typename _Alloc::const_reference const_reference; 00071 typedef typename _Alloc::value_type value_type; 00072 00073 private: 00074 // _M_extra is the number of objects that correspond to the 00075 // extra space where debug information is stored. 00076 size_type _M_extra; 00077 00078 _Alloc _M_allocator; 00079 00080 public: 00081 debug_allocator() 00082 { 00083 const size_t __obj_size = sizeof(value_type); 00084 _M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size; 00085 } 00086 00087 pointer 00088 allocate(size_type __n) 00089 { 00090 pointer __res = _M_allocator.allocate(__n + _M_extra); 00091 size_type* __ps = reinterpret_cast<size_type*>(__res); 00092 *__ps = __n; 00093 return __res + _M_extra; 00094 } 00095 00096 pointer 00097 allocate(size_type __n, const void* __hint) 00098 { 00099 pointer __res = _M_allocator.allocate(__n + _M_extra, __hint); 00100 size_type* __ps = reinterpret_cast<size_type*>(__res); 00101 *__ps = __n; 00102 return __res + _M_extra; 00103 } 00104 00105 void 00106 deallocate(pointer __p, size_type __n) 00107 { 00108 if (__p) 00109 { 00110 pointer __real_p = __p - _M_extra; 00111 if (*reinterpret_cast<size_type*>(__real_p) != __n) 00112 { 00113 throw std::runtime_error("debug_allocator::deallocate" 00114 " wrong size"); 00115 } 00116 _M_allocator.deallocate(__real_p, __n + _M_extra); 00117 } 00118 else 00119 throw std::runtime_error("debug_allocator::deallocate null pointer"); 00120 } 00121 }; 00122 00123 _GLIBCXX_END_NAMESPACE_VERSION 00124 } // namespace 00125 00126 #endif