1 // Allocator that wraps "C" malloc -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file ext/malloc_allocator.h
28 * This file is a GNU extension to the Standard C++ Library.
31 #ifndef _MALLOC_ALLOCATOR_H
32 #define _MALLOC_ALLOCATOR_H 1
36 #include <bits/functexcept.h>
37 #include <bits/move.h>
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @brief An allocator that uses malloc.
50 * This is precisely the allocator defined in the C++ Standard.
51 * - all allocation calls malloc
52 * - all deallocation calls free
54 template<typename _Tp>
55 class malloc_allocator
58 typedef size_t size_type;
59 typedef ptrdiff_t difference_type;
61 typedef const _Tp* const_pointer;
62 typedef _Tp& reference;
63 typedef const _Tp& const_reference;
64 typedef _Tp value_type;
66 template<typename _Tp1>
68 { typedef malloc_allocator<_Tp1> other; };
70 malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
72 malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
74 template<typename _Tp1>
75 malloc_allocator(const malloc_allocator<_Tp1>&)
76 _GLIBCXX_USE_NOEXCEPT { }
78 ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
81 address(reference __x) const _GLIBCXX_NOEXCEPT
82 { return std::__addressof(__x); }
85 address(const_reference __x) const _GLIBCXX_NOEXCEPT
86 { return std::__addressof(__x); }
88 // NB: __n is permitted to be 0. The C++ standard says nothing
89 // about what the return value is when __n == 0.
91 allocate(size_type __n, const void* = 0)
93 if (__n > this->max_size())
94 std::__throw_bad_alloc();
96 pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
98 std::__throw_bad_alloc();
102 // __p is not permitted to be a null pointer.
104 deallocate(pointer __p, size_type)
105 { std::free(static_cast<void*>(__p)); }
108 max_size() const _GLIBCXX_USE_NOEXCEPT
109 { return size_t(-1) / sizeof(_Tp); }
111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
112 template<typename _Up, typename... _Args>
114 construct(_Up* __p, _Args&&... __args)
115 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
117 template<typename _Up>
119 destroy(_Up* __p) { __p->~_Up(); }
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // 402. wrong new expression in [some_] allocator::construct
124 construct(pointer __p, const _Tp& __val)
125 { ::new((void *)__p) value_type(__val); }
128 destroy(pointer __p) { __p->~_Tp(); }
132 template<typename _Tp>
134 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
137 template<typename _Tp>
139 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
142 _GLIBCXX_END_NAMESPACE_VERSION