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