1*38fd1498Szrj // Allocators -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /*
26*38fd1498Szrj * Copyright (c) 1996-1997
27*38fd1498Szrj * Silicon Graphics Computer Systems, Inc.
28*38fd1498Szrj *
29*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
30*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
31*38fd1498Szrj * provided that the above copyright notice appear in all copies and
32*38fd1498Szrj * that both that copyright notice and this permission notice appear
33*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no
34*38fd1498Szrj * representations about the suitability of this software for any
35*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
36*38fd1498Szrj */
37*38fd1498Szrj
38*38fd1498Szrj /** @file ext/debug_allocator.h
39*38fd1498Szrj * This file is a GNU extension to the Standard C++ Library.
40*38fd1498Szrj */
41*38fd1498Szrj
42*38fd1498Szrj #ifndef _DEBUG_ALLOCATOR_H
43*38fd1498Szrj #define _DEBUG_ALLOCATOR_H 1
44*38fd1498Szrj
45*38fd1498Szrj #include <stdexcept>
46*38fd1498Szrj #include <bits/functexcept.h>
47*38fd1498Szrj #include <ext/alloc_traits.h>
48*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)49*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
50*38fd1498Szrj {
51*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
52*38fd1498Szrj
53*38fd1498Szrj using std::size_t;
54*38fd1498Szrj
55*38fd1498Szrj /**
56*38fd1498Szrj * @brief A meta-allocator with debugging bits.
57*38fd1498Szrj * @ingroup allocators
58*38fd1498Szrj *
59*38fd1498Szrj * This is precisely the allocator defined in the C++03 Standard.
60*38fd1498Szrj */
61*38fd1498Szrj template<typename _Alloc>
62*38fd1498Szrj class debug_allocator
63*38fd1498Szrj {
64*38fd1498Szrj template<typename> friend class debug_allocator;
65*38fd1498Szrj
66*38fd1498Szrj typedef __alloc_traits<_Alloc> _Traits;
67*38fd1498Szrj
68*38fd1498Szrj public:
69*38fd1498Szrj typedef typename _Traits::size_type size_type;
70*38fd1498Szrj typedef typename _Traits::difference_type difference_type;
71*38fd1498Szrj typedef typename _Traits::pointer pointer;
72*38fd1498Szrj typedef typename _Traits::const_pointer const_pointer;
73*38fd1498Szrj typedef typename _Traits::reference reference;
74*38fd1498Szrj typedef typename _Traits::const_reference const_reference;
75*38fd1498Szrj typedef typename _Traits::value_type value_type;
76*38fd1498Szrj
77*38fd1498Szrj template<typename _Up>
78*38fd1498Szrj class rebind
79*38fd1498Szrj {
80*38fd1498Szrj typedef typename _Traits::template rebind<_Up>::other __other;
81*38fd1498Szrj
82*38fd1498Szrj public:
83*38fd1498Szrj typedef debug_allocator<__other> other;
84*38fd1498Szrj };
85*38fd1498Szrj
86*38fd1498Szrj private:
87*38fd1498Szrj // _M_extra is the number of objects that correspond to the
88*38fd1498Szrj // extra space where debug information is stored.
89*38fd1498Szrj size_type _M_extra;
90*38fd1498Szrj
91*38fd1498Szrj _Alloc _M_allocator;
92*38fd1498Szrj
93*38fd1498Szrj template<typename _Alloc2,
94*38fd1498Szrj typename = typename _Alloc2::template rebind<value_type>::other>
95*38fd1498Szrj struct __convertible
96*38fd1498Szrj { };
97*38fd1498Szrj
98*38fd1498Szrj template<typename _Alloc2>
99*38fd1498Szrj struct __convertible<_Alloc2, _Alloc>
100*38fd1498Szrj {
101*38fd1498Szrj typedef void* __type;
102*38fd1498Szrj };
103*38fd1498Szrj
104*38fd1498Szrj size_type _S_extra()
105*38fd1498Szrj {
106*38fd1498Szrj const size_t __obj_size = sizeof(value_type);
107*38fd1498Szrj return (sizeof(size_type) + __obj_size - 1) / __obj_size;
108*38fd1498Szrj }
109*38fd1498Szrj
110*38fd1498Szrj public:
111*38fd1498Szrj debug_allocator() : _M_extra(_S_extra()) { }
112*38fd1498Szrj
113*38fd1498Szrj template<typename _Alloc2>
114*38fd1498Szrj debug_allocator(const debug_allocator<_Alloc2>& __a2,
115*38fd1498Szrj typename __convertible<_Alloc2>::__type = 0)
116*38fd1498Szrj : _M_allocator(__a2._M_allocator), _M_extra(_S_extra()) { }
117*38fd1498Szrj
118*38fd1498Szrj debug_allocator(const _Alloc& __a)
119*38fd1498Szrj : _M_allocator(__a), _M_extra(_S_extra()) { }
120*38fd1498Szrj
121*38fd1498Szrj pointer
122*38fd1498Szrj allocate(size_type __n)
123*38fd1498Szrj {
124*38fd1498Szrj pointer __res = _M_allocator.allocate(__n + _M_extra);
125*38fd1498Szrj size_type* __ps = reinterpret_cast<size_type*>(__res);
126*38fd1498Szrj *__ps = __n;
127*38fd1498Szrj return __res + _M_extra;
128*38fd1498Szrj }
129*38fd1498Szrj
130*38fd1498Szrj pointer
131*38fd1498Szrj allocate(size_type __n, const void* __hint)
132*38fd1498Szrj {
133*38fd1498Szrj pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
134*38fd1498Szrj size_type* __ps = reinterpret_cast<size_type*>(__res);
135*38fd1498Szrj *__ps = __n;
136*38fd1498Szrj return __res + _M_extra;
137*38fd1498Szrj }
138*38fd1498Szrj
139*38fd1498Szrj void
140*38fd1498Szrj deallocate(pointer __p, size_type __n)
141*38fd1498Szrj {
142*38fd1498Szrj using std::__throw_runtime_error;
143*38fd1498Szrj if (__p)
144*38fd1498Szrj {
145*38fd1498Szrj pointer __real_p = __p - _M_extra;
146*38fd1498Szrj if (*reinterpret_cast<size_type*>(__real_p) != __n)
147*38fd1498Szrj __throw_runtime_error("debug_allocator::deallocate wrong size");
148*38fd1498Szrj _M_allocator.deallocate(__real_p, __n + _M_extra);
149*38fd1498Szrj }
150*38fd1498Szrj else
151*38fd1498Szrj __throw_runtime_error("debug_allocator::deallocate null pointer");
152*38fd1498Szrj }
153*38fd1498Szrj
154*38fd1498Szrj void
155*38fd1498Szrj construct(pointer __p, const value_type& __val)
156*38fd1498Szrj { _Traits::construct(_M_allocator, __p, __val); }
157*38fd1498Szrj
158*38fd1498Szrj #if __cplusplus >= 201103L
159*38fd1498Szrj template<typename _Tp, typename... _Args>
160*38fd1498Szrj void
161*38fd1498Szrj construct(_Tp* __p, _Args&&... __args)
162*38fd1498Szrj {
163*38fd1498Szrj _Traits::construct(_M_allocator, __p,
164*38fd1498Szrj std::forward<_Args>(__args)...);
165*38fd1498Szrj }
166*38fd1498Szrj #endif
167*38fd1498Szrj
168*38fd1498Szrj template<typename _Tp>
169*38fd1498Szrj void
170*38fd1498Szrj destroy(_Tp* __p)
171*38fd1498Szrj { _Traits::destroy(_M_allocator, __p); }
172*38fd1498Szrj
173*38fd1498Szrj size_type
174*38fd1498Szrj max_size() const throw()
175*38fd1498Szrj { return _Traits::max_size(_M_allocator) - _M_extra; }
176*38fd1498Szrj
177*38fd1498Szrj friend bool
178*38fd1498Szrj operator==(const debug_allocator& __lhs, const debug_allocator& __rhs)
179*38fd1498Szrj { return __lhs._M_allocator == __rhs._M_allocator; }
180*38fd1498Szrj };
181*38fd1498Szrj
182*38fd1498Szrj template<typename _Alloc>
183*38fd1498Szrj inline bool
184*38fd1498Szrj operator!=(const debug_allocator<_Alloc>& __lhs,
185*38fd1498Szrj const debug_allocator<_Alloc>& __rhs)
186*38fd1498Szrj { return !(__lhs == __rhs); }
187*38fd1498Szrj
188*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
189*38fd1498Szrj } // namespace
190*38fd1498Szrj
191*38fd1498Szrj #endif
192