debug_allocator.h

Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  * Copyright (c) 1996-1997
00032  * Silicon Graphics Computer Systems, Inc.
00033  *
00034  * Permission to use, copy, modify, distribute and sell this software
00035  * and its documentation for any purpose is hereby granted without fee,
00036  * provided that the above copyright notice appear in all copies and
00037  * that both that copyright notice and this permission notice appear
00038  * in supporting documentation.  Silicon Graphics makes no
00039  * representations about the suitability of this software for any
00040  * purpose.  It is provided "as is" without express or implied warranty.
00041  */
00042 
00048 #ifndef _DEBUG_ALLOCATOR_H
00049 #define _DEBUG_ALLOCATOR_H 1
00050 
00051 #include <stdexcept>
00052 
00053 namespace __gnu_cxx
00054 {
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 } // namespace __gnu_cxx
00124 
00125 #endif

Generated on Tue Feb 2 16:55:53 2010 for GNU C++ STL by  doxygen 1.4.7