OSDN Git Service

2004-06-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / pool_allocator.h
1 // Allocators -*- 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 /*
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/pool_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 #ifndef _POOL_ALLOCATOR_H
48 #define _POOL_ALLOCATOR_H 1
49
50 #include <bits/c++config.h>
51 #include <new>
52 #include <bits/functexcept.h>
53 #include <bits/stl_threads.h>
54 #include <bits/atomicity.h>
55
56 namespace __gnu_cxx
57 {
58   /**
59    *  @if maint
60    *  Uses various allocators to fulfill underlying requests (and makes as
61    *  few requests as possible when in default high-speed pool mode).
62    *
63    *  Important implementation properties:
64    *  0. If globally mandated, then allocate objects from new
65    *  1. If the clients request an object of size > _S_max_bytes, the resulting
66    *     object will be obtained directly from new
67    *  2. In all other cases, we allocate an object of size exactly
68    *     _S_round_up(requested_size).  Thus the client has enough size
69    *     information that we can return the object to the proper free list
70    *     without permanently losing part of the object.
71    *
72    *  @endif
73    *  (See @link Allocators allocators info @endlink for more.)
74    */
75     class __pool_base
76     {
77     protected:
78
79       enum { _S_align = 8 };
80       enum { _S_max_bytes = 128 };
81       enum { _S_free_list_size = _S_max_bytes / _S_align };
82       
83       // It would be nice to use _STL_auto_lock here.  But we need a
84       // test whether threads are in use.
85       struct _Lock
86       {
87         static _STL_mutex_lock        _S_lock;
88         _Lock() { _S_lock._M_acquire_lock(); }
89         ~_Lock() { _S_lock._M_release_lock(); }
90       };
91
92       union _Obj
93       {
94         union _Obj* _M_free_list_link;
95         char        _M_client_data[1];    // The client sees this.
96       };
97       
98       static _Obj* volatile         _S_free_list[_S_free_list_size];
99       
100       // Chunk allocation state.
101       static char*                  _S_start_free;
102       static char*                  _S_end_free;
103       static size_t                 _S_heap_size;     
104       
105       size_t
106       _M_round_up(size_t __bytes)
107       { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
108       
109       _Obj* volatile*
110       _M_get_free_list(size_t __bytes);
111     
112       // Returns an object of size __n, and optionally adds to size __n
113       // free list.
114       void*
115       _M_refill(size_t __n);
116       
117       // Allocates a chunk for nobjs of size size.  nobjs may be reduced
118       // if it is inconvenient to allocate the requested number.
119       char*
120       _M_allocate_chunk(size_t __n, int& __nobjs);
121     };
122
123
124   template<typename _Tp>
125     class __pool_alloc : private __pool_base
126     {
127     private:
128       static _Atomic_word           _S_force_new;
129
130     public:
131       typedef size_t     size_type;
132       typedef ptrdiff_t  difference_type;
133       typedef _Tp*       pointer;
134       typedef const _Tp* const_pointer;
135       typedef _Tp&       reference;
136       typedef const _Tp& const_reference;
137       typedef _Tp        value_type;
138
139       template<typename _Tp1>
140         struct rebind
141         { typedef __pool_alloc<_Tp1> other; };
142
143       __pool_alloc() throw() { }
144
145       __pool_alloc(const __pool_alloc&) throw() { }
146
147       template<typename _Tp1>
148         __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
149
150       ~__pool_alloc() throw() { }
151
152       pointer
153       address(reference __x) const { return &__x; }
154
155       const_pointer
156       address(const_reference __x) const { return &__x; }
157
158       size_type
159       max_size() const throw() 
160       { return size_t(-1) / sizeof(_Tp); }
161
162       // _GLIBCXX_RESOLVE_LIB_DEFECTS
163       // 402. wrong new expression in [some_] allocator::construct
164       void 
165       construct(pointer __p, const _Tp& __val) 
166       { ::new(__p) _Tp(__val); }
167
168       void 
169       destroy(pointer __p) { __p->~_Tp(); }
170
171       pointer
172       allocate(size_type __n, const void* = 0);
173
174       void
175       deallocate(pointer __p, size_type __n);      
176     };
177
178   template<typename _Tp>
179     inline bool
180     operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
181     { return true; }
182
183   template<typename _Tp>
184     inline bool
185     operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
186     { return false; }
187
188   template<typename _Tp>
189     _Atomic_word
190     __pool_alloc<_Tp>::_S_force_new;
191
192   template<typename _Tp>
193     _Tp*
194     __pool_alloc<_Tp>::allocate(size_type __n, const void*)
195     {
196       pointer __ret = 0;
197       if (__n)
198         {
199           if (__n <= max_size())
200             {
201               // If there is a race through here, assume answer from getenv
202               // will resolve in same direction.  Inspired by techniques
203               // to efficiently support threading found in basic_string.h.
204               if (_S_force_new == 0)
205                 {
206                   if (getenv("GLIBCXX_FORCE_NEW"))
207                     __atomic_add(&_S_force_new, 1);
208                   else
209                     __atomic_add(&_S_force_new, -1);
210                 }
211
212               const size_t __bytes = __n * sizeof(_Tp);       
213               if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
214                 __ret = static_cast<_Tp*>(::operator new(__bytes));
215               else
216                 {
217                   _Obj* volatile* __free_list = _M_get_free_list(__bytes);
218
219                   // Acquire the lock here with a constructor call.  This
220                   // ensures that it is released in exit or during stack
221                   // unwinding.
222                   _Lock __lock_instance;
223                   _Obj* __restrict__ __result = *__free_list;
224                   if (__builtin_expect(__result == 0, 0))
225                     __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
226                   else
227                     {
228                       *__free_list = __result->_M_free_list_link;
229                       __ret = reinterpret_cast<_Tp*>(__result);
230                     }
231                   if (__builtin_expect(__ret == 0, 0))
232                     std::__throw_bad_alloc();
233                 }
234             }
235           else
236             std::__throw_bad_alloc();
237         }
238       return __ret;
239     }
240
241   template<typename _Tp>
242     void
243     __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
244     {
245       if (__n)
246         {
247           const size_t __bytes = __n * sizeof(_Tp);
248           if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
249             ::operator delete(__p);
250           else
251             {
252               _Obj* volatile* __free_list = _M_get_free_list(__bytes);
253               _Obj* __q = reinterpret_cast<_Obj*>(__p);
254
255               // Acquire the lock here with a constructor call.  This
256               // ensures that it is released in exit or during stack
257               // unwinding.
258               _Lock __lock_instance;
259               __q ->_M_free_list_link = *__free_list;
260               *__free_list = __q;
261             }
262         }
263     }
264 } // namespace __gnu_cxx
265
266 #endif