1132720Skan// Allocators -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4132720Skan//
5132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
6132720Skan// software; you can redistribute it and/or modify it under the
7132720Skan// terms of the GNU General Public License as published by the
8132720Skan// Free Software Foundation; either version 2, or (at your option)
9132720Skan// any later version.
10132720Skan
11132720Skan// This library is distributed in the hope that it will be useful,
12132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14132720Skan// GNU General Public License for more details.
15132720Skan
16132720Skan// You should have received a copy of the GNU General Public License along
17132720Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19132720Skan// USA.
20132720Skan
21132720Skan// As a special exception, you may use this file as part of a free software
22132720Skan// library without restriction.  Specifically, if other files instantiate
23132720Skan// templates or use macros or inline functions from this file, or you compile
24132720Skan// this file and link it with other files to produce an executable, this
25132720Skan// file does not by itself cause the resulting executable to be covered by
26132720Skan// the GNU General Public License.  This exception does not however
27132720Skan// invalidate any other reasons why the executable file might be covered by
28132720Skan// the GNU General Public License.
29132720Skan
30132720Skan/*
31132720Skan * Copyright (c) 1996-1997
32132720Skan * Silicon Graphics Computer Systems, Inc.
33132720Skan *
34132720Skan * Permission to use, copy, modify, distribute and sell this software
35132720Skan * and its documentation for any purpose is hereby granted without fee,
36132720Skan * provided that the above copyright notice appear in all copies and
37132720Skan * that both that copyright notice and this permission notice appear
38132720Skan * in supporting documentation.  Silicon Graphics makes no
39132720Skan * representations about the suitability of this software for any
40132720Skan * purpose.  It is provided "as is" without express or implied warranty.
41132720Skan */
42132720Skan
43132720Skan/** @file ext/debug_allocator.h
44132720Skan *  This file is a GNU extension to the Standard C++ Library.
45132720Skan *  You should only include this header if you are using GCC 3 or later.
46132720Skan */
47132720Skan
48132720Skan#ifndef _DEBUG_ALLOCATOR_H
49132720Skan#define _DEBUG_ALLOCATOR_H 1
50132720Skan
51169691Skan#include <stdexcept>
52132720Skan
53169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
54169691Skan
55169691Skan  using std::size_t;
56169691Skan
57132720Skan  /**
58132720Skan   *  @brief  A meta-allocator with debugging bits, as per [20.4].
59132720Skan   *
60132720Skan   *  This is precisely the allocator defined in the C++ Standard.
61132720Skan   *    - all allocation calls operator new
62132720Skan   *    - all deallocation calls operator delete
63132720Skan   */
64132720Skan  template<typename _Alloc>
65132720Skan    class debug_allocator
66132720Skan    {
67132720Skan    public:
68132720Skan      typedef typename _Alloc::size_type       	size_type;
69132720Skan      typedef typename _Alloc::difference_type	difference_type;
70132720Skan      typedef typename _Alloc::pointer       	pointer;
71132720Skan      typedef typename _Alloc::const_pointer    const_pointer;
72132720Skan      typedef typename _Alloc::reference       	reference;
73132720Skan      typedef typename _Alloc::const_reference  const_reference;
74132720Skan      typedef typename _Alloc::value_type       value_type;
75132720Skan
76132720Skan    private:
77132720Skan      // _M_extra is the number of objects that correspond to the
78132720Skan      // extra space where debug information is stored.
79132720Skan      size_type 		_M_extra;
80132720Skan
81132720Skan      _Alloc			_M_allocator;
82132720Skan
83132720Skan    public:
84132720Skan      debug_allocator()
85132720Skan      {
86132720Skan	const size_t __obj_size = sizeof(value_type);
87132720Skan	_M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size;
88132720Skan      }
89132720Skan
90132720Skan      pointer
91132720Skan      allocate(size_type __n)
92132720Skan      {
93132720Skan        pointer __res = _M_allocator.allocate(__n + _M_extra);
94132720Skan	size_type* __ps = reinterpret_cast<size_type*>(__res);
95132720Skan	*__ps = __n;
96132720Skan        return __res + _M_extra;
97132720Skan      }
98132720Skan
99132720Skan      pointer
100132720Skan      allocate(size_type __n, const void* __hint)
101132720Skan      {
102132720Skan        pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
103132720Skan	size_type* __ps = reinterpret_cast<size_type*>(__res);
104132720Skan	*__ps = __n;
105132720Skan        return __res + _M_extra;
106132720Skan      }
107132720Skan
108132720Skan      void
109132720Skan      deallocate(pointer __p, size_type __n)
110132720Skan      {
111169691Skan	if (__p)
112169691Skan	  {
113169691Skan	    pointer __real_p = __p - _M_extra;
114169691Skan	    if (*reinterpret_cast<size_type*>(__real_p) != __n)
115169691Skan	      {
116169691Skan		throw std::runtime_error("debug_allocator::deallocate"
117169691Skan					 " wrong size");
118169691Skan	      }
119169691Skan	    _M_allocator.deallocate(__real_p, __n + _M_extra);
120169691Skan	  }
121169691Skan	else
122169691Skan	  throw std::runtime_error("debug_allocator::deallocate null pointer");
123132720Skan      }
124132720Skan    };
125132720Skan
126169691Skan_GLIBCXX_END_NAMESPACE
127169691Skan
128132720Skan#endif
129