# debug_allocator.h

Go to the documentation of this file.

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