Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / ext / malloc_allocator.h
bloba5d31684e8e72763cab334c9a32c64e09026b72b
1 // Allocator that wraps "C" malloc -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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/stl_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.
51 * This is precisely the allocator defined in the C++ Standard.
52 * - all allocation calls malloc
53 * - all deallocation calls free
55 template<typename _Tp>
56 class malloc_allocator
58 public:
59 typedef size_t size_type;
60 typedef ptrdiff_t difference_type;
61 typedef _Tp* pointer;
62 typedef const _Tp* const_pointer;
63 typedef _Tp& reference;
64 typedef const _Tp& const_reference;
65 typedef _Tp value_type;
67 template<typename _Tp1>
68 struct rebind
69 { typedef malloc_allocator<_Tp1> other; };
71 malloc_allocator() throw() { }
73 malloc_allocator(const malloc_allocator&) throw() { }
75 template<typename _Tp1>
76 malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
78 ~malloc_allocator() throw() { }
80 pointer
81 address(reference __x) const { return &__x; }
83 const_pointer
84 address(const_reference __x) const { return &__x; }
86 // NB: __n is permitted to be 0. The C++ standard says nothing
87 // about what the return value is when __n == 0.
88 pointer
89 allocate(size_type __n, const void* = 0)
91 if (__builtin_expect(__n > this->max_size(), false))
92 std::__throw_bad_alloc();
94 pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
95 if (!__ret)
96 std::__throw_bad_alloc();
97 return __ret;
100 // __p is not permitted to be a null pointer.
101 void
102 deallocate(pointer __p, size_type)
103 { std::free(static_cast<void*>(__p)); }
105 size_type
106 max_size() const throw()
107 { return size_t(-1) / sizeof(_Tp); }
109 // _GLIBCXX_RESOLVE_LIB_DEFECTS
110 // 402. wrong new expression in [some_] allocator::construct
111 void
112 construct(pointer __p, const _Tp& __val)
113 { ::new((void *)__p) value_type(__val); }
115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
116 template<typename... _Args>
117 void
118 construct(pointer __p, _Args&&... __args)
119 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
120 #endif
122 void
123 destroy(pointer __p) { __p->~_Tp(); }
126 template<typename _Tp>
127 inline bool
128 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
129 { return true; }
131 template<typename _Tp>
132 inline bool
133 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
134 { return false; }
136 _GLIBCXX_END_NAMESPACE
138 #endif