libstdc++
|
00001 // Allocators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009, 2010 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 /* 00027 * Copyright (c) 1996-1997 00028 * Silicon Graphics Computer Systems, Inc. 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Silicon Graphics makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 */ 00038 00039 /** @file ext/debug_allocator.h 00040 * This file is a GNU extension to the Standard C++ Library. 00041 */ 00042 00043 #ifndef _DEBUG_ALLOCATOR_H 00044 #define _DEBUG_ALLOCATOR_H 1 00045 00046 #include <stdexcept> 00047 00048 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00049 { 00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00051 00052 using std::size_t; 00053 00054 /** 00055 * @brief A meta-allocator with debugging bits, as per [20.4]. 00056 * @ingroup allocators 00057 * 00058 * This is precisely the allocator defined in the C++ Standard. 00059 * - all allocation calls operator new 00060 * - all deallocation calls operator delete 00061 */ 00062 template<typename _Alloc> 00063 class debug_allocator 00064 { 00065 public: 00066 typedef typename _Alloc::size_type size_type; 00067 typedef typename _Alloc::difference_type difference_type; 00068 typedef typename _Alloc::pointer pointer; 00069 typedef typename _Alloc::const_pointer const_pointer; 00070 typedef typename _Alloc::reference reference; 00071 typedef typename _Alloc::const_reference const_reference; 00072 typedef typename _Alloc::value_type value_type; 00073 00074 private: 00075 // _M_extra is the number of objects that correspond to the 00076 // extra space where debug information is stored. 00077 size_type _M_extra; 00078 00079 _Alloc _M_allocator; 00080 00081 public: 00082 debug_allocator() 00083 { 00084 const size_t __obj_size = sizeof(value_type); 00085 _M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size; 00086 } 00087 00088 pointer 00089 allocate(size_type __n) 00090 { 00091 pointer __res = _M_allocator.allocate(__n + _M_extra); 00092 size_type* __ps = reinterpret_cast<size_type*>(__res); 00093 *__ps = __n; 00094 return __res + _M_extra; 00095 } 00096 00097 pointer 00098 allocate(size_type __n, const void* __hint) 00099 { 00100 pointer __res = _M_allocator.allocate(__n + _M_extra, __hint); 00101 size_type* __ps = reinterpret_cast<size_type*>(__res); 00102 *__ps = __n; 00103 return __res + _M_extra; 00104 } 00105 00106 void 00107 deallocate(pointer __p, size_type __n) 00108 { 00109 if (__p) 00110 { 00111 pointer __real_p = __p - _M_extra; 00112 if (*reinterpret_cast<size_type*>(__real_p) != __n) 00113 { 00114 throw std::runtime_error("debug_allocator::deallocate" 00115 " wrong size"); 00116 } 00117 _M_allocator.deallocate(__real_p, __n + _M_extra); 00118 } 00119 else 00120 throw std::runtime_error("debug_allocator::deallocate null pointer"); 00121 } 00122 }; 00123 00124 _GLIBCXX_END_NAMESPACE_VERSION 00125 } // namespace 00126 00127 #endif