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