1*e4b17023SJohn Marino // Allocator that wraps "C" malloc -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4*e4b17023SJohn Marino // 2010, 2011
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /** @file ext/malloc_allocator.h
28*e4b17023SJohn Marino * This file is a GNU extension to the Standard C++ Library.
29*e4b17023SJohn Marino */
30*e4b17023SJohn Marino
31*e4b17023SJohn Marino #ifndef _MALLOC_ALLOCATOR_H
32*e4b17023SJohn Marino #define _MALLOC_ALLOCATOR_H 1
33*e4b17023SJohn Marino
34*e4b17023SJohn Marino #include <cstdlib>
35*e4b17023SJohn Marino #include <new>
36*e4b17023SJohn Marino #include <bits/functexcept.h>
37*e4b17023SJohn Marino #include <bits/move.h>
38*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)39*e4b17023SJohn Marino namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
40*e4b17023SJohn Marino {
41*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino using std::size_t;
44*e4b17023SJohn Marino using std::ptrdiff_t;
45*e4b17023SJohn Marino
46*e4b17023SJohn Marino /**
47*e4b17023SJohn Marino * @brief An allocator that uses malloc.
48*e4b17023SJohn Marino * @ingroup allocators
49*e4b17023SJohn Marino *
50*e4b17023SJohn Marino * This is precisely the allocator defined in the C++ Standard.
51*e4b17023SJohn Marino * - all allocation calls malloc
52*e4b17023SJohn Marino * - all deallocation calls free
53*e4b17023SJohn Marino */
54*e4b17023SJohn Marino template<typename _Tp>
55*e4b17023SJohn Marino class malloc_allocator
56*e4b17023SJohn Marino {
57*e4b17023SJohn Marino public:
58*e4b17023SJohn Marino typedef size_t size_type;
59*e4b17023SJohn Marino typedef ptrdiff_t difference_type;
60*e4b17023SJohn Marino typedef _Tp* pointer;
61*e4b17023SJohn Marino typedef const _Tp* const_pointer;
62*e4b17023SJohn Marino typedef _Tp& reference;
63*e4b17023SJohn Marino typedef const _Tp& const_reference;
64*e4b17023SJohn Marino typedef _Tp value_type;
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino template<typename _Tp1>
67*e4b17023SJohn Marino struct rebind
68*e4b17023SJohn Marino { typedef malloc_allocator<_Tp1> other; };
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
73*e4b17023SJohn Marino
74*e4b17023SJohn Marino template<typename _Tp1>
75*e4b17023SJohn Marino malloc_allocator(const malloc_allocator<_Tp1>&)
76*e4b17023SJohn Marino _GLIBCXX_USE_NOEXCEPT { }
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino pointer
81*e4b17023SJohn Marino address(reference __x) const _GLIBCXX_NOEXCEPT
82*e4b17023SJohn Marino { return std::__addressof(__x); }
83*e4b17023SJohn Marino
84*e4b17023SJohn Marino const_pointer
85*e4b17023SJohn Marino address(const_reference __x) const _GLIBCXX_NOEXCEPT
86*e4b17023SJohn Marino { return std::__addressof(__x); }
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino // NB: __n is permitted to be 0. The C++ standard says nothing
89*e4b17023SJohn Marino // about what the return value is when __n == 0.
90*e4b17023SJohn Marino pointer
91*e4b17023SJohn Marino allocate(size_type __n, const void* = 0)
92*e4b17023SJohn Marino {
93*e4b17023SJohn Marino if (__n > this->max_size())
94*e4b17023SJohn Marino std::__throw_bad_alloc();
95*e4b17023SJohn Marino
96*e4b17023SJohn Marino pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
97*e4b17023SJohn Marino if (!__ret)
98*e4b17023SJohn Marino std::__throw_bad_alloc();
99*e4b17023SJohn Marino return __ret;
100*e4b17023SJohn Marino }
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino // __p is not permitted to be a null pointer.
103*e4b17023SJohn Marino void
104*e4b17023SJohn Marino deallocate(pointer __p, size_type)
105*e4b17023SJohn Marino { std::free(static_cast<void*>(__p)); }
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino size_type
108*e4b17023SJohn Marino max_size() const _GLIBCXX_USE_NOEXCEPT
109*e4b17023SJohn Marino { return size_t(-1) / sizeof(_Tp); }
110*e4b17023SJohn Marino
111*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
112*e4b17023SJohn Marino template<typename _Up, typename... _Args>
113*e4b17023SJohn Marino void
114*e4b17023SJohn Marino construct(_Up* __p, _Args&&... __args)
115*e4b17023SJohn Marino { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino template<typename _Up>
118*e4b17023SJohn Marino void
119*e4b17023SJohn Marino destroy(_Up* __p) { __p->~_Up(); }
120*e4b17023SJohn Marino #else
121*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
122*e4b17023SJohn Marino // 402. wrong new expression in [some_] allocator::construct
123*e4b17023SJohn Marino void
124*e4b17023SJohn Marino construct(pointer __p, const _Tp& __val)
125*e4b17023SJohn Marino { ::new((void *)__p) value_type(__val); }
126*e4b17023SJohn Marino
127*e4b17023SJohn Marino void
128*e4b17023SJohn Marino destroy(pointer __p) { __p->~_Tp(); }
129*e4b17023SJohn Marino #endif
130*e4b17023SJohn Marino };
131*e4b17023SJohn Marino
132*e4b17023SJohn Marino template<typename _Tp>
133*e4b17023SJohn Marino inline bool
134*e4b17023SJohn Marino operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
135*e4b17023SJohn Marino { return true; }
136*e4b17023SJohn Marino
137*e4b17023SJohn Marino template<typename _Tp>
138*e4b17023SJohn Marino inline bool
139*e4b17023SJohn Marino operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
140*e4b17023SJohn Marino { return false; }
141*e4b17023SJohn Marino
142*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
143*e4b17023SJohn Marino } // namespace
144*e4b17023SJohn Marino
145*e4b17023SJohn Marino #endif
146