]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/ext/malloc_allocator.h
Inital import
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / ext / malloc_allocator.h
1 // Allocator that wraps "C" malloc -*- C++ -*-
2
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.
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
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.
21
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.
30
31 /** @file ext/malloc_allocator.h
32  *  This file is a GNU extension to the Standard C++ Library.
33  */
34
35 #ifndef _MALLOC_ALLOCATOR_H
36 #define _MALLOC_ALLOCATOR_H 1
37
38 #include <cstdlib>
39 #include <new>
40 #include <bits/functexcept.h>
41 #include <bits/stl_move.h>
42
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44
45   using std::size_t;
46   using std::ptrdiff_t;
47
48   /**
49    *  @brief  An allocator that uses malloc.
50    *
51    *  This is precisely the allocator defined in the C++ Standard. 
52    *    - all allocation calls malloc
53    *    - all deallocation calls free
54    */
55   template<typename _Tp>
56     class malloc_allocator
57     {
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;
66
67       template<typename _Tp1>
68         struct rebind
69         { typedef malloc_allocator<_Tp1> other; };
70
71       malloc_allocator() throw() { }
72
73       malloc_allocator(const malloc_allocator&) throw() { }
74
75       template<typename _Tp1>
76         malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
77
78       ~malloc_allocator() throw() { }
79
80       pointer
81       address(reference __x) const { return &__x; }
82
83       const_pointer
84       address(const_reference __x) const { return &__x; }
85
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)
90       {
91         if (__builtin_expect(__n > this->max_size(), false))
92           std::__throw_bad_alloc();
93
94         pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
95         if (!__ret)
96           std::__throw_bad_alloc();
97         return __ret;
98       }
99
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)); }
104
105       size_type
106       max_size() const throw() 
107       { return size_t(-1) / sizeof(_Tp); }
108
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); }
114
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
121
122       void 
123       destroy(pointer __p) { __p->~_Tp(); }
124     };
125
126   template<typename _Tp>
127     inline bool
128     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
129     { return true; }
130   
131   template<typename _Tp>
132     inline bool
133     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
134     { return false; }
135
136 _GLIBCXX_END_NAMESPACE
137
138 #endif