1// Allocators -*- C++ -*- 2 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/* 26 * Copyright (c) 1996-1997 27 * Silicon Graphics Computer Systems, Inc. 28 * 29 * Permission to use, copy, modify, distribute and sell this software 30 * and its documentation for any purpose is hereby granted without fee, 31 * provided that the above copyright notice appear in all copies and 32 * that both that copyright notice and this permission notice appear 33 * in supporting documentation. Silicon Graphics makes no 34 * representations about the suitability of this software for any 35 * purpose. It is provided "as is" without express or implied warranty. 36 */ 37 38/** @file ext/debug_allocator.h 39 * This file is a GNU extension to the Standard C++ Library. 40 * You should only include this header if you are using GCC 3 or later. 41 */ 42 43#ifndef _DEBUG_ALLOCATOR_H 44#define _DEBUG_ALLOCATOR_H 1 45 46#include <stdexcept> 47 48_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 49 50 using std::size_t; 51 52 /** 53 * @brief A meta-allocator with debugging bits, as per [20.4]. 54 * @ingroup allocators 55 * 56 * This is precisely the allocator defined in the C++ Standard. 57 * - all allocation calls operator new 58 * - all deallocation calls operator delete 59 */ 60 template<typename _Alloc> 61 class debug_allocator 62 { 63 public: 64 typedef typename _Alloc::size_type size_type; 65 typedef typename _Alloc::difference_type difference_type; 66 typedef typename _Alloc::pointer pointer; 67 typedef typename _Alloc::const_pointer const_pointer; 68 typedef typename _Alloc::reference reference; 69 typedef typename _Alloc::const_reference const_reference; 70 typedef typename _Alloc::value_type value_type; 71 72 private: 73 // _M_extra is the number of objects that correspond to the 74 // extra space where debug information is stored. 75 size_type _M_extra; 76 77 _Alloc _M_allocator; 78 79 public: 80 debug_allocator() 81 { 82 const size_t __obj_size = sizeof(value_type); 83 _M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size; 84 } 85 86 pointer 87 allocate(size_type __n) 88 { 89 pointer __res = _M_allocator.allocate(__n + _M_extra); 90 size_type* __ps = reinterpret_cast<size_type*>(__res); 91 *__ps = __n; 92 return __res + _M_extra; 93 } 94 95 pointer 96 allocate(size_type __n, const void* __hint) 97 { 98 pointer __res = _M_allocator.allocate(__n + _M_extra, __hint); 99 size_type* __ps = reinterpret_cast<size_type*>(__res); 100 *__ps = __n; 101 return __res + _M_extra; 102 } 103 104 void 105 deallocate(pointer __p, size_type __n) 106 { 107 if (__p) 108 { 109 pointer __real_p = __p - _M_extra; 110 if (*reinterpret_cast<size_type*>(__real_p) != __n) 111 { 112 throw std::runtime_error("debug_allocator::deallocate" 113 " wrong size"); 114 } 115 _M_allocator.deallocate(__real_p, __n + _M_extra); 116 } 117 else 118 throw std::runtime_error("debug_allocator::deallocate null pointer"); 119 } 120 }; 121 122_GLIBCXX_END_NAMESPACE 123 124#endif 125