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