Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / ext / malloc_allocator.h
blob84989bc16e60fa0a875fcdddb13ab77f6f92e4f2
1 // Allocator that wraps "C" malloc -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
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
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file ext/malloc_allocator.h
32 * This file is a GNU extension to the Standard C++ Library.
35 #ifndef _MALLOC_ALLOCATOR_H
36 #define _MALLOC_ALLOCATOR_H 1
38 #include <cstdlib>
39 #include <new>
40 #include <bits/functexcept.h>
41 #include <bits/move.h>
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45 using std::size_t;
46 using std::ptrdiff_t;
48 /**
49 * @brief An allocator that uses malloc.
50 * @ingroup allocators
52 * This is precisely the allocator defined in the C++ Standard.
53 * - all allocation calls malloc
54 * - all deallocation calls free
56 template<typename _Tp>
57 class malloc_allocator
59 public:
60 typedef size_t size_type;
61 typedef ptrdiff_t difference_type;
62 typedef _Tp* pointer;
63 typedef const _Tp* const_pointer;
64 typedef _Tp& reference;
65 typedef const _Tp& const_reference;
66 typedef _Tp value_type;
68 template<typename _Tp1>
69 struct rebind
70 { typedef malloc_allocator<_Tp1> other; };
72 malloc_allocator() throw() { }
74 malloc_allocator(const malloc_allocator&) throw() { }
76 template<typename _Tp1>
77 malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
79 ~malloc_allocator() throw() { }
81 pointer
82 address(reference __x) const { return &__x; }
84 const_pointer
85 address(const_reference __x) const { return &__x; }
87 // NB: __n is permitted to be 0. The C++ standard says nothing
88 // about what the return value is when __n == 0.
89 pointer
90 allocate(size_type __n, const void* = 0)
92 if (__builtin_expect(__n > this->max_size(), false))
93 std::__throw_bad_alloc();
95 pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
96 if (!__ret)
97 std::__throw_bad_alloc();
98 return __ret;
101 // __p is not permitted to be a null pointer.
102 void
103 deallocate(pointer __p, size_type)
104 { std::free(static_cast<void*>(__p)); }
106 size_type
107 max_size() const throw()
108 { return size_t(-1) / sizeof(_Tp); }
110 // _GLIBCXX_RESOLVE_LIB_DEFECTS
111 // 402. wrong new expression in [some_] allocator::construct
112 void
113 construct(pointer __p, const _Tp& __val)
114 { ::new((void *)__p) value_type(__val); }
116 #ifdef __GXX_EXPERIMENTAL_CXX0X__
117 template<typename... _Args>
118 void
119 construct(pointer __p, _Args&&... __args)
120 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
121 #endif
123 void
124 destroy(pointer __p) { __p->~_Tp(); }
127 template<typename _Tp>
128 inline bool
129 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
130 { return true; }
132 template<typename _Tp>
133 inline bool
134 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
135 { return false; }
137 _GLIBCXX_END_NAMESPACE
139 #endif