kernel - support dummy reallocblks in devfs
[dragonfly.git] / contrib / gcc-4.7 / libstdc++-v3 / include / ext / malloc_allocator.h
blob4900723399324c61f57b5f244c0c13db193b90d7
1 // Allocator that wraps "C" malloc -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 // 2010, 2011
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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
34 #include <cstdlib>
35 #include <new>
36 #include <bits/functexcept.h>
37 #include <bits/move.h>
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 using std::size_t;
44 using std::ptrdiff_t;
46 /**
47 * @brief An allocator that uses malloc.
48 * @ingroup allocators
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
57 public:
58 typedef size_t size_type;
59 typedef ptrdiff_t difference_type;
60 typedef _Tp* pointer;
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>
67 struct rebind
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 { }
80 pointer
81 address(reference __x) const _GLIBCXX_NOEXCEPT
82 { return std::__addressof(__x); }
84 const_pointer
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.
90 pointer
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)));
97 if (!__ret)
98 std::__throw_bad_alloc();
99 return __ret;
102 // __p is not permitted to be a null pointer.
103 void
104 deallocate(pointer __p, size_type)
105 { std::free(static_cast<void*>(__p)); }
107 size_type
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>
113 void
114 construct(_Up* __p, _Args&&... __args)
115 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
117 template<typename _Up>
118 void
119 destroy(_Up* __p) { __p->~_Up(); }
120 #else
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // 402. wrong new expression in [some_] allocator::construct
123 void
124 construct(pointer __p, const _Tp& __val)
125 { ::new((void *)__p) value_type(__val); }
127 void
128 destroy(pointer __p) { __p->~_Tp(); }
129 #endif
132 template<typename _Tp>
133 inline bool
134 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
135 { return true; }
137 template<typename _Tp>
138 inline bool
139 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
140 { return false; }
142 _GLIBCXX_END_NAMESPACE_VERSION
143 } // namespace
145 #endif