]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libstdc++/include/ext/malloc_allocator.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libstdc++ / include / ext / malloc_allocator.h
1 // Allocator that wraps "C" malloc -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file ext/malloc_allocator.h
31  *  This file is a GNU extension to the Standard C++ Library.
32  */
33
34 #ifndef _MALLOC_ALLOCATOR_H
35 #define _MALLOC_ALLOCATOR_H 1
36
37 #include <cstdlib>
38 #include <new>
39 #include <bits/functexcept.h>
40
41 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
42
43   using std::size_t;
44   using std::ptrdiff_t;
45
46   /**
47    *  @brief  An allocator that uses malloc.
48    *
49    *  This is precisely the allocator defined in the C++ Standard. 
50    *    - all allocation calls malloc
51    *    - all deallocation calls free
52    */
53   template<typename _Tp>
54     class malloc_allocator
55     {
56     public:
57       typedef size_t     size_type;
58       typedef ptrdiff_t  difference_type;
59       typedef _Tp*       pointer;
60       typedef const _Tp* const_pointer;
61       typedef _Tp&       reference;
62       typedef const _Tp& const_reference;
63       typedef _Tp        value_type;
64
65       template<typename _Tp1>
66         struct rebind
67         { typedef malloc_allocator<_Tp1> other; };
68
69       malloc_allocator() throw() { }
70
71       malloc_allocator(const malloc_allocator&) throw() { }
72
73       template<typename _Tp1>
74         malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
75
76       ~malloc_allocator() throw() { }
77
78       pointer
79       address(reference __x) const { return &__x; }
80
81       const_pointer
82       address(const_reference __x) const { return &__x; }
83
84       // NB: __n is permitted to be 0.  The C++ standard says nothing
85       // about what the return value is when __n == 0.
86       pointer
87       allocate(size_type __n, const void* = 0)
88       {
89         if (__builtin_expect(__n > this->max_size(), false))
90           std::__throw_bad_alloc();
91
92         pointer __ret = static_cast<_Tp*>(malloc(__n * sizeof(_Tp)));
93         if (!__ret)
94           std::__throw_bad_alloc();
95         return __ret;
96       }
97
98       // __p is not permitted to be a null pointer.
99       void
100       deallocate(pointer __p, size_type)
101       { free(static_cast<void*>(__p)); }
102
103       size_type
104       max_size() const throw() 
105       { return size_t(-1) / sizeof(_Tp); }
106
107       // _GLIBCXX_RESOLVE_LIB_DEFECTS
108       // 402. wrong new expression in [some_] allocator::construct
109       void 
110       construct(pointer __p, const _Tp& __val) 
111       { ::new(__p) value_type(__val); }
112
113       void 
114       destroy(pointer __p) { __p->~_Tp(); }
115     };
116
117   template<typename _Tp>
118     inline bool
119     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
120     { return true; }
121   
122   template<typename _Tp>
123     inline bool
124     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
125     { return false; }
126
127 _GLIBCXX_END_NAMESPACE
128
129 #endif