OSDN Git Service

* include/bits/stl_bvector.h (_Bvector_alloc_base): Eliminate.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / malloc_allocator.h
1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 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 /*
31  * Copyright (c) 1996-1997
32  * Silicon Graphics Computer Systems, Inc.
33  *
34  * Permission to use, copy, modify, distribute and sell this software
35  * and its documentation for any purpose is hereby granted without fee,
36  * provided that the above copyright notice appear in all copies and
37  * that both that copyright notice and this permission notice appear
38  * in supporting documentation.  Silicon Graphics makes no
39  * representations about the suitability of this software for any
40  * purpose.  It is provided "as is" without express or implied warranty.
41  */
42
43 /** @file ext/debug_allocator.h
44  *  This file is a GNU extension to the Standard C++ Library.
45  *  You should only include this header if you are using GCC 3 or later.
46  */
47
48 #ifndef _MALLOC_ALLOCATOR_H
49 #define _MALLOC_ALLOCATOR_H 1
50
51 #include <bits/allocator_traits.h>
52
53 namespace __gnu_cxx
54 {
55   /**
56    *  @if maint
57    *  A malloc-based allocator.  Typically slower than the
58    *  __pool_alloc (below).  Typically thread-safe and more
59    *  storage efficient.  The template argument is unused and is only present
60    *  to permit multiple instantiations (but see __pool_alloc
61    *  for caveats).  "SGI" style, plus __set_malloc_handler for OOM conditions.
62    *  @endif
63    *  (See @link Allocators allocators info @endlink for more.)
64    */
65   template<int __inst>
66     class __malloc_alloc
67     {
68     private:
69       static void* _S_oom_malloc(size_t);
70       static void (* __malloc_alloc_oom_handler)();
71
72     public:
73       static void*
74       allocate(size_t __n)
75       {
76         void* __result = malloc(__n);
77         if (__builtin_expect(__result == 0, 0))
78           __result = _S_oom_malloc(__n);
79         return __result;
80       }
81
82       static void
83       deallocate(void* __p, size_t /* __n */)
84       { free(__p); }
85
86       static void (* __set_malloc_handler(void (*__f)()))()
87       {
88         void (* __old)() = __malloc_alloc_oom_handler;
89         __malloc_alloc_oom_handler = __f;
90         return __old;
91       }
92     };
93
94   // malloc_alloc out-of-memory handling
95   template<int __inst>
96     void (* __malloc_alloc<__inst>::__malloc_alloc_oom_handler)() = 0;
97
98   template<int __inst>
99     void*
100     __malloc_alloc<__inst>::
101     _S_oom_malloc(size_t __n)
102     {
103       void (* __my_malloc_handler)();
104       void* __result;
105
106       for (;;)
107         {
108           __my_malloc_handler = __malloc_alloc_oom_handler;
109           if (__builtin_expect(__my_malloc_handler == 0, 0))
110             __throw_bad_alloc();
111           (*__my_malloc_handler)();
112           __result = malloc(__n);
113           if (__result)
114             return __result;
115         }
116     }
117   //@{
118   /** Comparison operators for all of the predifined SGI-style allocators.
119    *  This ensures that __allocator<malloc_alloc> (for example) will work
120    *  correctly.  As required, all allocators compare equal.
121    */
122   template<int inst>
123     inline bool
124     operator==(const __malloc_alloc<inst>&, const __malloc_alloc<inst>&)
125     { return true; }
126
127   template<int __inst>
128     inline bool
129     operator!=(const __malloc_alloc<__inst>&, const __malloc_alloc<__inst>&)
130     { return false; }
131   //@}
132 } // namespace __gnu_cxx
133
134 namespace std
135 {
136   //@{
137   /// Versions for the predefined "SGI" style allocators.
138   template<typename _Tp, int __inst>
139     struct _Alloc_traits<_Tp, __gnu_cxx::__malloc_alloc<__inst> >
140     {
141       static const bool _S_instanceless = true;
142       typedef __gnu_cxx:: __malloc_alloc<__inst>        base_alloc_type;
143       typedef __simple_alloc<_Tp, base_alloc_type>      _Alloc_type;
144       typedef __allocator<_Tp, base_alloc_type>         allocator_type;
145     };
146   //@}
147
148   //@{
149   /// Versions for the __allocator adaptor used with the predefined
150   /// "SGI" style allocators.
151   template<typename _Tp, typename _Tp1, int __inst>
152     struct _Alloc_traits<_Tp, __allocator<_Tp1,
153                                           __gnu_cxx::__malloc_alloc<__inst> > >
154     {
155       static const bool _S_instanceless = true;
156       typedef __gnu_cxx:: __malloc_alloc<__inst>        base_alloc_type;
157       typedef __simple_alloc<_Tp, base_alloc_type>      _Alloc_type;
158       typedef __allocator<_Tp, base_alloc_type>         allocator_type;
159     };
160   //@}
161 } // namespace std
162
163 #endif