Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / libstdc++-v3 / include / ext / malloc_allocator.h
1 // Allocator that wraps "C" malloc -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #ifndef _MALLOC_ALLOCATOR_H
31 #define _MALLOC_ALLOCATOR_H 1
32
33 #include <new>
34
35 namespace __gnu_cxx
36 {
37   /**
38    *  @brief  An allocator that uses malloc
39    *
40    *  This is precisely the allocator defined in the C++ Standard. 
41    *    - all allocation calls malloc
42    *    - all deallocation calls free
43    *
44    *  (See @link Allocators allocators info @endlink for more.)
45    */
46   template<typename _Tp>
47     class malloc_allocator
48     {
49     public:
50       typedef size_t     size_type;
51       typedef ptrdiff_t  difference_type;
52       typedef _Tp*       pointer;
53       typedef const _Tp* const_pointer;
54       typedef _Tp&       reference;
55       typedef const _Tp& const_reference;
56       typedef _Tp        value_type;
57
58       template<typename _Tp1>
59         struct rebind
60         { typedef malloc_allocator<_Tp1> other; };
61
62       malloc_allocator() throw() { }
63
64       malloc_allocator(const malloc_allocator&) throw() { }
65
66       template<typename _Tp1>
67         malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
68
69       ~malloc_allocator() throw() { }
70
71       pointer
72       address(reference __x) const { return &__x; }
73
74       const_pointer
75       address(const_reference __x) const { return &__x; }
76
77       // NB: __n is permitted to be 0.  The C++ standard says nothing
78       // about what the return value is when __n == 0.
79       pointer
80       allocate(size_type __n, const void* = 0)
81       {
82         pointer __ret = static_cast<_Tp*>(malloc(__n * sizeof(_Tp)));
83         if (!__ret)
84           throw std::bad_alloc();
85         return __ret;
86       }
87
88       // __p is not permitted to be a null pointer.
89       void
90       deallocate(pointer __p, size_type)
91       { free(static_cast<void*>(__p)); }
92
93       size_type
94       max_size() const throw() 
95       { return size_t(-1) / sizeof(_Tp); }
96
97       // _GLIBCXX_RESOLVE_LIB_DEFECTS
98       // 402. wrong new expression in [some_] allocator::construct
99       void 
100       construct(pointer __p, const _Tp& __val) 
101       { ::new(__p) value_type(__val); }
102
103       void 
104       destroy(pointer __p) { __p->~_Tp(); }
105     };
106
107   template<typename _Tp>
108     inline bool
109     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
110     { return true; }
111   
112   template<typename _Tp>
113     inline bool
114     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
115     { return false; }
116 } // namespace __gnu_cxx
117
118 #endif