1 // Allocator that wraps "C" malloc -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
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)
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,
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
40 #include <bits/functexcept.h>
41 #include <bits/move.h>
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
49 * @brief An allocator that uses malloc.
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
60 typedef size_t size_type
;
61 typedef ptrdiff_t difference_type
;
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
>
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() { }
82 address(reference __x
) const { return &__x
; }
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.
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
)));
97 std::__throw_bad_alloc();
101 // __p is not permitted to be a null pointer.
103 deallocate(pointer __p
, size_type
)
104 { std::free(static_cast<void*>(__p
)); }
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
113 construct(pointer __p
, const _Tp
& __val
)
114 { ::new((void *)__p
) value_type(__val
); }
116 #ifdef __GXX_EXPERIMENTAL_CXX0X__
117 template<typename
... _Args
>
119 construct(pointer __p
, _Args
&&... __args
)
120 { ::new((void *)__p
) _Tp(std::forward
<_Args
>(__args
)...); }
124 destroy(pointer __p
) { __p
->~_Tp(); }
127 template<typename _Tp
>
129 operator==(const malloc_allocator
<_Tp
>&, const malloc_allocator
<_Tp
>&)
132 template<typename _Tp
>
134 operator!=(const malloc_allocator
<_Tp
>&, const malloc_allocator
<_Tp
>&)
137 _GLIBCXX_END_NAMESPACE